rgb_matrix.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2017 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "rgb_matrix.h"
  19. #include "progmem.h"
  20. #include "eeprom.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 <lib/lib8tion/lib8tion.h>
  29. #ifndef RGB_MATRIX_CENTER
  30. const led_point_t k_rgb_matrix_center = {112, 32};
  31. #else
  32. const led_point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
  33. #endif
  34. __attribute__((weak)) rgb_t rgb_matrix_hsv_to_rgb(hsv_t hsv) {
  35. return hsv_to_rgb(hsv);
  36. }
  37. // Generic effect runners
  38. #include "rgb_matrix_runners.inc"
  39. // ------------------------------------------
  40. // -----Begin rgb effect includes macros-----
  41. #define RGB_MATRIX_EFFECT(name)
  42. #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  43. #include "rgb_matrix_effects.inc"
  44. #ifdef RGB_MATRIX_CUSTOM_KB
  45. # include "rgb_matrix_kb.inc"
  46. #endif
  47. #ifdef RGB_MATRIX_CUSTOM_USER
  48. # include "rgb_matrix_user.inc"
  49. #endif
  50. #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  51. #undef RGB_MATRIX_EFFECT
  52. // -----End rgb effect includes macros-------
  53. // ------------------------------------------
  54. // globals
  55. rgb_config_t rgb_matrix_config; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
  56. uint32_t g_rgb_timer;
  57. #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
  58. uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
  59. #endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS
  60. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  61. last_hit_t g_last_hit_tracker;
  62. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  63. // internals
  64. static bool suspend_state = false;
  65. static uint8_t rgb_last_enable = UINT8_MAX;
  66. static uint8_t rgb_last_effect = UINT8_MAX;
  67. static effect_params_t rgb_effect_params = {0, LED_FLAG_ALL, false};
  68. static rgb_task_states rgb_task_state = SYNCING;
  69. // double buffers
  70. static uint32_t rgb_timer_buffer;
  71. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  72. static last_hit_t last_hit_buffer;
  73. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  74. // split rgb matrix
  75. #if defined(RGB_MATRIX_SPLIT)
  76. const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
  77. #endif
  78. EECONFIG_DEBOUNCE_HELPER(rgb_matrix, EECONFIG_RGB_MATRIX, rgb_matrix_config);
  79. void eeconfig_update_rgb_matrix(void) {
  80. eeconfig_flush_rgb_matrix(true);
  81. }
  82. void eeconfig_update_rgb_matrix_default(void) {
  83. dprintf("eeconfig_update_rgb_matrix_default\n");
  84. rgb_matrix_config.enable = RGB_MATRIX_DEFAULT_ON;
  85. rgb_matrix_config.mode = RGB_MATRIX_DEFAULT_MODE;
  86. rgb_matrix_config.hsv = (hsv_t){RGB_MATRIX_DEFAULT_HUE, RGB_MATRIX_DEFAULT_SAT, RGB_MATRIX_DEFAULT_VAL};
  87. rgb_matrix_config.speed = RGB_MATRIX_DEFAULT_SPD;
  88. rgb_matrix_config.flags = RGB_MATRIX_DEFAULT_FLAGS;
  89. eeconfig_flush_rgb_matrix(true);
  90. }
  91. void eeconfig_debug_rgb_matrix(void) {
  92. dprintf("rgb_matrix_config EEPROM\n");
  93. dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
  94. dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
  95. dprintf("rgb_matrix_config.hsv.h = %d\n", rgb_matrix_config.hsv.h);
  96. dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s);
  97. dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v);
  98. dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
  99. dprintf("rgb_matrix_config.flags = %d\n", rgb_matrix_config.flags);
  100. }
  101. void rgb_matrix_reload_from_eeprom(void) {
  102. rgb_matrix_disable_noeeprom();
  103. /* Reset back to what we have in eeprom */
  104. eeconfig_init_rgb_matrix();
  105. eeconfig_debug_rgb_matrix(); // display current eeprom values
  106. if (rgb_matrix_config.enable) {
  107. rgb_matrix_mode_noeeprom(rgb_matrix_config.mode);
  108. }
  109. }
  110. __attribute__((weak)) uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
  111. return 0;
  112. }
  113. uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  114. uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
  115. uint8_t led_index = g_led_config.matrix_co[row][column];
  116. if (led_index != NO_LED) {
  117. led_i[led_count] = led_index;
  118. led_count++;
  119. }
  120. return led_count;
  121. }
  122. void rgb_matrix_update_pwm_buffers(void) {
  123. rgb_matrix_driver.flush();
  124. }
  125. __attribute__((weak)) int rgb_matrix_led_index(int index) {
  126. #if defined(RGB_MATRIX_SPLIT)
  127. if (!is_keyboard_left() && index >= k_rgb_matrix_split[0]) {
  128. return index - k_rgb_matrix_split[0];
  129. }
  130. #endif
  131. return index;
  132. }
  133. void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  134. rgb_matrix_driver.set_color(rgb_matrix_led_index(index), red, green, blue);
  135. }
  136. void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  137. #if defined(RGB_MATRIX_SPLIT)
  138. for (uint8_t i = 0; i < RGB_MATRIX_LED_COUNT; i++)
  139. rgb_matrix_set_color(i, red, green, blue);
  140. #else
  141. rgb_matrix_driver.set_color_all(red, green, blue);
  142. #endif
  143. }
  144. void rgb_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed) {
  145. #ifndef RGB_MATRIX_SPLIT
  146. if (!is_keyboard_master()) return;
  147. #endif
  148. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  149. uint8_t led[LED_HITS_TO_REMEMBER];
  150. uint8_t led_count = 0;
  151. # if defined(RGB_MATRIX_KEYRELEASES)
  152. if (!pressed)
  153. # elif defined(RGB_MATRIX_KEYPRESSES)
  154. if (pressed)
  155. # endif // defined(RGB_MATRIX_KEYRELEASES)
  156. {
  157. led_count = rgb_matrix_map_row_column_to_led(row, col, led);
  158. }
  159. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  160. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  161. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  162. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  163. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  164. last_hit_buffer.count = LED_HITS_TO_REMEMBER - led_count;
  165. }
  166. for (uint8_t i = 0; i < led_count; i++) {
  167. uint8_t index = last_hit_buffer.count;
  168. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  169. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  170. last_hit_buffer.index[index] = led[i];
  171. last_hit_buffer.tick[index] = 0;
  172. last_hit_buffer.count++;
  173. }
  174. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  175. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)
  176. # if defined(RGB_MATRIX_KEYRELEASES)
  177. if (!pressed)
  178. # else
  179. if (pressed)
  180. # endif // defined(RGB_MATRIX_KEYRELEASES)
  181. {
  182. if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
  183. process_rgb_matrix_typing_heatmap(row, col);
  184. }
  185. }
  186. #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)
  187. }
  188. void rgb_matrix_test(void) {
  189. // Mask out bits 4 and 5
  190. // Increase the factor to make the test animation slower (and reduce to make it faster)
  191. uint8_t factor = 10;
  192. switch ((g_rgb_timer & (0b11 << factor)) >> factor) {
  193. case 0: {
  194. rgb_matrix_set_color_all(20, 0, 0);
  195. break;
  196. }
  197. case 1: {
  198. rgb_matrix_set_color_all(0, 20, 0);
  199. break;
  200. }
  201. case 2: {
  202. rgb_matrix_set_color_all(0, 0, 20);
  203. break;
  204. }
  205. case 3: {
  206. rgb_matrix_set_color_all(20, 20, 20);
  207. break;
  208. }
  209. }
  210. }
  211. static bool rgb_matrix_none(effect_params_t *params) {
  212. if (!params->init) {
  213. return false;
  214. }
  215. rgb_matrix_set_color_all(0, 0, 0);
  216. return false;
  217. }
  218. static void rgb_task_timers(void) {
  219. #if defined(RGB_MATRIX_KEYREACTIVE_ENABLED)
  220. uint32_t deltaTime = sync_timer_elapsed32(rgb_timer_buffer);
  221. #endif // defined(RGB_MATRIX_KEYREACTIVE_ENABLED)
  222. rgb_timer_buffer = sync_timer_read32();
  223. // Update double buffer last hit timers
  224. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  225. uint8_t count = last_hit_buffer.count;
  226. for (uint8_t i = 0; i < count; ++i) {
  227. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  228. last_hit_buffer.count--;
  229. continue;
  230. }
  231. last_hit_buffer.tick[i] += deltaTime;
  232. }
  233. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  234. }
  235. static void rgb_task_sync(void) {
  236. eeconfig_flush_rgb_matrix(false);
  237. // next task
  238. if (sync_timer_elapsed32(g_rgb_timer) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
  239. }
  240. static void rgb_task_start(void) {
  241. // reset iter
  242. rgb_effect_params.iter = 0;
  243. // update double buffers
  244. g_rgb_timer = rgb_timer_buffer;
  245. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  246. g_last_hit_tracker = last_hit_buffer;
  247. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  248. // next task
  249. rgb_task_state = RENDERING;
  250. }
  251. static void rgb_task_render(uint8_t effect) {
  252. bool rendering = false;
  253. rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
  254. if (rgb_effect_params.flags != rgb_matrix_config.flags) {
  255. rgb_effect_params.flags = rgb_matrix_config.flags;
  256. rgb_matrix_set_color_all(0, 0, 0);
  257. }
  258. // each effect can opt to do calculations
  259. // and/or request PWM buffer updates.
  260. switch (effect) {
  261. case RGB_MATRIX_NONE:
  262. rendering = rgb_matrix_none(&rgb_effect_params);
  263. break;
  264. // ---------------------------------------------
  265. // -----Begin rgb effect switch case macros-----
  266. #define RGB_MATRIX_EFFECT(name, ...) \
  267. case RGB_MATRIX_##name: \
  268. rendering = name(&rgb_effect_params); \
  269. break;
  270. #include "rgb_matrix_effects.inc"
  271. #undef RGB_MATRIX_EFFECT
  272. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  273. # define RGB_MATRIX_EFFECT(name, ...) \
  274. case RGB_MATRIX_CUSTOM_##name: \
  275. rendering = name(&rgb_effect_params); \
  276. break;
  277. # ifdef RGB_MATRIX_CUSTOM_KB
  278. # include "rgb_matrix_kb.inc"
  279. # endif
  280. # ifdef RGB_MATRIX_CUSTOM_USER
  281. # include "rgb_matrix_user.inc"
  282. # endif
  283. # undef RGB_MATRIX_EFFECT
  284. #endif
  285. // -----End rgb effect switch case macros-------
  286. // ---------------------------------------------
  287. // Factory default magic value
  288. case UINT8_MAX: {
  289. rgb_matrix_test();
  290. rgb_task_state = FLUSHING;
  291. }
  292. return;
  293. }
  294. rgb_effect_params.iter++;
  295. // next task
  296. if (!rendering) {
  297. rgb_task_state = FLUSHING;
  298. if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
  299. // We only need to flush once if we are RGB_MATRIX_NONE
  300. rgb_task_state = SYNCING;
  301. }
  302. }
  303. }
  304. static void rgb_task_flush(uint8_t effect) {
  305. // update last trackers after the first full render so we can init over several frames
  306. rgb_last_effect = effect;
  307. rgb_last_enable = rgb_matrix_config.enable;
  308. // update pwm buffers
  309. rgb_matrix_update_pwm_buffers();
  310. // next task
  311. rgb_task_state = SYNCING;
  312. }
  313. void rgb_matrix_task(void) {
  314. rgb_task_timers();
  315. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  316. // while suspended and just do a software shutdown. This is a cheap hack for now.
  317. bool suspend_backlight = suspend_state ||
  318. #if RGB_MATRIX_TIMEOUT > 0
  319. (last_input_activity_elapsed() > (uint32_t)RGB_MATRIX_TIMEOUT) ||
  320. #endif // RGB_MATRIX_TIMEOUT > 0
  321. false;
  322. uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
  323. switch (rgb_task_state) {
  324. case STARTING:
  325. rgb_task_start();
  326. break;
  327. case RENDERING:
  328. rgb_task_render(effect);
  329. if (effect) {
  330. if (rgb_task_state == FLUSHING) { // ensure we only draw basic indicators once rendering is finished
  331. rgb_matrix_indicators();
  332. }
  333. rgb_matrix_indicators_advanced(&rgb_effect_params);
  334. }
  335. break;
  336. case FLUSHING:
  337. rgb_task_flush(effect);
  338. break;
  339. case SYNCING:
  340. rgb_task_sync();
  341. break;
  342. }
  343. }
  344. void rgb_matrix_indicators(void) {
  345. rgb_matrix_indicators_kb();
  346. }
  347. __attribute__((weak)) bool rgb_matrix_indicators_kb(void) {
  348. return rgb_matrix_indicators_user();
  349. }
  350. __attribute__((weak)) bool rgb_matrix_indicators_user(void) {
  351. return true;
  352. }
  353. struct rgb_matrix_limits_t rgb_matrix_get_limits(uint8_t iter) {
  354. struct rgb_matrix_limits_t limits = {0};
  355. #if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < RGB_MATRIX_LED_COUNT
  356. # if defined(RGB_MATRIX_SPLIT)
  357. limits.led_min_index = RGB_MATRIX_LED_PROCESS_LIMIT * (iter);
  358. limits.led_max_index = limits.led_min_index + RGB_MATRIX_LED_PROCESS_LIMIT;
  359. if (limits.led_max_index > RGB_MATRIX_LED_COUNT) limits.led_max_index = RGB_MATRIX_LED_COUNT;
  360. if (is_keyboard_left() && (limits.led_max_index > k_rgb_matrix_split[0])) limits.led_max_index = k_rgb_matrix_split[0];
  361. if (!(is_keyboard_left()) && (limits.led_min_index < k_rgb_matrix_split[0])) limits.led_min_index = k_rgb_matrix_split[0];
  362. # else
  363. limits.led_min_index = RGB_MATRIX_LED_PROCESS_LIMIT * (iter);
  364. limits.led_max_index = limits.led_min_index + RGB_MATRIX_LED_PROCESS_LIMIT;
  365. if (limits.led_max_index > RGB_MATRIX_LED_COUNT) limits.led_max_index = RGB_MATRIX_LED_COUNT;
  366. # endif
  367. #else
  368. # if defined(RGB_MATRIX_SPLIT)
  369. limits.led_min_index = 0;
  370. limits.led_max_index = RGB_MATRIX_LED_COUNT;
  371. if (is_keyboard_left() && (limits.led_max_index > k_rgb_matrix_split[0])) limits.led_max_index = k_rgb_matrix_split[0];
  372. if (!(is_keyboard_left()) && (limits.led_min_index < k_rgb_matrix_split[0])) limits.led_min_index = k_rgb_matrix_split[0];
  373. # else
  374. limits.led_min_index = 0;
  375. limits.led_max_index = RGB_MATRIX_LED_COUNT;
  376. # endif
  377. #endif
  378. return limits;
  379. }
  380. void rgb_matrix_indicators_advanced(effect_params_t *params) {
  381. /* special handling is needed for "params->iter", since it's already been incremented.
  382. * Could move the invocations to rgb_task_render, but then it's missing a few checks
  383. * and not sure which would be better. Otherwise, this should be called from
  384. * rgb_task_render, right before the iter++ line.
  385. */
  386. RGB_MATRIX_USE_LIMITS_ITER(min, max, params->iter - 1);
  387. rgb_matrix_indicators_advanced_kb(min, max);
  388. }
  389. __attribute__((weak)) bool rgb_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {
  390. return rgb_matrix_indicators_advanced_user(led_min, led_max);
  391. }
  392. __attribute__((weak)) bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
  393. return true;
  394. }
  395. void rgb_matrix_init(void) {
  396. rgb_matrix_driver.init();
  397. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  398. g_last_hit_tracker.count = 0;
  399. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  400. g_last_hit_tracker.tick[i] = UINT16_MAX;
  401. }
  402. last_hit_buffer.count = 0;
  403. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  404. last_hit_buffer.tick[i] = UINT16_MAX;
  405. }
  406. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  407. eeconfig_init_rgb_matrix();
  408. if (!rgb_matrix_config.mode) {
  409. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  410. eeconfig_update_rgb_matrix_default();
  411. }
  412. eeconfig_debug_rgb_matrix(); // display current eeprom values
  413. }
  414. void rgb_matrix_set_suspend_state(bool state) {
  415. #ifdef RGB_MATRIX_SLEEP
  416. if (state && !suspend_state) { // only run if turning off, and only once
  417. rgb_task_render(0); // turn off all LEDs when suspending
  418. rgb_task_flush(0); // and actually flash led state to LEDs
  419. }
  420. suspend_state = state;
  421. #endif
  422. }
  423. bool rgb_matrix_get_suspend_state(void) {
  424. return suspend_state;
  425. }
  426. void rgb_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
  427. rgb_matrix_config.enable ^= 1;
  428. rgb_task_state = STARTING;
  429. eeconfig_flag_rgb_matrix(write_to_eeprom);
  430. dprintf("rgb matrix toggle [%s]: rgb_matrix_config.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.enable);
  431. }
  432. void rgb_matrix_toggle_noeeprom(void) {
  433. rgb_matrix_toggle_eeprom_helper(false);
  434. }
  435. void rgb_matrix_toggle(void) {
  436. rgb_matrix_toggle_eeprom_helper(true);
  437. }
  438. void rgb_matrix_enable(void) {
  439. rgb_matrix_enable_noeeprom();
  440. eeconfig_flag_rgb_matrix(true);
  441. }
  442. void rgb_matrix_enable_noeeprom(void) {
  443. if (!rgb_matrix_config.enable) rgb_task_state = STARTING;
  444. rgb_matrix_config.enable = 1;
  445. }
  446. void rgb_matrix_disable(void) {
  447. rgb_matrix_disable_noeeprom();
  448. eeconfig_flag_rgb_matrix(true);
  449. }
  450. void rgb_matrix_disable_noeeprom(void) {
  451. if (rgb_matrix_config.enable) rgb_task_state = STARTING;
  452. rgb_matrix_config.enable = 0;
  453. }
  454. uint8_t rgb_matrix_is_enabled(void) {
  455. return rgb_matrix_config.enable;
  456. }
  457. void rgb_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  458. if (!rgb_matrix_config.enable) {
  459. return;
  460. }
  461. if (mode < 1) {
  462. rgb_matrix_config.mode = 1;
  463. } else if (mode >= RGB_MATRIX_EFFECT_MAX) {
  464. rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  465. } else {
  466. rgb_matrix_config.mode = mode;
  467. }
  468. rgb_task_state = STARTING;
  469. eeconfig_flag_rgb_matrix(write_to_eeprom);
  470. dprintf("rgb matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.mode);
  471. }
  472. void rgb_matrix_mode_noeeprom(uint8_t mode) {
  473. rgb_matrix_mode_eeprom_helper(mode, false);
  474. }
  475. void rgb_matrix_mode(uint8_t mode) {
  476. rgb_matrix_mode_eeprom_helper(mode, true);
  477. }
  478. uint8_t rgb_matrix_get_mode(void) {
  479. return rgb_matrix_config.mode;
  480. }
  481. void rgb_matrix_step_helper(bool write_to_eeprom) {
  482. uint8_t mode = rgb_matrix_config.mode + 1;
  483. rgb_matrix_mode_eeprom_helper((mode < RGB_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
  484. }
  485. void rgb_matrix_step_noeeprom(void) {
  486. rgb_matrix_step_helper(false);
  487. }
  488. void rgb_matrix_step(void) {
  489. rgb_matrix_step_helper(true);
  490. }
  491. void rgb_matrix_step_reverse_helper(bool write_to_eeprom) {
  492. uint8_t mode = rgb_matrix_config.mode - 1;
  493. rgb_matrix_mode_eeprom_helper((mode < 1) ? RGB_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
  494. }
  495. void rgb_matrix_step_reverse_noeeprom(void) {
  496. rgb_matrix_step_reverse_helper(false);
  497. }
  498. void rgb_matrix_step_reverse(void) {
  499. rgb_matrix_step_reverse_helper(true);
  500. }
  501. void rgb_matrix_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
  502. if (!rgb_matrix_config.enable) {
  503. return;
  504. }
  505. rgb_matrix_config.hsv.h = hue;
  506. rgb_matrix_config.hsv.s = sat;
  507. rgb_matrix_config.hsv.v = (val > RGB_MATRIX_MAXIMUM_BRIGHTNESS) ? RGB_MATRIX_MAXIMUM_BRIGHTNESS : val;
  508. eeconfig_flag_rgb_matrix(write_to_eeprom);
  509. dprintf("rgb matrix set hsv [%s]: %u,%u,%u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v);
  510. }
  511. void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  512. rgb_matrix_sethsv_eeprom_helper(hue, sat, val, false);
  513. }
  514. void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  515. rgb_matrix_sethsv_eeprom_helper(hue, sat, val, true);
  516. }
  517. hsv_t rgb_matrix_get_hsv(void) {
  518. return rgb_matrix_config.hsv;
  519. }
  520. uint8_t rgb_matrix_get_hue(void) {
  521. return rgb_matrix_config.hsv.h;
  522. }
  523. uint8_t rgb_matrix_get_sat(void) {
  524. return rgb_matrix_config.hsv.s;
  525. }
  526. uint8_t rgb_matrix_get_val(void) {
  527. return rgb_matrix_config.hsv.v;
  528. }
  529. void rgb_matrix_increase_hue_helper(bool write_to_eeprom) {
  530. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h + RGB_MATRIX_HUE_STEP, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v, write_to_eeprom);
  531. }
  532. void rgb_matrix_increase_hue_noeeprom(void) {
  533. rgb_matrix_increase_hue_helper(false);
  534. }
  535. void rgb_matrix_increase_hue(void) {
  536. rgb_matrix_increase_hue_helper(true);
  537. }
  538. void rgb_matrix_decrease_hue_helper(bool write_to_eeprom) {
  539. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h - RGB_MATRIX_HUE_STEP, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v, write_to_eeprom);
  540. }
  541. void rgb_matrix_decrease_hue_noeeprom(void) {
  542. rgb_matrix_decrease_hue_helper(false);
  543. }
  544. void rgb_matrix_decrease_hue(void) {
  545. rgb_matrix_decrease_hue_helper(true);
  546. }
  547. void rgb_matrix_increase_sat_helper(bool write_to_eeprom) {
  548. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, qadd8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP), rgb_matrix_config.hsv.v, write_to_eeprom);
  549. }
  550. void rgb_matrix_increase_sat_noeeprom(void) {
  551. rgb_matrix_increase_sat_helper(false);
  552. }
  553. void rgb_matrix_increase_sat(void) {
  554. rgb_matrix_increase_sat_helper(true);
  555. }
  556. void rgb_matrix_decrease_sat_helper(bool write_to_eeprom) {
  557. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, qsub8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP), rgb_matrix_config.hsv.v, write_to_eeprom);
  558. }
  559. void rgb_matrix_decrease_sat_noeeprom(void) {
  560. rgb_matrix_decrease_sat_helper(false);
  561. }
  562. void rgb_matrix_decrease_sat(void) {
  563. rgb_matrix_decrease_sat_helper(true);
  564. }
  565. void rgb_matrix_increase_val_helper(bool write_to_eeprom) {
  566. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom);
  567. }
  568. void rgb_matrix_increase_val_noeeprom(void) {
  569. rgb_matrix_increase_val_helper(false);
  570. }
  571. void rgb_matrix_increase_val(void) {
  572. rgb_matrix_increase_val_helper(true);
  573. }
  574. void rgb_matrix_decrease_val_helper(bool write_to_eeprom) {
  575. rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom);
  576. }
  577. void rgb_matrix_decrease_val_noeeprom(void) {
  578. rgb_matrix_decrease_val_helper(false);
  579. }
  580. void rgb_matrix_decrease_val(void) {
  581. rgb_matrix_decrease_val_helper(true);
  582. }
  583. void rgb_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  584. rgb_matrix_config.speed = speed;
  585. eeconfig_flag_rgb_matrix(write_to_eeprom);
  586. dprintf("rgb matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.speed);
  587. }
  588. void rgb_matrix_set_speed_noeeprom(uint8_t speed) {
  589. rgb_matrix_set_speed_eeprom_helper(speed, false);
  590. }
  591. void rgb_matrix_set_speed(uint8_t speed) {
  592. rgb_matrix_set_speed_eeprom_helper(speed, true);
  593. }
  594. uint8_t rgb_matrix_get_speed(void) {
  595. return rgb_matrix_config.speed;
  596. }
  597. void rgb_matrix_increase_speed_helper(bool write_to_eeprom) {
  598. rgb_matrix_set_speed_eeprom_helper(qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom);
  599. }
  600. void rgb_matrix_increase_speed_noeeprom(void) {
  601. rgb_matrix_increase_speed_helper(false);
  602. }
  603. void rgb_matrix_increase_speed(void) {
  604. rgb_matrix_increase_speed_helper(true);
  605. }
  606. void rgb_matrix_decrease_speed_helper(bool write_to_eeprom) {
  607. rgb_matrix_set_speed_eeprom_helper(qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom);
  608. }
  609. void rgb_matrix_decrease_speed_noeeprom(void) {
  610. rgb_matrix_decrease_speed_helper(false);
  611. }
  612. void rgb_matrix_decrease_speed(void) {
  613. rgb_matrix_decrease_speed_helper(true);
  614. }
  615. void rgb_matrix_set_flags_eeprom_helper(led_flags_t flags, bool write_to_eeprom) {
  616. rgb_matrix_config.flags = flags;
  617. eeconfig_flag_rgb_matrix(write_to_eeprom);
  618. dprintf("rgb matrix set flags [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.flags);
  619. }
  620. led_flags_t rgb_matrix_get_flags(void) {
  621. return rgb_matrix_config.flags;
  622. }
  623. void rgb_matrix_set_flags(led_flags_t flags) {
  624. rgb_matrix_set_flags_eeprom_helper(flags, true);
  625. }
  626. void rgb_matrix_set_flags_noeeprom(led_flags_t flags) {
  627. rgb_matrix_set_flags_eeprom_helper(flags, false);
  628. }