rgb_matrix.c 28 KB

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