led.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright 2020 zvecr<git@zvecr.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "led.h"
  17. #include "host.h"
  18. #include "timer.h"
  19. #include "debug.h"
  20. #include "gpio.h"
  21. #ifdef BACKLIGHT_CAPS_LOCK
  22. # ifdef BACKLIGHT_ENABLE
  23. # include "backlight.h"
  24. extern backlight_config_t backlight_config;
  25. # else
  26. # pragma message "Cannot use BACKLIGHT_CAPS_LOCK without backlight being enabled"
  27. # undef BACKLIGHT_CAPS_LOCK
  28. # endif
  29. #endif
  30. #ifndef LED_PIN_ON_STATE
  31. # define LED_PIN_ON_STATE 1
  32. #endif
  33. #ifdef BACKLIGHT_CAPS_LOCK
  34. /** \brief Caps Lock indicator using backlight (for keyboards without dedicated LED)
  35. */
  36. static void handle_backlight_caps_lock(led_t led_state) {
  37. // Use backlight as Caps Lock indicator
  38. uint8_t bl_toggle_lvl = 0;
  39. if (led_state.caps_lock && !backlight_config.enable) {
  40. // Turning Caps Lock ON and backlight is disabled in config
  41. // Toggling backlight to the brightest level
  42. bl_toggle_lvl = BACKLIGHT_LEVELS;
  43. } else if (!led_state.caps_lock && backlight_config.enable) {
  44. // Turning Caps Lock OFF and backlight is enabled in config
  45. // Toggling backlight and restoring config level
  46. bl_toggle_lvl = backlight_config.level;
  47. }
  48. // Set level without modify backlight_config to keep ability to restore state
  49. backlight_set(bl_toggle_lvl);
  50. }
  51. #endif
  52. static uint32_t last_led_modification_time = 0;
  53. uint32_t last_led_activity_time(void) {
  54. return last_led_modification_time;
  55. }
  56. uint32_t last_led_activity_elapsed(void) {
  57. return timer_elapsed32(last_led_modification_time);
  58. }
  59. /** \brief Lock LED set callback - keymap/user level
  60. *
  61. * \deprecated Use led_update_user() instead.
  62. */
  63. __attribute__((weak)) void led_set_user(uint8_t usb_led) {}
  64. /** \brief Lock LED set callback - keyboard level
  65. *
  66. * \deprecated Use led_update_kb() instead.
  67. */
  68. __attribute__((weak)) void led_set_kb(uint8_t usb_led) {
  69. led_set_user(usb_led);
  70. }
  71. /** \brief Lock LED update callback - keymap/user level
  72. *
  73. * \return True if led_update_kb() should run its own code, false otherwise.
  74. */
  75. __attribute__((weak)) bool led_update_user(led_t led_state) {
  76. return true;
  77. }
  78. /** \brief Lock LED update callback - keyboard level
  79. *
  80. * \return Ignored for now.
  81. */
  82. __attribute__((weak)) bool led_update_kb(led_t led_state) {
  83. bool res = led_update_user(led_state);
  84. if (res) {
  85. #if defined(LED_NUM_LOCK_PIN) || defined(LED_CAPS_LOCK_PIN) || defined(LED_SCROLL_LOCK_PIN) || defined(LED_COMPOSE_PIN) || defined(LED_KANA_PIN)
  86. # if LED_PIN_ON_STATE == 0
  87. // invert the whole thing to avoid having to conditionally !led_state.x later
  88. led_state.raw = ~led_state.raw;
  89. # endif
  90. # ifdef LED_NUM_LOCK_PIN
  91. writePin(LED_NUM_LOCK_PIN, led_state.num_lock);
  92. # endif
  93. # ifdef LED_CAPS_LOCK_PIN
  94. writePin(LED_CAPS_LOCK_PIN, led_state.caps_lock);
  95. # endif
  96. # ifdef LED_SCROLL_LOCK_PIN
  97. writePin(LED_SCROLL_LOCK_PIN, led_state.scroll_lock);
  98. # endif
  99. # ifdef LED_COMPOSE_PIN
  100. writePin(LED_COMPOSE_PIN, led_state.compose);
  101. # endif
  102. # ifdef LED_KANA_PIN
  103. writePin(LED_KANA_PIN, led_state.kana);
  104. # endif
  105. #endif
  106. }
  107. return res;
  108. }
  109. /** \brief Initialise any LED related hardware and/or state
  110. */
  111. __attribute__((weak)) void led_init_ports(void) {
  112. #ifdef LED_NUM_LOCK_PIN
  113. setPinOutput(LED_NUM_LOCK_PIN);
  114. writePin(LED_NUM_LOCK_PIN, !LED_PIN_ON_STATE);
  115. #endif
  116. #ifdef LED_CAPS_LOCK_PIN
  117. setPinOutput(LED_CAPS_LOCK_PIN);
  118. writePin(LED_CAPS_LOCK_PIN, !LED_PIN_ON_STATE);
  119. #endif
  120. #ifdef LED_SCROLL_LOCK_PIN
  121. setPinOutput(LED_SCROLL_LOCK_PIN);
  122. writePin(LED_SCROLL_LOCK_PIN, !LED_PIN_ON_STATE);
  123. #endif
  124. #ifdef LED_COMPOSE_PIN
  125. setPinOutput(LED_COMPOSE_PIN);
  126. writePin(LED_COMPOSE_PIN, !LED_PIN_ON_STATE);
  127. #endif
  128. #ifdef LED_KANA_PIN
  129. setPinOutput(LED_KANA_PIN);
  130. writePin(LED_KANA_PIN, !LED_PIN_ON_STATE);
  131. #endif
  132. }
  133. /** \brief Entrypoint for protocol to LED binding
  134. */
  135. __attribute__((weak)) void led_set(uint8_t usb_led) {
  136. #ifdef BACKLIGHT_CAPS_LOCK
  137. handle_backlight_caps_lock((led_t)usb_led);
  138. #endif
  139. led_set_kb(usb_led);
  140. led_update_kb((led_t)usb_led);
  141. }
  142. /** \brief Trigger behaviour on transition to suspend
  143. */
  144. void led_suspend(void) {
  145. uint8_t leds_off = 0;
  146. #ifdef BACKLIGHT_CAPS_LOCK
  147. if (is_backlight_enabled()) {
  148. // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
  149. leds_off |= (1 << USB_LED_CAPS_LOCK);
  150. }
  151. #endif
  152. led_set(leds_off);
  153. }
  154. /** \brief Trigger behaviour on transition from suspend
  155. */
  156. void led_wakeup(void) {
  157. led_set(host_keyboard_leds());
  158. }
  159. /** \brief set host led state
  160. *
  161. * Only sets state if change detected
  162. */
  163. void led_task(void) {
  164. static uint8_t last_led_status = 0;
  165. // update LED
  166. uint8_t led_status = host_keyboard_leds();
  167. if (last_led_status != led_status) {
  168. last_led_status = led_status;
  169. last_led_modification_time = timer_read32();
  170. if (debug_keyboard) {
  171. debug("led_task: ");
  172. debug_hex8(led_status);
  173. debug("\n");
  174. }
  175. led_set(led_status);
  176. }
  177. }