rgb_matrix.c 19 KB

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