led_matrix.c 20 KB

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