rgb_matrix.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2017 Jack Humbert
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "rgb_matrix.h"
  18. #include <avr/io.h>
  19. #include "TWIlib.h"
  20. #include <util/delay.h>
  21. #include <avr/interrupt.h>
  22. #include "progmem.h"
  23. #include "config.h"
  24. #include "eeprom.h"
  25. #include "lufa.h"
  26. #include <math.h>
  27. #define BACKLIGHT_EFFECT_MAX 17
  28. rgb_matrix_config g_config = {
  29. .enabled = 1,
  30. .brightness = 255,
  31. .effect = 7,
  32. .color_1 = { .h = 130, .s = 255, .v = 255 },
  33. .color_2 = { .h = 70, .s = 255, .v = 255 },
  34. .caps_lock_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
  35. .layer_1_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
  36. .layer_2_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
  37. .layer_3_indicator = { .color = { .h = 0, .s = 0, .v = 255 }, .index = 255 },
  38. };
  39. bool g_suspend_state = false;
  40. uint8_t g_indicator_state = 0;
  41. // Global tick at 20 Hz
  42. uint32_t g_tick = 0;
  43. // Ticks since this key was last hit.
  44. uint8_t g_key_hit[DRIVER_LED_TOTAL];
  45. // Ticks since any key was last hit.
  46. uint32_t g_any_key_hit = 0;
  47. #ifndef PI
  48. #define PI 3.14159265
  49. #endif
  50. // Last led hit
  51. #define LED_HITS_TO_REMEMBER 8
  52. uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
  53. uint8_t g_last_led_count = 0;
  54. void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count)
  55. {
  56. rgb_led led;
  57. *led_count = 0;
  58. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  59. // map_index_to_led(i, &led);
  60. led = g_rgb_leds[i];
  61. if (row == led.matrix_co.row && column == led.matrix_co.col) {
  62. led_i[*led_count] = i;
  63. (*led_count)++;
  64. }
  65. }
  66. }
  67. void backlight_update_pwm_buffers(void)
  68. {
  69. IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  70. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  71. }
  72. void backlight_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
  73. {
  74. IS31FL3731_set_color( index, red, green, blue );
  75. }
  76. void backlight_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
  77. {
  78. IS31FL3731_set_color_all( red, green, blue );
  79. }
  80. void backlight_set_key_hit(uint8_t row, uint8_t column)
  81. {
  82. uint8_t led[8], led_count;
  83. map_row_column_to_led(row,column,led,&led_count);
  84. if (led_count > 0) {
  85. for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
  86. g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
  87. }
  88. g_last_led_hit[0] = led[0];
  89. g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
  90. }
  91. for(uint8_t i = 0; i < led_count; i++)
  92. g_key_hit[led[i]] = 0;
  93. g_any_key_hit = 0;
  94. }
  95. void backlight_unset_key_hit(uint8_t row, uint8_t column)
  96. {
  97. uint8_t led[8], led_count;
  98. map_row_column_to_led(row,column,led,&led_count);
  99. for(uint8_t i = 0; i < led_count; i++)
  100. g_key_hit[led[i]] = 255;
  101. g_any_key_hit = 255;
  102. }
  103. // This is (F_CPU/1024) / 20 Hz
  104. // = 15625 Hz / 20 Hz
  105. // = 781
  106. // #define TIMER3_TOP 781
  107. void backlight_timer_init(void)
  108. {
  109. static uint8_t backlight_timer_is_init = 0;
  110. if ( backlight_timer_is_init )
  111. {
  112. return;
  113. }
  114. backlight_timer_is_init = 1;
  115. // Timer 3 setup
  116. //TCCR3B = _BV(WGM32) | // CTC mode OCR3A as TOP
  117. // _BV(CS32) | _BV(CS30); // prescale by /1024
  118. // Set TOP value
  119. //uint8_t sreg = SREG;
  120. //cli();
  121. //OCR3AH = (TIMER3_TOP >> 8) & 0xff;
  122. //OCR3AL = TIMER3_TOP & 0xff;
  123. //SREG = sreg;
  124. }
  125. void backlight_timer_enable(void)
  126. {
  127. //TIMSK3 |= _BV(OCIE3A);
  128. }
  129. void backlight_timer_disable(void)
  130. {
  131. //TIMSK3 &= ~_BV(OCIE3A);
  132. }
  133. void backlight_set_suspend_state(bool state)
  134. {
  135. g_suspend_state = state;
  136. }
  137. void backlight_set_indicator_state(uint8_t state)
  138. {
  139. g_indicator_state = state;
  140. }
  141. void backlight_effect_rgb_test(void)
  142. {
  143. // Mask out bits 4 and 5
  144. // This 2-bit value will stay the same for 16 ticks.
  145. switch ( (g_tick & 0x30) >> 4 )
  146. {
  147. case 0:
  148. {
  149. backlight_set_color_all( 20, 0, 0 );
  150. break;
  151. }
  152. case 1:
  153. {
  154. backlight_set_color_all( 0, 20, 0 );
  155. break;
  156. }
  157. case 2:
  158. {
  159. backlight_set_color_all( 0, 0, 20 );
  160. break;
  161. }
  162. case 3:
  163. {
  164. backlight_set_color_all( 20, 20, 20 );
  165. break;
  166. }
  167. }
  168. }
  169. // This tests the LEDs
  170. // Note that it will change the LED control registers
  171. // in the LED drivers, and leave them in an invalid
  172. // state for other backlight effects.
  173. // ONLY USE THIS FOR TESTING LEDS!
  174. void backlight_effect_single_LED_test(void)
  175. {
  176. static uint8_t color = 0; // 0,1,2 for R,G,B
  177. static uint8_t row = 0;
  178. static uint8_t column = 0;
  179. static uint8_t tick = 0;
  180. tick++;
  181. if ( tick > 2 )
  182. {
  183. tick = 0;
  184. column++;
  185. }
  186. if ( column > MATRIX_COLS )
  187. {
  188. column = 0;
  189. row++;
  190. }
  191. if ( row > MATRIX_ROWS )
  192. {
  193. row = 0;
  194. color++;
  195. }
  196. if ( color > 2 )
  197. {
  198. color = 0;
  199. }
  200. uint8_t led[8], led_count;
  201. map_row_column_to_led(row,column,led,&led_count);
  202. for(uint8_t i = 0; i < led_count; i++) {
  203. backlight_set_color_all( 40, 40, 40 );
  204. backlight_test_led( led[i], color==0, color==1, color==2 );
  205. }
  206. }
  207. // All LEDs off
  208. void backlight_effect_all_off(void)
  209. {
  210. backlight_set_color_all( 0, 0, 0 );
  211. }
  212. // Solid color
  213. void backlight_effect_solid_color(void)
  214. {
  215. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  216. RGB rgb = hsv_to_rgb( hsv );
  217. backlight_set_color_all( rgb.r, rgb.g, rgb.b );
  218. }
  219. void backlight_effect_solid_reactive(void)
  220. {
  221. // Relies on hue being 8-bit and wrapping
  222. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  223. {
  224. uint16_t offset2 = g_key_hit[i]<<2;
  225. offset2 = (offset2<=130) ? (130-offset2) : 0;
  226. HSV hsv = { .h = g_config.color_1.h+offset2, .s = 255, .v = g_config.brightness };
  227. RGB rgb = hsv_to_rgb( hsv );
  228. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  229. }
  230. }
  231. // alphas = color1, mods = color2
  232. void backlight_effect_alphas_mods(void)
  233. {
  234. RGB rgb1 = hsv_to_rgb( (HSV){ .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness } );
  235. RGB rgb2 = hsv_to_rgb( (HSV){ .h = g_config.color_2.h, .s = g_config.color_2.s, .v = g_config.brightness } );
  236. rgb_led led;
  237. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  238. led = g_rgb_leds[i];
  239. if ( led.matrix_co.raw < 0xFF ) {
  240. if ( led.modifier )
  241. {
  242. backlight_set_color( i, rgb2.r, rgb2.g, rgb2.b );
  243. }
  244. else
  245. {
  246. backlight_set_color( i, rgb1.r, rgb1.g, rgb1.b );
  247. }
  248. }
  249. }
  250. }
  251. void backlight_effect_gradient_up_down(void)
  252. {
  253. int16_t h1 = g_config.color_1.h;
  254. int16_t h2 = g_config.color_2.h;
  255. int16_t deltaH = h2 - h1;
  256. // Take the shortest path between hues
  257. if ( deltaH > 127 )
  258. {
  259. deltaH -= 256;
  260. }
  261. else if ( deltaH < -127 )
  262. {
  263. deltaH += 256;
  264. }
  265. // Divide delta by 4, this gives the delta per row
  266. deltaH /= 4;
  267. int16_t s1 = g_config.color_1.s;
  268. int16_t s2 = g_config.color_2.s;
  269. int16_t deltaS = ( s2 - s1 ) / 4;
  270. HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
  271. RGB rgb;
  272. Point point;
  273. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  274. {
  275. // map_led_to_point( i, &point );
  276. point = g_rgb_leds[i].point;
  277. // The y range will be 0..64, map this to 0..4
  278. uint8_t y = (point.y>>4);
  279. // Relies on hue being 8-bit and wrapping
  280. hsv.h = g_config.color_1.h + ( deltaH * y );
  281. hsv.s = g_config.color_1.s + ( deltaS * y );
  282. rgb = hsv_to_rgb( hsv );
  283. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  284. }
  285. }
  286. void backlight_effect_raindrops(bool initialize)
  287. {
  288. int16_t h1 = g_config.color_1.h;
  289. int16_t h2 = g_config.color_2.h;
  290. int16_t deltaH = h2 - h1;
  291. deltaH /= 4;
  292. // Take the shortest path between hues
  293. if ( deltaH > 127 )
  294. {
  295. deltaH -= 256;
  296. }
  297. else if ( deltaH < -127 )
  298. {
  299. deltaH += 256;
  300. }
  301. int16_t s1 = g_config.color_1.s;
  302. int16_t s2 = g_config.color_2.s;
  303. int16_t deltaS = ( s2 - s1 ) / 4;
  304. HSV hsv;
  305. RGB rgb;
  306. // Change one LED every tick
  307. uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255;
  308. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  309. {
  310. // If initialize, all get set to random colors
  311. // If not, all but one will stay the same as before.
  312. if ( initialize || i == led_to_change )
  313. {
  314. hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
  315. hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
  316. // Override brightness with global brightness control
  317. hsv.v = g_config.brightness;;
  318. rgb = hsv_to_rgb( hsv );
  319. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  320. }
  321. }
  322. }
  323. void backlight_effect_cycle_all(void)
  324. {
  325. uint8_t offset = g_tick & 0xFF;
  326. rgb_led led;
  327. // Relies on hue being 8-bit and wrapping
  328. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  329. {
  330. // map_index_to_led(i, &led);
  331. led = g_rgb_leds[i];
  332. if (led.matrix_co.raw < 0xFF) {
  333. uint16_t offset2 = g_key_hit[i]<<2;
  334. offset2 = (offset2<=63) ? (63-offset2) : 0;
  335. HSV hsv = { .h = offset+offset2, .s = 255, .v = g_config.brightness };
  336. RGB rgb = hsv_to_rgb( hsv );
  337. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  338. }
  339. }
  340. }
  341. void backlight_effect_cycle_left_right(void)
  342. {
  343. uint8_t offset = g_tick & 0xFF;
  344. HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
  345. RGB rgb;
  346. Point point;
  347. rgb_led led;
  348. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  349. {
  350. // map_index_to_led(i, &led);
  351. led = g_rgb_leds[i];
  352. if (led.matrix_co.raw < 0xFF) {
  353. uint16_t offset2 = g_key_hit[i]<<2;
  354. offset2 = (offset2<=63) ? (63-offset2) : 0;
  355. // map_led_to_point( i, &point );
  356. point = g_rgb_leds[i].point;
  357. // Relies on hue being 8-bit and wrapping
  358. hsv.h = point.x + offset + offset2;
  359. rgb = hsv_to_rgb( hsv );
  360. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  361. }
  362. }
  363. }
  364. void backlight_effect_cycle_up_down(void)
  365. {
  366. uint8_t offset = g_tick & 0xFF;
  367. HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
  368. RGB rgb;
  369. Point point;
  370. rgb_led led;
  371. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  372. {
  373. // map_index_to_led(i, &led);
  374. led = g_rgb_leds[i];
  375. if (led.matrix_co.raw < 0xFF) {
  376. uint16_t offset2 = g_key_hit[i]<<2;
  377. offset2 = (offset2<=63) ? (63-offset2) : 0;
  378. // map_led_to_point( i, &point );
  379. point = g_rgb_leds[i].point;
  380. // Relies on hue being 8-bit and wrapping
  381. hsv.h = point.y + offset + offset2;
  382. rgb = hsv_to_rgb( hsv );
  383. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  384. }
  385. }
  386. }
  387. void backlight_effect_dual_beacon(void) {
  388. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  389. RGB rgb;
  390. rgb_led led;
  391. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  392. led = g_rgb_leds[i];
  393. hsv.h = ((led.point.y - 32.0)* cos(g_tick * PI / 128) / 32 + (led.point.x - 112.0) * sin(g_tick * PI / 128) / (112)) * (g_config.color_2.h - g_config.color_1.h) + g_config.color_1.h;
  394. rgb = hsv_to_rgb( hsv );
  395. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  396. }
  397. }
  398. void backlight_effect_rainbow_beacon(void) {
  399. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  400. RGB rgb;
  401. rgb_led led;
  402. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  403. led = g_rgb_leds[i];
  404. hsv.h = 1.5 * (led.point.y - 32.0)* cos(g_tick * PI / 128) + 1.5 * (led.point.x - 112.0) * sin(g_tick * PI / 128) + g_config.color_1.h;
  405. rgb = hsv_to_rgb( hsv );
  406. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  407. }
  408. }
  409. void backlight_effect_rainbow_pinwheels(void) {
  410. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  411. RGB rgb;
  412. rgb_led led;
  413. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  414. led = g_rgb_leds[i];
  415. hsv.h = 2 * (led.point.y - 32.0)* cos(g_tick * PI / 128) + 2 * (66 - abs(led.point.x - 112.0)) * sin(g_tick * PI / 128) + g_config.color_1.h;
  416. rgb = hsv_to_rgb( hsv );
  417. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  418. }
  419. }
  420. void backlight_effect_rainbow_moving_chevron(void) {
  421. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  422. RGB rgb;
  423. rgb_led led;
  424. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  425. led = g_rgb_leds[i];
  426. // uint8_t r = g_tick;
  427. uint8_t r = 32;
  428. hsv.h = 1.5 * abs(led.point.y - 32.0)* sin(r * PI / 128) + 1.5 * (led.point.x - (g_tick / 256.0 * 224)) * cos(r * PI / 128) + g_config.color_1.h;
  429. rgb = hsv_to_rgb( hsv );
  430. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  431. }
  432. }
  433. void backlight_effect_jellybean_raindrops( bool initialize )
  434. {
  435. HSV hsv;
  436. RGB rgb;
  437. // Change one LED every tick
  438. uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255;
  439. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  440. {
  441. // If initialize, all get set to random colors
  442. // If not, all but one will stay the same as before.
  443. if ( initialize || i == led_to_change )
  444. {
  445. hsv.h = rand() & 0xFF;
  446. hsv.s = rand() & 0xFF;
  447. // Override brightness with global brightness control
  448. hsv.v = g_config.brightness;;
  449. rgb = hsv_to_rgb( hsv );
  450. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  451. }
  452. }
  453. }
  454. void backlight_effect_multisplash(void) {
  455. // if (g_any_key_hit < 0xFF) {
  456. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  457. RGB rgb;
  458. rgb_led led;
  459. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  460. led = g_rgb_leds[i];
  461. uint16_t c = 0, d = 0;
  462. rgb_led last_led;
  463. // if (g_last_led_count) {
  464. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  465. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  466. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  467. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  468. c += MIN(MAX(effect, 0), 255);
  469. d += 255 - MIN(MAX(effect, 0), 255);
  470. }
  471. // } else {
  472. // d = 255;
  473. // }
  474. hsv.h = (g_config.color_1.h + c) % 256;
  475. hsv.v = MAX(MIN(d, 255), 0);
  476. rgb = hsv_to_rgb( hsv );
  477. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  478. }
  479. // } else {
  480. // backlight_set_color_all( 0, 0, 0 );
  481. // }
  482. }
  483. void backlight_effect_splash(void) {
  484. g_last_led_count = MIN(g_last_led_count, 1);
  485. backlight_effect_multisplash();
  486. }
  487. void backlight_effect_solid_multisplash(void) {
  488. // if (g_any_key_hit < 0xFF) {
  489. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  490. RGB rgb;
  491. rgb_led led;
  492. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  493. led = g_rgb_leds[i];
  494. uint16_t d = 0;
  495. rgb_led last_led;
  496. // if (g_last_led_count) {
  497. for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
  498. last_led = g_rgb_leds[g_last_led_hit[last_i]];
  499. uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
  500. uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
  501. d += 255 - MIN(MAX(effect, 0), 255);
  502. }
  503. // } else {
  504. // d = 255;
  505. // }
  506. hsv.v = MAX(MIN(d, 255), 0);
  507. rgb = hsv_to_rgb( hsv );
  508. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  509. }
  510. // } else {
  511. // backlight_set_color_all( 0, 0, 0 );
  512. // }
  513. }
  514. void backlight_effect_solid_splash(void) {
  515. g_last_led_count = MIN(g_last_led_count, 1);
  516. backlight_effect_solid_multisplash();
  517. }
  518. void backlight_effect_custom(void)
  519. {
  520. HSV hsv;
  521. RGB rgb;
  522. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  523. {
  524. backlight_get_key_color(i, &hsv);
  525. // Override brightness with global brightness control
  526. hsv.v = g_config.brightness;
  527. rgb = hsv_to_rgb( hsv );
  528. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  529. }
  530. }
  531. void backlight_effect_indicators_set_colors( uint8_t index, HSV hsv )
  532. {
  533. RGB rgb = hsv_to_rgb( hsv );
  534. if ( index == 254 )
  535. {
  536. backlight_set_color_all( rgb.r, rgb.g, rgb.b );
  537. }
  538. else
  539. {
  540. backlight_set_color( index, rgb.r, rgb.g, rgb.b );
  541. // If the spacebar LED is the indicator,
  542. // do the same for the spacebar stabilizers
  543. if ( index == 36+0 ) // LC0
  544. {
  545. #ifdef CONFIG_ZEAL65
  546. backlight_set_color( 36+7, rgb.r, rgb.g, rgb.b ); // LC7
  547. backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
  548. #else
  549. backlight_set_color( 36+6, rgb.r, rgb.g, rgb.b ); // LC6
  550. backlight_set_color( 54+13, rgb.r, rgb.g, rgb.b ); // LD13
  551. if ( g_config.use_7u_spacebar )
  552. {
  553. backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
  554. }
  555. #endif
  556. }
  557. }
  558. }
  559. // This runs after another backlight effect and replaces
  560. // colors already set
  561. void backlight_effect_indicators(void)
  562. {
  563. if ( g_config.caps_lock_indicator.index != 255 &&
  564. ( g_indicator_state & (1<<USB_LED_CAPS_LOCK) ) )
  565. {
  566. backlight_effect_indicators_set_colors( g_config.caps_lock_indicator.index, g_config.caps_lock_indicator.color );
  567. }
  568. // This if/else if structure allows higher layers to
  569. // override lower ones. If we set layer 3's indicator
  570. // to none, then it will NOT show layer 2 or layer 1
  571. // indicators, even if those layers are on via the
  572. // MO13/MO23 Fn combo magic.
  573. //
  574. // Basically we want to handle the case where layer 3 is
  575. // still the backlight configuration layer and we don't
  576. // want "all LEDs" indicators hiding the backlight effect,
  577. // but still allow end users to do whatever they want.
  578. if ( IS_LAYER_ON(3) )
  579. {
  580. if ( g_config.layer_3_indicator.index != 255 )
  581. {
  582. backlight_effect_indicators_set_colors( g_config.layer_3_indicator.index, g_config.layer_3_indicator.color );
  583. }
  584. }
  585. else if ( IS_LAYER_ON(2) )
  586. {
  587. if ( g_config.layer_2_indicator.index != 255 )
  588. {
  589. backlight_effect_indicators_set_colors( g_config.layer_2_indicator.index, g_config.layer_2_indicator.color );
  590. }
  591. }
  592. else if ( IS_LAYER_ON(1) )
  593. {
  594. if ( g_config.layer_1_indicator.index != 255 )
  595. {
  596. backlight_effect_indicators_set_colors( g_config.layer_1_indicator.index, g_config.layer_1_indicator.color );
  597. }
  598. }
  599. }
  600. void backlight_rgb_task(void) {
  601. if (!g_config.enabled) {
  602. backlight_effect_all_off();
  603. return;
  604. }
  605. // delay 1 second before driving LEDs or doing anything else
  606. static uint8_t startup_tick = 0;
  607. if ( startup_tick < 20 )
  608. {
  609. startup_tick++;
  610. return;
  611. }
  612. g_tick++;
  613. if ( g_any_key_hit < 0xFFFFFFFF )
  614. {
  615. g_any_key_hit++;
  616. }
  617. for ( int led = 0; led < DRIVER_LED_TOTAL; led++ )
  618. {
  619. if ( g_key_hit[led] < 255 )
  620. {
  621. if (g_key_hit[led] == 254)
  622. g_last_led_count = MAX(g_last_led_count - 1, 0);
  623. g_key_hit[led]++;
  624. }
  625. }
  626. // Factory default magic value
  627. if ( g_config.effect == 255 )
  628. {
  629. backlight_effect_rgb_test();
  630. return;
  631. }
  632. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  633. // while suspended and just do a software shutdown. This is a cheap hack for now.
  634. bool suspend_backlight = ((g_suspend_state && g_config.disable_when_usb_suspended) ||
  635. (g_config.disable_after_timeout > 0 && g_any_key_hit > g_config.disable_after_timeout * 60 * 20));
  636. uint8_t effect = suspend_backlight ? 0 : g_config.effect;
  637. // Keep track of the effect used last time,
  638. // detect change in effect, so each effect can
  639. // have an optional initialization.
  640. static uint8_t effect_last = 255;
  641. bool initialize = effect != effect_last;
  642. effect_last = effect;
  643. // this gets ticked at 20 Hz.
  644. // each effect can opt to do calculations
  645. // and/or request PWM buffer updates.
  646. switch ( effect )
  647. {
  648. case 0:
  649. backlight_effect_solid_color();
  650. break;
  651. case 1:
  652. backlight_effect_solid_reactive();
  653. break;
  654. case 2:
  655. backlight_effect_alphas_mods();
  656. break;
  657. case 3:
  658. backlight_effect_dual_beacon();
  659. break;
  660. case 4:
  661. backlight_effect_gradient_up_down();
  662. break;
  663. case 5:
  664. backlight_effect_raindrops( initialize );
  665. break;
  666. case 6:
  667. backlight_effect_cycle_all();
  668. break;
  669. case 7:
  670. backlight_effect_cycle_left_right();
  671. break;
  672. case 8:
  673. backlight_effect_cycle_up_down();
  674. break;
  675. case 9:
  676. backlight_effect_rainbow_beacon();
  677. break;
  678. case 10:
  679. backlight_effect_rainbow_pinwheels();
  680. break;
  681. case 11:
  682. backlight_effect_rainbow_moving_chevron();
  683. break;
  684. case 12:
  685. backlight_effect_jellybean_raindrops( initialize );
  686. break;
  687. case 13:
  688. backlight_effect_splash();
  689. break;
  690. case 14:
  691. backlight_effect_multisplash();
  692. break;
  693. case 15:
  694. backlight_effect_solid_splash();
  695. break;
  696. case 16:
  697. backlight_effect_solid_multisplash();
  698. break;
  699. case 17:
  700. default:
  701. backlight_effect_custom();
  702. break;
  703. }
  704. if ( ! suspend_backlight )
  705. {
  706. backlight_effect_indicators();
  707. }
  708. }
  709. // void backlight_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
  710. // {
  711. // if ( row >= MATRIX_ROWS )
  712. // {
  713. // // Special value, 255=none, 254=all
  714. // *index = row;
  715. // }
  716. // else
  717. // {
  718. // // This needs updated to something like
  719. // // uint8_t led[8], led_count;
  720. // // map_row_column_to_led(row,column,led,&led_count);
  721. // // for(uint8_t i = 0; i < led_count; i++)
  722. // map_row_column_to_led( row, column, index );
  723. // }
  724. // }
  725. void backlight_config_load(void)
  726. {
  727. eeprom_read_block( &g_config, EEPROM_BACKLIGHT_CONFIG_ADDR, sizeof(rgb_matrix_config) );
  728. }
  729. void backlight_config_save(void)
  730. {
  731. eeprom_update_block( &g_config, EEPROM_BACKLIGHT_CONFIG_ADDR, sizeof(rgb_matrix_config) );
  732. }
  733. void backlight_init_drivers(void)
  734. {
  735. //sei();
  736. // Initialize TWI
  737. TWIInit();
  738. IS31FL3731_init( DRIVER_ADDR_1 );
  739. IS31FL3731_init( DRIVER_ADDR_2 );
  740. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ )
  741. {
  742. bool enabled = true;
  743. // This only caches it for later
  744. IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
  745. }
  746. // This actually updates the LED drivers
  747. IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
  748. // TODO: put the 1 second startup delay here?
  749. // clear the key hits
  750. for ( int led=0; led<DRIVER_LED_TOTAL; led++ )
  751. {
  752. g_key_hit[led] = 255;
  753. }
  754. }
  755. // Deals with the messy details of incrementing an integer
  756. uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
  757. {
  758. int16_t new_value = value;
  759. new_value += step;
  760. return MIN( MAX( new_value, min ), max );
  761. }
  762. uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
  763. {
  764. int16_t new_value = value;
  765. new_value -= step;
  766. return MIN( MAX( new_value, min ), max );
  767. }
  768. void backlight_effect_increase(void)
  769. {
  770. g_config.effect = increment( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
  771. backlight_config_save();
  772. }
  773. void backlight_effect_decrease(void)
  774. {
  775. g_config.effect = decrement( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
  776. backlight_config_save();
  777. }
  778. void backlight_brightness_increase(void)
  779. {
  780. g_config.brightness = increment( g_config.brightness, 8, 0, 255 );
  781. backlight_config_save();
  782. }
  783. void backlight_brightness_decrease(void)
  784. {
  785. g_config.brightness = decrement( g_config.brightness, 8, 0, 255 );
  786. backlight_config_save();
  787. }
  788. void backlight_color_1_hue_increase(void)
  789. {
  790. g_config.color_1.h = increment( g_config.color_1.h, 8, 0, 255 );
  791. backlight_config_save();
  792. }
  793. void backlight_color_1_hue_decrease(void)
  794. {
  795. g_config.color_1.h = decrement( g_config.color_1.h, 8, 0, 255 );
  796. backlight_config_save();
  797. }
  798. void backlight_color_1_sat_increase(void)
  799. {
  800. g_config.color_1.s = increment( g_config.color_1.s, 8, 0, 255 );
  801. backlight_config_save();
  802. }
  803. void backlight_color_1_sat_decrease(void)
  804. {
  805. g_config.color_1.s = decrement( g_config.color_1.s, 8, 0, 255 );
  806. backlight_config_save();
  807. }
  808. void backlight_color_2_hue_increase(void)
  809. {
  810. g_config.color_2.h = increment( g_config.color_2.h, 8, 0, 255 );
  811. backlight_config_save();
  812. }
  813. void backlight_color_2_hue_decrease(void)
  814. {
  815. g_config.color_2.h = decrement( g_config.color_2.h, 8, 0, 255 );
  816. backlight_config_save();
  817. }
  818. void backlight_color_2_sat_increase(void)
  819. {
  820. g_config.color_2.s = increment( g_config.color_2.s, 8, 0, 255 );
  821. backlight_config_save();
  822. }
  823. void backlight_color_2_sat_decrease(void)
  824. {
  825. g_config.color_2.s = decrement( g_config.color_2.s, 8, 0, 255 );
  826. backlight_config_save();
  827. }
  828. void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
  829. {
  830. // 3 bytes per color
  831. return EEPROM_BACKLIGHT_KEY_COLOR_ADDR + ( led * 3 );
  832. }
  833. void backlight_get_key_color( uint8_t led, HSV *hsv )
  834. {
  835. void *address = backlight_get_custom_key_color_eeprom_address( led );
  836. hsv->h = eeprom_read_byte(address);
  837. hsv->s = eeprom_read_byte(address+1);
  838. hsv->v = eeprom_read_byte(address+2);
  839. }
  840. void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
  841. {
  842. uint8_t led[8], led_count;
  843. map_row_column_to_led(row,column,led,&led_count);
  844. for(uint8_t i = 0; i < led_count; i++) {
  845. if ( led[i] < DRIVER_LED_TOTAL )
  846. {
  847. void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
  848. eeprom_update_byte(address, hsv.h);
  849. eeprom_update_byte(address+1, hsv.s);
  850. eeprom_update_byte(address+2, hsv.v);
  851. }
  852. }
  853. }
  854. void backlight_test_led( uint8_t index, bool red, bool green, bool blue )
  855. {
  856. for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
  857. {
  858. if ( i == index )
  859. {
  860. IS31FL3731_set_led_control_register( i, red, green, blue );
  861. }
  862. else
  863. {
  864. IS31FL3731_set_led_control_register( i, false, false, false );
  865. }
  866. }
  867. }
  868. uint32_t backlight_get_tick(void)
  869. {
  870. return g_tick;
  871. }
  872. void backlight_debug_led( bool state )
  873. {
  874. // if (state)
  875. // {
  876. // // Output high.
  877. // DDRD |= (1<<6);
  878. // PORTD |= (1<<6);
  879. // }
  880. // else
  881. // {
  882. // // Output low.
  883. // DDRD &= ~(1<<6);
  884. // PORTD &= ~(1<<6);
  885. // }
  886. }
  887. void rgblight_toggle(void) {
  888. g_config.enabled ^= 1;
  889. backlight_config_save();
  890. }
  891. void rgblight_step(void) {
  892. g_config.effect = (g_config.effect + 1) % (BACKLIGHT_EFFECT_MAX + 1);
  893. backlight_config_save();
  894. }
  895. void rgblight_step_reverse(void) {
  896. g_config.effect = (g_config.effect - 1) % (BACKLIGHT_EFFECT_MAX + 1);
  897. backlight_config_save();
  898. }
  899. void rgblight_increase_hue(void) {
  900. backlight_color_1_hue_increase();
  901. backlight_color_2_hue_increase();
  902. }
  903. void rgblight_decrease_hue(void) {
  904. backlight_color_1_hue_decrease();
  905. backlight_color_2_hue_decrease();
  906. }
  907. void rgblight_increase_sat(void) {
  908. backlight_color_1_sat_increase();
  909. backlight_color_2_sat_increase();
  910. }
  911. void rgblight_decrease_sat(void) {
  912. backlight_color_1_sat_decrease();
  913. backlight_color_2_sat_decrease();
  914. }
  915. void rgblight_increase_val(void) {
  916. g_config.color_1.v = increment( g_config.color_1.s, 8, 0, 255 );
  917. g_config.color_2.v = increment( g_config.color_1.s, 8, 0, 255 );
  918. backlight_config_save();
  919. }
  920. void rgblight_decrease_val(void) {
  921. g_config.color_1.v = decrement( g_config.color_1.s, 8, 0, 255 );
  922. g_config.color_2.v = decrement( g_config.color_1.s, 8, 0, 255 );
  923. backlight_config_save();
  924. }
  925. void rgblight_mode(uint8_t mode) {
  926. g_config.effect = mode;
  927. }
  928. uint32_t rgblight_get_mode(void) {
  929. return g_config.effect;
  930. }