rgb_matrix.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 <math.h>
  23. rgb_config_t rgb_matrix_config;
  24. #ifndef MAX
  25. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  26. #endif
  27. #ifndef MIN
  28. #define MIN(a,b) ((a) < (b)? (a): (b))
  29. #endif
  30. #ifndef RGB_DISABLE_AFTER_TIMEOUT
  31. #define RGB_DISABLE_AFTER_TIMEOUT 0
  32. #endif
  33. #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
  34. #define RGB_DISABLE_WHEN_USB_SUSPENDED false
  35. #endif
  36. #ifndef EECONFIG_RGB_MATRIX
  37. #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
  38. #endif
  39. #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > 255
  40. #define RGB_MATRIX_MAXIMUM_BRIGHTNESS 255
  41. #endif
  42. bool g_suspend_state = false;
  43. // Global tick at 20 Hz
  44. uint32_t g_tick = 0;
  45. // Ticks since this key was last hit.
  46. uint8_t g_key_hit[DRIVER_LED_TOTAL];
  47. // Ticks since any key was last hit.
  48. uint32_t g_any_key_hit = 0;
  49. #ifndef PI
  50. #define PI 3.14159265
  51. #endif
  52. uint32_t eeconfig_read_rgb_matrix(void) {
  53. return eeprom_read_dword(EECONFIG_RGB_MATRIX);
  54. }
  55. void eeconfig_update_rgb_matrix(uint32_t val) {
  56. eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
  57. }
  58. void eeconfig_update_rgb_matrix_default(void) {
  59. dprintf("eeconfig_update_rgb_matrix_default\n");
  60. rgb_matrix_config.enable = 1;
  61. rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT;
  62. rgb_matrix_config.hue = 0;
  63. rgb_matrix_config.sat = 255;
  64. rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  65. rgb_matrix_config.speed = 1;
  66. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  67. }
  68. void eeconfig_debug_rgb_matrix(void) {
  69. dprintf("rgb_matrix_config eprom\n");
  70. dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
  71. dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
  72. dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
  73. dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
  74. dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
  75. dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
  76. }
  77. // Last led hit
  78. #define LED_HITS_TO_REMEMBER 8
  79. uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
  80. uint8_t g_last_led_count = 0;
  81. void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
  82. rgb_led led;
  83. *led_count = 0;
  84. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  85. // map_index_to_led(i, &led);
  86. led = g_rgb_leds[i];
  87. if (row == led.matrix_co.row && column == led.matrix_co.col) {
  88. led_i[*led_count] = i;
  89. (*led_count)++;
  90. }
  91. }
  92. }
  93. void rgb_matrix_update_pwm_buffers(void) {
  94. #ifdef IS31FL3731
  95. IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  96. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  97. #elif defined(IS31FL3733)
  98. IS31FL3733_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  99. IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  100. #elif defined(WS2812)
  101. WS2812_send_colors();
  102. #endif
  103. }
  104. void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
  105. #ifdef IS31FL3731
  106. IS31FL3731_set_color( index, red, green, blue );
  107. #elif defined(IS31FL3733)
  108. IS31FL3733_set_color( index, red, green, blue );
  109. #elif defined(WS2812)
  110. WS2812_set_color( index, red, green, blue );
  111. #endif
  112. }
  113. void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
  114. #ifdef IS31FL3731
  115. IS31FL3731_set_color_all( red, green, blue );
  116. #elif defined(IS31FL3733)
  117. IS31FL3733_set_color_all( red, green, blue );
  118. #elif defined(WS2812)
  119. WS2812_set_color_all( red, green, blue );
  120. #endif
  121. }
  122. bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
  123. if ( record->event.pressed ) {
  124. uint8_t led[8], led_count;
  125. map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
  126. if (led_count > 0) {
  127. for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
  128. g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
  129. }
  130. g_last_led_hit[0] = led[0];
  131. g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
  132. }
  133. for(uint8_t i = 0; i < led_count; i++)
  134. g_key_hit[led[i]] = 0;
  135. g_any_key_hit = 0;
  136. } else {
  137. #ifdef RGB_MATRIX_KEYRELEASES
  138. uint8_t led[8], led_count;
  139. map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
  140. for(uint8_t i = 0; i < led_count; i++)
  141. g_key_hit[led[i]] = 255;
  142. g_any_key_hit = 255;
  143. #endif
  144. }
  145. return true;
  146. }
  147. void rgb_matrix_set_suspend_state(bool state) {
  148. g_suspend_state = state;
  149. }
  150. void rgb_matrix_test(void) {
  151. // Mask out bits 4 and 5
  152. // Increase the factor to make the test animation slower (and reduce to make it faster)
  153. uint8_t factor = 10;
  154. switch ( (g_tick & (0b11 << factor)) >> factor )
  155. {
  156. case 0:
  157. {
  158. rgb_matrix_set_color_all( 20, 0, 0 );
  159. break;
  160. }
  161. case 1:
  162. {
  163. rgb_matrix_set_color_all( 0, 20, 0 );
  164. break;
  165. }
  166. case 2:
  167. {
  168. rgb_matrix_set_color_all( 0, 0, 20 );
  169. break;
  170. }
  171. case 3:
  172. {
  173. rgb_matrix_set_color_all( 20, 20, 20 );
  174. break;
  175. }
  176. }
  177. }
  178. // This tests the LEDs
  179. // Note that it will change the LED control registers
  180. // in the LED drivers, and leave them in an invalid
  181. // state for other backlight effects.
  182. // ONLY USE THIS FOR TESTING LEDS!
  183. void rgb_matrix_single_LED_test(void) {
  184. static uint8_t color = 0; // 0,1,2 for R,G,B
  185. static uint8_t row = 0;
  186. static uint8_t column = 0;
  187. static uint8_t tick = 0;
  188. tick++;
  189. if ( tick > 2 )
  190. {
  191. tick = 0;
  192. column++;
  193. }
  194. if ( column > MATRIX_COLS )
  195. {
  196. column = 0;
  197. row++;
  198. }
  199. if ( row > MATRIX_ROWS )
  200. {
  201. row = 0;
  202. color++;
  203. }
  204. if ( color > 2 )
  205. {
  206. color = 0;
  207. }
  208. uint8_t led[8], led_count;
  209. map_row_column_to_led(row,column,led,&led_count);
  210. for(uint8_t i = 0; i < led_count; i++) {
  211. rgb_matrix_set_color_all( 40, 40, 40 );
  212. rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
  213. }
  214. }
  215. // All LEDs off
  216. void rgb_matrix_all_off(void) {
  217. rgb_matrix_set_color_all( 0, 0, 0 );
  218. }
  219. // Solid color
  220. void rgb_matrix_solid_color(void) {
  221. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  222. RGB rgb = hsv_to_rgb( hsv );
  223. rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
  224. }
  225. void rgb_matrix_solid_reactive(void) {
  226. // Relies on hue being 8-bit and wrapping
  227. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  228. {
  229. uint16_t offset2 = g_key_hit[i]<<2;
  230. offset2 = (offset2<=130) ? (130-offset2) : 0;
  231. HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
  232. RGB rgb = hsv_to_rgb( hsv );
  233. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  234. }
  235. }
  236. // alphas = color1, mods = color2
  237. void rgb_matrix_alphas_mods(void) {
  238. RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
  239. RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
  240. rgb_led led;
  241. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  242. led = g_rgb_leds[i];
  243. if ( led.matrix_co.raw < 0xFF ) {
  244. if ( led.modifier )
  245. {
  246. rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
  247. }
  248. else
  249. {
  250. rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
  251. }
  252. }
  253. }
  254. }
  255. void rgb_matrix_gradient_up_down(void) {
  256. int16_t h1 = rgb_matrix_config.hue;
  257. int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
  258. int16_t deltaH = h2 - h1;
  259. // Take the shortest path between hues
  260. if ( deltaH > 127 )
  261. {
  262. deltaH -= 256;
  263. }
  264. else if ( deltaH < -127 )
  265. {
  266. deltaH += 256;
  267. }
  268. // Divide delta by 4, this gives the delta per row
  269. deltaH /= 4;
  270. int16_t s1 = rgb_matrix_config.sat;
  271. int16_t s2 = rgb_matrix_config.hue;
  272. int16_t deltaS = ( s2 - s1 ) / 4;
  273. HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
  274. RGB rgb;
  275. Point point;
  276. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  277. {
  278. // map_led_to_point( i, &point );
  279. point = g_rgb_leds[i].point;
  280. // The y range will be 0..64, map this to 0..4
  281. uint8_t y = (point.y>>4);
  282. // Relies on hue being 8-bit and wrapping
  283. hsv.h = rgb_matrix_config.hue + ( deltaH * y );
  284. hsv.s = rgb_matrix_config.sat + ( deltaS * y );
  285. rgb = hsv_to_rgb( hsv );
  286. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  287. }
  288. }
  289. void rgb_matrix_raindrops(bool initialize) {
  290. int16_t h1 = rgb_matrix_config.hue;
  291. int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
  292. int16_t deltaH = h2 - h1;
  293. deltaH /= 4;
  294. // Take the shortest path between hues
  295. if ( deltaH > 127 )
  296. {
  297. deltaH -= 256;
  298. }
  299. else if ( deltaH < -127 )
  300. {
  301. deltaH += 256;
  302. }
  303. int16_t s1 = rgb_matrix_config.sat;
  304. int16_t s2 = rgb_matrix_config.sat;
  305. int16_t deltaS = ( s2 - s1 ) / 4;
  306. HSV hsv;
  307. RGB rgb;
  308. // Change one LED every tick, make sure speed is not 0
  309. uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
  310. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  311. {
  312. // If initialize, all get set to random colors
  313. // If not, all but one will stay the same as before.
  314. if ( initialize || i == led_to_change )
  315. {
  316. hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
  317. hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
  318. // Override brightness with global brightness control
  319. hsv.v = rgb_matrix_config.val;
  320. rgb = hsv_to_rgb( hsv );
  321. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  322. }
  323. }
  324. }
  325. void rgb_matrix_cycle_all(void) {
  326. uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
  327. rgb_led led;
  328. // Relies on hue being 8-bit and wrapping
  329. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  330. {
  331. // map_index_to_led(i, &led);
  332. led = g_rgb_leds[i];
  333. if (led.matrix_co.raw < 0xFF) {
  334. uint16_t offset2 = g_key_hit[i]<<2;
  335. offset2 = (offset2<=63) ? (63-offset2) : 0;
  336. HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
  337. RGB rgb = hsv_to_rgb( hsv );
  338. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  339. }
  340. }
  341. }
  342. void rgb_matrix_cycle_left_right(void) {
  343. uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
  344. HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
  345. RGB rgb;
  346. Point point;
  347. rgb_led led;
  348. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  349. {
  350. // map_index_to_led(i, &led);
  351. led = g_rgb_leds[i];
  352. if (led.matrix_co.raw < 0xFF) {
  353. uint16_t offset2 = g_key_hit[i]<<2;
  354. offset2 = (offset2<=63) ? (63-offset2) : 0;
  355. // map_led_to_point( i, &point );
  356. point = g_rgb_leds[i].point;
  357. // Relies on hue being 8-bit and wrapping
  358. hsv.h = point.x + offset + offset2;
  359. rgb = hsv_to_rgb( hsv );
  360. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  361. }
  362. }
  363. }
  364. void rgb_matrix_cycle_up_down(void) {
  365. uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
  366. HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
  367. RGB rgb;
  368. Point point;
  369. rgb_led led;
  370. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  371. {
  372. // map_index_to_led(i, &led);
  373. led = g_rgb_leds[i];
  374. if (led.matrix_co.raw < 0xFF) {
  375. uint16_t offset2 = g_key_hit[i]<<2;
  376. offset2 = (offset2<=63) ? (63-offset2) : 0;
  377. // map_led_to_point( i, &point );
  378. point = g_rgb_leds[i].point;
  379. // Relies on hue being 8-bit and wrapping
  380. hsv.h = point.y + offset + offset2;
  381. rgb = hsv_to_rgb( hsv );
  382. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  383. }
  384. }
  385. }
  386. void rgb_matrix_dual_beacon(void) {
  387. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  388. RGB rgb;
  389. rgb_led led;
  390. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  391. led = g_rgb_leds[i];
  392. hsv.h = ((led.point.y - 32.0)* cos(g_tick * PI / 128) / 32 + (led.point.x - 112.0) * sin(g_tick * PI / 128) / (112)) * (180) + rgb_matrix_config.hue;
  393. rgb = hsv_to_rgb( hsv );
  394. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  395. }
  396. }
  397. void rgb_matrix_rainbow_beacon(void) {
  398. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  399. RGB rgb;
  400. rgb_led led;
  401. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  402. led = g_rgb_leds[i];
  403. hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - 112.0) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
  404. rgb = hsv_to_rgb( hsv );
  405. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  406. }
  407. }
  408. void rgb_matrix_rainbow_pinwheels(void) {
  409. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  410. RGB rgb;
  411. rgb_led led;
  412. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  413. led = g_rgb_leds[i];
  414. hsv.h = (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (66 - abs(led.point.x - 112.0)) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
  415. rgb = hsv_to_rgb( hsv );
  416. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  417. }
  418. }
  419. void rgb_matrix_rainbow_moving_chevron(void) {
  420. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  421. RGB rgb;
  422. rgb_led led;
  423. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  424. led = g_rgb_leds[i];
  425. // uint8_t r = g_tick;
  426. uint8_t r = 128;
  427. hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * abs(led.point.y - 32.0)* sin(r * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - (g_tick / 256.0 * 224)) * cos(r * PI / 128) + rgb_matrix_config.hue;
  428. rgb = hsv_to_rgb( hsv );
  429. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  430. }
  431. }
  432. void rgb_matrix_jellybean_raindrops( bool initialize ) {
  433. HSV hsv;
  434. RGB rgb;
  435. // Change one LED every tick, make sure speed is not 0
  436. uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
  437. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  438. {
  439. // If initialize, all get set to random colors
  440. // If not, all but one will stay the same as before.
  441. if ( initialize || i == led_to_change )
  442. {
  443. hsv.h = rand() & 0xFF;
  444. hsv.s = rand() & 0xFF;
  445. // Override brightness with global brightness control
  446. hsv.v = rgb_matrix_config.val;
  447. rgb = hsv_to_rgb( hsv );
  448. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  449. }
  450. }
  451. }
  452. void rgb_matrix_multisplash(void) {
  453. // if (g_any_key_hit < 0xFF) {
  454. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  455. RGB rgb;
  456. rgb_led led;
  457. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  458. led = g_rgb_leds[i];
  459. uint16_t c = 0, d = 0;
  460. rgb_led last_led;
  461. // if (g_last_led_count) {
  462. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  463. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  464. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  465. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  466. c += MIN(MAX(effect, 0), 255);
  467. d += 255 - MIN(MAX(effect, 0), 255);
  468. }
  469. // } else {
  470. // d = 255;
  471. // }
  472. hsv.h = (rgb_matrix_config.hue + c) % 256;
  473. hsv.v = MAX(MIN(d, 255), 0);
  474. rgb = hsv_to_rgb( hsv );
  475. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  476. }
  477. // } else {
  478. // rgb_matrix_set_color_all( 0, 0, 0 );
  479. // }
  480. }
  481. void rgb_matrix_splash(void) {
  482. g_last_led_count = MIN(g_last_led_count, 1);
  483. rgb_matrix_multisplash();
  484. }
  485. void rgb_matrix_solid_multisplash(void) {
  486. // if (g_any_key_hit < 0xFF) {
  487. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  488. RGB rgb;
  489. rgb_led led;
  490. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  491. led = g_rgb_leds[i];
  492. uint16_t d = 0;
  493. rgb_led last_led;
  494. // if (g_last_led_count) {
  495. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  496. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  497. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  498. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  499. d += 255 - MIN(MAX(effect, 0), 255);
  500. }
  501. // } else {
  502. // d = 255;
  503. // }
  504. hsv.v = MAX(MIN(d, 255), 0);
  505. rgb = hsv_to_rgb( hsv );
  506. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  507. }
  508. // } else {
  509. // rgb_matrix_set_color_all( 0, 0, 0 );
  510. // }
  511. }
  512. void rgb_matrix_solid_splash(void) {
  513. g_last_led_count = MIN(g_last_led_count, 1);
  514. rgb_matrix_solid_multisplash();
  515. }
  516. // Needs eeprom access that we don't have setup currently
  517. void rgb_matrix_custom(void) {
  518. // HSV hsv;
  519. // RGB rgb;
  520. // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  521. // {
  522. // backlight_get_key_color(i, &hsv);
  523. // // Override brightness with global brightness control
  524. // hsv.v = rgb_matrix_config.val;
  525. // rgb = hsv_to_rgb( hsv );
  526. // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  527. // }
  528. }
  529. static uint8_t tick_slower = 5;
  530. static uint8_t tick_counter = 0;
  531. void rgb_matrix_task(void) {
  532. static uint8_t toggle_enable_last = 255;
  533. if (!rgb_matrix_config.enable) {
  534. rgb_matrix_all_off();
  535. toggle_enable_last = rgb_matrix_config.enable;
  536. return;
  537. }
  538. // delay 1 second before driving LEDs or doing anything else
  539. static uint8_t startup_tick = 0;
  540. if ( startup_tick < 20 ) {
  541. startup_tick++;
  542. return;
  543. }
  544. if (tick_counter == 0) {
  545. g_tick++;
  546. }
  547. tick_counter = ( tick_counter + 1) % tick_slower;
  548. if ( g_any_key_hit < 0xFFFFFFFF ) {
  549. g_any_key_hit++;
  550. }
  551. for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
  552. if ( g_key_hit[led] < 255 ) {
  553. if (g_key_hit[led] == 254)
  554. g_last_led_count = MAX(g_last_led_count - 1, 0);
  555. g_key_hit[led]++;
  556. }
  557. }
  558. // Factory default magic value
  559. if ( rgb_matrix_config.mode == 255 ) {
  560. rgb_matrix_test();
  561. return;
  562. }
  563. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  564. // while suspended and just do a software shutdown. This is a cheap hack for now.
  565. bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
  566. (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
  567. uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
  568. // Keep track of the effect used last time,
  569. // detect change in effect, so each effect can
  570. // have an optional initialization.
  571. static uint8_t effect_last = 255;
  572. bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
  573. effect_last = effect;
  574. toggle_enable_last = rgb_matrix_config.enable;
  575. // this gets ticked at 20 Hz.
  576. // each effect can opt to do calculations
  577. // and/or request PWM buffer updates.
  578. switch ( effect ) {
  579. case RGB_MATRIX_SOLID_COLOR:
  580. rgb_matrix_solid_color();
  581. break;
  582. case RGB_MATRIX_ALPHAS_MODS:
  583. rgb_matrix_alphas_mods();
  584. break;
  585. case RGB_MATRIX_DUAL_BEACON:
  586. rgb_matrix_dual_beacon();
  587. break;
  588. case RGB_MATRIX_GRADIENT_UP_DOWN:
  589. rgb_matrix_gradient_up_down();
  590. break;
  591. case RGB_MATRIX_RAINDROPS:
  592. rgb_matrix_raindrops( initialize );
  593. break;
  594. case RGB_MATRIX_CYCLE_ALL:
  595. rgb_matrix_cycle_all();
  596. break;
  597. case RGB_MATRIX_CYCLE_LEFT_RIGHT:
  598. rgb_matrix_cycle_left_right();
  599. break;
  600. case RGB_MATRIX_CYCLE_UP_DOWN:
  601. rgb_matrix_cycle_up_down();
  602. break;
  603. case RGB_MATRIX_RAINBOW_BEACON:
  604. rgb_matrix_rainbow_beacon();
  605. break;
  606. case RGB_MATRIX_RAINBOW_PINWHEELS:
  607. rgb_matrix_rainbow_pinwheels();
  608. break;
  609. case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
  610. rgb_matrix_rainbow_moving_chevron();
  611. break;
  612. case RGB_MATRIX_JELLYBEAN_RAINDROPS:
  613. rgb_matrix_jellybean_raindrops( initialize );
  614. break;
  615. #ifdef RGB_MATRIX_KEYPRESSES
  616. case RGB_MATRIX_SOLID_REACTIVE:
  617. rgb_matrix_solid_reactive();
  618. break;
  619. case RGB_MATRIX_SPLASH:
  620. rgb_matrix_splash();
  621. break;
  622. case RGB_MATRIX_MULTISPLASH:
  623. rgb_matrix_multisplash();
  624. break;
  625. case RGB_MATRIX_SOLID_SPLASH:
  626. rgb_matrix_solid_splash();
  627. break;
  628. case RGB_MATRIX_SOLID_MULTISPLASH:
  629. rgb_matrix_solid_multisplash();
  630. break;
  631. #endif
  632. default:
  633. rgb_matrix_custom();
  634. break;
  635. }
  636. if ( ! suspend_backlight ) {
  637. rgb_matrix_indicators();
  638. }
  639. }
  640. void rgb_matrix_indicators(void) {
  641. rgb_matrix_indicators_kb();
  642. rgb_matrix_indicators_user();
  643. }
  644. __attribute__((weak))
  645. void rgb_matrix_indicators_kb(void) {}
  646. __attribute__((weak))
  647. void rgb_matrix_indicators_user(void) {}
  648. // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
  649. // {
  650. // if ( row >= MATRIX_ROWS )
  651. // {
  652. // // Special value, 255=none, 254=all
  653. // *index = row;
  654. // }
  655. // else
  656. // {
  657. // // This needs updated to something like
  658. // // uint8_t led[8], led_count;
  659. // // map_row_column_to_led(row,column,led,&led_count);
  660. // // for(uint8_t i = 0; i < led_count; i++)
  661. // map_row_column_to_led( row, column, index );
  662. // }
  663. // }
  664. void rgb_matrix_init(void) {
  665. rgb_matrix_setup_drivers();
  666. // TODO: put the 1 second startup delay here?
  667. // clear the key hits
  668. for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
  669. g_key_hit[led] = 255;
  670. }
  671. if (!eeconfig_is_enabled()) {
  672. dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
  673. eeconfig_init();
  674. eeconfig_update_rgb_matrix_default();
  675. }
  676. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  677. if (!rgb_matrix_config.mode) {
  678. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  679. eeconfig_update_rgb_matrix_default();
  680. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  681. }
  682. eeconfig_debug_rgb_matrix(); // display current eeprom values
  683. }
  684. void rgb_matrix_setup_drivers(void) {
  685. // Initialize TWI
  686. #ifdef IS31FL3731
  687. i2c_init();
  688. IS31FL3731_init( DRIVER_ADDR_1 );
  689. IS31FL3731_init( DRIVER_ADDR_2 );
  690. #elif defined (IS31FL3733)
  691. i2c_init();
  692. IS31FL3733_init( DRIVER_ADDR_1 );
  693. #elif defined(WS2812)
  694. WS2812_init();
  695. #endif
  696. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
  697. __attribute__((unused))
  698. bool enabled = true;
  699. // This only caches it for later
  700. #ifdef IS31FL3731
  701. IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
  702. #elif defined (IS31FL3733)
  703. IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
  704. #endif
  705. }
  706. // This actually updates the LED drivers
  707. #ifdef IS31FL3731
  708. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  709. #elif defined (IS31FL3733)
  710. IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  711. #endif
  712. }
  713. // Deals with the messy details of incrementing an integer
  714. uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  715. int16_t new_value = value;
  716. new_value += step;
  717. return MIN( MAX( new_value, min ), max );
  718. }
  719. uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  720. int16_t new_value = value;
  721. new_value -= step;
  722. return MIN( MAX( new_value, min ), max );
  723. }
  724. // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
  725. // {
  726. // // 3 bytes per color
  727. // return EECONFIG_RGB_MATRIX + ( led * 3 );
  728. // }
  729. // void backlight_get_key_color( uint8_t led, HSV *hsv )
  730. // {
  731. // void *address = backlight_get_custom_key_color_eeprom_address( led );
  732. // hsv->h = eeprom_read_byte(address);
  733. // hsv->s = eeprom_read_byte(address+1);
  734. // hsv->v = eeprom_read_byte(address+2);
  735. // }
  736. // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
  737. // {
  738. // uint8_t led[8], led_count;
  739. // map_row_column_to_led(row,column,led,&led_count);
  740. // for(uint8_t i = 0; i < led_count; i++) {
  741. // if ( led[i] < DRIVER_LED_TOTAL )
  742. // {
  743. // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
  744. // eeprom_update_byte(address, hsv.h);
  745. // eeprom_update_byte(address+1, hsv.s);
  746. // eeprom_update_byte(address+2, hsv.v);
  747. // }
  748. // }
  749. // }
  750. void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
  751. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  752. {
  753. if ( i == index )
  754. {
  755. #ifdef IS31FL3731
  756. IS31FL3731_set_led_control_register( i, red, green, blue );
  757. #elif defined (IS31FL3733)
  758. IS31FL3733_set_led_control_register( i, red, green, blue );
  759. #endif
  760. }
  761. else
  762. {
  763. #ifdef IS31FL3731
  764. IS31FL3731_set_led_control_register( i, false, false, false );
  765. #elif defined (IS31FL3733)
  766. IS31FL3733_set_led_control_register( i, false, false, false );
  767. #endif
  768. }
  769. }
  770. }
  771. uint32_t rgb_matrix_get_tick(void) {
  772. return g_tick;
  773. }
  774. void rgblight_toggle(void) {
  775. rgb_matrix_config.enable ^= 1;
  776. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  777. }
  778. void rgblight_step(void) {
  779. rgb_matrix_config.mode++;
  780. if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
  781. rgb_matrix_config.mode = 1;
  782. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  783. }
  784. void rgblight_step_reverse(void) {
  785. rgb_matrix_config.mode--;
  786. if (rgb_matrix_config.mode < 1)
  787. rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  788. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  789. }
  790. void rgblight_increase_hue(void) {
  791. rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
  792. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  793. }
  794. void rgblight_decrease_hue(void) {
  795. rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
  796. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  797. }
  798. void rgblight_increase_sat(void) {
  799. rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
  800. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  801. }
  802. void rgblight_decrease_sat(void) {
  803. rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
  804. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  805. }
  806. void rgblight_increase_val(void) {
  807. rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
  808. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  809. }
  810. void rgblight_decrease_val(void) {
  811. rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
  812. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  813. }
  814. void rgblight_increase_speed(void) {
  815. rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
  816. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  817. }
  818. void rgblight_decrease_speed(void) {
  819. rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
  820. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  821. }
  822. void rgblight_mode(uint8_t mode) {
  823. rgb_matrix_config.mode = mode;
  824. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  825. }
  826. uint32_t rgblight_get_mode(void) {
  827. return rgb_matrix_config.mode;
  828. }