led_matrix.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2017 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2019 Clueboard
  5. * Copyright 2021 Leo Deng
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma once
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include "led_matrix_types.h"
  24. #include "quantum.h"
  25. #ifdef IS31FL3731
  26. # include "is31fl3731-simple.h"
  27. #elif defined(IS31FLCOMMON)
  28. # include "is31flcommon.h"
  29. #endif
  30. #ifdef IS31FL3733
  31. # include "is31fl3733-simple.h"
  32. #endif
  33. #ifdef CKLED2001
  34. # include "ckled2001-simple.h"
  35. #endif
  36. #ifndef LED_MATRIX_LED_FLUSH_LIMIT
  37. # define LED_MATRIX_LED_FLUSH_LIMIT 16
  38. #endif
  39. #ifndef LED_MATRIX_LED_PROCESS_LIMIT
  40. # define LED_MATRIX_LED_PROCESS_LIMIT (LED_MATRIX_LED_COUNT + 4) / 5
  41. #endif
  42. #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < LED_MATRIX_LED_COUNT
  43. # if defined(LED_MATRIX_SPLIT)
  44. # define LED_MATRIX_USE_LIMITS(min, max) \
  45. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * params->iter; \
  46. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; \
  47. if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT; \
  48. uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; \
  49. if (is_keyboard_left() && (max > k_led_matrix_split[0])) max = k_led_matrix_split[0]; \
  50. if (!(is_keyboard_left()) && (min < k_led_matrix_split[0])) min = k_led_matrix_split[0];
  51. # else
  52. # define LED_MATRIX_USE_LIMITS(min, max) \
  53. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * params->iter; \
  54. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; \
  55. if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT;
  56. # endif
  57. #else
  58. # if defined(LED_MATRIX_SPLIT)
  59. # define LED_MATRIX_USE_LIMITS(min, max) \
  60. uint8_t min = 0; \
  61. uint8_t max = LED_MATRIX_LED_COUNT; \
  62. const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; \
  63. if (is_keyboard_left() && (max > k_led_matrix_split[0])) max = k_led_matrix_split[0]; \
  64. if (!(is_keyboard_left()) && (min < k_led_matrix_split[0])) min = k_led_matrix_split[0];
  65. # else
  66. # define LED_MATRIX_USE_LIMITS(min, max) \
  67. uint8_t min = 0; \
  68. uint8_t max = LED_MATRIX_LED_COUNT;
  69. # endif
  70. #endif
  71. #define LED_MATRIX_TEST_LED_FLAGS() \
  72. if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue
  73. enum led_matrix_effects {
  74. LED_MATRIX_NONE = 0,
  75. // --------------------------------------
  76. // -----Begin led effect enum macros-----
  77. #define LED_MATRIX_EFFECT(name, ...) LED_MATRIX_##name,
  78. #include "led_matrix_effects.inc"
  79. #undef LED_MATRIX_EFFECT
  80. #if defined(LED_MATRIX_CUSTOM_KB) || defined(LED_MATRIX_CUSTOM_USER)
  81. # define LED_MATRIX_EFFECT(name, ...) LED_MATRIX_CUSTOM_##name,
  82. # ifdef LED_MATRIX_CUSTOM_KB
  83. # include "led_matrix_kb.inc"
  84. # endif
  85. # ifdef LED_MATRIX_CUSTOM_USER
  86. # include "led_matrix_user.inc"
  87. # endif
  88. # undef LED_MATRIX_EFFECT
  89. #endif
  90. // --------------------------------------
  91. // -----End led effect enum macros-------
  92. LED_MATRIX_EFFECT_MAX
  93. };
  94. void eeconfig_update_led_matrix_default(void);
  95. void eeconfig_update_led_matrix(void);
  96. void eeconfig_debug_led_matrix(void);
  97. uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i);
  98. uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i);
  99. void led_matrix_set_value(int index, uint8_t value);
  100. void led_matrix_set_value_all(uint8_t value);
  101. void process_led_matrix(uint8_t row, uint8_t col, bool pressed);
  102. void led_matrix_task(void);
  103. // This runs after another backlight effect and replaces
  104. // values already set
  105. void led_matrix_indicators(void);
  106. bool led_matrix_indicators_kb(void);
  107. bool led_matrix_indicators_user(void);
  108. void led_matrix_indicators_advanced(effect_params_t *params);
  109. bool led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max);
  110. bool led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max);
  111. void led_matrix_init(void);
  112. void led_matrix_set_suspend_state(bool state);
  113. bool led_matrix_get_suspend_state(void);
  114. void led_matrix_toggle(void);
  115. void led_matrix_toggle_noeeprom(void);
  116. void led_matrix_enable(void);
  117. void led_matrix_enable_noeeprom(void);
  118. void led_matrix_disable(void);
  119. void led_matrix_disable_noeeprom(void);
  120. uint8_t led_matrix_is_enabled(void);
  121. void led_matrix_mode(uint8_t mode);
  122. void led_matrix_mode_noeeprom(uint8_t mode);
  123. uint8_t led_matrix_get_mode(void);
  124. void led_matrix_step(void);
  125. void led_matrix_step_noeeprom(void);
  126. void led_matrix_step_reverse(void);
  127. void led_matrix_step_reverse_noeeprom(void);
  128. void led_matrix_set_val(uint8_t val);
  129. void led_matrix_set_val_noeeprom(uint8_t val);
  130. uint8_t led_matrix_get_val(void);
  131. void led_matrix_increase_val(void);
  132. void led_matrix_increase_val_noeeprom(void);
  133. void led_matrix_decrease_val(void);
  134. void led_matrix_decrease_val_noeeprom(void);
  135. void led_matrix_set_speed(uint8_t speed);
  136. void led_matrix_set_speed_noeeprom(uint8_t speed);
  137. uint8_t led_matrix_get_speed(void);
  138. void led_matrix_increase_speed(void);
  139. void led_matrix_increase_speed_noeeprom(void);
  140. void led_matrix_decrease_speed(void);
  141. void led_matrix_decrease_speed_noeeprom(void);
  142. led_flags_t led_matrix_get_flags(void);
  143. void led_matrix_set_flags(led_flags_t flags);
  144. void led_matrix_set_flags_noeeprom(led_flags_t flags);
  145. typedef struct {
  146. /* Perform any initialisation required for the other driver functions to work. */
  147. void (*init)(void);
  148. /* Set the brightness of a single LED in the buffer. */
  149. void (*set_value)(int index, uint8_t value);
  150. /* Set the brightness of all LEDS on the keyboard in the buffer. */
  151. void (*set_value_all)(uint8_t value);
  152. /* Flush any buffered changes to the hardware. */
  153. void (*flush)(void);
  154. } led_matrix_driver_t;
  155. static inline bool led_matrix_check_finished_leds(uint8_t led_idx) {
  156. #if defined(LED_MATRIX_SPLIT)
  157. if (is_keyboard_left()) {
  158. uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
  159. return led_idx < k_led_matrix_split[0];
  160. } else
  161. return led_idx < LED_MATRIX_LED_COUNT;
  162. #else
  163. return led_idx < LED_MATRIX_LED_COUNT;
  164. #endif
  165. }
  166. extern const led_matrix_driver_t led_matrix_driver;
  167. extern led_eeconfig_t led_matrix_eeconfig;
  168. extern uint32_t g_led_timer;
  169. extern led_config_t g_led_config;
  170. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  171. extern last_hit_t g_last_hit_tracker;
  172. #endif
  173. #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS
  174. extern uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS];
  175. #endif