rgb_matrix.c 29 KB

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