rgb_matrix.c 22 KB

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