rgb_matrix.c 29 KB

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