You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

887 lines
28 KiB

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