led_matrix.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "keyboard.h"
  25. #if defined(LED_MATRIX_IS31FL3218)
  26. # include "is31fl3218-simple.h"
  27. #elif defined(LED_MATRIX_IS31FL3731)
  28. # include "is31fl3731-simple.h"
  29. #endif
  30. #ifdef LED_MATRIX_IS31FL3733
  31. # include "is31fl3733-simple.h"
  32. #endif
  33. #ifdef LED_MATRIX_IS31FL3736
  34. # include "is31fl3736-simple.h"
  35. #endif
  36. #ifdef LED_MATRIX_IS31FL3737
  37. # include "is31fl3737-simple.h"
  38. #endif
  39. #ifdef LED_MATRIX_IS31FL3741
  40. # include "is31fl3741-simple.h"
  41. #endif
  42. #if defined(IS31FLCOMMON)
  43. # include "is31flcommon.h"
  44. #endif
  45. #ifdef LED_MATRIX_SNLED27351
  46. # include "snled27351-simple.h"
  47. #endif
  48. #ifndef LED_MATRIX_TIMEOUT
  49. # define LED_MATRIX_TIMEOUT 0
  50. #endif
  51. #ifndef LED_MATRIX_MAXIMUM_BRIGHTNESS
  52. # define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  53. #endif
  54. #ifndef LED_MATRIX_VAL_STEP
  55. # define LED_MATRIX_VAL_STEP 8
  56. #endif
  57. #ifndef LED_MATRIX_SPD_STEP
  58. # define LED_MATRIX_SPD_STEP 16
  59. #endif
  60. #ifndef LED_MATRIX_DEFAULT_ON
  61. # define LED_MATRIX_DEFAULT_ON true
  62. #endif
  63. #ifndef LED_MATRIX_DEFAULT_MODE
  64. # define LED_MATRIX_DEFAULT_MODE LED_MATRIX_SOLID
  65. #endif
  66. #ifndef LED_MATRIX_DEFAULT_VAL
  67. # define LED_MATRIX_DEFAULT_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
  68. #endif
  69. #ifndef LED_MATRIX_DEFAULT_SPD
  70. # define LED_MATRIX_DEFAULT_SPD UINT8_MAX / 2
  71. #endif
  72. #ifndef LED_MATRIX_LED_FLUSH_LIMIT
  73. # define LED_MATRIX_LED_FLUSH_LIMIT 16
  74. #endif
  75. #ifndef LED_MATRIX_LED_PROCESS_LIMIT
  76. # define LED_MATRIX_LED_PROCESS_LIMIT ((LED_MATRIX_LED_COUNT + 4) / 5)
  77. #endif
  78. #define LED_MATRIX_LED_PROCESS_MAX_ITERATIONS ((LED_MATRIX_LED_COUNT + LED_MATRIX_LED_PROCESS_LIMIT - 1) / LED_MATRIX_LED_PROCESS_LIMIT)
  79. #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < LED_MATRIX_LED_COUNT
  80. # if defined(LED_MATRIX_SPLIT)
  81. # define LED_MATRIX_USE_LIMITS(min, max) \
  82. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * params->iter; \
  83. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; \
  84. if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT; \
  85. uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; \
  86. if (is_keyboard_left() && (max > k_led_matrix_split[0])) max = k_led_matrix_split[0]; \
  87. if (!(is_keyboard_left()) && (min < k_led_matrix_split[0])) min = k_led_matrix_split[0];
  88. # else
  89. # define LED_MATRIX_USE_LIMITS(min, max) \
  90. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * params->iter; \
  91. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; \
  92. if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT;
  93. # endif
  94. #else
  95. # if defined(LED_MATRIX_SPLIT)
  96. # define LED_MATRIX_USE_LIMITS(min, max) \
  97. uint8_t min = 0; \
  98. uint8_t max = LED_MATRIX_LED_COUNT; \
  99. const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; \
  100. if (is_keyboard_left() && (max > k_led_matrix_split[0])) max = k_led_matrix_split[0]; \
  101. if (!(is_keyboard_left()) && (min < k_led_matrix_split[0])) min = k_led_matrix_split[0];
  102. # else
  103. # define LED_MATRIX_USE_LIMITS(min, max) \
  104. uint8_t min = 0; \
  105. uint8_t max = LED_MATRIX_LED_COUNT;
  106. # endif
  107. #endif
  108. #define LED_MATRIX_TEST_LED_FLAGS() \
  109. if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue
  110. enum led_matrix_effects {
  111. LED_MATRIX_NONE = 0,
  112. // --------------------------------------
  113. // -----Begin led effect enum macros-----
  114. #define LED_MATRIX_EFFECT(name, ...) LED_MATRIX_##name,
  115. #include "led_matrix_effects.inc"
  116. #undef LED_MATRIX_EFFECT
  117. #if defined(LED_MATRIX_CUSTOM_KB) || defined(LED_MATRIX_CUSTOM_USER)
  118. # define LED_MATRIX_EFFECT(name, ...) LED_MATRIX_CUSTOM_##name,
  119. # ifdef LED_MATRIX_CUSTOM_KB
  120. # include "led_matrix_kb.inc"
  121. # endif
  122. # ifdef LED_MATRIX_CUSTOM_USER
  123. # include "led_matrix_user.inc"
  124. # endif
  125. # undef LED_MATRIX_EFFECT
  126. #endif
  127. // --------------------------------------
  128. // -----End led effect enum macros-------
  129. LED_MATRIX_EFFECT_MAX
  130. };
  131. void eeconfig_update_led_matrix_default(void);
  132. void eeconfig_update_led_matrix(void);
  133. void eeconfig_debug_led_matrix(void);
  134. uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i);
  135. uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i);
  136. void led_matrix_set_value(int index, uint8_t value);
  137. void led_matrix_set_value_all(uint8_t value);
  138. void process_led_matrix(uint8_t row, uint8_t col, bool pressed);
  139. void led_matrix_task(void);
  140. // This runs after another backlight effect and replaces
  141. // values already set
  142. void led_matrix_indicators(void);
  143. bool led_matrix_indicators_kb(void);
  144. bool led_matrix_indicators_user(void);
  145. void led_matrix_indicators_advanced(effect_params_t *params);
  146. bool led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max);
  147. bool led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max);
  148. void led_matrix_init(void);
  149. void led_matrix_set_suspend_state(bool state);
  150. bool led_matrix_get_suspend_state(void);
  151. void led_matrix_toggle(void);
  152. void led_matrix_toggle_noeeprom(void);
  153. void led_matrix_enable(void);
  154. void led_matrix_enable_noeeprom(void);
  155. void led_matrix_disable(void);
  156. void led_matrix_disable_noeeprom(void);
  157. uint8_t led_matrix_is_enabled(void);
  158. void led_matrix_mode(uint8_t mode);
  159. void led_matrix_mode_noeeprom(uint8_t mode);
  160. uint8_t led_matrix_get_mode(void);
  161. void led_matrix_step(void);
  162. void led_matrix_step_noeeprom(void);
  163. void led_matrix_step_reverse(void);
  164. void led_matrix_step_reverse_noeeprom(void);
  165. void led_matrix_set_val(uint8_t val);
  166. void led_matrix_set_val_noeeprom(uint8_t val);
  167. uint8_t led_matrix_get_val(void);
  168. void led_matrix_increase_val(void);
  169. void led_matrix_increase_val_noeeprom(void);
  170. void led_matrix_decrease_val(void);
  171. void led_matrix_decrease_val_noeeprom(void);
  172. void led_matrix_set_speed(uint8_t speed);
  173. void led_matrix_set_speed_noeeprom(uint8_t speed);
  174. uint8_t led_matrix_get_speed(void);
  175. void led_matrix_increase_speed(void);
  176. void led_matrix_increase_speed_noeeprom(void);
  177. void led_matrix_decrease_speed(void);
  178. void led_matrix_decrease_speed_noeeprom(void);
  179. led_flags_t led_matrix_get_flags(void);
  180. void led_matrix_set_flags(led_flags_t flags);
  181. void led_matrix_set_flags_noeeprom(led_flags_t flags);
  182. typedef struct {
  183. /* Perform any initialisation required for the other driver functions to work. */
  184. void (*init)(void);
  185. /* Set the brightness of a single LED in the buffer. */
  186. void (*set_value)(int index, uint8_t value);
  187. /* Set the brightness of all LEDS on the keyboard in the buffer. */
  188. void (*set_value_all)(uint8_t value);
  189. /* Flush any buffered changes to the hardware. */
  190. void (*flush)(void);
  191. } led_matrix_driver_t;
  192. static inline bool led_matrix_check_finished_leds(uint8_t led_idx) {
  193. #if defined(LED_MATRIX_SPLIT)
  194. if (is_keyboard_left()) {
  195. uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
  196. return led_idx < k_led_matrix_split[0];
  197. } else
  198. return led_idx < LED_MATRIX_LED_COUNT;
  199. #else
  200. return led_idx < LED_MATRIX_LED_COUNT;
  201. #endif
  202. }
  203. extern const led_matrix_driver_t led_matrix_driver;
  204. extern led_eeconfig_t led_matrix_eeconfig;
  205. extern uint32_t g_led_timer;
  206. extern led_config_t g_led_config;
  207. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  208. extern last_hit_t g_last_hit_tracker;
  209. #endif
  210. #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS
  211. extern uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS];
  212. #endif