rgb_matrix.c 22 KB

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