You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

540 lines
19 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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 "config.h"
  22. #include "eeprom.h"
  23. #include <string.h>
  24. #include <math.h>
  25. #include "led_tables.h"
  26. #include <lib/lib8tion/lib8tion.h>
  27. #if defined(LED_DISABLE_AFTER_TIMEOUT) && !defined(LED_DISABLE_TIMEOUT)
  28. # define LED_DISABLE_TIMEOUT (LED_DISABLE_AFTER_TIMEOUT * 1200UL)
  29. #endif
  30. #ifndef LED_DISABLE_TIMEOUT
  31. # define LED_DISABLE_TIMEOUT 0
  32. #endif
  33. #if LED_DISABLE_WHEN_USB_SUSPENDED == false
  34. # undef LED_DISABLE_WHEN_USB_SUSPENDED
  35. #endif
  36. #if !defined(LED_MATRIX_MAXIMUM_BRIGHTNESS) || LED_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  37. # undef LED_MATRIX_MAXIMUM_BRIGHTNESS
  38. # define LED_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  39. #endif
  40. #if !defined(LED_MATRIX_VAL_STEP)
  41. # define LED_MATRIX_VAL_STEP 8
  42. #endif
  43. #if !defined(LED_MATRIX_SPD_STEP)
  44. # define LED_MATRIX_SPD_STEP 16
  45. #endif
  46. #if !defined(LED_MATRIX_STARTUP_MODE)
  47. # define LED_MATRIX_STARTUP_MODE LED_MATRIX_UNIFORM_BRIGHTNESS
  48. #endif
  49. #if !defined(LED_MATRIX_STARTUP_VAL)
  50. # define LED_MATRIX_STARTUP_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS
  51. #endif
  52. #if !defined(LED_MATRIX_STARTUP_SPD)
  53. # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
  54. #endif
  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. #if LED_DISABLE_TIMEOUT > 0
  71. static uint32_t led_anykey_timer;
  72. #endif // LED_DISABLE_TIMEOUT > 0
  73. // double buffers
  74. static uint32_t led_timer_buffer;
  75. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  76. static last_hit_t last_hit_buffer;
  77. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  78. // split led matrix
  79. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  80. const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT;
  81. #endif
  82. void eeconfig_read_led_matrix(void) { eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
  83. void eeconfig_update_led_matrix(void) { eeprom_update_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); }
  84. void eeconfig_update_led_matrix_default(void) {
  85. dprintf("eeconfig_update_led_matrix_default\n");
  86. led_matrix_eeconfig.enable = 1;
  87. led_matrix_eeconfig.mode = LED_MATRIX_STARTUP_MODE;
  88. led_matrix_eeconfig.val = LED_MATRIX_STARTUP_VAL;
  89. led_matrix_eeconfig.speed = LED_MATRIX_STARTUP_SPD;
  90. led_matrix_eeconfig.flags = LED_FLAG_ALL;
  91. eeconfig_update_led_matrix();
  92. }
  93. void eeconfig_debug_led_matrix(void) {
  94. dprintf("led_matrix_eeconfig EEPROM\n");
  95. dprintf("led_matrix_eeconfig.enable = %d\n", led_matrix_eeconfig.enable);
  96. dprintf("led_matrix_eeconfig.mode = %d\n", led_matrix_eeconfig.mode);
  97. dprintf("led_matrix_eeconfig.val = %d\n", led_matrix_eeconfig.val);
  98. dprintf("led_matrix_eeconfig.speed = %d\n", led_matrix_eeconfig.speed);
  99. dprintf("led_matrix_eeconfig.flags = %d\n", led_matrix_eeconfig.flags);
  100. }
  101. __attribute__((weak)) uint8_t led_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; }
  102. uint8_t led_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  103. uint8_t led_count = led_matrix_map_row_column_to_led_kb(row, column, led_i);
  104. uint8_t led_index = g_led_config.matrix_co[row][column];
  105. if (led_index != NO_LED) {
  106. led_i[led_count] = led_index;
  107. led_count++;
  108. }
  109. return led_count;
  110. }
  111. void led_matrix_update_pwm_buffers(void) { led_matrix_driver.flush(); }
  112. void led_matrix_set_value(int index, uint8_t value) {
  113. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  114. if (!is_keyboard_left() && index >= k_led_matrix_split[0])
  115. # ifdef USE_CIE1931_CURVE
  116. led_matrix_driver.set_value(index - k_led_matrix_split[0], pgm_read_byte(&CIE1931_CURVE[value]));
  117. # else
  118. led_matrix_driver.set_value(index - k_led_matrix_split[0], value);
  119. # endif
  120. else if (is_keyboard_left() && index < k_led_matrix_split[0])
  121. #endif
  122. #ifdef USE_CIE1931_CURVE
  123. led_matrix_driver.set_value(index, pgm_read_byte(&CIE1931_CURVE[value]));
  124. #else
  125. led_matrix_driver.set_value(index, value);
  126. #endif
  127. }
  128. void led_matrix_set_value_all(uint8_t value) {
  129. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  130. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) led_matrix_set_value(i, value);
  131. #else
  132. # ifdef USE_CIE1931_CURVE
  133. led_matrix_driver.set_value_all(pgm_read_byte(&CIE1931_CURVE[value]));
  134. # else
  135. led_matrix_driver.set_value_all(value);
  136. # endif
  137. #endif
  138. }
  139. void process_led_matrix(uint8_t row, uint8_t col, bool pressed) {
  140. #ifndef LED_MATRIX_SPLIT
  141. if (!is_keyboard_master()) return;
  142. #endif
  143. #if LED_DISABLE_TIMEOUT > 0
  144. led_anykey_timer = 0;
  145. #endif // LED_DISABLE_TIMEOUT > 0
  146. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  147. uint8_t led[LED_HITS_TO_REMEMBER];
  148. uint8_t led_count = 0;
  149. # if defined(LED_MATRIX_KEYRELEASES)
  150. if (!pressed)
  151. # elif defined(LED_MATRIX_KEYPRESSES)
  152. if (pressed)
  153. # endif // defined(LED_MATRIX_KEYRELEASES)
  154. {
  155. led_count = led_matrix_map_row_column_to_led(row, col, led);
  156. }
  157. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  158. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  159. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  160. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  161. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  162. last_hit_buffer.count--;
  163. }
  164. for (uint8_t i = 0; i < led_count; i++) {
  165. uint8_t index = last_hit_buffer.count;
  166. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  167. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  168. last_hit_buffer.index[index] = led[i];
  169. last_hit_buffer.tick[index] = 0;
  170. last_hit_buffer.count++;
  171. }
  172. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  173. #if defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP)
  174. if (led_matrix_eeconfig.mode == LED_MATRIX_TYPING_HEATMAP) {
  175. process_led_matrix_typing_heatmap(row, col);
  176. }
  177. #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_LED_MATRIX_TYPING_HEATMAP)
  178. }
  179. static bool led_matrix_none(effect_params_t *params) {
  180. if (!params->init) {
  181. return false;
  182. }
  183. led_matrix_set_value_all(0);
  184. return false;
  185. }
  186. static bool led_matrix_uniform_brightness(effect_params_t *params) {
  187. LED_MATRIX_USE_LIMITS(led_min, led_max);
  188. uint8_t val = led_matrix_eeconfig.val;
  189. for (uint8_t i = led_min; i < led_max; i++) {
  190. LED_MATRIX_TEST_LED_FLAGS();
  191. led_matrix_set_value(i, val);
  192. }
  193. return led_max < DRIVER_LED_TOTAL;
  194. }
  195. static void led_task_timers(void) {
  196. #if defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0
  197. uint32_t deltaTime = sync_timer_elapsed32(led_timer_buffer);
  198. #endif // defined(LED_MATRIX_KEYREACTIVE_ENABLED) || LED_DISABLE_TIMEOUT > 0
  199. led_timer_buffer = sync_timer_read32();
  200. // Update double buffer timers
  201. #if LED_DISABLE_TIMEOUT > 0
  202. if (led_anykey_timer < UINT32_MAX) {
  203. if (UINT32_MAX - deltaTime < led_anykey_timer) {
  204. led_anykey_timer = UINT32_MAX;
  205. } else {
  206. led_anykey_timer += deltaTime;
  207. }
  208. }
  209. #endif // LED_DISABLE_TIMEOUT > 0
  210. // Update double buffer last hit timers
  211. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  212. uint8_t count = last_hit_buffer.count;
  213. for (uint8_t i = 0; i < count; ++i) {
  214. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  215. last_hit_buffer.count--;
  216. continue;
  217. }
  218. last_hit_buffer.tick[i] += deltaTime;
  219. }
  220. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  221. }
  222. static void led_task_sync(void) {
  223. // next task
  224. if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING;
  225. }
  226. static void led_task_start(void) {
  227. // reset iter
  228. led_effect_params.iter = 0;
  229. // update double buffers
  230. g_led_timer = led_timer_buffer;
  231. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  232. g_last_hit_tracker = last_hit_buffer;
  233. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  234. // next task
  235. led_task_state = RENDERING;
  236. }
  237. static void led_task_render(uint8_t effect) {
  238. bool rendering = false;
  239. led_effect_params.init = (effect != led_last_effect) || (led_matrix_eeconfig.enable != led_last_enable);
  240. if (led_effect_params.flags != led_matrix_eeconfig.flags) {
  241. led_effect_params.flags = led_matrix_eeconfig.flags;
  242. led_matrix_set_value_all(0);
  243. }
  244. // each effect can opt to do calculations
  245. // and/or request PWM buffer updates.
  246. switch (effect) {
  247. case LED_MATRIX_NONE:
  248. rendering = led_matrix_none(&led_effect_params);
  249. break;
  250. case LED_MATRIX_UNIFORM_BRIGHTNESS:
  251. rendering = led_matrix_uniform_brightness(&led_effect_params);
  252. break;
  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_DISABLE_TIMEOUT > 0
  279. (led_anykey_timer > (uint32_t)LED_DISABLE_TIMEOUT) ||
  280. #endif // LED_DISABLE_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. led_matrix_indicators();
  291. led_matrix_indicators_advanced(&led_effect_params);
  292. }
  293. break;
  294. case FLUSHING:
  295. led_task_flush(effect);
  296. break;
  297. case SYNCING:
  298. led_task_sync();
  299. break;
  300. }
  301. }
  302. void led_matrix_indicators(void) {
  303. led_matrix_indicators_kb();
  304. led_matrix_indicators_user();
  305. }
  306. __attribute__((weak)) void led_matrix_indicators_kb(void) {}
  307. __attribute__((weak)) void led_matrix_indicators_user(void) {}
  308. void led_matrix_indicators_advanced(effect_params_t *params) {
  309. /* special handling is needed for "params->iter", since it's already been incremented.
  310. * Could move the invocations to led_task_render, but then it's missing a few checks
  311. * and not sure which would be better. Otherwise, this should be called from
  312. * led_task_render, right before the iter++ line.
  313. */
  314. #if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL
  315. uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1);
  316. uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT;
  317. if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL;
  318. #else
  319. uint8_t min = 0;
  320. uint8_t max = DRIVER_LED_TOTAL;
  321. #endif
  322. led_matrix_indicators_advanced_kb(min, max);
  323. led_matrix_indicators_advanced_user(min, max);
  324. }
  325. __attribute__((weak)) void led_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {}
  326. __attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {}
  327. void led_matrix_init(void) {
  328. led_matrix_driver.init();
  329. #ifdef LED_MATRIX_KEYREACTIVE_ENABLED
  330. g_last_hit_tracker.count = 0;
  331. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  332. g_last_hit_tracker.tick[i] = UINT16_MAX;
  333. }
  334. last_hit_buffer.count = 0;
  335. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  336. last_hit_buffer.tick[i] = UINT16_MAX;
  337. }
  338. #endif // LED_MATRIX_KEYREACTIVE_ENABLED
  339. if (!eeconfig_is_enabled()) {
  340. dprintf("led_matrix_init_drivers eeconfig is not enabled.\n");
  341. eeconfig_init();
  342. eeconfig_update_led_matrix_default();
  343. }
  344. eeconfig_read_led_matrix();
  345. if (!led_matrix_eeconfig.mode) {
  346. dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n");
  347. eeconfig_update_led_matrix_default();
  348. }
  349. eeconfig_debug_led_matrix(); // display current eeprom values
  350. }
  351. void led_matrix_set_suspend_state(bool state) {
  352. #ifdef LED_DISABLE_WHEN_USB_SUSPENDED
  353. if (state) {
  354. led_matrix_set_value_all(0); // turn off all LEDs when suspending
  355. }
  356. suspend_state = state;
  357. #endif
  358. }
  359. bool led_matrix_get_suspend_state(void) { return suspend_state; }
  360. void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
  361. led_matrix_eeconfig.enable ^= 1;
  362. led_task_state = STARTING;
  363. if (write_to_eeprom) {
  364. eeconfig_update_led_matrix();
  365. }
  366. dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable);
  367. }
  368. void led_matrix_toggle_noeeprom(void) { led_matrix_toggle_eeprom_helper(false); }
  369. void led_matrix_toggle(void) { led_matrix_toggle_eeprom_helper(true); }
  370. void led_matrix_enable(void) {
  371. led_matrix_enable_noeeprom();
  372. eeconfig_update_led_matrix();
  373. }
  374. void led_matrix_enable_noeeprom(void) {
  375. if (!led_matrix_eeconfig.enable) led_task_state = STARTING;
  376. led_matrix_eeconfig.enable = 1;
  377. }
  378. void led_matrix_disable(void) {
  379. led_matrix_disable_noeeprom();
  380. eeconfig_update_led_matrix();
  381. }
  382. void led_matrix_disable_noeeprom(void) {
  383. if (led_matrix_eeconfig.enable) led_task_state = STARTING;
  384. led_matrix_eeconfig.enable = 0;
  385. }
  386. uint8_t led_matrix_is_enabled(void) { return led_matrix_eeconfig.enable; }
  387. void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  388. if (!led_matrix_eeconfig.enable) {
  389. return;
  390. }
  391. if (mode < 1) {
  392. led_matrix_eeconfig.mode = 1;
  393. } else if (mode >= LED_MATRIX_EFFECT_MAX) {
  394. led_matrix_eeconfig.mode = LED_MATRIX_EFFECT_MAX - 1;
  395. } else {
  396. led_matrix_eeconfig.mode = mode;
  397. }
  398. led_task_state = STARTING;
  399. if (write_to_eeprom) {
  400. eeconfig_update_led_matrix();
  401. }
  402. dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode);
  403. }
  404. void led_matrix_mode_noeeprom(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, false); }
  405. void led_matrix_mode(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, true); }
  406. uint8_t led_matrix_get_mode(void) { return led_matrix_eeconfig.mode; }
  407. void led_matrix_step_helper(bool write_to_eeprom) {
  408. uint8_t mode = led_matrix_eeconfig.mode + 1;
  409. led_matrix_mode_eeprom_helper((mode < LED_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
  410. }
  411. void led_matrix_step_noeeprom(void) { led_matrix_step_helper(false); }
  412. void led_matrix_step(void) { led_matrix_step_helper(true); }
  413. void led_matrix_step_reverse_helper(bool write_to_eeprom) {
  414. uint8_t mode = led_matrix_eeconfig.mode - 1;
  415. led_matrix_mode_eeprom_helper((mode < 1) ? LED_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
  416. }
  417. void led_matrix_step_reverse_noeeprom(void) { led_matrix_step_reverse_helper(false); }
  418. void led_matrix_step_reverse(void) { led_matrix_step_reverse_helper(true); }
  419. void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) {
  420. if (!led_matrix_eeconfig.enable) {
  421. return;
  422. }
  423. led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val;
  424. if (write_to_eeprom) {
  425. eeconfig_update_led_matrix();
  426. }
  427. dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val);
  428. }
  429. void led_matrix_set_val_noeeprom(uint8_t val) { led_matrix_set_val_eeprom_helper(val, false); }
  430. void led_matrix_set_val(uint8_t val) { led_matrix_set_val_eeprom_helper(val, true); }
  431. uint8_t led_matrix_get_val(void) { return led_matrix_eeconfig.val; }
  432. void led_matrix_increase_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); }
  433. void led_matrix_increase_val_noeeprom(void) { led_matrix_increase_val_helper(false); }
  434. void led_matrix_increase_val(void) { led_matrix_increase_val_helper(true); }
  435. void led_matrix_decrease_val_helper(bool write_to_eeprom) { led_matrix_set_val_eeprom_helper(qsub8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); }
  436. void led_matrix_decrease_val_noeeprom(void) { led_matrix_decrease_val_helper(false); }
  437. void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); }
  438. void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  439. led_matrix_eeconfig.speed = speed;
  440. if (write_to_eeprom) {
  441. eeconfig_update_led_matrix();
  442. }
  443. dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed);
  444. }
  445. void led_matrix_set_speed_noeeprom(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, false); }
  446. void led_matrix_set_speed(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, true); }
  447. uint8_t led_matrix_get_speed(void) { return led_matrix_eeconfig.speed; }
  448. void led_matrix_increase_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qadd8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); }
  449. void led_matrix_increase_speed_noeeprom(void) { led_matrix_increase_speed_helper(false); }
  450. void led_matrix_increase_speed(void) { led_matrix_increase_speed_helper(true); }
  451. void led_matrix_decrease_speed_helper(bool write_to_eeprom) { led_matrix_set_speed_eeprom_helper(qsub8(led_matrix_eeconfig.speed, LED_MATRIX_SPD_STEP), write_to_eeprom); }
  452. void led_matrix_decrease_speed_noeeprom(void) { led_matrix_decrease_speed_helper(false); }
  453. void led_matrix_decrease_speed(void) { led_matrix_decrease_speed_helper(true); }
  454. led_flags_t led_matrix_get_flags(void) { return led_matrix_eeconfig.flags; }
  455. void led_matrix_set_flags(led_flags_t flags) { led_matrix_eeconfig.flags = flags; }