rgb_matrix.c 28 KB

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