rgb_matrix.c 28 KB

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