logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

led.c (5347B)


  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 update callback - keymap/user level
  60. *
  61. * \return True if led_update_kb() should run its own code, false otherwise.
  62. */
  63. __attribute__((weak)) bool led_update_user(led_t led_state) {
  64. return true;
  65. }
  66. /** \brief Lock LED update callback - keyboard level
  67. *
  68. * \return Ignored for now.
  69. */
  70. __attribute__((weak)) bool led_update_kb(led_t led_state) {
  71. bool res = led_update_user(led_state);
  72. if (res) {
  73. led_update_ports(led_state);
  74. }
  75. return res;
  76. }
  77. /** \brief Write LED state to hardware
  78. */
  79. __attribute__((weak)) void led_update_ports(led_t led_state) {
  80. #if LED_PIN_ON_STATE == 0
  81. // invert the whole thing to avoid having to conditionally !led_state.x later
  82. led_state.raw = ~led_state.raw;
  83. #endif
  84. #ifdef LED_NUM_LOCK_PIN
  85. gpio_write_pin(LED_NUM_LOCK_PIN, led_state.num_lock);
  86. #endif
  87. #ifdef LED_CAPS_LOCK_PIN
  88. gpio_write_pin(LED_CAPS_LOCK_PIN, led_state.caps_lock);
  89. #endif
  90. #ifdef LED_SCROLL_LOCK_PIN
  91. gpio_write_pin(LED_SCROLL_LOCK_PIN, led_state.scroll_lock);
  92. #endif
  93. #ifdef LED_COMPOSE_PIN
  94. gpio_write_pin(LED_COMPOSE_PIN, led_state.compose);
  95. #endif
  96. #ifdef LED_KANA_PIN
  97. gpio_write_pin(LED_KANA_PIN, led_state.kana);
  98. #endif
  99. }
  100. /** \brief Initialise any LED related hardware and/or state
  101. */
  102. __attribute__((weak)) void led_init_ports(void) {
  103. #ifdef LED_NUM_LOCK_PIN
  104. gpio_set_pin_output(LED_NUM_LOCK_PIN);
  105. gpio_write_pin(LED_NUM_LOCK_PIN, !LED_PIN_ON_STATE);
  106. #endif
  107. #ifdef LED_CAPS_LOCK_PIN
  108. gpio_set_pin_output(LED_CAPS_LOCK_PIN);
  109. gpio_write_pin(LED_CAPS_LOCK_PIN, !LED_PIN_ON_STATE);
  110. #endif
  111. #ifdef LED_SCROLL_LOCK_PIN
  112. gpio_set_pin_output(LED_SCROLL_LOCK_PIN);
  113. gpio_write_pin(LED_SCROLL_LOCK_PIN, !LED_PIN_ON_STATE);
  114. #endif
  115. #ifdef LED_COMPOSE_PIN
  116. gpio_set_pin_output(LED_COMPOSE_PIN);
  117. gpio_write_pin(LED_COMPOSE_PIN, !LED_PIN_ON_STATE);
  118. #endif
  119. #ifdef LED_KANA_PIN
  120. gpio_set_pin_output(LED_KANA_PIN);
  121. gpio_write_pin(LED_KANA_PIN, !LED_PIN_ON_STATE);
  122. #endif
  123. }
  124. /** \brief Entrypoint for protocol to LED binding
  125. */
  126. __attribute__((weak)) void led_set(uint8_t usb_led) {
  127. #ifdef BACKLIGHT_CAPS_LOCK
  128. handle_backlight_caps_lock((led_t)usb_led);
  129. #endif
  130. led_update_kb((led_t)usb_led);
  131. }
  132. /** \brief Trigger behaviour on transition to suspend
  133. */
  134. void led_suspend(void) {
  135. led_t leds_off = {0};
  136. #ifdef BACKLIGHT_CAPS_LOCK
  137. if (is_backlight_enabled()) {
  138. // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
  139. leds_off.caps_lock = true;
  140. }
  141. #endif
  142. led_set(leds_off.raw);
  143. }
  144. /** \brief Trigger behaviour on transition from suspend
  145. */
  146. void led_wakeup(void) {
  147. led_set(host_keyboard_leds());
  148. }
  149. /** \brief set host led state
  150. *
  151. * Only sets state if change detected
  152. */
  153. void led_task(void) {
  154. static uint8_t last_led_status = 0;
  155. // update LED
  156. uint8_t led_status = host_keyboard_leds();
  157. if (last_led_status != led_status) {
  158. last_led_status = led_status;
  159. last_led_modification_time = timer_read32();
  160. if (debug_keyboard) {
  161. dprintf("led_task: %02X\n", led_status);
  162. }
  163. led_set(led_status);
  164. }
  165. }