led_matrix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. // clang-format off
  33. #ifndef LED_MATRIX_IMMEDIATE_EEPROM
  34. # define led_eeconfig_update(v) led_update_eeprom |= v
  35. #else
  36. # define led_eeconfig_update(v) if (v) eeconfig_update_led_matrix()
  37. #endif
  38. // clang-format on
  39. // Generic effect runners
  40. #include "led_matrix_runners.inc"
  41. // ------------------------------------------
  42. // -----Begin led effect includes macros-----
  43. #define LED_MATRIX_EFFECT(name)
  44. #define LED_MATRIX_CUSTOM_EFFECT_IMPLS
  45. #include "led_matrix_effects.inc"
  46. #ifdef LED_MATRIX_CUSTOM_KB
  47. # include "led_matrix_kb.inc"
  48. #endif
  49. #ifdef LED_MATRIX_CUSTOM_USER
  50. # include "led_matrix_user.inc"
  51. #endif
  52. #undef LED_MATRIX_CUSTOM_EFFECT_IMPLS
  53. #undef LED_MATRIX_EFFECT
  54. // -----End led effect includes macros-------
  55. // ------------------------------------------
  56. #if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT)
  57. # define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL)
  58. #endif
  59. #ifndef LED_DISABLE_TIMEOUT
  60. # define LED_DISABLE_TIMEOUT 0
  61. #endif
  62. #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  63. # undef LED_MATRIX_MAXIMUM_BRIGHTNESS
  64. # define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  65. #endif
  66. #if !defined(LED_MATRIX_VAL_STEP)
  67. # define LED_MATRIX_VAL_STEP 8
  68. #endif
  69. #if !defined(LED_MATRIX_SPD_STEP)
  70. # define LED_MATRIX_SPD_STEP 16
  71. #endif
  72. #if !defined(LED_MATRIX_STARTUP_MODE)
  73. # define LED_MATRIX_STARTUP_MODE LED_MATRIX_SOLID
  74. #endif
  75. #if !defined(LED_MATRIX_STARTUP_VAL)
  76. # define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
  77. #endif
  78. #if !defined(LED_MATRIX_STARTUP_SPD)
  79. # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
  80. #endif
  81. // globals
  82. led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
  83. uint32_t g_led_timer;
  84. #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS
  85. uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
  86. #endif // LED_MATRIX_FRAMEBUFFER_EFFECTS
  87. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  88. last_hit_t g_last_hit_tracker;
  89. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  90. // internals
  91. static bool suspend_state = false;
  92. static bool led_update_eeprom = 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 = LED_HITS_TO_REMEMBER - led_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 (led_update_eeprom) eeconfig_update_led_matrix();
  243. led_update_eeprom = false;
  244. if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING;
  245. }
  246. static void led_task_start(void) {
  247. // reset iter
  248. led_effect_params.iter = 0;
  249. // update double buffers
  250. g_led_timer = led_timer_buffer;
  251. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  252. g_last_hit_tracker = last_hit_buffer;
  253. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  254. // next task
  255. led_task_state = RENDERING;
  256. }
  257. static void led_task_render(uint8_t effect) {
  258. bool rendering = false;
  259. led_effect_params.init = (effect != led_last_effect) || (led_matrix_eeconfig.enable != led_last_enable);
  260. if (led_effect_params.flags != led_matrix_eeconfig.flags) {
  261. led_effect_params.flags = led_matrix_eeconfig.flags;
  262. led_matrix_set_value_all(0);
  263. }
  264. // each effect can opt to do calculations
  265. // and/or request PWM buffer updates.
  266. switch (effect) {
  267. case LED_MATRIX_NONE:
  268. rendering = led_matrix_none(&led_effect_params);
  269. break;
  270. // ---------------------------------------------
  271. // -----Begin led effect switch case macros-----
  272. #define LED_MATRIX_EFFECT(name, ...) \
  273. case LED_MATRIX_##name: \
  274. rendering = name(&led_effect_params); \
  275. break;
  276. #include "led_matrix_effects.inc"
  277. #undef LED_MATRIX_EFFECT
  278. #if defined(LED_MATRIX_CUSTOM_KB) || defined(LED_MATRIX_CUSTOM_USER)
  279. # define LED_MATRIX_EFFECT(name, ...) \
  280. case LED_MATRIX_CUSTOM_##name: \
  281. rendering = name(&led_effect_params); \
  282. break;
  283. # ifdef LED_MATRIX_CUSTOM_KB
  284. # include "led_matrix_kb.inc"
  285. # endif
  286. # ifdef LED_MATRIX_CUSTOM_USER
  287. # include "led_matrix_user.inc"
  288. # endif
  289. # undef LED_MATRIX_EFFECT
  290. #endif
  291. // -----End led effect switch case macros-------
  292. // ---------------------------------------------
  293. }
  294. led_effect_params.iter++;
  295. // next task
  296. if (!rendering) {
  297. led_task_state = FLUSHING;
  298. if (!led_effect_params.init && effect == LED_MATRIX_NONE) {
  299. // We only need to flush once if we are LED_MATRIX_NONE
  300. led_task_state = SYNCING;
  301. }
  302. }
  303. }
  304. static void led_task_flush(uint8_t effect) {
  305. // update last trackers after the first full render so we can init over several frames
  306. led_last_effect = effect;
  307. led_last_enable = led_matrix_eeconfig.enable;
  308. // update pwm buffers
  309. led_matrix_update_pwm_buffers();
  310. // next task
  311. led_task_state = SYNCING;
  312. }
  313. void led_matrix_task(void) {
  314. led_task_timers();
  315. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  316. // while suspended and just do a software shutdown. This is a cheap hack for now.
  317. bool suspend_backlight = suspend_state ||
  318. #if LED_DISABLE_TIMEOUT > 0
  319. (led_anykey_timer > (uint32_t)LED_DISABLE_TIMEOUT) ||
  320. #endif // LED_DISABLE_TIMEOUT > 0
  321. false;
  322. uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode;
  323. switch (led_task_state) {
  324. case STARTING:
  325. led_task_start();
  326. break;
  327. case RENDERING:
  328. led_task_render(effect);
  329. if (effect) {
  330. led_matrix_indicators();
  331. led_matrix_indicators_advanced(&led_effect_params);
  332. }
  333. break;
  334. case FLUSHING:
  335. led_task_flush(effect);
  336. break;
  337. case SYNCING:
  338. led_task_sync();
  339. break;
  340. }
  341. }
  342. void led_matrix_indicators(void) {
  343. led_matrix_indicators_kb();
  344. led_matrix_indicators_user();
  345. }
  346. __attribute__((weak)) void led_matrix_indicators_kb(void) {}
  347. __attribute__((weak)) void led_matrix_indicators_user(void) {}
  348. void led_matrix_indicators_advanced(effect_params_t *params) {
  349. /* special handling is needed for "params->iter", since it's already been incremented.
  350. * Could move the invocations to led_task_render, but then it's missing a few checks
  351. * and not sure which would be better. Otherwise, this should be called from
  352. * led_task_render, right before the iter++ line.
  353. */
  354. #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL
  355. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1);
  356. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT;
  357. if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL;
  358. #else
  359. uint8_t min = 0;
  360. uint8_t max = DRIVER_LED_TOTAL;
  361. #endif
  362. led_matrix_indicators_advanced_kb(min, max);
  363. led_matrix_indicators_advanced_user(min, max);
  364. }
  365. __attribute__((weak)) void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {}
  366. __attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {}
  367. void led_matrix_init(void) {
  368. led_matrix_driver.init();
  369. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  370. g_last_hit_tracker.count = 0;
  371. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  372. g_last_hit_tracker.tick[i] = UINT16_MAX;
  373. }
  374. last_hit_buffer.count = 0;
  375. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  376. last_hit_buffer.tick[i] = UINT16_MAX;
  377. }
  378. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  379. if (!eeconfig_is_enabled()) {
  380. dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
  381. eeconfig_init();
  382. eeconfig_update_led_matrix_default();
  383. }
  384. eeconfig_read_led_matrix();
  385. if (!led_matrix_eeconfig.mode) {
  386. dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
  387. eeconfig_update_led_matrix_default();
  388. }
  389. eeconfig_debug_led_matrix(); // display current eeprom values
  390. }
  391. void led_matrix_set_suspend_state(bool state) {
  392. #ifdef LED_DISABLE_WHEN_USB_SUSPENDED
  393. if (state && !suspend_state && is_keyboard_master()) { // only run if turning off, and only once
  394. led_task_effect(0); // turn off all LEDs when suspending
  395. led_task_flush(0); // and actually flash led state to LEDs
  396. }
  397. suspend_state = state;
  398. #endif
  399. }
  400. bool led_matrix_get_suspend_state(void) { return suspend_state; }
  401. void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
  402. led_matrix_eeconfig.enable ^= 1;
  403. led_task_state = STARTING;
  404. led_eeconfig_update(write_to_eeprom);
  405. dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable);
  406. }
  407. void led_matrix_toggle_noeeprom(void) { led_matrix_toggle_eeprom_helper(false); }
  408. void led_matrix_toggle(void) { led_matrix_toggle_eeprom_helper(true); }
  409. void led_matrix_enable(void) {
  410. led_matrix_enable_noeeprom();
  411. led_eeconfig_update(true);
  412. }
  413. void led_matrix_enable_noeeprom(void) {
  414. if (!led_matrix_eeconfig.enable) led_task_state = STARTING;
  415. led_matrix_eeconfig.enable = 1;
  416. }
  417. void led_matrix_disable(void) {
  418. led_matrix_disable_noeeprom();
  419. led_eeconfig_update(true);
  420. }
  421. void led_matrix_disable_noeeprom(void) {
  422. if (led_matrix_eeconfig.enable) led_task_state = STARTING;
  423. led_matrix_eeconfig.enable = 0;
  424. }
  425. uint8_t led_matrix_is_enabled(void) { return led_matrix_eeconfig.enable; }
  426. void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  427. if (!led_matrix_eeconfig.enable) {
  428. return;
  429. }
  430. if (mode < 1) {
  431. led_matrix_eeconfig.mode = 1;
  432. } else if (mode >= LED_MATRIX_EFFECT_MAX) {
  433. led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
  434. } else {
  435. led_matrix_eeconfig.mode = mode;
  436. }
  437. led_task_state = STARTING;
  438. led_eeconfig_update(write_to_eeprom);
  439. dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode);
  440. }
  441. void led_matrix_mode_noeeprom(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, false); }
  442. void led_matrix_mode(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, true); }
  443. uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
  444. void led_matrix_step_helper(bool write_to_eeprom) {
  445. uint8_t mode = led_matrix_eeconfig.mode + 1;
  446. led_matrix_mode_eeprom_helper((mode < LED_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
  447. }
  448. void led_matrix_step_noeeprom(void) { led_matrix_step_helper(false); }
  449. void led_matrix_step(void) { led_matrix_step_helper(true); }
  450. void led_matrix_step_reverse_helper(bool write_to_eeprom) {
  451. uint8_t mode = led_matrix_eeconfig.mode - 1;
  452. led_matrix_mode_eeprom_helper((mode < 1) ? LED_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
  453. }
  454. void led_matrix_step_reverse_noeeprom(void) { led_matrix_step_reverse_helper(false); }
  455. void led_matrix_step_reverse(void) { led_matrix_step_reverse_helper(true); }
  456. void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) {
  457. if (!led_matrix_eeconfig.enable) {
  458. return;
  459. }
  460. led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val;
  461. led_eeconfig_update(write_to_eeprom);
  462. dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val);
  463. }
  464. void led_matrix_set_val_noeeprom(uint8_t val) { led_matrix_set_val_eeprom_helper(val, false); }
  465. void led_matrix_set_val(uint8_t val) { led_matrix_set_val_eeprom_helper(val, true); }
  466. uint8_t led_matrix_get_val(void) { return led_matrix_eeconfig.val; }
  467. 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); }
  468. void led_matrix_increase_val_noeeprom(void) { led_matrix_increase_val_helper(false); }
  469. void led_matrix_increase_val(void) { led_matrix_increase_val_helper(true); }
  470. 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); }
  471. void led_matrix_decrease_val_noeeprom(void) { led_matrix_decrease_val_helper(false); }
  472. void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); }
  473. void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  474. led_matrix_eeconfig.speed = speed;
  475. led_eeconfig_update(write_to_eeprom);
  476. dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed);
  477. }
  478. void led_matrix_set_speed_noeeprom(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, false); }
  479. void led_matrix_set_speed(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, true); }
  480. uint8_t led_matrix_get_speed(void) { return led_matrix_eeconfig.speed; }
  481. 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); }
  482. void led_matrix_increase_speed_noeeprom(void) { led_matrix_increase_speed_helper(false); }
  483. void led_matrix_increase_speed(void) { led_matrix_increase_speed_helper(true); }
  484. 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); }
  485. void led_matrix_decrease_speed_noeeprom(void) { led_matrix_decrease_speed_helper(false); }
  486. void led_matrix_decrease_speed(void) { led_matrix_decrease_speed_helper(true); }
  487. led_flags_t led_matrix_get_flags(void) { return led_matrix_eeconfig.flags; }
  488. void led_matrix_set_flags(led_flags_t flags) { led_matrix_eeconfig.flags = flags; }