rgb_matrix.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. // Solid color
  193. void rgb_matrix_solid_color(void) {
  194. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  195. RGB rgb = hsv_to_rgb( hsv );
  196. rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
  197. }
  198. void rgb_matrix_solid_reactive(void) {
  199. // Relies on hue being 8-bit and wrapping
  200. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  201. {
  202. uint16_t offset2 = g_key_hit[i]<<2;
  203. offset2 = (offset2<=130) ? (130-offset2) : 0;
  204. HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
  205. RGB rgb = hsv_to_rgb( hsv );
  206. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  207. }
  208. }
  209. // alphas = color1, mods = color2
  210. void rgb_matrix_alphas_mods(void) {
  211. RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
  212. RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
  213. rgb_led led;
  214. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  215. led = g_rgb_leds[i];
  216. if ( led.matrix_co.raw < 0xFF ) {
  217. if ( led.modifier )
  218. {
  219. rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
  220. }
  221. else
  222. {
  223. rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
  224. }
  225. }
  226. }
  227. }
  228. void rgb_matrix_gradient_up_down(void) {
  229. int16_t h1 = rgb_matrix_config.hue;
  230. int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
  231. int16_t deltaH = h2 - h1;
  232. // Take the shortest path between hues
  233. if ( deltaH > 127 )
  234. {
  235. deltaH -= 256;
  236. }
  237. else if ( deltaH < -127 )
  238. {
  239. deltaH += 256;
  240. }
  241. // Divide delta by 4, this gives the delta per row
  242. deltaH /= 4;
  243. int16_t s1 = rgb_matrix_config.sat;
  244. int16_t s2 = rgb_matrix_config.hue;
  245. int16_t deltaS = ( s2 - s1 ) / 4;
  246. HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
  247. RGB rgb;
  248. Point point;
  249. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  250. {
  251. // map_led_to_point( i, &point );
  252. point = g_rgb_leds[i].point;
  253. // The y range will be 0..64, map this to 0..4
  254. uint8_t y = (point.y>>4);
  255. // Relies on hue being 8-bit and wrapping
  256. hsv.h = rgb_matrix_config.hue + ( deltaH * y );
  257. hsv.s = rgb_matrix_config.sat + ( deltaS * y );
  258. rgb = hsv_to_rgb( hsv );
  259. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  260. }
  261. }
  262. void rgb_matrix_raindrops(bool initialize) {
  263. int16_t h1 = rgb_matrix_config.hue;
  264. int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
  265. int16_t deltaH = h2 - h1;
  266. deltaH /= 4;
  267. // Take the shortest path between hues
  268. if ( deltaH > 127 )
  269. {
  270. deltaH -= 256;
  271. }
  272. else if ( deltaH < -127 )
  273. {
  274. deltaH += 256;
  275. }
  276. int16_t s1 = rgb_matrix_config.sat;
  277. int16_t s2 = rgb_matrix_config.sat;
  278. int16_t deltaS = ( s2 - s1 ) / 4;
  279. HSV hsv;
  280. RGB rgb;
  281. // Change one LED every tick
  282. uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255;
  283. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  284. {
  285. // If initialize, all get set to random colors
  286. // If not, all but one will stay the same as before.
  287. if ( initialize || i == led_to_change )
  288. {
  289. hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
  290. hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
  291. // Override brightness with global brightness control
  292. hsv.v = rgb_matrix_config.val;
  293. rgb = hsv_to_rgb( hsv );
  294. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  295. }
  296. }
  297. }
  298. void rgb_matrix_cycle_all(void) {
  299. uint8_t offset = g_tick & 0xFF;
  300. rgb_led led;
  301. // Relies on hue being 8-bit and wrapping
  302. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  303. {
  304. // map_index_to_led(i, &led);
  305. led = g_rgb_leds[i];
  306. if (led.matrix_co.raw < 0xFF) {
  307. uint16_t offset2 = g_key_hit[i]<<2;
  308. offset2 = (offset2<=63) ? (63-offset2) : 0;
  309. HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
  310. RGB rgb = hsv_to_rgb( hsv );
  311. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  312. }
  313. }
  314. }
  315. void rgb_matrix_cycle_left_right(void) {
  316. uint8_t offset = g_tick & 0xFF;
  317. HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
  318. RGB rgb;
  319. Point point;
  320. rgb_led led;
  321. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  322. {
  323. // map_index_to_led(i, &led);
  324. led = g_rgb_leds[i];
  325. if (led.matrix_co.raw < 0xFF) {
  326. uint16_t offset2 = g_key_hit[i]<<2;
  327. offset2 = (offset2<=63) ? (63-offset2) : 0;
  328. // map_led_to_point( i, &point );
  329. point = g_rgb_leds[i].point;
  330. // Relies on hue being 8-bit and wrapping
  331. hsv.h = point.x + offset + offset2;
  332. 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_up_down(void) {
  338. uint8_t offset = g_tick & 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.y + 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_dual_beacon(void) {
  360. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  361. RGB rgb;
  362. rgb_led led;
  363. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  364. led = g_rgb_leds[i];
  365. 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;
  366. rgb = hsv_to_rgb( hsv );
  367. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  368. }
  369. }
  370. void rgb_matrix_rainbow_beacon(void) {
  371. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  372. RGB rgb;
  373. rgb_led led;
  374. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  375. led = g_rgb_leds[i];
  376. 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;
  377. rgb = hsv_to_rgb( hsv );
  378. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  379. }
  380. }
  381. void rgb_matrix_rainbow_pinwheels(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 = 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;
  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_moving_chevron(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. // uint8_t r = g_tick;
  399. uint8_t r = 32;
  400. 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;
  401. rgb = hsv_to_rgb( hsv );
  402. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  403. }
  404. }
  405. void rgb_matrix_jellybean_raindrops( bool initialize ) {
  406. HSV hsv;
  407. RGB rgb;
  408. // Change one LED every tick
  409. uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255;
  410. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  411. {
  412. // If initialize, all get set to random colors
  413. // If not, all but one will stay the same as before.
  414. if ( initialize || i == led_to_change )
  415. {
  416. hsv.h = rand() & 0xFF;
  417. hsv.s = rand() & 0xFF;
  418. // Override brightness with global brightness control
  419. hsv.v = rgb_matrix_config.val;
  420. rgb = hsv_to_rgb( hsv );
  421. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  422. }
  423. }
  424. }
  425. void rgb_matrix_multisplash(void) {
  426. // if (g_any_key_hit < 0xFF) {
  427. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  428. RGB rgb;
  429. rgb_led led;
  430. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  431. led = g_rgb_leds[i];
  432. uint16_t c = 0, d = 0;
  433. rgb_led last_led;
  434. // if (g_last_led_count) {
  435. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  436. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  437. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  438. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  439. c += MIN(MAX(effect, 0), 255);
  440. d += 255 - MIN(MAX(effect, 0), 255);
  441. }
  442. // } else {
  443. // d = 255;
  444. // }
  445. hsv.h = (rgb_matrix_config.hue + c) % 256;
  446. hsv.v = MAX(MIN(d, 255), 0);
  447. rgb = hsv_to_rgb( hsv );
  448. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  449. }
  450. // } else {
  451. // rgb_matrix_set_color_all( 0, 0, 0 );
  452. // }
  453. }
  454. void rgb_matrix_splash(void) {
  455. g_last_led_count = MIN(g_last_led_count, 1);
  456. rgb_matrix_multisplash();
  457. }
  458. void rgb_matrix_solid_multisplash(void) {
  459. // if (g_any_key_hit < 0xFF) {
  460. HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
  461. RGB rgb;
  462. rgb_led led;
  463. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  464. led = g_rgb_leds[i];
  465. uint16_t d = 0;
  466. rgb_led last_led;
  467. // if (g_last_led_count) {
  468. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  469. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  470. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  471. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  472. d += 255 - MIN(MAX(effect, 0), 255);
  473. }
  474. // } else {
  475. // d = 255;
  476. // }
  477. hsv.v = MAX(MIN(d, 255), 0);
  478. rgb = hsv_to_rgb( hsv );
  479. rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  480. }
  481. // } else {
  482. // rgb_matrix_set_color_all( 0, 0, 0 );
  483. // }
  484. }
  485. void rgb_matrix_solid_splash(void) {
  486. g_last_led_count = MIN(g_last_led_count, 1);
  487. rgb_matrix_solid_multisplash();
  488. }
  489. // Needs eeprom access that we don't have setup currently
  490. void rgb_matrix_custom(void) {
  491. // HSV hsv;
  492. // RGB rgb;
  493. // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  494. // {
  495. // backlight_get_key_color(i, &hsv);
  496. // // Override brightness with global brightness control
  497. // hsv.v = rgb_matrix_config.val;
  498. // rgb = hsv_to_rgb( hsv );
  499. // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
  500. // }
  501. }
  502. void rgb_matrix_task(void) {
  503. if (!rgb_matrix_config.enable) {
  504. rgb_matrix_all_off();
  505. return;
  506. }
  507. // delay 1 second before driving LEDs or doing anything else
  508. static uint8_t startup_tick = 0;
  509. if ( startup_tick < 20 ) {
  510. startup_tick++;
  511. return;
  512. }
  513. g_tick++;
  514. if ( g_any_key_hit < 0xFFFFFFFF ) {
  515. g_any_key_hit++;
  516. }
  517. for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
  518. if ( g_key_hit[led] < 255 ) {
  519. if (g_key_hit[led] == 254)
  520. g_last_led_count = MAX(g_last_led_count - 1, 0);
  521. g_key_hit[led]++;
  522. }
  523. }
  524. // Factory default magic value
  525. if ( rgb_matrix_config.mode == 255 ) {
  526. rgb_matrix_test();
  527. return;
  528. }
  529. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  530. // while suspended and just do a software shutdown. This is a cheap hack for now.
  531. bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
  532. (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
  533. uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
  534. // Keep track of the effect used last time,
  535. // detect change in effect, so each effect can
  536. // have an optional initialization.
  537. static uint8_t effect_last = 255;
  538. bool initialize = effect != effect_last;
  539. effect_last = effect;
  540. // this gets ticked at 20 Hz.
  541. // each effect can opt to do calculations
  542. // and/or request PWM buffer updates.
  543. switch ( effect ) {
  544. case RGB_MATRIX_SOLID_COLOR:
  545. rgb_matrix_solid_color();
  546. break;
  547. case RGB_MATRIX_SOLID_REACTIVE:
  548. rgb_matrix_solid_reactive();
  549. break;
  550. case RGB_MATRIX_ALPHAS_MODS:
  551. rgb_matrix_alphas_mods();
  552. break;
  553. case RGB_MATRIX_DUAL_BEACON:
  554. rgb_matrix_dual_beacon();
  555. break;
  556. case RGB_MATRIX_GRADIENT_UP_DOWN:
  557. rgb_matrix_gradient_up_down();
  558. break;
  559. case RGB_MATRIX_RAINDROPS:
  560. rgb_matrix_raindrops( initialize );
  561. break;
  562. case RGB_MATRIX_CYCLE_ALL:
  563. rgb_matrix_cycle_all();
  564. break;
  565. case RGB_MATRIX_CYCLE_LEFT_RIGHT:
  566. rgb_matrix_cycle_left_right();
  567. break;
  568. case RGB_MATRIX_CYCLE_UP_DOWN:
  569. rgb_matrix_cycle_up_down();
  570. break;
  571. case RGB_MATRIX_RAINBOW_BEACON:
  572. rgb_matrix_rainbow_beacon();
  573. break;
  574. case RGB_MATRIX_RAINBOW_PINWHEELS:
  575. rgb_matrix_rainbow_pinwheels();
  576. break;
  577. case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
  578. rgb_matrix_rainbow_moving_chevron();
  579. break;
  580. case RGB_MATRIX_JELLYBEAN_RAINDROPS:
  581. rgb_matrix_jellybean_raindrops( initialize );
  582. break;
  583. #ifdef RGB_MATRIX_KEYPRESSES
  584. case RGB_MATRIX_SPLASH:
  585. rgb_matrix_splash();
  586. break;
  587. case RGB_MATRIX_MULTISPLASH:
  588. rgb_matrix_multisplash();
  589. break;
  590. case RGB_MATRIX_SOLID_SPLASH:
  591. rgb_matrix_solid_splash();
  592. break;
  593. case RGB_MATRIX_SOLID_MULTISPLASH:
  594. rgb_matrix_solid_multisplash();
  595. break;
  596. #endif
  597. default:
  598. rgb_matrix_custom();
  599. break;
  600. }
  601. if ( ! suspend_backlight ) {
  602. rgb_matrix_indicators();
  603. }
  604. }
  605. void rgb_matrix_indicators(void) {
  606. rgb_matrix_indicators_kb();
  607. rgb_matrix_indicators_user();
  608. }
  609. __attribute__((weak))
  610. void rgb_matrix_indicators_kb(void) {}
  611. __attribute__((weak))
  612. void rgb_matrix_indicators_user(void) {}
  613. // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
  614. // {
  615. // if ( row >= MATRIX_ROWS )
  616. // {
  617. // // Special value, 255=none, 254=all
  618. // *index = row;
  619. // }
  620. // else
  621. // {
  622. // // This needs updated to something like
  623. // // uint8_t led[8], led_count;
  624. // // map_row_column_to_led(row,column,led,&led_count);
  625. // // for(uint8_t i = 0; i < led_count; i++)
  626. // map_row_column_to_led( row, column, index );
  627. // }
  628. // }
  629. void rgb_matrix_init_drivers(void) {
  630. //sei();
  631. // Initialize TWI
  632. TWIInit();
  633. IS31FL3731_init( DRIVER_ADDR_1 );
  634. IS31FL3731_init( DRIVER_ADDR_2 );
  635. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
  636. bool enabled = true;
  637. // This only caches it for later
  638. IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
  639. }
  640. // This actually updates the LED drivers
  641. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  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. // Deals with the messy details of incrementing an integer
  661. uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  662. int16_t new_value = value;
  663. new_value += step;
  664. return MIN( MAX( new_value, min ), max );
  665. }
  666. uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  667. int16_t new_value = value;
  668. new_value -= step;
  669. return MIN( MAX( new_value, min ), max );
  670. }
  671. // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
  672. // {
  673. // // 3 bytes per color
  674. // return EECONFIG_RGB_MATRIX + ( led * 3 );
  675. // }
  676. // void backlight_get_key_color( uint8_t led, HSV *hsv )
  677. // {
  678. // void *address = backlight_get_custom_key_color_eeprom_address( led );
  679. // hsv->h = eeprom_read_byte(address);
  680. // hsv->s = eeprom_read_byte(address+1);
  681. // hsv->v = eeprom_read_byte(address+2);
  682. // }
  683. // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
  684. // {
  685. // uint8_t led[8], led_count;
  686. // map_row_column_to_led(row,column,led,&led_count);
  687. // for(uint8_t i = 0; i < led_count; i++) {
  688. // if ( led[i] < DRIVER_LED_TOTAL )
  689. // {
  690. // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
  691. // eeprom_update_byte(address, hsv.h);
  692. // eeprom_update_byte(address+1, hsv.s);
  693. // eeprom_update_byte(address+2, hsv.v);
  694. // }
  695. // }
  696. // }
  697. void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
  698. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  699. {
  700. if ( i == index )
  701. {
  702. IS31FL3731_set_led_control_register( i, red, green, blue );
  703. }
  704. else
  705. {
  706. IS31FL3731_set_led_control_register( i, false, false, false );
  707. }
  708. }
  709. }
  710. uint32_t rgb_matrix_get_tick(void) {
  711. return g_tick;
  712. }
  713. void rgblight_toggle(void) {
  714. rgb_matrix_config.enable ^= 1;
  715. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  716. }
  717. void rgblight_step(void) {
  718. rgb_matrix_config.mode++;
  719. if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
  720. rgb_matrix_config.mode = 1;
  721. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  722. }
  723. void rgblight_step_reverse(void) {
  724. rgb_matrix_config.mode--;
  725. if (rgb_matrix_config.mode <= 1)
  726. rgb_matrix_config.mode = (RGB_MATRIX_EFFECT_MAX - 1);
  727. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  728. }
  729. void rgblight_increase_hue(void) {
  730. rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
  731. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  732. }
  733. void rgblight_decrease_hue(void) {
  734. rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
  735. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  736. }
  737. void rgblight_increase_sat(void) {
  738. rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
  739. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  740. }
  741. void rgblight_decrease_sat(void) {
  742. rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
  743. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  744. }
  745. void rgblight_increase_val(void) {
  746. rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, 255 );
  747. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  748. }
  749. void rgblight_decrease_val(void) {
  750. rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, 255 );
  751. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  752. }
  753. void rgblight_mode(uint8_t mode) {
  754. rgb_matrix_config.mode = mode;
  755. eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
  756. }
  757. uint32_t rgblight_get_mode(void) {
  758. return rgb_matrix_config.mode;
  759. }