led_matrix.c 21 KB

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