rgb_matrix.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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_digital_rain( const bool initialize ) {
  448. // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain
  449. const uint8_t drop_ticks = 28;
  450. const uint8_t new_drop_probability = 24;
  451. const uint8_t pure_green_intensity = 0xd0;
  452. const uint8_t max_brightness_boost = 0xc0;
  453. const uint8_t max_intensity = 0xff;
  454. static uint8_t map[MATRIX_COLS][MATRIX_ROWS] = {{0}};
  455. static uint8_t drop = 0;
  456. if (initialize) {
  457. rgb_matrix_set_color_all(0, 0, 0);
  458. memset(map, 0, sizeof map);
  459. drop = 0;
  460. }
  461. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  462. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  463. if (row == 0 && drop == 0 && rand() < RAND_MAX / new_drop_probability) {
  464. // top row, pixels have just fallen and we're
  465. // making a new rain drop in this column
  466. map[col][row] = max_intensity;
  467. }
  468. else if (map[col][row] > 0 && map[col][row] < max_intensity) {
  469. // neither fully bright nor dark, decay it
  470. map[col][row]--;
  471. }
  472. // set the pixel colour
  473. uint8_t led, led_count;
  474. map_row_column_to_led(row, col, &led, &led_count);
  475. if (map[col][row] > pure_green_intensity) {
  476. const uint8_t boost = (uint8_t) ((uint16_t) max_brightness_boost
  477. * (map[col][row] - pure_green_intensity) / (max_intensity - pure_green_intensity));
  478. rgb_matrix_set_color(led, boost, max_intensity, boost);
  479. }
  480. else {
  481. const uint8_t green = (uint8_t) ((uint16_t) max_intensity * map[col][row] / pure_green_intensity);
  482. rgb_matrix_set_color(led, 0, green, 0);
  483. }
  484. }
  485. }
  486. if (++drop > drop_ticks) {
  487. // reset drop timer
  488. drop = 0;
  489. for (uint8_t row = MATRIX_ROWS - 1; row > 0; row--) {
  490. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  491. // if ths is on the bottom row and bright allow decay
  492. if (row == MATRIX_ROWS - 1 && map[col][row] == max_intensity) {
  493. map[col][row]--;
  494. }
  495. // check if the pixel above is bright
  496. if (map[col][row - 1] == max_intensity) {
  497. // allow old bright pixel to decay
  498. map[col][row - 1]--;
  499. // make this pixel bright
  500. map[col][row] = max_intensity;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. void rgb_matrix_multisplash(void) {
  507. // if (g_any_key_hit < 0xFF) {
  508. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  509. RGB rgb;
  510. rgb_led led;
  511. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  512. led = g_rgb_leds[i];
  513. uint16_t c = 0, d = 0;
  514. rgb_led last_led;
  515. // if (g_last_led_count) {
  516. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  517. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  518. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  519. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  520. c += MIN(MAX(effect, 0), 255);
  521. d += 255 - MIN(MAX(effect, 0), 255);
  522. }
  523. // } else {
  524. // d = 255;
  525. // }
  526. hsv.h = (rgb_matrix_config.hue + c) % 256;
  527. hsv.v = MAX(MIN(d, 255), 0);
  528. rgb = hsv_to_rgb( hsv );
  529. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  530. }
  531. // } else {
  532. // rgb_matrix_set_color_all( 0, 0, 0 );
  533. // }
  534. }
  535. void rgb_matrix_splash(void) {
  536. g_last_led_count = MIN(g_last_led_count, 1);
  537. rgb_matrix_multisplash();
  538. }
  539. void rgb_matrix_solid_multisplash(void) {
  540. // if (g_any_key_hit < 0xFF) {
  541. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  542. RGB rgb;
  543. rgb_led led;
  544. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  545. led = g_rgb_leds[i];
  546. uint16_t d = 0;
  547. rgb_led last_led;
  548. // if (g_last_led_count) {
  549. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  550. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  551. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  552. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  553. d += 255 - MIN(MAX(effect, 0), 255);
  554. }
  555. // } else {
  556. // d = 255;
  557. // }
  558. hsv.v = MAX(MIN(d, 255), 0);
  559. rgb = hsv_to_rgb( hsv );
  560. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  561. }
  562. // } else {
  563. // rgb_matrix_set_color_all( 0, 0, 0 );
  564. // }
  565. }
  566. void rgb_matrix_solid_splash(void) {
  567. g_last_led_count = MIN(g_last_led_count, 1);
  568. rgb_matrix_solid_multisplash();
  569. }
  570. // Needs eeprom access that we don't have setup currently
  571. void rgb_matrix_custom(void) {
  572. // HSV hsv;
  573. // RGB rgb;
  574. // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  575. // {
  576. // backlight_get_key_color(i, &hsv);
  577. // // Override brightness with global brightness control
  578. // hsv.v = rgb_matrix_config.val;
  579. // rgb = hsv_to_rgb( hsv );
  580. // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  581. // }
  582. }
  583. void rgb_matrix_task(void) {
  584. static uint8_t toggle_enable_last = 255;
  585. if (!rgb_matrix_config.enable) {
  586. rgb_matrix_all_off();
  587. toggle_enable_last = rgb_matrix_config.enable;
  588. return;
  589. }
  590. // delay 1 second before driving LEDs or doing anything else
  591. static uint8_t startup_tick = 0;
  592. if ( startup_tick < 20 ) {
  593. startup_tick++;
  594. return;
  595. }
  596. g_tick++;
  597. if ( g_any_key_hit < 0xFFFFFFFF ) {
  598. g_any_key_hit++;
  599. }
  600. for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
  601. if ( g_key_hit[led] < 255 ) {
  602. if (g_key_hit[led] == 254)
  603. g_last_led_count = MAX(g_last_led_count - 1, 0);
  604. g_key_hit[led]++;
  605. }
  606. }
  607. // Factory default magic value
  608. if ( rgb_matrix_config.mode == 255 ) {
  609. rgb_matrix_test();
  610. return;
  611. }
  612. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  613. // while suspended and just do a software shutdown. This is a cheap hack for now.
  614. bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
  615. (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
  616. uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
  617. // Keep track of the effect used last time,
  618. // detect change in effect, so each effect can
  619. // have an optional initialization.
  620. static uint8_t effect_last = 255;
  621. bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
  622. effect_last = effect;
  623. toggle_enable_last = rgb_matrix_config.enable;
  624. // this gets ticked at 20 Hz.
  625. // each effect can opt to do calculations
  626. // and/or request PWM buffer updates.
  627. switch ( effect ) {
  628. case RGB_MATRIX_SOLID_COLOR:
  629. rgb_matrix_solid_color();
  630. break;
  631. case RGB_MATRIX_ALPHAS_MODS:
  632. rgb_matrix_alphas_mods();
  633. break;
  634. case RGB_MATRIX_DUAL_BEACON:
  635. rgb_matrix_dual_beacon();
  636. break;
  637. case RGB_MATRIX_GRADIENT_UP_DOWN:
  638. rgb_matrix_gradient_up_down();
  639. break;
  640. case RGB_MATRIX_RAINDROPS:
  641. rgb_matrix_raindrops( initialize );
  642. break;
  643. case RGB_MATRIX_CYCLE_ALL:
  644. rgb_matrix_cycle_all();
  645. break;
  646. case RGB_MATRIX_CYCLE_LEFT_RIGHT:
  647. rgb_matrix_cycle_left_right();
  648. break;
  649. case RGB_MATRIX_CYCLE_UP_DOWN:
  650. rgb_matrix_cycle_up_down();
  651. break;
  652. case RGB_MATRIX_RAINBOW_BEACON:
  653. rgb_matrix_rainbow_beacon();
  654. break;
  655. case RGB_MATRIX_RAINBOW_PINWHEELS:
  656. rgb_matrix_rainbow_pinwheels();
  657. break;
  658. case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
  659. rgb_matrix_rainbow_moving_chevron();
  660. break;
  661. case RGB_MATRIX_JELLYBEAN_RAINDROPS:
  662. rgb_matrix_jellybean_raindrops( initialize );
  663. break;
  664. case RGB_MATRIX_DIGITAL_RAIN:
  665. rgb_matrix_digital_rain( initialize );
  666. break;
  667. #ifdef RGB_MATRIX_KEYPRESSES
  668. case RGB_MATRIX_SOLID_REACTIVE:
  669. rgb_matrix_solid_reactive();
  670. break;
  671. case RGB_MATRIX_SPLASH:
  672. rgb_matrix_splash();
  673. break;
  674. case RGB_MATRIX_MULTISPLASH:
  675. rgb_matrix_multisplash();
  676. break;
  677. case RGB_MATRIX_SOLID_SPLASH:
  678. rgb_matrix_solid_splash();
  679. break;
  680. case RGB_MATRIX_SOLID_MULTISPLASH:
  681. rgb_matrix_solid_multisplash();
  682. break;
  683. #endif
  684. default:
  685. rgb_matrix_custom();
  686. break;
  687. }
  688. if ( ! suspend_backlight ) {
  689. rgb_matrix_indicators();
  690. }
  691. }
  692. void rgb_matrix_indicators(void) {
  693. rgb_matrix_indicators_kb();
  694. rgb_matrix_indicators_user();
  695. }
  696. __attribute__((weak))
  697. void rgb_matrix_indicators_kb(void) {}
  698. __attribute__((weak))
  699. void rgb_matrix_indicators_user(void) {}
  700. // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
  701. // {
  702. // if ( row >= MATRIX_ROWS )
  703. // {
  704. // // Special value, 255=none, 254=all
  705. // *index = row;
  706. // }
  707. // else
  708. // {
  709. // // This needs updated to something like
  710. // // uint8_t led[8], led_count;
  711. // // map_row_column_to_led(row,column,led,&led_count);
  712. // // for(uint8_t i = 0; i < led_count; i++)
  713. // map_row_column_to_led( row, column, index );
  714. // }
  715. // }
  716. void rgb_matrix_init(void) {
  717. rgb_matrix_setup_drivers();
  718. // TODO: put the 1 second startup delay here?
  719. // clear the key hits
  720. for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
  721. g_key_hit[led] = 255;
  722. }
  723. if (!eeconfig_is_enabled()) {
  724. dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
  725. eeconfig_init();
  726. eeconfig_update_rgb_matrix_default();
  727. }
  728. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  729. if (!rgb_matrix_config.mode) {
  730. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  731. eeconfig_update_rgb_matrix_default();
  732. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  733. }
  734. eeconfig_debug_rgb_matrix(); // display current eeprom values
  735. }
  736. void rgb_matrix_setup_drivers(void) {
  737. // Initialize TWI
  738. i2c_init();
  739. #ifdef IS31FL3731
  740. IS31FL3731_init( DRIVER_ADDR_1 );
  741. IS31FL3731_init( DRIVER_ADDR_2 );
  742. #elif defined (IS31FL3733)
  743. IS31FL3733_init( DRIVER_ADDR_1 );
  744. #endif
  745. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
  746. bool enabled = true;
  747. // This only caches it for later
  748. #ifdef IS31FL3731
  749. IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
  750. #elif defined (IS31FL3733)
  751. IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
  752. #endif
  753. }
  754. // This actually updates the LED drivers
  755. #ifdef IS31FL3731
  756. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  757. #elif defined (IS31FL3733)
  758. IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  759. #endif
  760. }
  761. // Deals with the messy details of incrementing an integer
  762. uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  763. int16_t new_value = value;
  764. new_value += step;
  765. return MIN( MAX( new_value, min ), max );
  766. }
  767. uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  768. int16_t new_value = value;
  769. new_value -= step;
  770. return MIN( MAX( new_value, min ), max );
  771. }
  772. // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
  773. // {
  774. // // 3 bytes per color
  775. // return EECONFIG_RGB_MATRIX + ( led * 3 );
  776. // }
  777. // void backlight_get_key_color( uint8_t led, HSV *hsv )
  778. // {
  779. // void *address = backlight_get_custom_key_color_eeprom_address( led );
  780. // hsv->h = eeprom_read_byte(address);
  781. // hsv->s = eeprom_read_byte(address+1);
  782. // hsv->v = eeprom_read_byte(address+2);
  783. // }
  784. // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
  785. // {
  786. // uint8_t led[8], led_count;
  787. // map_row_column_to_led(row,column,led,&led_count);
  788. // for(uint8_t i = 0; i < led_count; i++) {
  789. // if ( led[i] < DRIVER_LED_TOTAL )
  790. // {
  791. // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
  792. // eeprom_update_byte(address, hsv.h);
  793. // eeprom_update_byte(address+1, hsv.s);
  794. // eeprom_update_byte(address+2, hsv.v);
  795. // }
  796. // }
  797. // }
  798. void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
  799. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  800. {
  801. if ( i == index )
  802. {
  803. #ifdef IS31FL3731
  804. IS31FL3731_set_led_control_register( i, red, green, blue );
  805. #elif defined (IS31FL3733)
  806. IS31FL3733_set_led_control_register( i, red, green, blue );
  807. #endif
  808. }
  809. else
  810. {
  811. #ifdef IS31FL3731
  812. IS31FL3731_set_led_control_register( i, false, false, false );
  813. #elif defined (IS31FL3733)
  814. IS31FL3733_set_led_control_register( i, false, false, false );
  815. #endif
  816. }
  817. }
  818. }
  819. uint32_t rgb_matrix_get_tick(void) {
  820. return g_tick;
  821. }
  822. void rgblight_toggle(void) {
  823. rgb_matrix_config.enable ^= 1;
  824. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  825. }
  826. void rgblight_step(void) {
  827. rgb_matrix_config.mode++;
  828. if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
  829. rgb_matrix_config.mode = 1;
  830. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  831. }
  832. void rgblight_step_reverse(void) {
  833. rgb_matrix_config.mode--;
  834. if (rgb_matrix_config.mode < 1)
  835. rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
  836. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  837. }
  838. void rgblight_increase_hue(void) {
  839. rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
  840. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  841. }
  842. void rgblight_decrease_hue(void) {
  843. rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
  844. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  845. }
  846. void rgblight_increase_sat(void) {
  847. rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
  848. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  849. }
  850. void rgblight_decrease_sat(void) {
  851. rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
  852. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  853. }
  854. void rgblight_increase_val(void) {
  855. rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
  856. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  857. }
  858. void rgblight_decrease_val(void) {
  859. rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
  860. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  861. }
  862. void rgblight_increase_speed(void) {
  863. rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
  864. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  865. }
  866. void rgblight_decrease_speed(void) {
  867. rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
  868. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
  869. }
  870. void rgblight_mode(uint8_t mode) {
  871. rgb_matrix_config.mode = mode;
  872. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  873. }
  874. uint32_t rgblight_get_mode(void) {
  875. return rgb_matrix_config.mode;
  876. }