led_matrix.c 25 KB

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