logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

led_matrix.c (22138B)


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