suspend.c 3.3 KB

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