rgb_matrix.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. #ifndef RGB_MATRIX_CENTER
  26. const led_point_t k_rgb_matrix_center = {112, 32};
  27. #else
  28. const led_point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
  29. #endif
  30. __attribute__((weak)) RGB rgb_matrix_hsv_to_rgb(HSV hsv) {
  31. return hsv_to_rgb(hsv);
  32. }
  33. // Generic effect runners
  34. #include "rgb_matrix_runners.inc"
  35. // ------------------------------------------
  36. // -----Begin rgb effect includes macros-----
  37. #define RGB_MATRIX_EFFECT(name)
  38. #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  39. #include "rgb_matrix_effects.inc"
  40. #ifdef RGB_MATRIX_CUSTOM_KB
  41. # include "rgb_matrix_kb.inc"
  42. #endif
  43. #ifdef RGB_MATRIX_CUSTOM_USER
  44. # include "rgb_matrix_user.inc"
  45. #endif
  46. #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  47. #undef RGB_MATRIX_EFFECT
  48. // -----End rgb effect includes macros-------
  49. // ------------------------------------------
  50. #ifndef RGB_MATRIX_TIMEOUT
  51. # define RGB_MATRIX_TIMEOUT 0
  52. #endif
  53. #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  54. # undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
  55. # define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  56. #endif
  57. #if !defined(RGB_MATRIX_HUE_STEP)
  58. # define RGB_MATRIX_HUE_STEP 8
  59. #endif
  60. #if !defined(RGB_MATRIX_SAT_STEP)
  61. # define RGB_MATRIX_SAT_STEP 16
  62. #endif
  63. #if !defined(RGB_MATRIX_VAL_STEP)
  64. # define RGB_MATRIX_VAL_STEP 16
  65. #endif
  66. #if !defined(RGB_MATRIX_SPD_STEP)
  67. # define RGB_MATRIX_SPD_STEP 16
  68. #endif
  69. #if !defined(RGB_MATRIX_DEFAULT_MODE)
  70. # ifdef ENABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
  71. # define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
  72. # else
  73. // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
  74. # define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_SOLID_COLOR
  75. # endif
  76. #endif
  77. #if !defined(RGB_MATRIX_DEFAULT_HUE)
  78. # define RGB_MATRIX_DEFAULT_HUE 0
  79. #endif
  80. #if !defined(RGB_MATRIX_DEFAULT_SAT)
  81. # define RGB_MATRIX_DEFAULT_SAT UINT8_MAX
  82. #endif
  83. #if !defined(RGB_MATRIX_DEFAULT_VAL)
  84. # define RGB_MATRIX_DEFAULT_VAL RGB_MATRIX_MAXIMUM_BRIGHTNESS
  85. #endif
  86. #if !defined(RGB_MATRIX_DEFAULT_SPD)
  87. # define RGB_MATRIX_DEFAULT_SPD UINT8_MAX / 2
  88. #endif
  89. // globals
  90. rgb_config_t rgb_matrix_config; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
  91. uint32_t g_rgb_timer;
  92. #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
  93. uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
  94. #endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS
  95. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  96. last_hit_t g_last_hit_tracker;
  97. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  98. // internals
  99. static bool suspend_state = false;
  100. static uint8_t rgb_last_enable = UINT8_MAX;
  101. static uint8_t rgb_last_effect = UINT8_MAX;
  102. static effect_params_t rgb_effect_params = {0, LED_FLAG_ALL, false};
  103. static rgb_task_states rgb_task_state = SYNCING;
  104. #if RGB_MATRIX_TIMEOUT > 0
  105. static uint32_t rgb_anykey_timer;
  106. #endif // RGB_MATRIX_TIMEOUT > 0
  107. // double buffers
  108. static uint32_t rgb_timer_buffer;
  109. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  110. static last_hit_t last_hit_buffer;
  111. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  112. // split rgb matrix
  113. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  114. const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
  115. #endif
  116. EECONFIG_DEBOUNCE_HELPER(rgb_matrix, EECONFIG_RGB_MATRIX, rgb_matrix_config);
  117. void eeconfig_update_rgb_matrix(void) {
  118. eeconfig_flush_rgb_matrix(true);
  119. }
  120. void eeconfig_update_rgb_matrix_default(void) {
  121. dprintf("eeconfig_update_rgb_matrix_default\n");
  122. rgb_matrix_config.enable = 1;
  123. rgb_matrix_config.mode = RGB_MATRIX_DEFAULT_MODE;
  124. rgb_matrix_config.hsv = (HSV){RGB_MATRIX_DEFAULT_HUE, RGB_MATRIX_DEFAULT_SAT, RGB_MATRIX_DEFAULT_VAL};
  125. rgb_matrix_config.speed = RGB_MATRIX_DEFAULT_SPD;
  126. rgb_matrix_config.flags = LED_FLAG_ALL;
  127. eeconfig_flush_rgb_matrix(true);
  128. }
  129. void eeconfig_debug_rgb_matrix(void) {
  130. dprintf("rgb_matrix_config EEPROM\n");
  131. dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
  132. dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
  133. dprintf("rgb_matrix_config.hsv.h = %d\n", rgb_matrix_config.hsv.h);
  134. dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s);
  135. dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v);
  136. dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
  137. dprintf("rgb_matrix_config.flags = %d\n", rgb_matrix_config.flags);
  138. }
  139. void rgb_matrix_reload_from_eeprom(void) {
  140. rgb_matrix_disable_noeeprom();
  141. /* Reset back to what we have in eeprom */
  142. eeconfig_init_rgb_matrix();
  143. eeconfig_debug_rgb_matrix(); // display current eeprom values
  144. if (rgb_matrix_config.enable) {
  145. rgb_matrix_mode_noeeprom(rgb_matrix_config.mode);
  146. }
  147. }
  148. __attribute__((weak)) uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
  149. return 0;
  150. }
  151. uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  152. uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
  153. uint8_t led_index = g_led_config.matrix_co[row][column];
  154. if (led_index != NO_LED) {
  155. led_i[led_count] = led_index;
  156. led_count++;
  157. }
  158. return led_count;
  159. }
  160. void rgb_matrix_update_pwm_buffers(void) {
  161. rgb_matrix_driver.flush();
  162. }
  163. void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  164. rgb_matrix_driver.set_color(index, red, green, blue);
  165. }
  166. void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  167. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  168. for (uint8_t i = 0; i < RGB_MATRIX_LED_COUNT; i++)
  169. rgb_matrix_set_color(i, red, green, blue);
  170. #else
  171. rgb_matrix_driver.set_color_all(red, green, blue);
  172. #endif
  173. }
  174. void process_rgb_matrix(uint8_t row, uint8_t col, bool pressed) {
  175. #ifndef RGB_MATRIX_SPLIT
  176. if (!is_keyboard_master()) return;
  177. #endif
  178. #if RGB_MATRIX_TIMEOUT > 0
  179. rgb_anykey_timer = 0;
  180. #endif // RGB_MATRIX_TIMEOUT > 0
  181. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  182. uint8_t led[LED_HITS_TO_REMEMBER];
  183. uint8_t led_count = 0;
  184. # if defined(RGB_MATRIX_KEYRELEASES)
  185. if (!pressed)
  186. # elif defined(RGB_MATRIX_KEYPRESSES)
  187. if (pressed)
  188. # endif // defined(RGB_MATRIX_KEYRELEASES)
  189. {
  190. led_count = rgb_matrix_map_row_column_to_led(row, col, led);
  191. }
  192. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  193. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  194. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  195. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  196. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  197. last_hit_buffer.count = LED_HITS_TO_REMEMBER - led_count;
  198. }
  199. for (uint8_t i = 0; i < led_count; i++) {
  200. uint8_t index = last_hit_buffer.count;
  201. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  202. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  203. last_hit_buffer.index[index] = led[i];
  204. last_hit_buffer.tick[index] = 0;
  205. last_hit_buffer.count++;
  206. }
  207. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  208. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)
  209. # if defined(RGB_MATRIX_KEYRELEASES)
  210. if (!pressed)
  211. # else
  212. if (pressed)
  213. # endif // defined(RGB_MATRIX_KEYRELEASES)
  214. {
  215. if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
  216. process_rgb_matrix_typing_heatmap(row, col);
  217. }
  218. }
  219. #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)
  220. }
  221. void rgb_matrix_test(void) {
  222. // Mask out bits 4 and 5
  223. // Increase the factor to make the test animation slower (and reduce to make it faster)
  224. uint8_t factor = 10;
  225. switch ((g_rgb_timer & (0b11 << factor)) >> factor) {
  226. case 0: {
  227. rgb_matrix_set_color_all(20, 0, 0);
  228. break;
  229. }
  230. case 1: {
  231. rgb_matrix_set_color_all(0, 20, 0);
  232. break;
  233. }
  234. case 2: {
  235. rgb_matrix_set_color_all(0, 0, 20);
  236. break;
  237. }
  238. case 3: {
  239. rgb_matrix_set_color_all(20, 20, 20);
  240. break;
  241. }
  242. }
  243. }
  244. static bool rgb_matrix_none(effect_params_t *params) {
  245. if (!params->init) {
  246. return false;
  247. }
  248. rgb_matrix_set_color_all(0, 0, 0);
  249. return false;
  250. }
  251. static void rgb_task_timers(void) {
  252. #if defined(RGB_MATRIX_KEYREACTIVE_ENABLED) || RGB_MATRIX_TIMEOUT > 0
  253. uint32_t deltaTime = sync_timer_elapsed32(rgb_timer_buffer);
  254. #endif // defined(RGB_MATRIX_KEYREACTIVE_ENABLED) || RGB_MATRIX_TIMEOUT > 0
  255. rgb_timer_buffer = sync_timer_read32();
  256. // Update double buffer timers
  257. #if RGB_MATRIX_TIMEOUT > 0
  258. if (rgb_anykey_timer + deltaTime <= UINT32_MAX) {
  259. rgb_anykey_timer += deltaTime;
  260. }
  261. #endif // RGB_MATRIX_TIMEOUT > 0
  262. // Update double buffer last hit timers
  263. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  264. uint8_t count = last_hit_buffer.count;
  265. for (uint8_t i = 0; i < count; ++i) {
  266. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  267. last_hit_buffer.count--;
  268. continue;
  269. }
  270. last_hit_buffer.tick[i] += deltaTime;
  271. }
  272. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  273. }
  274. static void rgb_task_sync(void) {
  275. eeconfig_flush_rgb_matrix(false);
  276. // next task
  277. if (sync_timer_elapsed32(g_rgb_timer) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
  278. }
  279. static void rgb_task_start(void) {
  280. // reset iter
  281. rgb_effect_params.iter = 0;
  282. // update double buffers
  283. g_rgb_timer = rgb_timer_buffer;
  284. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  285. g_last_hit_tracker = last_hit_buffer;
  286. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  287. // next task
  288. rgb_task_state = RENDERING;
  289. }
  290. static void rgb_task_render(uint8_t effect) {
  291. bool rendering = false;
  292. rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
  293. if (rgb_effect_params.flags != rgb_matrix_config.flags) {
  294. rgb_effect_params.flags = rgb_matrix_config.flags;
  295. rgb_matrix_set_color_all(0, 0, 0);
  296. }
  297. // each effect can opt to do calculations
  298. // and/or request PWM buffer updates.
  299. switch (effect) {
  300. case RGB_MATRIX_NONE:
  301. rendering = rgb_matrix_none(&rgb_effect_params);
  302. break;
  303. // ---------------------------------------------
  304. // -----Begin rgb effect switch case macros-----
  305. #define RGB_MATRIX_EFFECT(name, ...) \
  306. case RGB_MATRIX_##name: \
  307. rendering = name(&rgb_effect_params); \
  308. break;
  309. #include "rgb_matrix_effects.inc"
  310. #undef RGB_MATRIX_EFFECT
  311. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  312. # define RGB_MATRIX_EFFECT(name, ...) \
  313. case RGB_MATRIX_CUSTOM_##name: \
  314. rendering = name(&rgb_effect_params); \
  315. break;
  316. # ifdef RGB_MATRIX_CUSTOM_KB
  317. # include "rgb_matrix_kb.inc"
  318. # endif
  319. # ifdef RGB_MATRIX_CUSTOM_USER
  320. # include "rgb_matrix_user.inc"
  321. # endif
  322. # undef RGB_MATRIX_EFFECT
  323. #endif
  324. // -----End rgb effect switch case macros-------
  325. // ---------------------------------------------
  326. // Factory default magic value
  327. case UINT8_MAX: {
  328. rgb_matrix_test();
  329. rgb_task_state = FLUSHING;
  330. }
  331. return;
  332. }
  333. rgb_effect_params.iter++;
  334. // next task
  335. if (!rendering) {
  336. rgb_task_state = FLUSHING;
  337. if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
  338. // We only need to flush once if we are RGB_MATRIX_NONE
  339. rgb_task_state = SYNCING;
  340. }
  341. }
  342. }
  343. static void rgb_task_flush(uint8_t effect) {
  344. // update last trackers after the first full render so we can init over several frames
  345. rgb_last_effect = effect;
  346. rgb_last_enable = rgb_matrix_config.enable;
  347. // update pwm buffers
  348. rgb_matrix_update_pwm_buffers();
  349. // next task
  350. rgb_task_state = SYNCING;
  351. }
  352. void rgb_matrix_task(void) {
  353. rgb_task_timers();
  354. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  355. // while suspended and just do a software shutdown. This is a cheap hack for now.
  356. bool suspend_backlight = suspend_state ||
  357. #if RGB_MATRIX_TIMEOUT > 0
  358. (rgb_anykey_timer > (uint32_t)RGB_MATRIX_TIMEOUT) ||
  359. #endif // RGB_MATRIX_TIMEOUT > 0
  360. false;
  361. uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
  362. switch (rgb_task_state) {
  363. case STARTING:
  364. rgb_task_start();
  365. break;
  366. case RENDERING:
  367. rgb_task_render(effect);
  368. if (effect) {
  369. rgb_matrix_indicators();
  370. rgb_matrix_indicators_advanced(&rgb_effect_params);
  371. }
  372. break;
  373. case FLUSHING:
  374. rgb_task_flush(effect);
  375. break;
  376. case SYNCING:
  377. rgb_task_sync();
  378. break;
  379. }
  380. }
  381. void rgb_matrix_indicators(void) {
  382. rgb_matrix_indicators_kb();
  383. }
  384. __attribute__((weak)) bool rgb_matrix_indicators_kb(void) {
  385. return rgb_matrix_indicators_user();
  386. }
  387. __attribute__((weak)) bool rgb_matrix_indicators_user(void) {
  388. return true;
  389. }
  390. void rgb_matrix_indicators_advanced(effect_params_t *params) {
  391. /* special handling is needed for "params->iter", since it's already been incremented.
  392. * Could move the invocations to rgb_task_render, but then it's missing a few checks
  393. * and not sure which would be better. Otherwise, this should be called from
  394. * rgb_task_render, right before the iter++ line.
  395. */
  396. #if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < RGB_MATRIX_LED_COUNT
  397. uint8_t min = RGB_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1);
  398. uint8_t max = min + RGB_MATRIX_LED_PROCESS_LIMIT;
  399. if (max > RGB_MATRIX_LED_COUNT) max = RGB_MATRIX_LED_COUNT;
  400. #else
  401. uint8_t min = 0;
  402. uint8_t max = RGB_MATRIX_LED_COUNT;
  403. #endif
  404. rgb_matrix_indicators_advanced_kb(min, max);
  405. }
  406. __attribute__((weak)) bool rgb_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {
  407. return rgb_matrix_indicators_advanced_user(led_min, led_max);
  408. }
  409. __attribute__((weak)) bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
  410. return true;
  411. }
  412. void rgb_matrix_init(void) {
  413. rgb_matrix_driver.init();
  414. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  415. g_last_hit_tracker.count = 0;
  416. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  417. g_last_hit_tracker.tick[i] = UINT16_MAX;
  418. }
  419. last_hit_buffer.count = 0;
  420. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  421. last_hit_buffer.tick[i] = UINT16_MAX;
  422. }
  423. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  424. if (!eeconfig_is_enabled()) {
  425. dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
  426. eeconfig_init();
  427. eeconfig_update_rgb_matrix_default();
  428. }
  429. eeconfig_init_rgb_matrix();
  430. if (!rgb_matrix_config.mode) {
  431. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  432. eeconfig_update_rgb_matrix_default();
  433. }
  434. eeconfig_debug_rgb_matrix(); // display current eeprom values
  435. }
  436. void rgb_matrix_set_suspend_state(bool state) {
  437. #ifdef RGB_DISABLE_WHEN_USB_SUSPENDED
  438. if (state && !suspend_state) { // only run if turning off, and only once
  439. rgb_task_render(0); // turn off all LEDs when suspending
  440. rgb_task_flush(0); // and actually flash led state to LEDs
  441. }
  442. suspend_state = state;
  443. #endif
  444. }
  445. bool rgb_matrix_get_suspend_state(void) {
  446. return suspend_state;
  447. }
  448. void rgb_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
  449. rgb_matrix_config.enable ^= 1;
  450. rgb_task_state = STARTING;
  451. eeconfig_flag_rgb_matrix(write_to_eeprom);
  452. dprintf("rgb matrix toggle [%s]: rgb_matrix_config.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.enable);
  453. }
  454. void rgb_matrix_toggle_noeeprom(void) {
  455. rgb_matrix_toggle_eeprom_helper(false);
  456. }
  457. void rgb_matrix_toggle(void) {
  458. rgb_matrix_toggle_eeprom_helper(true);
  459. }
  460. void rgb_matrix_enable(void) {
  461. rgb_matrix_enable_noeeprom();
  462. eeconfig_flag_rgb_matrix(true);
  463. }
  464. void rgb_matrix_enable_noeeprom(void) {
  465. if (!rgb_matrix_config.enable) rgb_task_state = STARTING;
  466. rgb_matrix_config.enable = 1;
  467. }
  468. void rgb_matrix_disable(void) {
  469. rgb_matrix_disable_noeeprom();
  470. eeconfig_flag_rgb_matrix(true);
  471. }
  472. void rgb_matrix_disable_noeeprom(void) {
  473. if (rgb_matrix_config.enable) rgb_task_state = STARTING;
  474. rgb_matrix_config.enable = 0;
  475. }
  476. uint8_t rgb_matrix_is_enabled(void) {
  477. return rgb_matrix_config.enable;
  478. }
  479. void rgb_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  480. if (!rgb_matrix_config.enable) {
  481. return;
  482. }
  483. if (mode < 1) {
  484. rgb_matrix_config.mode = 1;
  485. } else if (mode >= RGB_MATRIX_EFFECT_MAX) {
  486. rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  487. } else {
  488. rgb_matrix_config.mode = mode;
  489. }
  490. rgb_task_state = STARTING;
  491. eeconfig_flag_rgb_matrix(write_to_eeprom);
  492. dprintf("rgb matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.mode);
  493. }
  494. void rgb_matrix_mode_noeeprom(uint8_t mode) {
  495. rgb_matrix_mode_eeprom_helper(mode, false);
  496. }
  497. void rgb_matrix_mode(uint8_t mode) {
  498. rgb_matrix_mode_eeprom_helper(mode, true);
  499. }
  500. uint8_t rgb_matrix_get_mode(void) {
  501. return rgb_matrix_config.mode;
  502. }
  503. void rgb_matrix_step_helper(bool write_to_eeprom) {
  504. uint8_t mode = rgb_matrix_config.mode + 1;
  505. rgb_matrix_mode_eeprom_helper((mode < RGB_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
  506. }
  507. void rgb_matrix_step_noeeprom(void) {
  508. rgb_matrix_step_helper(false);
  509. }
  510. void rgb_matrix_step(void) {
  511. rgb_matrix_step_helper(true);
  512. }
  513. void rgb_matrix_step_reverse_helper(bool write_to_eeprom) {
  514. uint8_t mode = rgb_matrix_config.mode - 1;
  515. rgb_matrix_mode_eeprom_helper((mode < 1) ? RGB_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
  516. }
  517. void rgb_matrix_step_reverse_noeeprom(void) {
  518. rgb_matrix_step_reverse_helper(false);
  519. }
  520. void rgb_matrix_step_reverse(void) {
  521. rgb_matrix_step_reverse_helper(true);
  522. }
  523. void rgb_matrix_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
  524. if (!rgb_matrix_config.enable) {
  525. return;
  526. }
  527. rgb_matrix_config.hsv.h = hue;
  528. rgb_matrix_config.hsv.s = sat;
  529. rgb_matrix_config.hsv.v = (val > RGB_MATRIX_MAXIMUM_BRIGHTNESS) ? RGB_MATRIX_MAXIMUM_BRIGHTNESS : val;
  530. eeconfig_flag_rgb_matrix(write_to_eeprom);
  531. dprintf("rgb matrix set hsv [%s]: %u,%u,%u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v);
  532. }
  533. void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  534. rgb_matrix_sethsv_eeprom_helper(hue, sat, val, false);
  535. }
  536. void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  537. rgb_matrix_sethsv_eeprom_helper(hue, sat, val, true);
  538. }
  539. HSV rgb_matrix_get_hsv(void) {
  540. return rgb_matrix_config.hsv;
  541. }
  542. uint8_t rgb_matrix_get_hue(void) {
  543. return rgb_matrix_config.hsv.h;
  544. }
  545. uint8_t rgb_matrix_get_sat(void) {
  546. return rgb_matrix_config.hsv.s;
  547. }
  548. uint8_t rgb_matrix_get_val(void) {
  549. return rgb_matrix_config.hsv.v;
  550. }
  551. void rgb_matrix_increase_hue_helper(bool write_to_eeprom) {
  552. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h + RGB_MATRIX_HUE_STEP, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v, write_to_eeprom);
  553. }
  554. void rgb_matrix_increase_hue_noeeprom(void) {
  555. rgb_matrix_increase_hue_helper(false);
  556. }
  557. void rgb_matrix_increase_hue(void) {
  558. rgb_matrix_increase_hue_helper(true);
  559. }
  560. void rgb_matrix_decrease_hue_helper(bool write_to_eeprom) {
  561. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h - RGB_MATRIX_HUE_STEP, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v, write_to_eeprom);
  562. }
  563. void rgb_matrix_decrease_hue_noeeprom(void) {
  564. rgb_matrix_decrease_hue_helper(false);
  565. }
  566. void rgb_matrix_decrease_hue(void) {
  567. rgb_matrix_decrease_hue_helper(true);
  568. }
  569. void rgb_matrix_increase_sat_helper(bool write_to_eeprom) {
  570. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, qadd8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP), rgb_matrix_config.hsv.v, write_to_eeprom);
  571. }
  572. void rgb_matrix_increase_sat_noeeprom(void) {
  573. rgb_matrix_increase_sat_helper(false);
  574. }
  575. void rgb_matrix_increase_sat(void) {
  576. rgb_matrix_increase_sat_helper(true);
  577. }
  578. void rgb_matrix_decrease_sat_helper(bool write_to_eeprom) {
  579. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, qsub8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP), rgb_matrix_config.hsv.v, write_to_eeprom);
  580. }
  581. void rgb_matrix_decrease_sat_noeeprom(void) {
  582. rgb_matrix_decrease_sat_helper(false);
  583. }
  584. void rgb_matrix_decrease_sat(void) {
  585. rgb_matrix_decrease_sat_helper(true);
  586. }
  587. void rgb_matrix_increase_val_helper(bool write_to_eeprom) {
  588. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom);
  589. }
  590. void rgb_matrix_increase_val_noeeprom(void) {
  591. rgb_matrix_increase_val_helper(false);
  592. }
  593. void rgb_matrix_increase_val(void) {
  594. rgb_matrix_increase_val_helper(true);
  595. }
  596. void rgb_matrix_decrease_val_helper(bool write_to_eeprom) {
  597. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom);
  598. }
  599. void rgb_matrix_decrease_val_noeeprom(void) {
  600. rgb_matrix_decrease_val_helper(false);
  601. }
  602. void rgb_matrix_decrease_val(void) {
  603. rgb_matrix_decrease_val_helper(true);
  604. }
  605. void rgb_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  606. rgb_matrix_config.speed = speed;
  607. eeconfig_flag_rgb_matrix(write_to_eeprom);
  608. dprintf("rgb matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.speed);
  609. }
  610. void rgb_matrix_set_speed_noeeprom(uint8_t speed) {
  611. rgb_matrix_set_speed_eeprom_helper(speed, false);
  612. }
  613. void rgb_matrix_set_speed(uint8_t speed) {
  614. rgb_matrix_set_speed_eeprom_helper(speed, true);
  615. }
  616. uint8_t rgb_matrix_get_speed(void) {
  617. return rgb_matrix_config.speed;
  618. }
  619. void rgb_matrix_increase_speed_helper(bool write_to_eeprom) {
  620. rgb_matrix_set_speed_eeprom_helper(qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom);
  621. }
  622. void rgb_matrix_increase_speed_noeeprom(void) {
  623. rgb_matrix_increase_speed_helper(false);
  624. }
  625. void rgb_matrix_increase_speed(void) {
  626. rgb_matrix_increase_speed_helper(true);
  627. }
  628. void rgb_matrix_decrease_speed_helper(bool write_to_eeprom) {
  629. rgb_matrix_set_speed_eeprom_helper(qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom);
  630. }
  631. void rgb_matrix_decrease_speed_noeeprom(void) {
  632. rgb_matrix_decrease_speed_helper(false);
  633. }
  634. void rgb_matrix_decrease_speed(void) {
  635. rgb_matrix_decrease_speed_helper(true);
  636. }
  637. void rgb_matrix_set_flags_eeprom_helper(led_flags_t flags, bool write_to_eeprom) {
  638. rgb_matrix_config.flags = flags;
  639. eeconfig_flag_rgb_matrix(write_to_eeprom);
  640. dprintf("rgb matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.flags);
  641. }
  642. led_flags_t rgb_matrix_get_flags(void) {
  643. return rgb_matrix_config.flags;
  644. }
  645. void rgb_matrix_set_flags(led_flags_t flags) {
  646. rgb_matrix_set_flags_eeprom_helper(flags, true);
  647. }
  648. void rgb_matrix_set_flags_noeeprom(led_flags_t flags) {
  649. rgb_matrix_set_flags_eeprom_helper(flags, false);
  650. }