suspend.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* TODO */
  2. #include <ch.h>
  3. #include <hal.h>
  4. #include "matrix.h"
  5. #include "action.h"
  6. #include "action_util.h"
  7. #include "mousekey.h"
  8. #include "host.h"
  9. #include "suspend.h"
  10. #include "led.h"
  11. #include "wait.h"
  12. #ifdef AUDIO_ENABLE
  13. # include "audio.h"
  14. #endif /* AUDIO_ENABLE */
  15. #ifdef BACKLIGHT_ENABLE
  16. # include "backlight.h"
  17. #endif
  18. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  19. # include "rgblight.h"
  20. #endif
  21. #ifdef RGB_MATRIX_ENABLE
  22. # include "rgb_matrix.h"
  23. #endif
  24. /** \brief suspend idle
  25. *
  26. * FIXME: needs doc
  27. */
  28. void suspend_idle(uint8_t time) {
  29. // TODO: this is not used anywhere - what units is 'time' in?
  30. wait_ms(time);
  31. }
  32. /** \brief Run keyboard level Power down
  33. *
  34. * FIXME: needs doc
  35. */
  36. __attribute__((weak)) void suspend_power_down_user(void) {}
  37. /** \brief Run keyboard level Power down
  38. *
  39. * FIXME: needs doc
  40. */
  41. __attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }
  42. /** \brief suspend power down
  43. *
  44. * FIXME: needs doc
  45. */
  46. void suspend_power_down(void) {
  47. #ifdef BACKLIGHT_ENABLE
  48. backlight_set(0);
  49. #endif
  50. #ifdef RGB_MATRIX_ENABLE
  51. rgb_matrix_task();
  52. #endif
  53. // Turn off LED indicators
  54. uint8_t leds_off = 0;
  55. #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  56. if (is_backlight_enabled()) {
  57. // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
  58. leds_off |= (1 << USB_LED_CAPS_LOCK);
  59. }
  60. #endif
  61. led_set(leds_off);
  62. // TODO: figure out what to power down and how
  63. // shouldn't power down TPM/FTM if we want a breathing LED
  64. // also shouldn't power down USB
  65. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  66. rgblight_suspend();
  67. #endif
  68. #ifdef AUDIO_ENABLE
  69. stop_all_notes();
  70. #endif /* AUDIO_ENABLE */
  71. suspend_power_down_kb();
  72. // on AVR, this enables the watchdog for 15ms (max), and goes to
  73. // SLEEP_MODE_PWR_DOWN
  74. wait_ms(17);
  75. }
  76. /** \brief suspend wakeup condition
  77. *
  78. * FIXME: needs doc
  79. */
  80. __attribute__((weak)) void matrix_power_up(void) {}
  81. __attribute__((weak)) void matrix_power_down(void) {}
  82. bool suspend_wakeup_condition(void) {
  83. matrix_power_up();
  84. matrix_scan();
  85. matrix_power_down();
  86. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  87. if (matrix_get_row(r)) return true;
  88. }
  89. return false;
  90. }
  91. /** \brief run user level code immediately after wakeup
  92. *
  93. * FIXME: needs doc
  94. */
  95. __attribute__((weak)) void suspend_wakeup_init_user(void) {}
  96. /** \brief run keyboard level code immediately after wakeup
  97. *
  98. * FIXME: needs doc
  99. */
  100. __attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }
  101. /** \brief suspend wakeup condition
  102. *
  103. * run immediately after wakeup
  104. * FIXME: needs doc
  105. */
  106. void suspend_wakeup_init(void) {
  107. // clear keyboard state
  108. // need to do it manually, because we're running from ISR
  109. // and clear_keyboard() calls print
  110. // so only clear the variables in memory
  111. // the reports will be sent from main.c afterwards
  112. // or if the PC asks for GET_REPORT
  113. clear_mods();
  114. clear_weak_mods();
  115. clear_keys();
  116. #ifdef MOUSEKEY_ENABLE
  117. mousekey_clear();
  118. #endif /* MOUSEKEY_ENABLE */
  119. #ifdef EXTRAKEY_ENABLE
  120. host_system_send(0);
  121. host_consumer_send(0);
  122. #endif /* EXTRAKEY_ENABLE */
  123. #ifdef BACKLIGHT_ENABLE
  124. backlight_init();
  125. #endif /* BACKLIGHT_ENABLE */
  126. led_set(host_keyboard_leds());
  127. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  128. rgblight_wakeup();
  129. #endif
  130. suspend_wakeup_init_kb();
  131. }