logo

qmk_firmware

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

rgb_matrix.c (25129B)


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