rgb_matrix.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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 "config.h"
  21. #include "eeprom.h"
  22. #include <string.h>
  23. #include <math.h>
  24. #include "lib/lib8tion/lib8tion.h"
  25. #include "rgb_matrix_animations/solid_color_anim.h"
  26. #include "rgb_matrix_animations/alpha_mods_anim.h"
  27. #include "rgb_matrix_animations/dual_beacon_anim.h"
  28. #include "rgb_matrix_animations/gradient_up_down_anim.h"
  29. #include "rgb_matrix_animations/raindrops_anim.h"
  30. #include "rgb_matrix_animations/cycle_all_anim.h"
  31. #include "rgb_matrix_animations/cycle_left_right_anim.h"
  32. #include "rgb_matrix_animations/cycle_up_down_anim.h"
  33. #include "rgb_matrix_animations/rainbow_beacon_anim.h"
  34. #include "rgb_matrix_animations/rainbow_pinwheels_anim.h"
  35. #include "rgb_matrix_animations/rainbow_moving_chevron_anim.h"
  36. #include "rgb_matrix_animations/jellybean_raindrops_anim.h"
  37. #include "rgb_matrix_animations/digital_rain_anim.h"
  38. #include "rgb_matrix_animations/solid_reactive_simple_anim.h"
  39. #include "rgb_matrix_animations/solid_reactive_anim.h"
  40. #include "rgb_matrix_animations/solid_reactive_wide.h"
  41. #include "rgb_matrix_animations/solid_reactive_cross.h"
  42. #include "rgb_matrix_animations/solid_reactive_nexus.h"
  43. #include "rgb_matrix_animations/splash_anim.h"
  44. #include "rgb_matrix_animations/solid_splash_anim.h"
  45. #include "rgb_matrix_animations/breathing_anim.h"
  46. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  47. #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  48. #define RGB_MATRIX_EFFECT(name, ...)
  49. #ifdef RGB_MATRIX_CUSTOM_KB
  50. #include "rgb_matrix_kb.inc"
  51. #endif
  52. #ifdef RGB_MATRIX_CUSTOM_USER
  53. #include "rgb_matrix_user.inc"
  54. #endif
  55. #undef RGB_MATRIX_EFFECT
  56. #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
  57. #endif
  58. #ifndef RGB_DISABLE_AFTER_TIMEOUT
  59. #define RGB_DISABLE_AFTER_TIMEOUT 0
  60. #endif
  61. #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
  62. #define RGB_DISABLE_WHEN_USB_SUSPENDED false
  63. #endif
  64. #ifndef EECONFIG_RGB_MATRIX
  65. #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
  66. #endif
  67. #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
  68. #undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
  69. #define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
  70. #endif
  71. #if !defined(RGB_MATRIX_HUE_STEP)
  72. #define RGB_MATRIX_HUE_STEP 8
  73. #endif
  74. #if !defined(RGB_MATRIX_SAT_STEP)
  75. #define RGB_MATRIX_SAT_STEP 16
  76. #endif
  77. #if !defined(RGB_MATRIX_VAL_STEP)
  78. #define RGB_MATRIX_VAL_STEP 16
  79. #endif
  80. #if !defined(RGB_MATRIX_SPD_STEP)
  81. #define RGB_MATRIX_SPD_STEP 16
  82. #endif
  83. #if !defined(RGB_MATRIX_STARTUP_MODE)
  84. #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
  85. #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
  86. #else
  87. // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
  88. #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
  89. #endif
  90. #endif
  91. bool g_suspend_state = false;
  92. rgb_config_t rgb_matrix_config;
  93. rgb_counters_t g_rgb_counters;
  94. static uint32_t rgb_counters_buffer;
  95. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  96. last_hit_t g_last_hit_tracker;
  97. static last_hit_t last_hit_buffer;
  98. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  99. uint32_t eeconfig_read_rgb_matrix(void) {
  100. return eeprom_read_dword(EECONFIG_RGB_MATRIX);
  101. }
  102. void eeconfig_update_rgb_matrix(uint32_t val) {
  103. eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
  104. }
  105. void eeconfig_update_rgb_matrix_default(void) {
  106. dprintf("eeconfig_update_rgb_matrix_default\n");
  107. rgb_matrix_config.enable = 1;
  108. rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
  109. rgb_matrix_config.hue = 0;
  110. rgb_matrix_config.sat = UINT8_MAX;
  111. rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  112. rgb_matrix_config.speed = UINT8_MAX / 2;
  113. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  114. }
  115. void eeconfig_debug_rgb_matrix(void) {
  116. dprintf("rgb_matrix_config eprom\n");
  117. dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
  118. dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
  119. dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
  120. dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
  121. dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
  122. dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
  123. }
  124. uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
  125. // TODO: This is kinda expensive, fix this soonish
  126. uint8_t led_count = 0;
  127. for (uint8_t i = 0; i < DRIVER_LED_TOTAL && led_count < LED_HITS_TO_REMEMBER; i++) {
  128. matrix_co_t matrix_co = g_rgb_leds[i].matrix_co;
  129. if (row == matrix_co.row && column == matrix_co.col) {
  130. led_i[led_count] = i;
  131. led_count++;
  132. }
  133. }
  134. return led_count;
  135. }
  136. void rgb_matrix_update_pwm_buffers(void) {
  137. rgb_matrix_driver.flush();
  138. }
  139. void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
  140. #ifdef RGB_MATRIX_EXTRA_TOG
  141. const bool is_key = g_rgb_leds[index].matrix_co.raw != 0xff;
  142. if (
  143. (rgb_matrix_config.enable == RGB_ZONE_KEYS && !is_key) ||
  144. (rgb_matrix_config.enable == RGB_ZONE_UNDER && is_key)
  145. ) {
  146. rgb_matrix_driver.set_color(index, 0, 0, 0);
  147. return;
  148. }
  149. #endif
  150. rgb_matrix_driver.set_color(index, red, green, blue);
  151. }
  152. void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
  153. #ifdef RGB_MATRIX_EXTRA_TOG
  154. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  155. rgb_matrix_set_color(i, red, green, blue);
  156. }
  157. #else
  158. rgb_matrix_driver.set_color_all(red, green, blue);
  159. #endif
  160. }
  161. bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
  162. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  163. uint8_t led[LED_HITS_TO_REMEMBER];
  164. uint8_t led_count = 0;
  165. #if defined(RGB_MATRIX_KEYRELEASES)
  166. if (!record->event.pressed) {
  167. led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  168. g_rgb_counters.any_key_hit = 0;
  169. }
  170. #elif defined(RGB_MATRIX_KEYPRESSES)
  171. if (record->event.pressed) {
  172. led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
  173. g_rgb_counters.any_key_hit = 0;
  174. }
  175. #endif // defined(RGB_MATRIX_KEYRELEASES)
  176. if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
  177. memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
  178. memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
  179. memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
  180. memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
  181. last_hit_buffer.count--;
  182. }
  183. for(uint8_t i = 0; i < led_count; i++) {
  184. uint8_t index = last_hit_buffer.count;
  185. last_hit_buffer.x[index] = g_rgb_leds[led[i]].point.x;
  186. last_hit_buffer.y[index] = g_rgb_leds[led[i]].point.y;
  187. last_hit_buffer.index[index] = led[i];
  188. last_hit_buffer.tick[index] = 0;
  189. last_hit_buffer.count++;
  190. }
  191. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  192. return true;
  193. }
  194. void rgb_matrix_test(void) {
  195. // Mask out bits 4 and 5
  196. // Increase the factor to make the test animation slower (and reduce to make it faster)
  197. uint8_t factor = 10;
  198. switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
  199. {
  200. case 0: {
  201. rgb_matrix_set_color_all( 20, 0, 0 );
  202. break;
  203. }
  204. case 1: {
  205. rgb_matrix_set_color_all( 0, 20, 0 );
  206. break;
  207. }
  208. case 2: {
  209. rgb_matrix_set_color_all( 0, 0, 20 );
  210. break;
  211. }
  212. case 3: {
  213. rgb_matrix_set_color_all( 20, 20, 20 );
  214. break;
  215. }
  216. }
  217. }
  218. static bool rgb_matrix_none(effect_params_t* params) {
  219. if (!params->init) {
  220. return false;
  221. }
  222. RGB_MATRIX_USE_LIMITS(led_min, led_max);
  223. for (uint8_t i = led_min; i < led_max; i++) {
  224. rgb_matrix_set_color(i, 0, 0, 0);
  225. }
  226. return led_max < DRIVER_LED_TOTAL;
  227. }
  228. static uint8_t rgb_last_enable = UINT8_MAX;
  229. static uint8_t rgb_last_effect = UINT8_MAX;
  230. static effect_params_t rgb_effect_params = { 0, 0 };
  231. static rgb_task_states rgb_task_state = SYNCING;
  232. static void rgb_task_timers(void) {
  233. // Update double buffer timers
  234. uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
  235. rgb_counters_buffer = timer_read32();
  236. if (g_rgb_counters.any_key_hit < UINT32_MAX) {
  237. if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
  238. g_rgb_counters.any_key_hit = UINT32_MAX;
  239. } else {
  240. g_rgb_counters.any_key_hit += deltaTime;
  241. }
  242. }
  243. // Update double buffer last hit timers
  244. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  245. uint8_t count = last_hit_buffer.count;
  246. for (uint8_t i = 0; i < count; ++i) {
  247. if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
  248. last_hit_buffer.count--;
  249. continue;
  250. }
  251. last_hit_buffer.tick[i] += deltaTime;
  252. }
  253. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  254. }
  255. static void rgb_task_sync(void) {
  256. // next task
  257. if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
  258. rgb_task_state = STARTING;
  259. }
  260. static void rgb_task_start(void) {
  261. // reset iter
  262. rgb_effect_params.iter = 0;
  263. // update double buffers
  264. g_rgb_counters.tick = rgb_counters_buffer;
  265. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  266. g_last_hit_tracker = last_hit_buffer;
  267. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  268. // next task
  269. rgb_task_state = RENDERING;
  270. }
  271. static void rgb_task_render(uint8_t effect) {
  272. bool rendering = false;
  273. rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
  274. // each effect can opt to do calculations
  275. // and/or request PWM buffer updates.
  276. switch (effect) {
  277. case RGB_MATRIX_NONE:
  278. rendering = rgb_matrix_none(&rgb_effect_params);
  279. break;
  280. case RGB_MATRIX_SOLID_COLOR:
  281. rendering = rgb_matrix_solid_color(&rgb_effect_params); // Max 1ms Avg 0ms
  282. break;
  283. #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS
  284. case RGB_MATRIX_ALPHAS_MODS:
  285. rendering = rgb_matrix_alphas_mods(&rgb_effect_params); // Max 2ms Avg 1ms
  286. break;
  287. #endif // DISABLE_RGB_MATRIX_ALPHAS_MODS
  288. #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
  289. case RGB_MATRIX_GRADIENT_UP_DOWN:
  290. rendering = rgb_matrix_gradient_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
  291. break;
  292. #endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
  293. #ifndef DISABLE_RGB_MATRIX_BREATHING
  294. case RGB_MATRIX_BREATHING:
  295. rendering = rgb_matrix_breathing(&rgb_effect_params); // Max 1ms Avg 0ms
  296. break;
  297. #endif // DISABLE_RGB_MATRIX_BREATHING
  298. #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
  299. case RGB_MATRIX_CYCLE_ALL:
  300. rendering = rgb_matrix_cycle_all(&rgb_effect_params); // Max 4ms Avg 3ms
  301. break;
  302. #endif // DISABLE_RGB_MATRIX_CYCLE_ALL
  303. #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
  304. case RGB_MATRIX_CYCLE_LEFT_RIGHT:
  305. rendering = rgb_matrix_cycle_left_right(&rgb_effect_params); // Max 4ms Avg 3ms
  306. break;
  307. #endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
  308. #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
  309. case RGB_MATRIX_CYCLE_UP_DOWN:
  310. rendering = rgb_matrix_cycle_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
  311. break;
  312. #endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
  313. #ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
  314. case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
  315. rendering = rgb_matrix_rainbow_moving_chevron(&rgb_effect_params); // Max 4ms Avg 3ms
  316. break;
  317. #endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
  318. #ifndef DISABLE_RGB_MATRIX_DUAL_BEACON
  319. case RGB_MATRIX_DUAL_BEACON:
  320. rendering = rgb_matrix_dual_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
  321. break;
  322. #endif // DISABLE_RGB_MATRIX_DUAL_BEACON
  323. #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON
  324. case RGB_MATRIX_RAINBOW_BEACON:
  325. rendering = rgb_matrix_rainbow_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
  326. break;
  327. #endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON
  328. #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
  329. case RGB_MATRIX_RAINBOW_PINWHEELS:
  330. rendering = rgb_matrix_rainbow_pinwheels(&rgb_effect_params); // Max 4ms Avg 3ms
  331. break;
  332. #endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
  333. #ifndef DISABLE_RGB_MATRIX_RAINDROPS
  334. case RGB_MATRIX_RAINDROPS:
  335. rendering = rgb_matrix_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
  336. break;
  337. #endif // DISABLE_RGB_MATRIX_RAINDROPS
  338. #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
  339. case RGB_MATRIX_JELLYBEAN_RAINDROPS:
  340. rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
  341. break;
  342. #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
  343. #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN
  344. case RGB_MATRIX_DIGITAL_RAIN:
  345. rendering = rgb_matrix_digital_rain(&rgb_effect_params); // Max 9ms Avg 8ms | this is expensive, fix it
  346. break;
  347. #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN
  348. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  349. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE
  350. case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
  351. rendering = rgb_matrix_solid_reactive_simple(&rgb_effect_params);// Max 4ms Avg 3ms
  352. break;
  353. #endif
  354. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE
  355. case RGB_MATRIX_SOLID_REACTIVE:
  356. rendering = rgb_matrix_solid_reactive(&rgb_effect_params); // Max 4ms Avg 3ms
  357. break;
  358. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE
  359. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
  360. case RGB_MATRIX_SOLID_REACTIVE_WIDE:
  361. rendering = rgb_matrix_solid_reactive_wide(&rgb_effect_params); // Max ?? ms Avg ?? ms
  362. break;
  363. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
  364. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
  365. case RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE:
  366. rendering = rgb_matrix_solid_reactive_multiwide(&rgb_effect_params); // Max ?? ms Avg ?? ms
  367. break;
  368. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
  369. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
  370. case RGB_MATRIX_SOLID_REACTIVE_CROSS:
  371. rendering = rgb_matrix_solid_reactive_cross(&rgb_effect_params); // Max ?? ms Avg ?? ms
  372. break;
  373. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
  374. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
  375. case RGB_MATRIX_SOLID_REACTIVE_MULTICROSS:
  376. rendering = rgb_matrix_solid_reactive_multicross(&rgb_effect_params); // Max ?? ms Avg ?? ms
  377. break;
  378. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
  379. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
  380. case RGB_MATRIX_SOLID_REACTIVE_NEXUS:
  381. rendering = rgb_matrix_solid_reactive_nexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
  382. break;
  383. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
  384. #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
  385. case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
  386. rendering = rgb_matrix_solid_reactive_multinexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
  387. break;
  388. #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
  389. #ifndef DISABLE_RGB_MATRIX_SPLASH
  390. case RGB_MATRIX_SPLASH:
  391. rendering = rgb_matrix_splash(&rgb_effect_params); // Max 5ms Avg 3ms
  392. break;
  393. #endif // DISABLE_RGB_MATRIX_SPLASH
  394. #ifndef DISABLE_RGB_MATRIX_MULTISPLASH
  395. case RGB_MATRIX_MULTISPLASH:
  396. rendering = rgb_matrix_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
  397. break;
  398. #endif // DISABLE_RGB_MATRIX_MULTISPLASH
  399. #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH
  400. case RGB_MATRIX_SOLID_SPLASH:
  401. rendering = rgb_matrix_solid_splash(&rgb_effect_params); // Max 5ms Avg 3ms
  402. break;
  403. #endif // DISABLE_RGB_MATRIX_SOLID_SPLASH
  404. #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
  405. case RGB_MATRIX_SOLID_MULTISPLASH:
  406. rendering = rgb_matrix_solid_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
  407. break;
  408. #endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
  409. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  410. #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
  411. #define RGB_MATRIX_EFFECT(name, ...) \
  412. case RGB_MATRIX_CUSTOM_##name: \
  413. rendering = name(&rgb_effect_params); \
  414. break;
  415. #ifdef RGB_MATRIX_CUSTOM_KB
  416. #include "rgb_matrix_kb.inc"
  417. #endif
  418. #ifdef RGB_MATRIX_CUSTOM_USER
  419. #include "rgb_matrix_user.inc"
  420. #endif
  421. #undef RGB_MATRIX_EFFECT
  422. #endif
  423. // Factory default magic value
  424. case UINT8_MAX: {
  425. rgb_matrix_test();
  426. rgb_task_state = FLUSHING;
  427. }
  428. return;
  429. }
  430. rgb_effect_params.iter++;
  431. // next task
  432. if (!rendering) {
  433. rgb_task_state = FLUSHING;
  434. if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
  435. // We only need to flush once if we are RGB_MATRIX_NONE
  436. rgb_task_state = SYNCING;
  437. }
  438. }
  439. }
  440. static void rgb_task_flush(uint8_t effect) {
  441. // update last trackers after the first full render so we can init over several frames
  442. rgb_last_effect = effect;
  443. rgb_last_enable = rgb_matrix_config.enable;
  444. // update pwm buffers
  445. rgb_matrix_update_pwm_buffers();
  446. // next task
  447. rgb_task_state = SYNCING;
  448. }
  449. void rgb_matrix_task(void) {
  450. rgb_task_timers();
  451. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  452. // while suspended and just do a software shutdown. This is a cheap hack for now.
  453. bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_rgb_counters.any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
  454. uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
  455. switch (rgb_task_state) {
  456. case STARTING:
  457. rgb_task_start();
  458. break;
  459. case RENDERING:
  460. rgb_task_render(effect);
  461. break;
  462. case FLUSHING:
  463. rgb_task_flush(effect);
  464. break;
  465. case SYNCING:
  466. rgb_task_sync();
  467. break;
  468. }
  469. if (!suspend_backlight) {
  470. rgb_matrix_indicators();
  471. }
  472. }
  473. void rgb_matrix_indicators(void) {
  474. rgb_matrix_indicators_kb();
  475. rgb_matrix_indicators_user();
  476. }
  477. __attribute__((weak))
  478. void rgb_matrix_indicators_kb(void) {}
  479. __attribute__((weak))
  480. void rgb_matrix_indicators_user(void) {}
  481. void rgb_matrix_init(void) {
  482. rgb_matrix_driver.init();
  483. // TODO: put the 1 second startup delay here?
  484. #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
  485. g_last_hit_tracker.count = 0;
  486. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  487. g_last_hit_tracker.tick[i] = UINT16_MAX;
  488. }
  489. last_hit_buffer.count = 0;
  490. for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
  491. last_hit_buffer.tick[i] = UINT16_MAX;
  492. }
  493. #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
  494. if (!eeconfig_is_enabled()) {
  495. dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
  496. eeconfig_init();
  497. eeconfig_update_rgb_matrix_default();
  498. }
  499. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  500. rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
  501. if (!rgb_matrix_config.mode) {
  502. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  503. eeconfig_update_rgb_matrix_default();
  504. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  505. }
  506. eeconfig_debug_rgb_matrix(); // display current eeprom values
  507. }
  508. void rgb_matrix_set_suspend_state(bool state) {
  509. g_suspend_state = state;
  510. }
  511. void rgb_matrix_toggle(void) {
  512. rgb_matrix_config.enable++;
  513. if (!rgb_matrix_config.enable) {
  514. rgb_task_state = STARTING;
  515. }
  516. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  517. }
  518. void rgb_matrix_enable(void) {
  519. rgb_matrix_config.enable = 1;
  520. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  521. }
  522. void rgb_matrix_enable_noeeprom(void) {
  523. rgb_matrix_config.enable = 1;
  524. }
  525. void rgb_matrix_disable(void) {
  526. rgb_matrix_config.enable = 0;
  527. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  528. }
  529. void rgb_matrix_disable_noeeprom(void) {
  530. rgb_matrix_config.enable = 0;
  531. }
  532. void rgb_matrix_step(void) {
  533. rgb_matrix_config.mode++;
  534. if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
  535. rgb_matrix_config.mode = 1;
  536. rgb_task_state = STARTING;
  537. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  538. }
  539. void rgb_matrix_step_reverse(void) {
  540. rgb_matrix_config.mode--;
  541. if (rgb_matrix_config.mode < 1)
  542. rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  543. rgb_task_state = STARTING;
  544. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  545. }
  546. void rgb_matrix_increase_hue(void) {
  547. rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP;
  548. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  549. }
  550. void rgb_matrix_decrease_hue(void) {
  551. rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP;
  552. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  553. }
  554. void rgb_matrix_increase_sat(void) {
  555. rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
  556. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  557. }
  558. void rgb_matrix_decrease_sat(void) {
  559. rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
  560. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  561. }
  562. void rgb_matrix_increase_val(void) {
  563. rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
  564. if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
  565. rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  566. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  567. }
  568. void rgb_matrix_decrease_val(void) {
  569. rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
  570. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  571. }
  572. void rgb_matrix_increase_speed(void) {
  573. rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
  574. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  575. }
  576. void rgb_matrix_decrease_speed(void) {
  577. rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
  578. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  579. }
  580. void rgb_matrix_mode(uint8_t mode) {
  581. rgb_matrix_config.mode = mode;
  582. rgb_task_state = STARTING;
  583. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  584. }
  585. void rgb_matrix_mode_noeeprom(uint8_t mode) {
  586. rgb_matrix_config.mode = mode;
  587. }
  588. uint8_t rgb_matrix_get_mode(void) {
  589. return rgb_matrix_config.mode;
  590. }
  591. void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  592. rgb_matrix_sethsv_noeeprom(hue, sat, val);
  593. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  594. }
  595. void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  596. rgb_matrix_config.hue = hue;
  597. rgb_matrix_config.sat = sat;
  598. rgb_matrix_config.val = val;
  599. if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
  600. rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  601. }