rgb_matrix.c 29 KB

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