rgb_matrix.c 26 KB

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