led_matrix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 "eeprom.h"
  22. #include "eeconfig.h"
  23. #include "keyboard.h"
  24. #include "sync_timer.h"
  25. #include "debug.h"
  26. #include <string.h>
  27. #include <math.h>
  28. #include <stdlib.h>
  29. #include "led_tables.h"
  30. #include <lib/lib8tion/lib8tion.h>
  31. #ifndef LED_MATRIX_CENTER
  32. const led_point_t k_led_matrix_center = {112, 32};
  33. #else
  34. const led_point_t k_led_matrix_center = LED_MATRIX_CENTER;
  35. #endif
  36. // Generic effect runners
  37. #include "led_matrix_runners.inc"
  38. // ------------------------------------------
  39. // -----Begin led effect includes macros-----
  40. #define LED_MATRIX_EFFECT(name)
  41. #define LED_MATRIX_CUSTOM_EFFECT_IMPLS
  42. #include "led_matrix_effects.inc"
  43. #ifdef LED_MATRIX_CUSTOM_KB
  44. # include "led_matrix_kb.inc"
  45. #endif
  46. #ifdef LED_MATRIX_CUSTOM_USER
  47. # include "led_matrix_user.inc"
  48. #endif
  49. #undef LED_MATRIX_CUSTOM_EFFECT_IMPLS
  50. #undef LED_MATRIX_EFFECT
  51. // -----End led effect includes macros-------
  52. // ------------------------------------------
  53. #ifndef LED_MATRIX_TIMEOUT
  54. # define LED_MATRIX_TIMEOUT 0
  55. #endif
  56. #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  57. # undef LED_MATRIX_MAXIMUM_BRIGHTNESS
  58. # define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  59. #endif
  60. #if !defined(LED_MATRIX_VAL_STEP)
  61. # define LED_MATRIX_VAL_STEP 8
  62. #endif
  63. #if !defined(LED_MATRIX_SPD_STEP)
  64. # define LED_MATRIX_SPD_STEP 16
  65. #endif
  66. #if !defined(LED_MATRIX_DEFAULT_MODE)
  67. # define LED_MATRIX_DEFAULT_MODE LED_MATRIX_SOLID
  68. #endif
  69. #if !defined(LED_MATRIX_DEFAULT_VAL)
  70. # define LED_MATRIX_DEFAULT_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
  71. #endif
  72. #if !defined(LED_MATRIX_DEFAULT_SPD)
  73. # define LED_MATRIX_DEFAULT_SPD UINT8_MAX / 2
  74. #endif
  75. // globals
  76. led_eeconfig_t led_matrix_eeconfig; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
  77. uint32_t g_led_timer;
  78. #ifdef LED_MATRIX_FRAMEBUFFER_EFFECTS
  79. uint8_t g_led_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
  80. #endif // LED_MATRIX_FRAMEBUFFER_EFFECTS
  81. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  82. last_hit_t g_last_hit_tracker;
  83. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  84. // internals
  85. static bool suspend_state = false;
  86. static uint8_t led_last_enable = UINT8_MAX;
  87. static uint8_t led_last_effect = UINT8_MAX;
  88. static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false};
  89. static led_task_states led_task_state = SYNCING;
  90. #if LED_MATRIX_TIMEOUT > 0
  91. static uint32_t led_anykey_timer;
  92. #endif // LED_MATRIX_TIMEOUT > 0
  93. // double buffers
  94. static uint32_t led_timer_buffer;
  95. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  96. static last_hit_t last_hit_buffer;
  97. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  98. // split led matrix
  99. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  100. const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
  101. #endif
  102. EECONFIG_DEBOUNCE_HELPER(led_matrix, EECONFIG_LED_MATRIX, led_matrix_eeconfig);
  103. void eeconfig_update_led_matrix(void) {
  104. eeconfig_flush_led_matrix(true);
  105. }
  106. void eeconfig_update_led_matrix_default(void) {
  107. dprintf("eeconfig_update_led_matrix_default\n");
  108. led_matrix_eeconfig.enable = 1;
  109. led_matrix_eeconfig.mode = LED_MATRIX_DEFAULT_MODE;
  110. led_matrix_eeconfig.val = LED_MATRIX_DEFAULT_VAL;
  111. led_matrix_eeconfig.speed = LED_MATRIX_DEFAULT_SPD;
  112. led_matrix_eeconfig.flags = LED_FLAG_ALL;
  113. eeconfig_flush_led_matrix(true);
  114. }
  115. void eeconfig_debug_led_matrix(void) {
  116. dprintf("led_matrix_eeconfig EEPROM\n");
  117. dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
  118. dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
  119. dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
  120. dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
  121. dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags);
  122. }
  123. __attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
  124. return 0;
  125. }
  126. uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  127. uint8_t led_count = led_matrix_map_row_column_to_led_kb(row, column, led_i);
  128. uint8_t led_index = g_led_config.matrix_co[row][column];
  129. if (led_index != NO_LED) {
  130. led_i[led_count] = led_index;
  131. led_count++;
  132. }
  133. return led_count;
  134. }
  135. void led_matrix_update_pwm_buffers(void) {
  136. led_matrix_driver.flush();
  137. }
  138. void led_matrix_set_value(int index, uint8_t value) {
  139. #ifdef USE_CIE1931_CURVE
  140. value = pgm_read_byte(&CIE1931_CURVE[value]);
  141. #endif
  142. led_matrix_driver.set_value(index, value);
  143. }
  144. void led_matrix_set_value_all(uint8_t value) {
  145. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  146. for (uint8_t i = 0; i < LED_MATRIX_LED_COUNT; i++)
  147. led_matrix_set_value(i, value);
  148. #else
  149. # ifdef USE_CIE1931_CURVE
  150. led_matrix_driver.set_value_all(pgm_read_byte(&CIE1931_CURVE[value]));
  151. # else
  152. led_matrix_driver.set_value_all(value);
  153. # endif
  154. #endif
  155. }
  156. void process_led_matrix(uint8_t row, uint8_t col, bool pressed) {
  157. #ifndef LED_MATRIX_SPLIT
  158. if (!is_keyboard_master()) return;
  159. #endif
  160. #if LED_MATRIX_TIMEOUT > 0
  161. led_anykey_timer = 0;
  162. #endif // LED_MATRIX_TIMEOUT > 0
  163. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  164. uint8_t led[LED_HITS_TO_REMEMBER];
  165. uint8_t led_count = 0;
  166. # if defined(LED_MATRIX_KEYRELEASES)
  167. if (!pressed)
  168. # elif defined(LED_MATRIX_KEYPRESSES)
  169. if (pressed)
  170. # endif // defined(LED_MATRIX_KEYRELEASES)
  171. {
  172. led_count = led_matrix_map_row_column_to_led(row, col, led);
  173. }
  174. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  175. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  176. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  177. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  178. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  179. last_hit_buffer.count = LED_HITS_TO_REMEMBER - led_count;
  180. }
  181. for (uint8_t i = 0; i < led_count; i++) {
  182. uint8_t index = last_hit_buffer.count;
  183. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  184. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  185. last_hit_buffer.index[index] = led[i];
  186. last_hit_buffer.tick[index] = 0;
  187. last_hit_buffer.count++;
  188. }
  189. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  190. #if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_LED_MATRIX_TYPING_HEATMAP)
  191. if (led_matrix_eeconfig.mode == LED_MATRIX_TYPING_HEATMAP) {
  192. process_led_matrix_typing_heatmap(row, col);
  193. }
  194. #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_LED_MATRIX_TYPING_HEATMAP)
  195. }
  196. static bool led_matrix_none(effect_params_t *params) {
  197. if (!params->init) {
  198. return false;
  199. }
  200. led_matrix_set_value_all(0);
  201. return false;
  202. }
  203. static void led_task_timers(void) {
  204. #if defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_MATRIX_TIMEOUT > 0
  205. uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer);
  206. #endif // defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_MATRIX_TIMEOUT > 0
  207. led_timer_buffer = sync_timer_read32();
  208. // Update double buffer timers
  209. #if LED_MATRIX_TIMEOUT > 0
  210. if (led_anykey_timer < UINT32_MAX) {
  211. if (UINT32_MAX - deltaTime < led_anykey_timer) {
  212. led_anykey_timer = UINT32_MAX;
  213. } else {
  214. led_anykey_timer += deltaTime;
  215. }
  216. }
  217. #endif // LED_MATRIX_TIMEOUT > 0
  218. // Update double buffer last hit timers
  219. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  220. uint8_t count = last_hit_buffer.count;
  221. for (uint8_t i = 0; i < count; ++i) {
  222. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  223. last_hit_buffer.count--;
  224. continue;
  225. }
  226. last_hit_buffer.tick[i] += deltaTime;
  227. }
  228. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  229. }
  230. static void led_task_sync(void) {
  231. eeconfig_flush_led_matrix(false);
  232. // next task
  233. if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING;
  234. }
  235. static void led_task_start(void) {
  236. // reset iter
  237. led_effect_params.iter = 0;
  238. // update double buffers
  239. g_led_timer = led_timer_buffer;
  240. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  241. g_last_hit_tracker = last_hit_buffer;
  242. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  243. // next task
  244. led_task_state = RENDERING;
  245. }
  246. static void led_task_render(uint8_t effect) {
  247. bool rendering = false;
  248. led_effect_params.init = (effect != led_last_effect) || (led_matrix_eeconfig.enable != led_last_enable);
  249. if (led_effect_params.flags != led_matrix_eeconfig.flags) {
  250. led_effect_params.flags = led_matrix_eeconfig.flags;
  251. led_matrix_set_value_all(0);
  252. }
  253. // each effect can opt to do calculations
  254. // and/or request PWM buffer updates.
  255. switch (effect) {
  256. case LED_MATRIX_NONE:
  257. rendering = led_matrix_none(&led_effect_params);
  258. break;
  259. // ---------------------------------------------
  260. // -----Begin led effect switch case macros-----
  261. #define LED_MATRIX_EFFECT(name, ...) \
  262. case LED_MATRIX_##name: \
  263. rendering = name(&led_effect_params); \
  264. break;
  265. #include "led_matrix_effects.inc"
  266. #undef LED_MATRIX_EFFECT
  267. #if defined(LED_MATRIX_CUSTOM_KB) || defined(LED_MATRIX_CUSTOM_USER)
  268. # define LED_MATRIX_EFFECT(name, ...) \
  269. case LED_MATRIX_CUSTOM_##name: \
  270. rendering = name(&led_effect_params); \
  271. break;
  272. # ifdef LED_MATRIX_CUSTOM_KB
  273. # include "led_matrix_kb.inc"
  274. # endif
  275. # ifdef LED_MATRIX_CUSTOM_USER
  276. # include "led_matrix_user.inc"
  277. # endif
  278. # undef LED_MATRIX_EFFECT
  279. #endif
  280. // -----End led effect switch case macros-------
  281. // ---------------------------------------------
  282. }
  283. led_effect_params.iter++;
  284. // next task
  285. if (!rendering) {
  286. led_task_state = FLUSHING;
  287. if (!led_effect_params.init && effect == LED_MATRIX_NONE) {
  288. // We only need to flush once if we are LED_MATRIX_NONE
  289. led_task_state = SYNCING;
  290. }
  291. }
  292. }
  293. static void led_task_flush(uint8_t effect) {
  294. // update last trackers after the first full render so we can init over several frames
  295. led_last_effect = effect;
  296. led_last_enable = led_matrix_eeconfig.enable;
  297. // update pwm buffers
  298. led_matrix_update_pwm_buffers();
  299. // next task
  300. led_task_state = SYNCING;
  301. }
  302. void led_matrix_task(void) {
  303. led_task_timers();
  304. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  305. // while suspended and just do a software shutdown. This is a cheap hack for now.
  306. bool suspend_backlight = suspend_state ||
  307. #if LED_MATRIX_TIMEOUT > 0
  308. (led_anykey_timer > (uint32_t)LED_MATRIX_TIMEOUT) ||
  309. #endif // LED_MATRIX_TIMEOUT > 0
  310. false;
  311. uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode;
  312. switch (led_task_state) {
  313. case STARTING:
  314. led_task_start();
  315. break;
  316. case RENDERING:
  317. led_task_render(effect);
  318. if (effect) {
  319. // Only run the basic indicators in the last render iteration (default there are 5 iterations)
  320. if (led_effect_params.iter == LED_MATRIX_LED_PROCESS_MAX_ITERATIONS) {
  321. led_matrix_indicators();
  322. }
  323. led_matrix_indicators_advanced(&led_effect_params);
  324. }
  325. break;
  326. case FLUSHING:
  327. led_task_flush(effect);
  328. break;
  329. case SYNCING:
  330. led_task_sync();
  331. break;
  332. }
  333. }
  334. void led_matrix_indicators(void) {
  335. led_matrix_indicators_kb();
  336. led_matrix_indicators_user();
  337. }
  338. __attribute__((weak)) bool led_matrix_indicators_kb(void) {
  339. return led_matrix_indicators_user();
  340. }
  341. __attribute__((weak)) bool led_matrix_indicators_user(void) {
  342. return true;
  343. }
  344. void led_matrix_indicators_advanced(effect_params_t *params) {
  345. /* special handling is needed for "params->iter", since it's already been incremented.
  346. * Could move the invocations to led_task_render, but then it's missing a few checks
  347. * and not sure which would be better. Otherwise, this should be called from
  348. * led_task_render, right before the iter++ line.
  349. */
  350. #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < LED_MATRIX_LED_COUNT
  351. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1);
  352. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT;
  353. if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT;
  354. #else
  355. uint8_t min = 0;
  356. uint8_t max = LED_MATRIX_LED_COUNT;
  357. #endif
  358. led_matrix_indicators_advanced_kb(min, max);
  359. led_matrix_indicators_advanced_user(min, max);
  360. }
  361. __attribute__((weak)) bool led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {
  362. return led_matrix_indicators_advanced_user(led_min, led_max);
  363. }
  364. __attribute__((weak)) bool led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
  365. return true;
  366. }
  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_init_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_render(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) {
  401. return suspend_state;
  402. }
  403. void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
  404. led_matrix_eeconfig.enable ^= 1;
  405. led_task_state = STARTING;
  406. eeconfig_flag_led_matrix(write_to_eeprom);
  407. dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable);
  408. }
  409. void led_matrix_toggle_noeeprom(void) {
  410. led_matrix_toggle_eeprom_helper(false);
  411. }
  412. void led_matrix_toggle(void) {
  413. led_matrix_toggle_eeprom_helper(true);
  414. }
  415. void led_matrix_enable(void) {
  416. led_matrix_enable_noeeprom();
  417. eeconfig_flag_led_matrix(true);
  418. }
  419. void led_matrix_enable_noeeprom(void) {
  420. if (!led_matrix_eeconfig.enable) led_task_state = STARTING;
  421. led_matrix_eeconfig.enable = 1;
  422. }
  423. void led_matrix_disable(void) {
  424. led_matrix_disable_noeeprom();
  425. eeconfig_flag_led_matrix(true);
  426. }
  427. void led_matrix_disable_noeeprom(void) {
  428. if (led_matrix_eeconfig.enable) led_task_state = STARTING;
  429. led_matrix_eeconfig.enable = 0;
  430. }
  431. uint8_t led_matrix_is_enabled(void) {
  432. return led_matrix_eeconfig.enable;
  433. }
  434. void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  435. if (!led_matrix_eeconfig.enable) {
  436. return;
  437. }
  438. if (mode < 1) {
  439. led_matrix_eeconfig.mode = 1;
  440. } else if (mode >= LED_MATRIX_EFFECT_MAX) {
  441. led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
  442. } else {
  443. led_matrix_eeconfig.mode = mode;
  444. }
  445. led_task_state = STARTING;
  446. eeconfig_flag_led_matrix(write_to_eeprom);
  447. dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode);
  448. }
  449. void led_matrix_mode_noeeprom(uint8_t mode) {
  450. led_matrix_mode_eeprom_helper(mode, false);
  451. }
  452. void led_matrix_mode(uint8_t mode) {
  453. led_matrix_mode_eeprom_helper(mode, true);
  454. }
  455. uint8_t led_matrix_get_mode(void) {
  456. return led_matrix_eeconfig.mode;
  457. }
  458. void led_matrix_step_helper(bool write_to_eeprom) {
  459. uint8_t mode = led_matrix_eeconfig.mode + 1;
  460. led_matrix_mode_eeprom_helper((mode < LED_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
  461. }
  462. void led_matrix_step_noeeprom(void) {
  463. led_matrix_step_helper(false);
  464. }
  465. void led_matrix_step(void) {
  466. led_matrix_step_helper(true);
  467. }
  468. void led_matrix_step_reverse_helper(bool write_to_eeprom) {
  469. uint8_t mode = led_matrix_eeconfig.mode - 1;
  470. led_matrix_mode_eeprom_helper((mode < 1) ? LED_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
  471. }
  472. void led_matrix_step_reverse_noeeprom(void) {
  473. led_matrix_step_reverse_helper(false);
  474. }
  475. void led_matrix_step_reverse(void) {
  476. led_matrix_step_reverse_helper(true);
  477. }
  478. void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) {
  479. if (!led_matrix_eeconfig.enable) {
  480. return;
  481. }
  482. led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val;
  483. eeconfig_flag_led_matrix(write_to_eeprom);
  484. dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val);
  485. }
  486. void led_matrix_set_val_noeeprom(uint8_t val) {
  487. led_matrix_set_val_eeprom_helper(val, false);
  488. }
  489. void led_matrix_set_val(uint8_t val) {
  490. led_matrix_set_val_eeprom_helper(val, true);
  491. }
  492. uint8_t led_matrix_get_val(void) {
  493. return led_matrix_eeconfig.val;
  494. }
  495. void led_matrix_increase_val_helper(bool write_to_eeprom) {
  496. led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom);
  497. }
  498. void led_matrix_increase_val_noeeprom(void) {
  499. led_matrix_increase_val_helper(false);
  500. }
  501. void led_matrix_increase_val(void) {
  502. led_matrix_increase_val_helper(true);
  503. }
  504. void led_matrix_decrease_val_helper(bool write_to_eeprom) {
  505. led_matrix_set_val_eeprom_helper(qsub8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom);
  506. }
  507. void led_matrix_decrease_val_noeeprom(void) {
  508. led_matrix_decrease_val_helper(false);
  509. }
  510. void led_matrix_decrease_val(void) {
  511. led_matrix_decrease_val_helper(true);
  512. }
  513. void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  514. led_matrix_eeconfig.speed = speed;
  515. eeconfig_flag_led_matrix(write_to_eeprom);
  516. dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed);
  517. }
  518. void led_matrix_set_speed_noeeprom(uint8_t speed) {
  519. led_matrix_set_speed_eeprom_helper(speed, false);
  520. }
  521. void led_matrix_set_speed(uint8_t speed) {
  522. led_matrix_set_speed_eeprom_helper(speed, true);
  523. }
  524. uint8_t led_matrix_get_speed(void) {
  525. return led_matrix_eeconfig.speed;
  526. }
  527. void led_matrix_increase_speed_helper(bool write_to_eeprom) {
  528. led_matrix_set_speed_eeprom_helper(qadd8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom);
  529. }
  530. void led_matrix_increase_speed_noeeprom(void) {
  531. led_matrix_increase_speed_helper(false);
  532. }
  533. void led_matrix_increase_speed(void) {
  534. led_matrix_increase_speed_helper(true);
  535. }
  536. void led_matrix_decrease_speed_helper(bool write_to_eeprom) {
  537. led_matrix_set_speed_eeprom_helper(qsub8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom);
  538. }
  539. void led_matrix_decrease_speed_noeeprom(void) {
  540. led_matrix_decrease_speed_helper(false);
  541. }
  542. void led_matrix_decrease_speed(void) {
  543. led_matrix_decrease_speed_helper(true);
  544. }
  545. void led_matrix_set_flags_eeprom_helper(led_flags_t flags, bool write_to_eeprom) {
  546. led_matrix_eeconfig.flags = flags;
  547. eeconfig_flag_led_matrix(write_to_eeprom);
  548. dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.flags);
  549. }
  550. led_flags_t led_matrix_get_flags(void) {
  551. return led_matrix_eeconfig.flags;
  552. }
  553. void led_matrix_set_flags(led_flags_t flags) {
  554. led_matrix_set_flags_eeprom_helper(flags, true);
  555. }
  556. void led_matrix_set_flags_noeeprom(led_flags_t flags) {
  557. led_matrix_set_flags_eeprom_helper(flags, false);
  558. }