2
0

rgb_matrix.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 "TWIlib.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. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  61. }
  62. void eeconfig_debug_rgb_matrix(void) {
  63. dprintf("rgb_matrix_config eprom\n");
  64. dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
  65. dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
  66. dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
  67. dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
  68. dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
  69. }
  70. // Last led hit
  71. #define LED_HITS_TO_REMEMBER 8
  72. uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
  73. uint8_t g_last_led_count = 0;
  74. void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
  75. rgb_led led;
  76. *led_count = 0;
  77. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  78. // map_index_to_led(i, &led);
  79. led = g_rgb_leds[i];
  80. if (row == led.matrix_co.row && column == led.matrix_co.col) {
  81. led_i[*led_count] = i;
  82. (*led_count)++;
  83. }
  84. }
  85. }
  86. void rgb_matrix_update_pwm_buffers(void) {
  87. IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  88. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  89. }
  90. void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
  91. IS31FL3731_set_color( index, red, green, blue );
  92. }
  93. void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
  94. IS31FL3731_set_color_all( red, green, blue );
  95. }
  96. bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
  97. if ( record->event.pressed ) {
  98. uint8_t led[8], led_count;
  99. map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
  100. if (led_count > 0) {
  101. for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
  102. g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
  103. }
  104. g_last_led_hit[0] = led[0];
  105. g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
  106. }
  107. for(uint8_t i = 0; i < led_count; i++)
  108. g_key_hit[led[i]] = 0;
  109. g_any_key_hit = 0;
  110. } else {
  111. #ifdef RGB_MATRIX_KEYRELEASES
  112. uint8_t led[8], led_count;
  113. map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
  114. for(uint8_t i = 0; i < led_count; i++)
  115. g_key_hit[led[i]] = 255;
  116. g_any_key_hit = 255;
  117. #endif
  118. }
  119. return true;
  120. }
  121. void rgb_matrix_set_suspend_state(bool state) {
  122. g_suspend_state = state;
  123. }
  124. void rgb_matrix_test(void) {
  125. // Mask out bits 4 and 5
  126. // This 2-bit value will stay the same for 16 ticks.
  127. switch ( (g_tick & 0x30) >> 4 )
  128. {
  129. case 0:
  130. {
  131. rgb_matrix_set_color_all( 20, 0, 0 );
  132. break;
  133. }
  134. case 1:
  135. {
  136. rgb_matrix_set_color_all( 0, 20, 0 );
  137. break;
  138. }
  139. case 2:
  140. {
  141. rgb_matrix_set_color_all( 0, 0, 20 );
  142. break;
  143. }
  144. case 3:
  145. {
  146. rgb_matrix_set_color_all( 20, 20, 20 );
  147. break;
  148. }
  149. }
  150. }
  151. // This tests the LEDs
  152. // Note that it will change the LED control registers
  153. // in the LED drivers, and leave them in an invalid
  154. // state for other backlight effects.
  155. // ONLY USE THIS FOR TESTING LEDS!
  156. void rgb_matrix_single_LED_test(void) {
  157. static uint8_t color = 0; // 0,1,2 for R,G,B
  158. static uint8_t row = 0;
  159. static uint8_t column = 0;
  160. static uint8_t tick = 0;
  161. tick++;
  162. if ( tick > 2 )
  163. {
  164. tick = 0;
  165. column++;
  166. }
  167. if ( column > MATRIX_COLS )
  168. {
  169. column = 0;
  170. row++;
  171. }
  172. if ( row > MATRIX_ROWS )
  173. {
  174. row = 0;
  175. color++;
  176. }
  177. if ( color > 2 )
  178. {
  179. color = 0;
  180. }
  181. uint8_t led[8], led_count;
  182. map_row_column_to_led(row,column,led,&led_count);
  183. for(uint8_t i = 0; i < led_count; i++) {
  184. rgb_matrix_set_color_all( 40, 40, 40 );
  185. rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
  186. }
  187. }
  188. // All LEDs off
  189. void rgb_matrix_all_off(void) {
  190. rgb_matrix_set_color_all( 0, 0, 0 );
  191. }
  192. static HSV current_solid_color;
  193. // Solid color
  194. void rgb_matrix_solid_color(void) {
  195. if (current_solid_color.h != rgb_matrix_config.hue &&
  196. current_solid_color.s != rgb_matrix_config.sat &&
  197. current_solid_color.v != rgb_matrix_config.val) {
  198. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  199. RGB rgb = hsv_to_rgb( hsv );
  200. rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
  201. current_solid_color.h = hsv.h;
  202. current_solid_color.s = hsv.s;
  203. current_solid_color.v = hsv.v;
  204. }
  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
  290. uint8_t led_to_change = ( g_tick & 0x000 ) == 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 & 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 & 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 & 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 * (led.point.y - 32.0)* cos(g_tick * PI / 128) + 1.5 * (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 * (led.point.y - 32.0)* cos(g_tick * PI / 128) + 2 * (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 * abs(led.point.y - 32.0)* sin(r * PI / 128) + 1.5 * (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
  417. uint8_t led_to_change = ( g_tick & 0x000 ) == 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. if (!rgb_matrix_config.enable) {
  512. rgb_matrix_all_off();
  513. return;
  514. }
  515. // delay 1 second before driving LEDs or doing anything else
  516. static uint8_t startup_tick = 0;
  517. if ( startup_tick < 20 ) {
  518. startup_tick++;
  519. return;
  520. }
  521. g_tick++;
  522. if ( g_any_key_hit < 0xFFFFFFFF ) {
  523. g_any_key_hit++;
  524. }
  525. for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
  526. if ( g_key_hit[led] < 255 ) {
  527. if (g_key_hit[led] == 254)
  528. g_last_led_count = MAX(g_last_led_count - 1, 0);
  529. g_key_hit[led]++;
  530. }
  531. }
  532. // Factory default magic value
  533. if ( rgb_matrix_config.mode == 255 ) {
  534. rgb_matrix_test();
  535. return;
  536. }
  537. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  538. // while suspended and just do a software shutdown. This is a cheap hack for now.
  539. bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
  540. (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
  541. uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
  542. // Keep track of the effect used last time,
  543. // detect change in effect, so each effect can
  544. // have an optional initialization.
  545. static uint8_t effect_last = 255;
  546. bool initialize = effect != effect_last;
  547. effect_last = effect;
  548. // this gets ticked at 20 Hz.
  549. // each effect can opt to do calculations
  550. // and/or request PWM buffer updates.
  551. switch ( effect ) {
  552. case RGB_MATRIX_SOLID_COLOR:
  553. rgb_matrix_solid_color();
  554. break;
  555. case RGB_MATRIX_SOLID_REACTIVE:
  556. rgb_matrix_solid_reactive();
  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_SPLASH:
  593. rgb_matrix_splash();
  594. break;
  595. case RGB_MATRIX_MULTISPLASH:
  596. rgb_matrix_multisplash();
  597. break;
  598. case RGB_MATRIX_SOLID_SPLASH:
  599. rgb_matrix_solid_splash();
  600. break;
  601. case RGB_MATRIX_SOLID_MULTISPLASH:
  602. rgb_matrix_solid_multisplash();
  603. break;
  604. #endif
  605. default:
  606. rgb_matrix_custom();
  607. break;
  608. }
  609. if ( ! suspend_backlight ) {
  610. rgb_matrix_indicators();
  611. }
  612. rgb_matrix_update_pwm_buffers();
  613. }
  614. void rgb_matrix_indicators(void) {
  615. rgb_matrix_indicators_kb();
  616. rgb_matrix_indicators_user();
  617. }
  618. __attribute__((weak))
  619. void rgb_matrix_indicators_kb(void) {}
  620. __attribute__((weak))
  621. void rgb_matrix_indicators_user(void) {}
  622. // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
  623. // {
  624. // if ( row >= MATRIX_ROWS )
  625. // {
  626. // // Special value, 255=none, 254=all
  627. // *index = row;
  628. // }
  629. // else
  630. // {
  631. // // This needs updated to something like
  632. // // uint8_t led[8], led_count;
  633. // // map_row_column_to_led(row,column,led,&led_count);
  634. // // for(uint8_t i = 0; i < led_count; i++)
  635. // map_row_column_to_led( row, column, index );
  636. // }
  637. // }
  638. void rgb_matrix_init_drivers(void) {
  639. //sei();
  640. // Initialize TWI
  641. TWIInit();
  642. IS31FL3731_init( DRIVER_ADDR_1 );
  643. IS31FL3731_init( DRIVER_ADDR_2 );
  644. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
  645. bool enabled = true;
  646. // This only caches it for later
  647. IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
  648. }
  649. // This actually updates the LED drivers
  650. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  651. // TODO: put the 1 second startup delay here?
  652. // clear the key hits
  653. for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
  654. g_key_hit[led] = 255;
  655. }
  656. if (!eeconfig_is_enabled()) {
  657. dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
  658. eeconfig_init();
  659. eeconfig_update_rgb_matrix_default();
  660. }
  661. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  662. if (!rgb_matrix_config.mode) {
  663. dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
  664. eeconfig_update_rgb_matrix_default();
  665. rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
  666. }
  667. eeconfig_debug_rgb_matrix(); // display current eeprom values
  668. }
  669. // Deals with the messy details of incrementing an integer
  670. uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  671. int16_t new_value = value;
  672. new_value += step;
  673. return MIN( MAX( new_value, min ), max );
  674. }
  675. uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  676. int16_t new_value = value;
  677. new_value -= step;
  678. return MIN( MAX( new_value, min ), max );
  679. }
  680. // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
  681. // {
  682. // // 3 bytes per color
  683. // return EECONFIG_RGB_MATRIX + ( led * 3 );
  684. // }
  685. // void backlight_get_key_color( uint8_t led, HSV *hsv )
  686. // {
  687. // void *address = backlight_get_custom_key_color_eeprom_address( led );
  688. // hsv->h = eeprom_read_byte(address);
  689. // hsv->s = eeprom_read_byte(address+1);
  690. // hsv->v = eeprom_read_byte(address+2);
  691. // }
  692. // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
  693. // {
  694. // uint8_t led[8], led_count;
  695. // map_row_column_to_led(row,column,led,&led_count);
  696. // for(uint8_t i = 0; i < led_count; i++) {
  697. // if ( led[i] < DRIVER_LED_TOTAL )
  698. // {
  699. // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
  700. // eeprom_update_byte(address, hsv.h);
  701. // eeprom_update_byte(address+1, hsv.s);
  702. // eeprom_update_byte(address+2, hsv.v);
  703. // }
  704. // }
  705. // }
  706. void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
  707. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  708. {
  709. if ( i == index )
  710. {
  711. IS31FL3731_set_led_control_register( i, red, green, blue );
  712. }
  713. else
  714. {
  715. IS31FL3731_set_led_control_register( i, false, false, false );
  716. }
  717. }
  718. }
  719. uint32_t rgb_matrix_get_tick(void) {
  720. return g_tick;
  721. }
  722. void rgblight_toggle(void) {
  723. rgb_matrix_config.enable ^= 1;
  724. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  725. }
  726. void rgblight_step(void) {
  727. rgb_matrix_config.mode++;
  728. if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
  729. rgb_matrix_config.mode = 1;
  730. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  731. }
  732. void rgblight_step_reverse(void) {
  733. rgb_matrix_config.mode--;
  734. if (rgb_matrix_config.mode <= 1)
  735. rgb_matrix_config.mode = (RGB_MATRIX_EFFECT_MAX - 1);
  736. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  737. }
  738. void rgblight_increase_hue(void) {
  739. rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
  740. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  741. }
  742. void rgblight_decrease_hue(void) {
  743. rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
  744. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  745. }
  746. void rgblight_increase_sat(void) {
  747. rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
  748. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  749. }
  750. void rgblight_decrease_sat(void) {
  751. rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
  752. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  753. }
  754. void rgblight_increase_val(void) {
  755. rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, 255 );
  756. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  757. }
  758. void rgblight_decrease_val(void) {
  759. rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, 255 );
  760. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  761. }
  762. void rgblight_mode(uint8_t mode) {
  763. rgb_matrix_config.mode = 1;
  764. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  765. }
  766. uint32_t rgblight_get_mode(void) {
  767. return rgb_matrix_config.mode;
  768. }