rgb_matrix.c 22 KB

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