led_matrix.c 21 KB

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