rgb_matrix.c 24 KB

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