led_matrix.c 21 KB

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