rgb_matrix.c 28 KB

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