rgb_matrix.c 20 KB

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