rgb_matrix.c 22 KB

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