rgb_matrix.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 rgb_matrix_hsv_to_rgb(HSV 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){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. void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
  126. rgb_matrix_driver.set_color(index, red, green, blue);
  127. }
  128. void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
  129. #if defined(RGB_MATRIX_SPLIT)
  130. for (uint8_t i = 0; i < RGB_MATRIX_LED_COUNT; i++)
  131. rgb_matrix_set_color(i, red, green, blue);
  132. #else
  133. rgb_matrix_driver.set_color_all(red, green, blue);
  134. #endif
  135. }
  136. void rgb_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed) {
  137. #ifndef RGB_MATRIX_SPLIT
  138. if (!is_keyboard_master()) return;
  139. #endif
  140. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  141. uint8_t led[LED_HITS_TO_REMEMBER];
  142. uint8_t led_count = 0;
  143. # if defined(RGB_MATRIX_KEYRELEASES)
  144. if (!pressed)
  145. # elif defined(RGB_MATRIX_KEYPRESSES)
  146. if (pressed)
  147. # endif // defined(RGB_MATRIX_KEYRELEASES)
  148. {
  149. led_count = rgb_matrix_map_row_column_to_led(row, col, led);
  150. }
  151. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  152. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  153. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  154. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  155. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  156. last_hit_buffer.count = LED_HITS_TO_REMEMBER - led_count;
  157. }
  158. for (uint8_t i = 0; i < led_count; i++) {
  159. uint8_t index = last_hit_buffer.count;
  160. last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
  161. last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
  162. last_hit_buffer.index[index] = led[i];
  163. last_hit_buffer.tick[index] = 0;
  164. last_hit_buffer.count++;
  165. }
  166. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  167. #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)
  168. # if defined(RGB_MATRIX_KEYRELEASES)
  169. if (!pressed)
  170. # else
  171. if (pressed)
  172. # endif // defined(RGB_MATRIX_KEYRELEASES)
  173. {
  174. if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
  175. process_rgb_matrix_typing_heatmap(row, col);
  176. }
  177. }
  178. #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_RGB_MATRIX_TYPING_HEATMAP)
  179. }
  180. void rgb_matrix_test(void) {
  181. // Mask out bits 4 and 5
  182. // Increase the factor to make the test animation slower (and reduce to make it faster)
  183. uint8_t factor = 10;
  184. switch ((g_rgb_timer & (0b11 << factor)) >> factor) {
  185. case 0: {
  186. rgb_matrix_set_color_all(20, 0, 0);
  187. break;
  188. }
  189. case 1: {
  190. rgb_matrix_set_color_all(0, 20, 0);
  191. break;
  192. }
  193. case 2: {
  194. rgb_matrix_set_color_all(0, 0, 20);
  195. break;
  196. }
  197. case 3: {
  198. rgb_matrix_set_color_all(20, 20, 20);
  199. break;
  200. }
  201. }
  202. }
  203. static bool rgb_matrix_none(effect_params_t *params) {
  204. if (!params->init) {
  205. return false;
  206. }
  207. rgb_matrix_set_color_all(0, 0, 0);
  208. return false;
  209. }
  210. static void rgb_task_timers(void) {
  211. #if defined(RGB_MATRIX_KEYREACTIVE_ENABLED)
  212. uint32_t deltaTime = sync_timer_elapsed32(rgb_timer_buffer);
  213. #endif // defined(RGB_MATRIX_KEYREACTIVE_ENABLED)
  214. rgb_timer_buffer = sync_timer_read32();
  215. // Update double buffer last hit timers
  216. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  217. uint8_t count = last_hit_buffer.count;
  218. for (uint8_t i = 0; i < count; ++i) {
  219. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  220. last_hit_buffer.count--;
  221. continue;
  222. }
  223. last_hit_buffer.tick[i] += deltaTime;
  224. }
  225. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  226. }
  227. static void rgb_task_sync(void) {
  228. eeconfig_flush_rgb_matrix(false);
  229. // next task
  230. if (sync_timer_elapsed32(g_rgb_timer) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
  231. }
  232. static void rgb_task_start(void) {
  233. // reset iter
  234. rgb_effect_params.iter = 0;
  235. // update double buffers
  236. g_rgb_timer = rgb_timer_buffer;
  237. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  238. g_last_hit_tracker = last_hit_buffer;
  239. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  240. // next task
  241. rgb_task_state = RENDERING;
  242. }
  243. static void rgb_task_render(uint8_t effect) {
  244. bool rendering = false;
  245. rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
  246. if (rgb_effect_params.flags != rgb_matrix_config.flags) {
  247. rgb_effect_params.flags = rgb_matrix_config.flags;
  248. rgb_matrix_set_color_all(0, 0, 0);
  249. }
  250. // each effect can opt to do calculations
  251. // and/or request PWM buffer updates.
  252. switch (effect) {
  253. case RGB_MATRIX_NONE:
  254. rendering = rgb_matrix_none(&rgb_effect_params);
  255. break;
  256. // ---------------------------------------------
  257. // -----Begin rgb effect switch case macros-----
  258. #define RGB_MATRIX_EFFECT(name, ...) \
  259. case RGB_MATRIX_##name: \
  260. rendering = name(&rgb_effect_params); \
  261. break;
  262. #include "rgb_matrix_effects.inc"
  263. #undef RGB_MATRIX_EFFECT
  264. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  265. # define RGB_MATRIX_EFFECT(name, ...) \
  266. case RGB_MATRIX_CUSTOM_##name: \
  267. rendering = name(&rgb_effect_params); \
  268. break;
  269. # ifdef RGB_MATRIX_CUSTOM_KB
  270. # include "rgb_matrix_kb.inc"
  271. # endif
  272. # ifdef RGB_MATRIX_CUSTOM_USER
  273. # include "rgb_matrix_user.inc"
  274. # endif
  275. # undef RGB_MATRIX_EFFECT
  276. #endif
  277. // -----End rgb effect switch case macros-------
  278. // ---------------------------------------------
  279. // Factory default magic value
  280. case UINT8_MAX: {
  281. rgb_matrix_test();
  282. rgb_task_state = FLUSHING;
  283. }
  284. return;
  285. }
  286. rgb_effect_params.iter++;
  287. // next task
  288. if (!rendering) {
  289. rgb_task_state = FLUSHING;
  290. if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
  291. // We only need to flush once if we are RGB_MATRIX_NONE
  292. rgb_task_state = SYNCING;
  293. }
  294. }
  295. }
  296. static void rgb_task_flush(uint8_t effect) {
  297. // update last trackers after the first full render so we can init over several frames
  298. rgb_last_effect = effect;
  299. rgb_last_enable = rgb_matrix_config.enable;
  300. // update pwm buffers
  301. rgb_matrix_update_pwm_buffers();
  302. // next task
  303. rgb_task_state = SYNCING;
  304. }
  305. void rgb_matrix_task(void) {
  306. rgb_task_timers();
  307. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  308. // while suspended and just do a software shutdown. This is a cheap hack for now.
  309. bool suspend_backlight = suspend_state ||
  310. #if RGB_MATRIX_TIMEOUT > 0
  311. (last_input_activity_elapsed() > (uint32_t)RGB_MATRIX_TIMEOUT) ||
  312. #endif // RGB_MATRIX_TIMEOUT > 0
  313. false;
  314. uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
  315. switch (rgb_task_state) {
  316. case STARTING:
  317. rgb_task_start();
  318. break;
  319. case RENDERING:
  320. rgb_task_render(effect);
  321. if (effect) {
  322. if (rgb_task_state == FLUSHING) { // ensure we only draw basic indicators once rendering is finished
  323. rgb_matrix_indicators();
  324. }
  325. rgb_matrix_indicators_advanced(&rgb_effect_params);
  326. }
  327. break;
  328. case FLUSHING:
  329. rgb_task_flush(effect);
  330. break;
  331. case SYNCING:
  332. rgb_task_sync();
  333. break;
  334. }
  335. }
  336. void rgb_matrix_indicators(void) {
  337. rgb_matrix_indicators_kb();
  338. }
  339. __attribute__((weak)) bool rgb_matrix_indicators_kb(void) {
  340. return rgb_matrix_indicators_user();
  341. }
  342. __attribute__((weak)) bool rgb_matrix_indicators_user(void) {
  343. return true;
  344. }
  345. struct rgb_matrix_limits_t rgb_matrix_get_limits(uint8_t iter) {
  346. struct rgb_matrix_limits_t limits = {0};
  347. #if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < RGB_MATRIX_LED_COUNT
  348. # if defined(RGB_MATRIX_SPLIT)
  349. limits.led_min_index = RGB_MATRIX_LED_PROCESS_LIMIT * (iter);
  350. limits.led_max_index = limits.led_min_index + RGB_MATRIX_LED_PROCESS_LIMIT;
  351. if (limits.led_max_index > RGB_MATRIX_LED_COUNT) limits.led_max_index = RGB_MATRIX_LED_COUNT;
  352. uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
  353. if (is_keyboard_left() && (limits.led_max_index > k_rgb_matrix_split[0])) limits.led_max_index = k_rgb_matrix_split[0];
  354. if (!(is_keyboard_left()) && (limits.led_min_index < k_rgb_matrix_split[0])) limits.led_min_index = k_rgb_matrix_split[0];
  355. # else
  356. limits.led_min_index = RGB_MATRIX_LED_PROCESS_LIMIT * (iter);
  357. limits.led_max_index = limits.led_min_index + RGB_MATRIX_LED_PROCESS_LIMIT;
  358. if (limits.led_max_index > RGB_MATRIX_LED_COUNT) limits.led_max_index = RGB_MATRIX_LED_COUNT;
  359. # endif
  360. #else
  361. # if defined(RGB_MATRIX_SPLIT)
  362. limits.led_min_index = 0;
  363. limits.led_max_index = RGB_MATRIX_LED_COUNT;
  364. const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
  365. if (is_keyboard_left() && (limits.led_max_index > k_rgb_matrix_split[0])) limits.led_max_index = k_rgb_matrix_split[0];
  366. if (!(is_keyboard_left()) && (limits.led_min_index < k_rgb_matrix_split[0])) limits.led_min_index = k_rgb_matrix_split[0];
  367. # else
  368. limits.led_min_index = 0;
  369. limits.led_max_index = RGB_MATRIX_LED_COUNT;
  370. # endif
  371. #endif
  372. return limits;
  373. }
  374. void rgb_matrix_indicators_advanced(effect_params_t *params) {
  375. /* special handling is needed for "params->iter", since it's already been incremented.
  376. * Could move the invocations to rgb_task_render, but then it's missing a few checks
  377. * and not sure which would be better. Otherwise, this should be called from
  378. * rgb_task_render, right before the iter++ line.
  379. */
  380. RGB_MATRIX_USE_LIMITS_ITER(min, max, params->iter - 1);
  381. rgb_matrix_indicators_advanced_kb(min, max);
  382. }
  383. __attribute__((weak)) bool rgb_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {
  384. return rgb_matrix_indicators_advanced_user(led_min, led_max);
  385. }
  386. __attribute__((weak)) bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
  387. return true;
  388. }
  389. void rgb_matrix_init(void) {
  390. rgb_matrix_driver.init();
  391. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  392. g_last_hit_tracker.count = 0;
  393. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  394. g_last_hit_tracker.tick[i] = UINT16_MAX;
  395. }
  396. last_hit_buffer.count = 0;
  397. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  398. last_hit_buffer.tick[i] = UINT16_MAX;
  399. }
  400. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  401. eeconfig_init_rgb_matrix();
  402. if (!rgb_matrix_config.mode) {
  403. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  404. eeconfig_update_rgb_matrix_default();
  405. }
  406. eeconfig_debug_rgb_matrix(); // display current eeprom values
  407. }
  408. void rgb_matrix_set_suspend_state(bool state) {
  409. #ifdef RGB_MATRIX_SLEEP
  410. if (state && !suspend_state) { // only run if turning off, and only once
  411. rgb_task_render(0); // turn off all LEDs when suspending
  412. rgb_task_flush(0); // and actually flash led state to LEDs
  413. }
  414. suspend_state = state;
  415. #endif
  416. }
  417. bool rgb_matrix_get_suspend_state(void) {
  418. return suspend_state;
  419. }
  420. void rgb_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
  421. rgb_matrix_config.enable ^= 1;
  422. rgb_task_state = STARTING;
  423. eeconfig_flag_rgb_matrix(write_to_eeprom);
  424. dprintf("rgb matrix toggle [%s]: rgb_matrix_config.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.enable);
  425. }
  426. void rgb_matrix_toggle_noeeprom(void) {
  427. rgb_matrix_toggle_eeprom_helper(false);
  428. }
  429. void rgb_matrix_toggle(void) {
  430. rgb_matrix_toggle_eeprom_helper(true);
  431. }
  432. void rgb_matrix_enable(void) {
  433. rgb_matrix_enable_noeeprom();
  434. eeconfig_flag_rgb_matrix(true);
  435. }
  436. void rgb_matrix_enable_noeeprom(void) {
  437. if (!rgb_matrix_config.enable) rgb_task_state = STARTING;
  438. rgb_matrix_config.enable = 1;
  439. }
  440. void rgb_matrix_disable(void) {
  441. rgb_matrix_disable_noeeprom();
  442. eeconfig_flag_rgb_matrix(true);
  443. }
  444. void rgb_matrix_disable_noeeprom(void) {
  445. if (rgb_matrix_config.enable) rgb_task_state = STARTING;
  446. rgb_matrix_config.enable = 0;
  447. }
  448. uint8_t rgb_matrix_is_enabled(void) {
  449. return rgb_matrix_config.enable;
  450. }
  451. void rgb_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  452. if (!rgb_matrix_config.enable) {
  453. return;
  454. }
  455. if (mode < 1) {
  456. rgb_matrix_config.mode = 1;
  457. } else if (mode >= RGB_MATRIX_EFFECT_MAX) {
  458. rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  459. } else {
  460. rgb_matrix_config.mode = mode;
  461. }
  462. rgb_task_state = STARTING;
  463. eeconfig_flag_rgb_matrix(write_to_eeprom);
  464. dprintf("rgb matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.mode);
  465. }
  466. void rgb_matrix_mode_noeeprom(uint8_t mode) {
  467. rgb_matrix_mode_eeprom_helper(mode, false);
  468. }
  469. void rgb_matrix_mode(uint8_t mode) {
  470. rgb_matrix_mode_eeprom_helper(mode, true);
  471. }
  472. uint8_t rgb_matrix_get_mode(void) {
  473. return rgb_matrix_config.mode;
  474. }
  475. void rgb_matrix_step_helper(bool write_to_eeprom) {
  476. uint8_t mode = rgb_matrix_config.mode + 1;
  477. rgb_matrix_mode_eeprom_helper((mode < RGB_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom);
  478. }
  479. void rgb_matrix_step_noeeprom(void) {
  480. rgb_matrix_step_helper(false);
  481. }
  482. void rgb_matrix_step(void) {
  483. rgb_matrix_step_helper(true);
  484. }
  485. void rgb_matrix_step_reverse_helper(bool write_to_eeprom) {
  486. uint8_t mode = rgb_matrix_config.mode - 1;
  487. rgb_matrix_mode_eeprom_helper((mode < 1) ? RGB_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom);
  488. }
  489. void rgb_matrix_step_reverse_noeeprom(void) {
  490. rgb_matrix_step_reverse_helper(false);
  491. }
  492. void rgb_matrix_step_reverse(void) {
  493. rgb_matrix_step_reverse_helper(true);
  494. }
  495. void rgb_matrix_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
  496. if (!rgb_matrix_config.enable) {
  497. return;
  498. }
  499. rgb_matrix_config.hsv.h = hue;
  500. rgb_matrix_config.hsv.s = sat;
  501. rgb_matrix_config.hsv.v = (val > RGB_MATRIX_MAXIMUM_BRIGHTNESS) ? RGB_MATRIX_MAXIMUM_BRIGHTNESS : val;
  502. eeconfig_flag_rgb_matrix(write_to_eeprom);
  503. 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);
  504. }
  505. void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  506. rgb_matrix_sethsv_eeprom_helper(hue, sat, val, false);
  507. }
  508. void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  509. rgb_matrix_sethsv_eeprom_helper(hue, sat, val, true);
  510. }
  511. HSV rgb_matrix_get_hsv(void) {
  512. return rgb_matrix_config.hsv;
  513. }
  514. uint8_t rgb_matrix_get_hue(void) {
  515. return rgb_matrix_config.hsv.h;
  516. }
  517. uint8_t rgb_matrix_get_sat(void) {
  518. return rgb_matrix_config.hsv.s;
  519. }
  520. uint8_t rgb_matrix_get_val(void) {
  521. return rgb_matrix_config.hsv.v;
  522. }
  523. void rgb_matrix_increase_hue_helper(bool write_to_eeprom) {
  524. 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);
  525. }
  526. void rgb_matrix_increase_hue_noeeprom(void) {
  527. rgb_matrix_increase_hue_helper(false);
  528. }
  529. void rgb_matrix_increase_hue(void) {
  530. rgb_matrix_increase_hue_helper(true);
  531. }
  532. void rgb_matrix_decrease_hue_helper(bool write_to_eeprom) {
  533. 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);
  534. }
  535. void rgb_matrix_decrease_hue_noeeprom(void) {
  536. rgb_matrix_decrease_hue_helper(false);
  537. }
  538. void rgb_matrix_decrease_hue(void) {
  539. rgb_matrix_decrease_hue_helper(true);
  540. }
  541. void rgb_matrix_increase_sat_helper(bool write_to_eeprom) {
  542. 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);
  543. }
  544. void rgb_matrix_increase_sat_noeeprom(void) {
  545. rgb_matrix_increase_sat_helper(false);
  546. }
  547. void rgb_matrix_increase_sat(void) {
  548. rgb_matrix_increase_sat_helper(true);
  549. }
  550. void rgb_matrix_decrease_sat_helper(bool write_to_eeprom) {
  551. 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);
  552. }
  553. void rgb_matrix_decrease_sat_noeeprom(void) {
  554. rgb_matrix_decrease_sat_helper(false);
  555. }
  556. void rgb_matrix_decrease_sat(void) {
  557. rgb_matrix_decrease_sat_helper(true);
  558. }
  559. void rgb_matrix_increase_val_helper(bool write_to_eeprom) {
  560. 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);
  561. }
  562. void rgb_matrix_increase_val_noeeprom(void) {
  563. rgb_matrix_increase_val_helper(false);
  564. }
  565. void rgb_matrix_increase_val(void) {
  566. rgb_matrix_increase_val_helper(true);
  567. }
  568. void rgb_matrix_decrease_val_helper(bool write_to_eeprom) {
  569. 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);
  570. }
  571. void rgb_matrix_decrease_val_noeeprom(void) {
  572. rgb_matrix_decrease_val_helper(false);
  573. }
  574. void rgb_matrix_decrease_val(void) {
  575. rgb_matrix_decrease_val_helper(true);
  576. }
  577. void rgb_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  578. rgb_matrix_config.speed = speed;
  579. eeconfig_flag_rgb_matrix(write_to_eeprom);
  580. dprintf("rgb matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.speed);
  581. }
  582. void rgb_matrix_set_speed_noeeprom(uint8_t speed) {
  583. rgb_matrix_set_speed_eeprom_helper(speed, false);
  584. }
  585. void rgb_matrix_set_speed(uint8_t speed) {
  586. rgb_matrix_set_speed_eeprom_helper(speed, true);
  587. }
  588. uint8_t rgb_matrix_get_speed(void) {
  589. return rgb_matrix_config.speed;
  590. }
  591. void rgb_matrix_increase_speed_helper(bool write_to_eeprom) {
  592. rgb_matrix_set_speed_eeprom_helper(qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom);
  593. }
  594. void rgb_matrix_increase_speed_noeeprom(void) {
  595. rgb_matrix_increase_speed_helper(false);
  596. }
  597. void rgb_matrix_increase_speed(void) {
  598. rgb_matrix_increase_speed_helper(true);
  599. }
  600. void rgb_matrix_decrease_speed_helper(bool write_to_eeprom) {
  601. rgb_matrix_set_speed_eeprom_helper(qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom);
  602. }
  603. void rgb_matrix_decrease_speed_noeeprom(void) {
  604. rgb_matrix_decrease_speed_helper(false);
  605. }
  606. void rgb_matrix_decrease_speed(void) {
  607. rgb_matrix_decrease_speed_helper(true);
  608. }
  609. void rgb_matrix_set_flags_eeprom_helper(led_flags_t flags, bool write_to_eeprom) {
  610. rgb_matrix_config.flags = flags;
  611. eeconfig_flag_rgb_matrix(write_to_eeprom);
  612. dprintf("rgb matrix set flags [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.flags);
  613. }
  614. led_flags_t rgb_matrix_get_flags(void) {
  615. return rgb_matrix_config.flags;
  616. }
  617. void rgb_matrix_set_flags(led_flags_t flags) {
  618. rgb_matrix_set_flags_eeprom_helper(flags, true);
  619. }
  620. void rgb_matrix_set_flags_noeeprom(led_flags_t flags) {
  621. rgb_matrix_set_flags_eeprom_helper(flags, false);
  622. }