led_matrix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2017 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2019 Clueboard
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "led_matrix.h"
  20. #include "progmem.h"
  21. #include "config.h"
  22. #include "eeprom.h"
  23. #include <string.h>
  24. #include <math.h>
  25. #include "led_tables.h"
  26. #include <lib/lib8tion/lib8tion.h>
  27. #ifndef LED_MATRIX_CENTER
  28. const led_point_t k_led_matrix_center = {112, 32};
  29. #else
  30. const led_point_t k_led_matrix_center = LED_MATRIX_CENTER;
  31. #endif
  32. // Generic effect runners
  33. #include "led_matrix_runners/effect_runner_dx_dy_dist.h"
  34. #include "led_matrix_runners/effect_runner_dx_dy.h"
  35. #include "led_matrix_runners/effect_runner_i.h"
  36. #include "led_matrix_runners/effect_runner_sin_cos_i.h"
  37. #include "led_matrix_runners/effect_runner_reactive.h"
  38. #include "led_matrix_runners/effect_runner_reactive_splash.h"
  39. // ------------------------------------------
  40. // -----Begin led effect includes macros-----
  41. #define LED_MATRIX_EFFECT(name)
  42. #define LED_MATRIX_CUSTOM_EFFECT_IMPLS
  43. #include "led_matrix_animations/led_matrix_effects.inc"
  44. #ifdef LED_MATRIX_CUSTOM_KB
  45. # include "led_matrix_kb.inc"
  46. #endif
  47. #ifdef LED_MATRIX_CUSTOM_USER
  48. # include "led_matrix_user.inc"
  49. #endif
  50. #undef LED_MATRIX_CUSTOM_EFFECT_IMPLS
  51. #undef LED_MATRIX_EFFECT
  52. // -----End led effect includes macros-------
  53. // ------------------------------------------
  54. #if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT)
  55. # define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL)
  56. #endif
  57. #ifndef LED_DISABLE_TIMEOUT
  58. # define LED_DISABLE_TIMEOUT 0
  59. #endif
  60. #if LED_DISABLE_WHEN_USB_SUSPENDED != 1
  61. # undef LED_DISABLE_WHEN_USB_SUSPENDED
  62. #endif
  63. #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  64. # undef LED_MATRIX_MAXIMUM_BRIGHTNESS
  65. # define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  66. #endif
  67. #if !defined(LED_MATRIX_VAL_STEP)
  68. # define LED_MATRIX_VAL_STEP 8
  69. #endif
  70. #if !defined(LED_MATRIX_SPD_STEP)
  71. # define LED_MATRIX_SPD_STEP 16
  72. #endif
  73. #if !defined(LED_MATRIX_STARTUP_MODE)
  74. # define LED_MATRIX_STARTUP_MODE LED_MATRIX_SOLID
  75. #endif
  76. #if !defined(LED_MATRIX_STARTUP_VAL)
  77. # define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
  78. #endif
  79. #if !defined(LED_MATRIX_STARTUP_SPD)
  80. # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
  81. #endif
  82. // globals
  83. led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
  84. uint32_t g_led_timer;
  85. #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS
  86. uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
  87. #endif // LED_MATRIX_FRAMEBUFFER_EFFECTS
  88. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  89. last_hit_t g_last_hit_tracker;
  90. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  91. // internals
  92. static bool suspend_state = false;
  93. static uint8_t led_last_enable = UINT8_MAX;
  94. static uint8_t led_last_effect = UINT8_MAX;
  95. static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false};
  96. static led_task_states led_task_state = SYNCING;
  97. #if LED_DISABLE_TIMEOUT > 0
  98. static uint32_t led_anykey_timer;
  99. #endif // LED_DISABLE_TIMEOUT > 0
  100. // double buffers
  101. static uint32_t led_timer_buffer;
  102. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  103. static last_hit_t last_hit_buffer;
  104. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  105. // split led matrix
  106. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  107. const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
  108. #endif
  109. void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
  110. void eeconfig_update_led_matrix(void) { eeprom_update_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
  111. void eeconfig_update_led_matrix_default(void) {
  112. dprintf("eeconfig_update_led_matrix_default\n");
  113. led_matrix_eeconfig.enable = 1;
  114. led_matrix_eeconfig.mode = LED_MATRIX_STARTUP_MODE;
  115. led_matrix_eeconfig.val = LED_MATRIX_STARTUP_VAL;
  116. led_matrix_eeconfig.speed = LED_MATRIX_STARTUP_SPD;
  117. led_matrix_eeconfig.flags = LED_FLAG_ALL;
  118. eeconfig_update_led_matrix();
  119. }
  120. void eeconfig_debug_led_matrix(void) {
  121. dprintf("led_matrix_eeconfig EEPROM\n");
  122. dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
  123. dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
  124. dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
  125. dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
  126. dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags);
  127. }
  128. __attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; }
  129. uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  130. uint8_t led_count = led_matrix_map_row_column_to_led_kb(row, column, led_i);
  131. uint8_t led_index = g_led_config.matrix_co[row][column];
  132. if (led_index != NO_LED) {
  133. led_i[led_count] = led_index;
  134. led_count++;
  135. }
  136. return led_count;
  137. }
  138. void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); }
  139. void led_matrix_set_value(int index, uint8_t value) {
  140. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  141. if (!is_keyboard_left() && index >= k_led_matrix_split[0])
  142. # ifdef USE_CIE1931_CURVE
  143. led_matrix_driver.set_value(index - k_led_matrix_split[0], pgm_read_byte(&CIE1931_CURVE[value]));
  144. # else
  145. led_matrix_driver.set_value(index - k_led_matrix_split[0], value);
  146. # endif
  147. else if (is_keyboard_left() && index < k_led_matrix_split[0])
  148. #endif
  149. #ifdef USE_CIE1931_CURVE
  150. led_matrix_driver.set_value(index, pgm_read_byte(&CIE1931_CURVE[value]));
  151. #else
  152. led_matrix_driver.set_value(index, value);
  153. #endif
  154. }
  155. void led_matrix_set_value_all(uint8_t value) {
  156. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  157. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) led_matrix_set_value(i, value);
  158. #else
  159. # ifdef USE_CIE1931_CURVE
  160. led_matrix_driver.set_value_all(pgm_read_byte(&CIE1931_CURVE[value]));
  161. # else
  162. led_matrix_driver.set_value_all(value);
  163. # endif
  164. #endif
  165. }
  166. void process_led_matrix(uint8_t row, uint8_t col, bool pressed) {
  167. #ifndef LED_MATRIX_SPLIT
  168. if (!is_keyboard_master()) return;
  169. #endif
  170. #if LED_DISABLE_TIMEOUT > 0
  171. led_anykey_timer = 0;
  172. #endif // LED_DISABLE_TIMEOUT > 0
  173. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  174. uint8_t led[LED_HITS_TO_REMEMBER];
  175. uint8_t led_count = 0;
  176. # if defined(LED_MATRIX_KEYRELEASES)
  177. if (!pressed)
  178. # elif defined(LED_MATRIX_KEYPRESSES)
  179. if (pressed)
  180. # endif // defined(LED_MATRIX_KEYRELEASES)
  181. {
  182. led_count = led_matrix_map_row_column_to_led(row, col, led);
  183. }
  184. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  185. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  186. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  187. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  188. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  189. last_hit_buffer.count--;
  190. }
  191. for (uint8_t i = 0; i < led_count; i++) {
  192. uint8_t index = last_hit_buffer.count;
  193. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  194. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  195. last_hit_buffer.index[index] = led[i];
  196. last_hit_buffer.tick[index] = 0;
  197. last_hit_buffer.count++;
  198. }
  199. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  200. #if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP)
  201. if (led_matrix_eeconfig.mode == LED_MATRIX_TYPING_HEATMAP) {
  202. process_led_matrix_typing_heatmap(row, col);
  203. }
  204. #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP)
  205. }
  206. static bool led_matrix_none(effect_params_t *params) {
  207. if (!params->init) {
  208. return false;
  209. }
  210. led_matrix_set_value_all(0);
  211. return false;
  212. }
  213. static void led_task_timers(void) {
  214. #if defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0
  215. uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer);
  216. #endif // defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0
  217. led_timer_buffer = sync_timer_read32();
  218. // Update double buffer timers
  219. #if LED_DISABLE_TIMEOUT > 0
  220. if (led_anykey_timer < UINT32_MAX) {
  221. if (UINT32_MAX - deltaTime < led_anykey_timer) {
  222. led_anykey_timer = UINT32_MAX;
  223. } else {
  224. led_anykey_timer += deltaTime;
  225. }
  226. }
  227. #endif // LED_DISABLE_TIMEOUT > 0
  228. // Update double buffer last hit timers
  229. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  230. uint8_t count = last_hit_buffer.count;
  231. for (uint8_t i = 0; i < count; ++i) {
  232. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  233. last_hit_buffer.count--;
  234. continue;
  235. }
  236. last_hit_buffer.tick[i] += deltaTime;
  237. }
  238. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  239. }
  240. static void led_task_sync(void) {
  241. // next task
  242. if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING;
  243. }
  244. static void led_task_start(void) {
  245. // reset iter
  246. led_effect_params.iter = 0;
  247. // update double buffers
  248. g_led_timer = led_timer_buffer;
  249. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  250. g_last_hit_tracker = last_hit_buffer;
  251. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  252. // next task
  253. led_task_state = RENDERING;
  254. }
  255. static void led_task_render(uint8_t effect) {
  256. bool rendering = false;
  257. led_effect_params.init = (effect != led_last_effect) || (led_matrix_eeconfig.enable != led_last_enable);
  258. if (led_effect_params.flags != led_matrix_eeconfig.flags) {
  259. led_effect_params.flags = led_matrix_eeconfig.flags;
  260. led_matrix_set_value_all(0);
  261. }
  262. // each effect can opt to do calculations
  263. // and/or request PWM buffer updates.
  264. switch (effect) {
  265. case LED_MATRIX_NONE:
  266. rendering = led_matrix_none(&led_effect_params);
  267. break;
  268. // ---------------------------------------------
  269. // -----Begin led effect switch case macros-----
  270. #define LED_MATRIX_EFFECT(name, ...) \
  271. case LED_MATRIX_##name: \
  272. rendering = name(&led_effect_params); \
  273. break;
  274. #include "led_matrix_animations/led_matrix_effects.inc"
  275. #undef LED_MATRIX_EFFECT
  276. #if defined(LED_MATRIX_CUSTOM_KB) || defined(LED_MATRIX_CUSTOM_USER)
  277. # define LED_MATRIX_EFFECT(name, ...) \
  278. case LED_MATRIX_CUSTOM_##name: \
  279. rendering = name(&led_effect_params); \
  280. break;
  281. # ifdef LED_MATRIX_CUSTOM_KB
  282. # include "led_matrix_kb.inc"
  283. # endif
  284. # ifdef LED_MATRIX_CUSTOM_USER
  285. # include "led_matrix_user.inc"
  286. # endif
  287. # undef LED_MATRIX_EFFECT
  288. #endif
  289. // -----End led effect switch case macros-------
  290. // ---------------------------------------------
  291. }
  292. led_effect_params.iter++;
  293. // next task
  294. if (!rendering) {
  295. led_task_state = FLUSHING;
  296. if (!led_effect_params.init && effect == LED_MATRIX_NONE) {
  297. // We only need to flush once if we are LED_MATRIX_NONE
  298. led_task_state = SYNCING;
  299. }
  300. }
  301. }
  302. static void led_task_flush(uint8_t effect) {
  303. // update last trackers after the first full render so we can init over several frames
  304. led_last_effect = effect;
  305. led_last_enable = led_matrix_eeconfig.enable;
  306. // update pwm buffers
  307. led_matrix_update_pwm_buffers();
  308. // next task
  309. led_task_state = SYNCING;
  310. }
  311. void led_matrix_task(void) {
  312. led_task_timers();
  313. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  314. // while suspended and just do a software shutdown. This is a cheap hack for now.
  315. bool suspend_backlight = suspend_state ||
  316. #if LED_DISABLE_TIMEOUT > 0
  317. (led_anykey_timer > (uint32_t)LED_DISABLE_TIMEOUT) ||
  318. #endif // LED_DISABLE_TIMEOUT > 0
  319. false;
  320. uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode;
  321. switch (led_task_state) {
  322. case STARTING:
  323. led_task_start();
  324. break;
  325. case RENDERING:
  326. led_task_render(effect);
  327. if (effect) {
  328. led_matrix_indicators();
  329. led_matrix_indicators_advanced(&led_effect_params);
  330. }
  331. break;
  332. case FLUSHING:
  333. led_task_flush(effect);
  334. break;
  335. case SYNCING:
  336. led_task_sync();
  337. break;
  338. }
  339. }
  340. void led_matrix_indicators(void) {
  341. led_matrix_indicators_kb();
  342. led_matrix_indicators_user();
  343. }
  344. __attribute__((weak)) void led_matrix_indicators_kb(void) {}
  345. __attribute__((weak)) void led_matrix_indicators_user(void) {}
  346. void led_matrix_indicators_advanced(effect_params_t *params) {
  347. /* special handling is needed for "params->iter", since it's already been incremented.
  348. * Could move the invocations to led_task_render, but then it's missing a few checks
  349. * and not sure which would be better. Otherwise, this should be called from
  350. * led_task_render, right before the iter++ line.
  351. */
  352. #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL
  353. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1);
  354. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT;
  355. if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL;
  356. #else
  357. uint8_t min = 0;
  358. uint8_t max = DRIVER_LED_TOTAL;
  359. #endif
  360. led_matrix_indicators_advanced_kb(min, max);
  361. led_matrix_indicators_advanced_user(min, max);
  362. }
  363. __attribute__((weak)) void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {}
  364. __attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {}
  365. void led_matrix_init(void) {
  366. led_matrix_driver.init();
  367. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  368. g_last_hit_tracker.count = 0;
  369. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  370. g_last_hit_tracker.tick[i] = UINT16_MAX;
  371. }
  372. last_hit_buffer.count = 0;
  373. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  374. last_hit_buffer.tick[i] = UINT16_MAX;
  375. }
  376. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  377. if (!eeconfig_is_enabled()) {
  378. dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
  379. eeconfig_init();
  380. eeconfig_update_led_matrix_default();
  381. }
  382. eeconfig_read_led_matrix();
  383. if (!led_matrix_eeconfig.mode) {
  384. dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
  385. eeconfig_update_led_matrix_default();
  386. }
  387. eeconfig_debug_led_matrix(); // display current eeprom values
  388. }
  389. void led_matrix_set_suspend_state(bool state) {
  390. #ifdef LED_DISABLE_WHEN_USB_SUSPENDED
  391. if (state) {
  392. led_matrix_set_value_all(0); // turn off all LEDs when suspending
  393. }
  394. suspend_state = state;
  395. #endif
  396. }
  397. bool led_matrix_get_suspend_state(void) { return suspend_state; }
  398. void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
  399. led_matrix_eeconfig.enable ^= 1;
  400. led_task_state = STARTING;
  401. if (write_to_eeprom) {
  402. eeconfig_update_led_matrix();
  403. }
  404. dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable);
  405. }
  406. void led_matrix_toggle_noeeprom(void) { led_matrix_toggle_eeprom_helper(false); }
  407. void led_matrix_toggle(void) { led_matrix_toggle_eeprom_helper(true); }
  408. void led_matrix_enable(void) {
  409. led_matrix_enable_noeeprom();
  410. eeconfig_update_led_matrix();
  411. }
  412. void led_matrix_enable_noeeprom(void) {
  413. if (!led_matrix_eeconfig.enable) led_task_state = STARTING;
  414. led_matrix_eeconfig.enable = 1;
  415. }
  416. void led_matrix_disable(void) {
  417. led_matrix_disable_noeeprom();
  418. eeconfig_update_led_matrix();
  419. }
  420. void led_matrix_disable_noeeprom(void) {
  421. if (led_matrix_eeconfig.enable) led_task_state = STARTING;
  422. led_matrix_eeconfig.enable = 0;
  423. }
  424. uint8_t led_matrix_is_enabled(void) { return led_matrix_eeconfig.enable; }
  425. void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  426. if (!led_matrix_eeconfig.enable) {
  427. return;
  428. }
  429. if (mode < 1) {
  430. led_matrix_eeconfig.mode = 1;
  431. } else if (mode >= LED_MATRIX_EFFECT_MAX) {
  432. led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
  433. } else {
  434. led_matrix_eeconfig.mode = mode;
  435. }
  436. led_task_state = STARTING;
  437. if (write_to_eeprom) {
  438. eeconfig_update_led_matrix();
  439. }
  440. dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode);
  441. }
  442. void led_matrix_mode_noeeprom(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, false); }
  443. void led_matrix_mode(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, true); }
  444. uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
  445. void led_matrix_step_helper(bool write_to_eeprom) {
  446. uint8_t mode = led_matrix_eeconfig.mode + 1;
  447. led_matrix_mode_eeprom_helper((mode < LED_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
  448. }
  449. void led_matrix_step_noeeprom(void) { led_matrix_step_helper(false); }
  450. void led_matrix_step(void) { led_matrix_step_helper(true); }
  451. void led_matrix_step_reverse_helper(bool write_to_eeprom) {
  452. uint8_t mode = led_matrix_eeconfig.mode - 1;
  453. led_matrix_mode_eeprom_helper((mode < 1) ? LED_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
  454. }
  455. void led_matrix_step_reverse_noeeprom(void) { led_matrix_step_reverse_helper(false); }
  456. void led_matrix_step_reverse(void) { led_matrix_step_reverse_helper(true); }
  457. void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) {
  458. if (!led_matrix_eeconfig.enable) {
  459. return;
  460. }
  461. led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val;
  462. if (write_to_eeprom) {
  463. eeconfig_update_led_matrix();
  464. }
  465. dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val);
  466. }
  467. void led_matrix_set_val_noeeprom(uint8_t val) { led_matrix_set_val_eeprom_helper(val, false); }
  468. void led_matrix_set_val(uint8_t val) { led_matrix_set_val_eeprom_helper(val, true); }
  469. uint8_t led_matrix_get_val(void) { return led_matrix_eeconfig.val; }
  470. void led_matrix_increase_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); }
  471. void led_matrix_increase_val_noeeprom(void) { led_matrix_increase_val_helper(false); }
  472. void led_matrix_increase_val(void) { led_matrix_increase_val_helper(true); }
  473. void led_matrix_decrease_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qsub8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); }
  474. void led_matrix_decrease_val_noeeprom(void) { led_matrix_decrease_val_helper(false); }
  475. void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); }
  476. void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  477. led_matrix_eeconfig.speed = speed;
  478. if (write_to_eeprom) {
  479. eeconfig_update_led_matrix();
  480. }
  481. dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed);
  482. }
  483. void led_matrix_set_speed_noeeprom(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, false); }
  484. void led_matrix_set_speed(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, true); }
  485. uint8_t led_matrix_get_speed(void) { return led_matrix_eeconfig.speed; }
  486. void led_matrix_increase_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qadd8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); }
  487. void led_matrix_increase_speed_noeeprom(void) { led_matrix_increase_speed_helper(false); }
  488. void led_matrix_increase_speed(void) { led_matrix_increase_speed_helper(true); }
  489. void led_matrix_decrease_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qsub8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); }
  490. void led_matrix_decrease_speed_noeeprom(void) { led_matrix_decrease_speed_helper(false); }
  491. void led_matrix_decrease_speed(void) { led_matrix_decrease_speed_helper(true); }
  492. led_flags_t led_matrix_get_flags(void) { return led_matrix_eeconfig.flags; }
  493. void led_matrix_set_flags(led_flags_t flags) { led_matrix_eeconfig.flags = flags; }