rgb_matrix.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. #include "rgb_matrix.h"
  19. #include "progmem.h"
  20. #include "config.h"
  21. #include "eeprom.h"
  22. #include <string.h>
  23. #include <math.h>
  24. #include "lib/lib8tion/lib8tion.h"
  25. #include "rgb_matrix_animations/solid_color_anim.h"
  26. #include "rgb_matrix_animations/alpha_mods_anim.h"
  27. #include "rgb_matrix_animations/dual_beacon_anim.h"
  28. #include "rgb_matrix_animations/gradient_up_down_anim.h"
  29. #include "rgb_matrix_animations/raindrops_anim.h"
  30. #include "rgb_matrix_animations/cycle_all_anim.h"
  31. #include "rgb_matrix_animations/cycle_left_right_anim.h"
  32. #include "rgb_matrix_animations/cycle_up_down_anim.h"
  33. #include "rgb_matrix_animations/rainbow_beacon_anim.h"
  34. #include "rgb_matrix_animations/rainbow_pinwheels_anim.h"
  35. #include "rgb_matrix_animations/rainbow_moving_chevron_anim.h"
  36. #include "rgb_matrix_animations/jellybean_raindrops_anim.h"
  37. #include "rgb_matrix_animations/typing_heatmap_anim.h"
  38. #include "rgb_matrix_animations/digital_rain_anim.h"
  39. #include "rgb_matrix_animations/solid_reactive_simple_anim.h"
  40. #include "rgb_matrix_animations/solid_reactive_anim.h"
  41. #include "rgb_matrix_animations/solid_reactive_wide.h"
  42. #include "rgb_matrix_animations/solid_reactive_cross.h"
  43. #include "rgb_matrix_animations/solid_reactive_nexus.h"
  44. #include "rgb_matrix_animations/splash_anim.h"
  45. #include "rgb_matrix_animations/solid_splash_anim.h"
  46. #include "rgb_matrix_animations/breathing_anim.h"
  47. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  48. #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  49. #define RGB_MATRIX_EFFECT(name, ...)
  50. #ifdef RGB_MATRIX_CUSTOM_KB
  51. #include "rgb_matrix_kb.inc"
  52. #endif
  53. #ifdef RGB_MATRIX_CUSTOM_USER
  54. #include "rgb_matrix_user.inc"
  55. #endif
  56. #undef RGB_MATRIX_EFFECT
  57. #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  58. #endif
  59. #ifndef RGB_DISABLE_AFTER_TIMEOUT
  60. #define RGB_DISABLE_AFTER_TIMEOUT 0
  61. #endif
  62. #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
  63. #define RGB_DISABLE_WHEN_USB_SUSPENDED false
  64. #endif
  65. #ifndef EECONFIG_RGB_MATRIX
  66. #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
  67. #endif
  68. #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  69. #undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
  70. #define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  71. #endif
  72. #if !defined(RGB_MATRIX_HUE_STEP)
  73. #define RGB_MATRIX_HUE_STEP 8
  74. #endif
  75. #if !defined(RGB_MATRIX_SAT_STEP)
  76. #define RGB_MATRIX_SAT_STEP 16
  77. #endif
  78. #if !defined(RGB_MATRIX_VAL_STEP)
  79. #define RGB_MATRIX_VAL_STEP 16
  80. #endif
  81. #if !defined(RGB_MATRIX_SPD_STEP)
  82. #define RGB_MATRIX_SPD_STEP 16
  83. #endif
  84. #if !defined(RGB_MATRIX_STARTUP_MODE)
  85. #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
  86. #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
  87. #else
  88. // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
  89. #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
  90. #endif
  91. #endif
  92. bool g_suspend_state = false;
  93. extern led_config_t g_led_config;
  94. rgb_config_t rgb_matrix_config;
  95. rgb_counters_t g_rgb_counters;
  96. static uint32_t rgb_counters_buffer;
  97. #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
  98. uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
  99. #endif
  100. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  101. last_hit_t g_last_hit_tracker;
  102. static last_hit_t last_hit_buffer;
  103. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  104. uint32_t eeconfig_read_rgb_matrix(void) {
  105. return eeprom_read_dword(EECONFIG_RGB_MATRIX);
  106. }
  107. void eeconfig_update_rgb_matrix(uint32_t val) {
  108. eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
  109. }
  110. void eeconfig_update_rgb_matrix_default(void) {
  111. dprintf("eeconfig_update_rgb_matrix_default\n");
  112. rgb_matrix_config.enable = 1;
  113. rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
  114. rgb_matrix_config.hue = 0;
  115. rgb_matrix_config.sat = UINT8_MAX;
  116. rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  117. rgb_matrix_config.speed = UINT8_MAX / 2;
  118. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  119. }
  120. void eeconfig_debug_rgb_matrix(void) {
  121. dprintf("rgb_matrix_config eprom\n");
  122. dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
  123. dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
  124. dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
  125. dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
  126. dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
  127. dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
  128. }
  129. __attribute__ ((weak))
  130. uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
  131. return 0;
  132. }
  133. uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  134. uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
  135. uint8_t led_index = g_led_config.matrix_co[row][column];
  136. if (led_index != NO_LED) {
  137. led_i[led_count] = led_index;
  138. led_count++;
  139. }
  140. return led_count;
  141. }
  142. void rgb_matrix_update_pwm_buffers(void) {
  143. rgb_matrix_driver.flush();
  144. }
  145. void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
  146. rgb_matrix_driver.set_color(index, red, green, blue);
  147. }
  148. void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
  149. rgb_matrix_driver.set_color_all(red, green, blue);
  150. }
  151. bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
  152. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  153. uint8_t led[LED_HITS_TO_REMEMBER];
  154. uint8_t led_count = 0;
  155. #if defined(RGB_MATRIX_KEYRELEASES)
  156. if (!record->event.pressed) {
  157. led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  158. g_rgb_counters.any_key_hit = 0;
  159. }
  160. #elif defined(RGB_MATRIX_KEYPRESSES)
  161. if (record->event.pressed) {
  162. led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  163. g_rgb_counters.any_key_hit = 0;
  164. }
  165. #endif // defined(RGB_MATRIX_KEYRELEASES)
  166. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  167. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  168. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  169. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  170. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  171. last_hit_buffer.count--;
  172. }
  173. for(uint8_t i = 0; i < led_count; i++) {
  174. uint8_t index = last_hit_buffer.count;
  175. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  176. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  177. last_hit_buffer.index[index] = led[i];
  178. last_hit_buffer.tick[index] = 0;
  179. last_hit_buffer.count++;
  180. }
  181. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  182. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
  183. if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
  184. process_rgb_matrix_typing_heatmap(record);
  185. }
  186. #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
  187. return true;
  188. }
  189. void rgb_matrix_test(void) {
  190. // Mask out bits 4 and 5
  191. // Increase the factor to make the test animation slower (and reduce to make it faster)
  192. uint8_t factor = 10;
  193. switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
  194. {
  195. case 0: {
  196. rgb_matrix_set_color_all( 20, 0, 0 );
  197. break;
  198. }
  199. case 1: {
  200. rgb_matrix_set_color_all( 0, 20, 0 );
  201. break;
  202. }
  203. case 2: {
  204. rgb_matrix_set_color_all( 0, 0, 20 );
  205. break;
  206. }
  207. case 3: {
  208. rgb_matrix_set_color_all( 20, 20, 20 );
  209. break;
  210. }
  211. }
  212. }
  213. static bool rgb_matrix_none(effect_params_t* params) {
  214. if (!params->init) {
  215. return false;
  216. }
  217. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  218. for (uint8_t i = led_min; i < led_max; i++) {
  219. rgb_matrix_set_color(i, 0, 0, 0);
  220. }
  221. return led_max < DRIVER_LED_TOTAL;
  222. }
  223. static uint8_t rgb_last_enable = UINT8_MAX;
  224. static uint8_t rgb_last_effect = UINT8_MAX;
  225. static effect_params_t rgb_effect_params = { 0, 0xFF };
  226. static rgb_task_states rgb_task_state = SYNCING;
  227. static void rgb_task_timers(void) {
  228. // Update double buffer timers
  229. uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
  230. rgb_counters_buffer = timer_read32();
  231. if (g_rgb_counters.any_key_hit < UINT32_MAX) {
  232. if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
  233. g_rgb_counters.any_key_hit = UINT32_MAX;
  234. } else {
  235. g_rgb_counters.any_key_hit += deltaTime;
  236. }
  237. }
  238. // Update double buffer last hit timers
  239. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  240. uint8_t count = last_hit_buffer.count;
  241. for (uint8_t i = 0; i < count; ++i) {
  242. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  243. last_hit_buffer.count--;
  244. continue;
  245. }
  246. last_hit_buffer.tick[i] += deltaTime;
  247. }
  248. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  249. }
  250. static void rgb_task_sync(void) {
  251. // next task
  252. if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
  253. rgb_task_state = STARTING;
  254. }
  255. static void rgb_task_start(void) {
  256. // reset iter
  257. rgb_effect_params.iter = 0;
  258. // update double buffers
  259. g_rgb_counters.tick = rgb_counters_buffer;
  260. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  261. g_last_hit_tracker = last_hit_buffer;
  262. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  263. // next task
  264. rgb_task_state = RENDERING;
  265. }
  266. static void rgb_task_render(uint8_t effect) {
  267. bool rendering = false;
  268. rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
  269. // each effect can opt to do calculations
  270. // and/or request PWM buffer updates.
  271. switch (effect) {
  272. case RGB_MATRIX_NONE:
  273. rendering = rgb_matrix_none(&rgb_effect_params);
  274. break;
  275. case RGB_MATRIX_SOLID_COLOR:
  276. rendering = rgb_matrix_solid_color(&rgb_effect_params); // Max 1ms Avg 0ms
  277. break;
  278. #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS
  279. case RGB_MATRIX_ALPHAS_MODS:
  280. rendering = rgb_matrix_alphas_mods(&rgb_effect_params); // Max 2ms Avg 1ms
  281. break;
  282. #endif // DISABLE_RGB_MATRIX_ALPHAS_MODS
  283. #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
  284. case RGB_MATRIX_GRADIENT_UP_DOWN:
  285. rendering = rgb_matrix_gradient_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
  286. break;
  287. #endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
  288. #ifndef DISABLE_RGB_MATRIX_BREATHING
  289. case RGB_MATRIX_BREATHING:
  290. rendering = rgb_matrix_breathing(&rgb_effect_params); // Max 1ms Avg 0ms
  291. break;
  292. #endif // DISABLE_RGB_MATRIX_BREATHING
  293. #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
  294. case RGB_MATRIX_CYCLE_ALL:
  295. rendering = rgb_matrix_cycle_all(&rgb_effect_params); // Max 4ms Avg 3ms
  296. break;
  297. #endif // DISABLE_RGB_MATRIX_CYCLE_ALL
  298. #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
  299. case RGB_MATRIX_CYCLE_LEFT_RIGHT:
  300. rendering = rgb_matrix_cycle_left_right(&rgb_effect_params); // Max 4ms Avg 3ms
  301. break;
  302. #endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
  303. #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
  304. case RGB_MATRIX_CYCLE_UP_DOWN:
  305. rendering = rgb_matrix_cycle_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
  306. break;
  307. #endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
  308. #ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
  309. case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
  310. rendering = rgb_matrix_rainbow_moving_chevron(&rgb_effect_params); // Max 4ms Avg 3ms
  311. break;
  312. #endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
  313. #ifndef DISABLE_RGB_MATRIX_DUAL_BEACON
  314. case RGB_MATRIX_DUAL_BEACON:
  315. rendering = rgb_matrix_dual_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
  316. break;
  317. #endif // DISABLE_RGB_MATRIX_DUAL_BEACON
  318. #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON
  319. case RGB_MATRIX_RAINBOW_BEACON:
  320. rendering = rgb_matrix_rainbow_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
  321. break;
  322. #endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON
  323. #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
  324. case RGB_MATRIX_RAINBOW_PINWHEELS:
  325. rendering = rgb_matrix_rainbow_pinwheels(&rgb_effect_params); // Max 4ms Avg 3ms
  326. break;
  327. #endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
  328. #ifndef DISABLE_RGB_MATRIX_RAINDROPS
  329. case RGB_MATRIX_RAINDROPS:
  330. rendering = rgb_matrix_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
  331. break;
  332. #endif // DISABLE_RGB_MATRIX_RAINDROPS
  333. #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
  334. case RGB_MATRIX_JELLYBEAN_RAINDROPS:
  335. rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
  336. break;
  337. #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
  338. #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
  339. #ifndef DISABLE_RGB_MATRIX_TYPING_HEATMAP
  340. case RGB_MATRIX_TYPING_HEATMAP:
  341. rendering = rgb_matrix_typing_heatmap(&rgb_effect_params); // Max 4ms Avg 3ms
  342. break;
  343. #endif // DISABLE_RGB_MATRIX_TYPING_HEATMAP
  344. #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN
  345. case RGB_MATRIX_DIGITAL_RAIN:
  346. rendering = rgb_matrix_digital_rain(&rgb_effect_params); // Max 9ms Avg 8ms | this is expensive, fix it
  347. break;
  348. #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN
  349. #endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS
  350. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  351. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE
  352. case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
  353. rendering = rgb_matrix_solid_reactive_simple(&rgb_effect_params);// Max 4ms Avg 3ms
  354. break;
  355. #endif
  356. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE
  357. case RGB_MATRIX_SOLID_REACTIVE:
  358. rendering = rgb_matrix_solid_reactive(&rgb_effect_params); // Max 4ms Avg 3ms
  359. break;
  360. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE
  361. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
  362. case RGB_MATRIX_SOLID_REACTIVE_WIDE:
  363. rendering = rgb_matrix_solid_reactive_wide(&rgb_effect_params); // Max ?? ms Avg ?? ms
  364. break;
  365. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
  366. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
  367. case RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE:
  368. rendering = rgb_matrix_solid_reactive_multiwide(&rgb_effect_params); // Max ?? ms Avg ?? ms
  369. break;
  370. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
  371. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
  372. case RGB_MATRIX_SOLID_REACTIVE_CROSS:
  373. rendering = rgb_matrix_solid_reactive_cross(&rgb_effect_params); // Max ?? ms Avg ?? ms
  374. break;
  375. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
  376. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
  377. case RGB_MATRIX_SOLID_REACTIVE_MULTICROSS:
  378. rendering = rgb_matrix_solid_reactive_multicross(&rgb_effect_params); // Max ?? ms Avg ?? ms
  379. break;
  380. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
  381. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
  382. case RGB_MATRIX_SOLID_REACTIVE_NEXUS:
  383. rendering = rgb_matrix_solid_reactive_nexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
  384. break;
  385. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
  386. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
  387. case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
  388. rendering = rgb_matrix_solid_reactive_multinexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
  389. break;
  390. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
  391. #ifndef DISABLE_RGB_MATRIX_SPLASH
  392. case RGB_MATRIX_SPLASH:
  393. rendering = rgb_matrix_splash(&rgb_effect_params); // Max 5ms Avg 3ms
  394. break;
  395. #endif // DISABLE_RGB_MATRIX_SPLASH
  396. #ifndef DISABLE_RGB_MATRIX_MULTISPLASH
  397. case RGB_MATRIX_MULTISPLASH:
  398. rendering = rgb_matrix_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
  399. break;
  400. #endif // DISABLE_RGB_MATRIX_MULTISPLASH
  401. #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH
  402. case RGB_MATRIX_SOLID_SPLASH:
  403. rendering = rgb_matrix_solid_splash(&rgb_effect_params); // Max 5ms Avg 3ms
  404. break;
  405. #endif // DISABLE_RGB_MATRIX_SOLID_SPLASH
  406. #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
  407. case RGB_MATRIX_SOLID_MULTISPLASH:
  408. rendering = rgb_matrix_solid_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
  409. break;
  410. #endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
  411. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  412. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  413. #define RGB_MATRIX_EFFECT(name, ...) \
  414. case RGB_MATRIX_CUSTOM_##name: \
  415. rendering = name(&rgb_effect_params); \
  416. break;
  417. #ifdef RGB_MATRIX_CUSTOM_KB
  418. #include "rgb_matrix_kb.inc"
  419. #endif
  420. #ifdef RGB_MATRIX_CUSTOM_USER
  421. #include "rgb_matrix_user.inc"
  422. #endif
  423. #undef RGB_MATRIX_EFFECT
  424. #endif
  425. // Factory default magic value
  426. case UINT8_MAX: {
  427. rgb_matrix_test();
  428. rgb_task_state = FLUSHING;
  429. }
  430. return;
  431. }
  432. rgb_effect_params.iter++;
  433. // next task
  434. if (!rendering) {
  435. rgb_task_state = FLUSHING;
  436. if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
  437. // We only need to flush once if we are RGB_MATRIX_NONE
  438. rgb_task_state = SYNCING;
  439. }
  440. }
  441. }
  442. static void rgb_task_flush(uint8_t effect) {
  443. // update last trackers after the first full render so we can init over several frames
  444. rgb_last_effect = effect;
  445. rgb_last_enable = rgb_matrix_config.enable;
  446. // update pwm buffers
  447. rgb_matrix_update_pwm_buffers();
  448. // next task
  449. rgb_task_state = SYNCING;
  450. }
  451. void rgb_matrix_task(void) {
  452. rgb_task_timers();
  453. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  454. // while suspended and just do a software shutdown. This is a cheap hack for now.
  455. bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_rgb_counters.any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
  456. uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
  457. switch (rgb_task_state) {
  458. case STARTING:
  459. rgb_task_start();
  460. break;
  461. case RENDERING:
  462. rgb_task_render(effect);
  463. break;
  464. case FLUSHING:
  465. rgb_task_flush(effect);
  466. break;
  467. case SYNCING:
  468. rgb_task_sync();
  469. break;
  470. }
  471. if (!suspend_backlight) {
  472. rgb_matrix_indicators();
  473. }
  474. }
  475. void rgb_matrix_indicators(void) {
  476. rgb_matrix_indicators_kb();
  477. rgb_matrix_indicators_user();
  478. }
  479. __attribute__((weak))
  480. void rgb_matrix_indicators_kb(void) {}
  481. __attribute__((weak))
  482. void rgb_matrix_indicators_user(void) {}
  483. void rgb_matrix_init(void) {
  484. rgb_matrix_driver.init();
  485. // TODO: put the 1 second startup delay here?
  486. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  487. g_last_hit_tracker.count = 0;
  488. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  489. g_last_hit_tracker.tick[i] = UINT16_MAX;
  490. }
  491. last_hit_buffer.count = 0;
  492. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  493. last_hit_buffer.tick[i] = UINT16_MAX;
  494. }
  495. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  496. if (!eeconfig_is_enabled()) {
  497. dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
  498. eeconfig_init();
  499. eeconfig_update_rgb_matrix_default();
  500. }
  501. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  502. rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
  503. if (!rgb_matrix_config.mode) {
  504. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  505. eeconfig_update_rgb_matrix_default();
  506. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  507. }
  508. eeconfig_debug_rgb_matrix(); // display current eeprom values
  509. }
  510. void rgb_matrix_set_suspend_state(bool state) {
  511. g_suspend_state = state;
  512. }
  513. void rgb_matrix_toggle(void) {
  514. rgb_matrix_config.enable ^= 1;
  515. rgb_task_state = STARTING;
  516. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  517. }
  518. void rgb_matrix_enable(void) {
  519. rgb_matrix_enable_noeeprom();
  520. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  521. }
  522. void rgb_matrix_enable_noeeprom(void) {
  523. if (!rgb_matrix_config.enable)
  524. rgb_task_state = STARTING;
  525. rgb_matrix_config.enable = 1;
  526. }
  527. void rgb_matrix_disable(void) {
  528. rgb_matrix_disable_noeeprom();
  529. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  530. }
  531. void rgb_matrix_disable_noeeprom(void) {
  532. if (rgb_matrix_config.enable)
  533. rgb_task_state = STARTING;
  534. rgb_matrix_config.enable = 0;
  535. }
  536. void rgb_matrix_step(void) {
  537. rgb_matrix_config.mode++;
  538. if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
  539. rgb_matrix_config.mode = 1;
  540. rgb_task_state = STARTING;
  541. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  542. }
  543. void rgb_matrix_step_reverse(void) {
  544. rgb_matrix_config.mode--;
  545. if (rgb_matrix_config.mode < 1)
  546. rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  547. rgb_task_state = STARTING;
  548. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  549. }
  550. void rgb_matrix_increase_hue(void) {
  551. rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP;
  552. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  553. }
  554. void rgb_matrix_decrease_hue(void) {
  555. rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP;
  556. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  557. }
  558. void rgb_matrix_increase_sat(void) {
  559. rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
  560. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  561. }
  562. void rgb_matrix_decrease_sat(void) {
  563. rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
  564. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  565. }
  566. void rgb_matrix_increase_val(void) {
  567. rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
  568. if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
  569. rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  570. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  571. }
  572. void rgb_matrix_decrease_val(void) {
  573. rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
  574. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  575. }
  576. void rgb_matrix_increase_speed(void) {
  577. rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
  578. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  579. }
  580. void rgb_matrix_decrease_speed(void) {
  581. rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
  582. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  583. }
  584. led_flags_t rgb_matrix_get_flags(void) {
  585. return rgb_effect_params.flags;
  586. }
  587. void rgb_matrix_set_flags(led_flags_t flags) {
  588. rgb_effect_params.flags = flags;
  589. }
  590. void rgb_matrix_mode(uint8_t mode) {
  591. rgb_matrix_config.mode = mode;
  592. rgb_task_state = STARTING;
  593. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  594. }
  595. void rgb_matrix_mode_noeeprom(uint8_t mode) {
  596. rgb_matrix_config.mode = mode;
  597. }
  598. uint8_t rgb_matrix_get_mode(void) {
  599. return rgb_matrix_config.mode;
  600. }
  601. void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  602. rgb_matrix_sethsv_noeeprom(hue, sat, val);
  603. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  604. }
  605. void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  606. rgb_matrix_config.hue = hue;
  607. rgb_matrix_config.sat = sat;
  608. rgb_matrix_config.val = val;
  609. if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
  610. rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  611. }