You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.3 KiB

  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 "quantum.h"
  17. #ifdef BACKLIGHT_ENABLE
  18. # include "backlight.h"
  19. extern backlight_config_t backlight_config;
  20. #else
  21. // Cannot use BACKLIGHT_CAPS_LOCK without backlight being enabled
  22. # undef BACKLIGHT_CAPS_LOCK
  23. #endif
  24. #ifndef LED_PIN_ON_STATE
  25. # define LED_PIN_ON_STATE 1
  26. #endif
  27. #if defined(BACKLIGHT_CAPS_LOCK)
  28. /** \brief Caps Lock indicator using backlight (for keyboards without dedicated LED)
  29. */
  30. static void handle_backlight_caps_lock(led_t led_state) {
  31. // Use backlight as Caps Lock indicator
  32. uint8_t bl_toggle_lvl = 0;
  33. if (led_state.caps_lock && !backlight_config.enable) {
  34. // Turning Caps Lock ON and backlight is disabled in config
  35. // Toggling backlight to the brightest level
  36. bl_toggle_lvl = BACKLIGHT_LEVELS;
  37. } else if (!led_state.caps_lock && backlight_config.enable) {
  38. // Turning Caps Lock OFF and backlight is enabled in config
  39. // Toggling backlight and restoring config level
  40. bl_toggle_lvl = backlight_config.level;
  41. }
  42. // Set level without modify backlight_config to keep ability to restore state
  43. backlight_set(bl_toggle_lvl);
  44. }
  45. #endif
  46. /** \brief Lock LED set callback - keymap/user level
  47. *
  48. * \deprecated Use led_update_user() instead.
  49. */
  50. __attribute__((weak)) void led_set_user(uint8_t usb_led) {}
  51. /** \brief Lock LED set callback - keyboard level
  52. *
  53. * \deprecated Use led_update_kb() instead.
  54. */
  55. __attribute__((weak)) void led_set_kb(uint8_t usb_led) { led_set_user(usb_led); }
  56. /** \brief Lock LED update callback - keymap/user level
  57. *
  58. * \return True if led_update_kb() should run its own code, false otherwise.
  59. */
  60. __attribute__((weak)) bool led_update_user(led_t led_state) { return true; }
  61. /** \brief Lock LED update callback - keyboard level
  62. *
  63. * \return Ignored for now.
  64. */
  65. __attribute__((weak)) bool led_update_kb(led_t led_state) {
  66. bool res = led_update_user(led_state);
  67. if (res) {
  68. #if defined(LED_NUM_LOCK_PIN) || defined(LED_CAPS_LOCK_PIN) || defined(LED_SCROLL_LOCK_PIN) || defined(LED_COMPOSE_PIN) || defined(LED_KANA_PIN)
  69. # if LED_PIN_ON_STATE == 0
  70. // invert the whole thing to avoid having to conditionally !led_state.x later
  71. led_state.raw = ~led_state.raw;
  72. # endif
  73. # ifdef LED_NUM_LOCK_PIN
  74. writePin(LED_NUM_LOCK_PIN, led_state.num_lock);
  75. # endif
  76. # ifdef LED_CAPS_LOCK_PIN
  77. writePin(LED_CAPS_LOCK_PIN, led_state.caps_lock);
  78. # endif
  79. # ifdef LED_SCROLL_LOCK_PIN
  80. writePin(LED_SCROLL_LOCK_PIN, led_state.scroll_lock);
  81. # endif
  82. # ifdef LED_COMPOSE_PIN
  83. writePin(LED_COMPOSE_PIN, led_state.compose);
  84. # endif
  85. # ifdef LED_KANA_PIN
  86. writePin(LED_KANA_PIN, led_state.kana);
  87. # endif
  88. #endif
  89. }
  90. return res;
  91. }
  92. /** \brief Initialise any LED related hardware and/or state
  93. */
  94. __attribute__((weak)) void led_init_ports(void) {
  95. #ifdef LED_NUM_LOCK_PIN
  96. setPinOutput(LED_NUM_LOCK_PIN);
  97. writePin(LED_NUM_LOCK_PIN, !LED_PIN_ON_STATE);
  98. #endif
  99. #ifdef LED_CAPS_LOCK_PIN
  100. setPinOutput(LED_CAPS_LOCK_PIN);
  101. writePin(LED_CAPS_LOCK_PIN, !LED_PIN_ON_STATE);
  102. #endif
  103. #ifdef LED_SCROLL_LOCK_PIN
  104. setPinOutput(LED_SCROLL_LOCK_PIN);
  105. writePin(LED_SCROLL_LOCK_PIN, !LED_PIN_ON_STATE);
  106. #endif
  107. #ifdef LED_COMPOSE_PIN
  108. setPinOutput(LED_COMPOSE_PIN);
  109. writePin(LED_COMPOSE_PIN, !LED_PIN_ON_STATE);
  110. #endif
  111. #ifdef LED_KANA_PIN
  112. setPinOutput(LED_KANA_PIN);
  113. writePin(LED_KANA_PIN, !LED_PIN_ON_STATE);
  114. #endif
  115. }
  116. /** \brief Entrypoint for protocol to LED binding
  117. */
  118. __attribute__((weak)) void led_set(uint8_t usb_led) {
  119. #ifdef BACKLIGHT_CAPS_LOCK
  120. handle_backlight_caps_lock((led_t)usb_led);
  121. #endif
  122. led_set_kb(usb_led);
  123. led_update_kb((led_t)usb_led);
  124. }