rgb_matrix.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2017 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma once
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include "rgb_matrix_types.h"
  22. #include "color.h"
  23. #include "quantum.h"
  24. #ifdef IS31FL3731
  25. # include "is31fl3731.h"
  26. #elif defined(IS31FL3733)
  27. # include "is31fl3733.h"
  28. #elif defined(IS31FL3737)
  29. # include "is31fl3737.h"
  30. #elif defined(IS31FL3741)
  31. # include "is31fl3741.h"
  32. #elif defined(IS31FLCOMMON)
  33. # include "is31flcommon.h"
  34. #elif defined(CKLED2001)
  35. # include "ckled2001.h"
  36. #elif defined(AW20216)
  37. # include "aw20216.h"
  38. #elif defined(WS2812)
  39. # include "ws2812.h"
  40. #endif
  41. #ifndef RGB_MATRIX_LED_FLUSH_LIMIT
  42. # define RGB_MATRIX_LED_FLUSH_LIMIT 16
  43. #endif
  44. #ifndef RGB_MATRIX_LED_PROCESS_LIMIT
  45. # define RGB_MATRIX_LED_PROCESS_LIMIT (RGB_MATRIX_LED_COUNT + 4) / 5
  46. #endif
  47. #if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < RGB_MATRIX_LED_COUNT
  48. # if defined(RGB_MATRIX_SPLIT)
  49. # define RGB_MATRIX_USE_LIMITS(min, max) \
  50. uint8_t min = RGB_MATRIX_LED_PROCESS_LIMIT * params->iter; \
  51. uint8_t max = min + RGB_MATRIX_LED_PROCESS_LIMIT; \
  52. if (max > RGB_MATRIX_LED_COUNT) max = RGB_MATRIX_LED_COUNT; \
  53. uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT; \
  54. if (is_keyboard_left() && (max > k_rgb_matrix_split[0])) max = k_rgb_matrix_split[0]; \
  55. if (!(is_keyboard_left()) && (min < k_rgb_matrix_split[0])) min = k_rgb_matrix_split[0];
  56. # else
  57. # define RGB_MATRIX_USE_LIMITS(min, max) \
  58. uint8_t min = RGB_MATRIX_LED_PROCESS_LIMIT * params->iter; \
  59. uint8_t max = min + RGB_MATRIX_LED_PROCESS_LIMIT; \
  60. if (max > RGB_MATRIX_LED_COUNT) max = RGB_MATRIX_LED_COUNT;
  61. # endif
  62. #else
  63. # if defined(RGB_MATRIX_SPLIT)
  64. # define RGB_MATRIX_USE_LIMITS(min, max) \
  65. uint8_t min = 0; \
  66. uint8_t max = RGB_MATRIX_LED_COUNT; \
  67. const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT; \
  68. if (is_keyboard_left() && (max > k_rgb_matrix_split[0])) max = k_rgb_matrix_split[0]; \
  69. if (!(is_keyboard_left()) && (min < k_rgb_matrix_split[0])) min = k_rgb_matrix_split[0];
  70. # else
  71. # define RGB_MATRIX_USE_LIMITS(min, max) \
  72. uint8_t min = 0; \
  73. uint8_t max = RGB_MATRIX_LED_COUNT;
  74. # endif
  75. #endif
  76. #define RGB_MATRIX_INDICATOR_SET_COLOR(i, r, g, b) \
  77. if (i >= led_min && i < led_max) { \
  78. rgb_matrix_set_color(i, r, g, b); \
  79. }
  80. #define RGB_MATRIX_TEST_LED_FLAGS() \
  81. if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue
  82. enum rgb_matrix_effects {
  83. RGB_MATRIX_NONE = 0,
  84. // --------------------------------------
  85. // -----Begin rgb effect enum macros-----
  86. #define RGB_MATRIX_EFFECT(name, ...) RGB_MATRIX_##name,
  87. #include "rgb_matrix_effects.inc"
  88. #undef RGB_MATRIX_EFFECT
  89. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  90. # define RGB_MATRIX_EFFECT(name, ...) RGB_MATRIX_CUSTOM_##name,
  91. # ifdef RGB_MATRIX_CUSTOM_KB
  92. # include "rgb_matrix_kb.inc"
  93. # endif
  94. # ifdef RGB_MATRIX_CUSTOM_USER
  95. # include "rgb_matrix_user.inc"
  96. # endif
  97. # undef RGB_MATRIX_EFFECT
  98. #endif
  99. // --------------------------------------
  100. // -----End rgb effect enum macros-------
  101. RGB_MATRIX_EFFECT_MAX
  102. };
  103. void eeconfig_update_rgb_matrix_default(void);
  104. void eeconfig_update_rgb_matrix(void);
  105. uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i);
  106. uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i);
  107. void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
  108. void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
  109. void process_rgb_matrix(uint8_t row, uint8_t col, bool pressed);
  110. void rgb_matrix_task(void);
  111. // This runs after another backlight effect and replaces
  112. // colors already set
  113. void rgb_matrix_indicators(void);
  114. bool rgb_matrix_indicators_kb(void);
  115. bool rgb_matrix_indicators_user(void);
  116. void rgb_matrix_indicators_advanced(effect_params_t *params);
  117. bool rgb_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max);
  118. bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max);
  119. void rgb_matrix_init(void);
  120. void rgb_matrix_reload_from_eeprom(void);
  121. void rgb_matrix_set_suspend_state(bool state);
  122. bool rgb_matrix_get_suspend_state(void);
  123. void rgb_matrix_toggle(void);
  124. void rgb_matrix_toggle_noeeprom(void);
  125. void rgb_matrix_enable(void);
  126. void rgb_matrix_enable_noeeprom(void);
  127. void rgb_matrix_disable(void);
  128. void rgb_matrix_disable_noeeprom(void);
  129. uint8_t rgb_matrix_is_enabled(void);
  130. void rgb_matrix_mode(uint8_t mode);
  131. void rgb_matrix_mode_noeeprom(uint8_t mode);
  132. uint8_t rgb_matrix_get_mode(void);
  133. void rgb_matrix_step(void);
  134. void rgb_matrix_step_noeeprom(void);
  135. void rgb_matrix_step_reverse(void);
  136. void rgb_matrix_step_reverse_noeeprom(void);
  137. void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val);
  138. void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val);
  139. HSV rgb_matrix_get_hsv(void);
  140. uint8_t rgb_matrix_get_hue(void);
  141. uint8_t rgb_matrix_get_sat(void);
  142. uint8_t rgb_matrix_get_val(void);
  143. void rgb_matrix_increase_hue(void);
  144. void rgb_matrix_increase_hue_noeeprom(void);
  145. void rgb_matrix_decrease_hue(void);
  146. void rgb_matrix_decrease_hue_noeeprom(void);
  147. void rgb_matrix_increase_sat(void);
  148. void rgb_matrix_increase_sat_noeeprom(void);
  149. void rgb_matrix_decrease_sat(void);
  150. void rgb_matrix_decrease_sat_noeeprom(void);
  151. void rgb_matrix_increase_val(void);
  152. void rgb_matrix_increase_val_noeeprom(void);
  153. void rgb_matrix_decrease_val(void);
  154. void rgb_matrix_decrease_val_noeeprom(void);
  155. void rgb_matrix_set_speed(uint8_t speed);
  156. void rgb_matrix_set_speed_noeeprom(uint8_t speed);
  157. uint8_t rgb_matrix_get_speed(void);
  158. void rgb_matrix_increase_speed(void);
  159. void rgb_matrix_increase_speed_noeeprom(void);
  160. void rgb_matrix_decrease_speed(void);
  161. void rgb_matrix_decrease_speed_noeeprom(void);
  162. led_flags_t rgb_matrix_get_flags(void);
  163. void rgb_matrix_set_flags(led_flags_t flags);
  164. void rgb_matrix_set_flags_noeeprom(led_flags_t flags);
  165. #ifndef RGBLIGHT_ENABLE
  166. # define eeconfig_update_rgblight_current eeconfig_update_rgb_matrix
  167. # define rgblight_reload_from_eeprom rgb_matrix_reload_from_eeprom
  168. # define rgblight_toggle rgb_matrix_toggle
  169. # define rgblight_toggle_noeeprom rgb_matrix_toggle_noeeprom
  170. # define rgblight_enable rgb_matrix_enable
  171. # define rgblight_enable_noeeprom rgb_matrix_enable_noeeprom
  172. # define rgblight_disable rgb_matrix_disable
  173. # define rgblight_disable_noeeprom rgb_matrix_disable_noeeprom
  174. # define rgblight_is_enabled rgb_matrix_is_enabled
  175. # define rgblight_mode rgb_matrix_mode
  176. # define rgblight_mode_noeeprom rgb_matrix_mode_noeeprom
  177. # define rgblight_get_mode rgb_matrix_get_mode
  178. # define rgblight_get_hue rgb_matrix_get_hue
  179. # define rgblight_get_sat rgb_matrix_get_sat
  180. # define rgblight_get_val rgb_matrix_get_val
  181. # define rgblight_get_hsv rgb_matrix_get_hsv
  182. # define rgblight_step rgb_matrix_step
  183. # define rgblight_step_noeeprom rgb_matrix_step_noeeprom
  184. # define rgblight_step_reverse rgb_matrix_step_reverse
  185. # define rgblight_step_reverse_noeeprom rgb_matrix_step_reverse_noeeprom
  186. # define rgblight_sethsv rgb_matrix_sethsv
  187. # define rgblight_sethsv_noeeprom rgb_matrix_sethsv_noeeprom
  188. # define rgblight_increase_hue rgb_matrix_increase_hue
  189. # define rgblight_increase_hue_noeeprom rgb_matrix_increase_hue_noeeprom
  190. # define rgblight_decrease_hue rgb_matrix_decrease_hue
  191. # define rgblight_decrease_hue_noeeprom rgb_matrix_decrease_hue_noeeprom
  192. # define rgblight_increase_sat rgb_matrix_increase_sat
  193. # define rgblight_increase_sat_noeeprom rgb_matrix_increase_sat_noeeprom
  194. # define rgblight_decrease_sat rgb_matrix_decrease_sat
  195. # define rgblight_decrease_sat_noeeprom rgb_matrix_decrease_sat_noeeprom
  196. # define rgblight_increase_val rgb_matrix_increase_val
  197. # define rgblight_increase_val_noeeprom rgb_matrix_increase_val_noeeprom
  198. # define rgblight_decrease_val rgb_matrix_decrease_val
  199. # define rgblight_decrease_val_noeeprom rgb_matrix_decrease_val_noeeprom
  200. # define rgblight_set_speed rgb_matrix_set_speed
  201. # define rgblight_set_speed_noeeprom rgb_matrix_set_speed_noeeprom
  202. # define rgblight_get_speed rgb_matrix_get_speed
  203. # define rgblight_increase_speed rgb_matrix_increase_speed
  204. # define rgblight_increase_speed_noeeprom rgb_matrix_increase_speed_noeeprom
  205. # define rgblight_decrease_speed rgb_matrix_decrease_speed
  206. # define rgblight_decrease_speed_noeeprom rgb_matrix_decrease_speed_noeeprom
  207. #endif
  208. typedef struct {
  209. /* Perform any initialisation required for the other driver functions to work. */
  210. void (*init)(void);
  211. /* Set the colour of a single LED in the buffer. */
  212. void (*set_color)(int index, uint8_t r, uint8_t g, uint8_t b);
  213. /* Set the colour of all LEDS on the keyboard in the buffer. */
  214. void (*set_color_all)(uint8_t r, uint8_t g, uint8_t b);
  215. /* Flush any buffered changes to the hardware. */
  216. void (*flush)(void);
  217. } rgb_matrix_driver_t;
  218. static inline bool rgb_matrix_check_finished_leds(uint8_t led_idx) {
  219. #if defined(RGB_MATRIX_SPLIT)
  220. if (is_keyboard_left()) {
  221. uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
  222. return led_idx < k_rgb_matrix_split[0];
  223. } else
  224. return led_idx < RGB_MATRIX_LED_COUNT;
  225. #else
  226. return led_idx < RGB_MATRIX_LED_COUNT;
  227. #endif
  228. }
  229. extern const rgb_matrix_driver_t rgb_matrix_driver;
  230. extern rgb_config_t rgb_matrix_config;
  231. extern uint32_t g_rgb_timer;
  232. extern led_config_t g_led_config;
  233. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  234. extern last_hit_t g_last_hit_tracker;
  235. #endif
  236. #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
  237. extern uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS];
  238. #endif