rgb_matrix.c 29 KB

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