rgblight.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* Copyright 2016-2017 Yang Liu
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <math.h>
  17. #include <avr/eeprom.h>
  18. #include <avr/interrupt.h>
  19. #include <util/delay.h>
  20. #include "progmem.h"
  21. #include "timer.h"
  22. #include "rgblight.h"
  23. #include "debug.h"
  24. #include "led_tables.h"
  25. __attribute__ ((weak))
  26. const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  27. __attribute__ ((weak))
  28. const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  29. __attribute__ ((weak))
  30. const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  31. __attribute__ ((weak))
  32. const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  33. __attribute__ ((weak))
  34. const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
  35. __attribute__ ((weak))
  36. const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
  37. rgblight_config_t rgblight_config;
  38. rgblight_config_t inmem_config;
  39. LED_TYPE led[RGBLED_NUM];
  40. uint8_t rgblight_inited = 0;
  41. bool rgblight_timer_enabled = false;
  42. void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
  43. uint8_t r = 0, g = 0, b = 0, base, color;
  44. if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
  45. r = val;
  46. g = val;
  47. b = val;
  48. } else {
  49. base = ((255 - sat) * val) >> 8;
  50. color = (val - base) * (hue % 60) / 60;
  51. switch (hue / 60) {
  52. case 0:
  53. r = val;
  54. g = base + color;
  55. b = base;
  56. break;
  57. case 1:
  58. r = val - color;
  59. g = val;
  60. b = base;
  61. break;
  62. case 2:
  63. r = base;
  64. g = val;
  65. b = base + color;
  66. break;
  67. case 3:
  68. r = base;
  69. g = val - color;
  70. b = val;
  71. break;
  72. case 4:
  73. r = base + color;
  74. g = base;
  75. b = val;
  76. break;
  77. case 5:
  78. r = val;
  79. g = base;
  80. b = val - color;
  81. break;
  82. }
  83. }
  84. r = pgm_read_byte(&CIE1931_CURVE[r]);
  85. g = pgm_read_byte(&CIE1931_CURVE[g]);
  86. b = pgm_read_byte(&CIE1931_CURVE[b]);
  87. setrgb(r, g, b, led1);
  88. }
  89. void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
  90. (*led1).r = r;
  91. (*led1).g = g;
  92. (*led1).b = b;
  93. }
  94. uint32_t eeconfig_read_rgblight(void) {
  95. return eeprom_read_dword(EECONFIG_RGBLIGHT);
  96. }
  97. void eeconfig_update_rgblight(uint32_t val) {
  98. eeprom_update_dword(EECONFIG_RGBLIGHT, val);
  99. }
  100. void eeconfig_update_rgblight_default(void) {
  101. dprintf("eeconfig_update_rgblight_default\n");
  102. rgblight_config.enable = 1;
  103. rgblight_config.mode = 1;
  104. rgblight_config.hue = 0;
  105. rgblight_config.sat = 255;
  106. rgblight_config.val = 255;
  107. eeconfig_update_rgblight(rgblight_config.raw);
  108. }
  109. void eeconfig_debug_rgblight(void) {
  110. dprintf("rgblight_config eprom\n");
  111. dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
  112. dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
  113. dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
  114. dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
  115. dprintf("rgblight_config.val = %d\n", rgblight_config.val);
  116. }
  117. void rgblight_init(void) {
  118. debug_enable = 1; // Debug ON!
  119. dprintf("rgblight_init called.\n");
  120. rgblight_inited = 1;
  121. dprintf("rgblight_init start!\n");
  122. if (!eeconfig_is_enabled()) {
  123. dprintf("rgblight_init eeconfig is not enabled.\n");
  124. eeconfig_init();
  125. eeconfig_update_rgblight_default();
  126. }
  127. rgblight_config.raw = eeconfig_read_rgblight();
  128. if (!rgblight_config.mode) {
  129. dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
  130. eeconfig_update_rgblight_default();
  131. rgblight_config.raw = eeconfig_read_rgblight();
  132. }
  133. eeconfig_debug_rgblight(); // display current eeprom values
  134. #ifdef RGBLIGHT_ANIMATIONS
  135. rgblight_timer_init(); // setup the timer
  136. #endif
  137. if (rgblight_config.enable) {
  138. rgblight_mode(rgblight_config.mode);
  139. }
  140. }
  141. void rgblight_update_dword(uint32_t dword) {
  142. rgblight_config.raw = dword;
  143. eeconfig_update_rgblight(rgblight_config.raw);
  144. if (rgblight_config.enable)
  145. rgblight_mode(rgblight_config.mode);
  146. else {
  147. #ifdef RGBLIGHT_ANIMATIONS
  148. rgblight_timer_disable();
  149. #endif
  150. rgblight_set();
  151. }
  152. }
  153. void rgblight_increase(void) {
  154. uint8_t mode = 0;
  155. if (rgblight_config.mode < RGBLIGHT_MODES) {
  156. mode = rgblight_config.mode + 1;
  157. }
  158. rgblight_mode(mode);
  159. }
  160. void rgblight_decrease(void) {
  161. uint8_t mode = 0;
  162. // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
  163. if (rgblight_config.mode > 1) {
  164. mode = rgblight_config.mode - 1;
  165. }
  166. rgblight_mode(mode);
  167. }
  168. void rgblight_step(void) {
  169. uint8_t mode = 0;
  170. mode = rgblight_config.mode + 1;
  171. if (mode > RGBLIGHT_MODES) {
  172. mode = 1;
  173. }
  174. rgblight_mode(mode);
  175. }
  176. void rgblight_step_reverse(void) {
  177. uint8_t mode = 0;
  178. mode = rgblight_config.mode - 1;
  179. if (mode < 1) {
  180. mode = RGBLIGHT_MODES;
  181. }
  182. rgblight_mode(mode);
  183. }
  184. uint32_t rgblight_get_mode(void) {
  185. if (!rgblight_config.enable) {
  186. return false;
  187. }
  188. return rgblight_config.mode;
  189. }
  190. void rgblight_mode(uint8_t mode) {
  191. if (!rgblight_config.enable) {
  192. return;
  193. }
  194. if (mode < 1) {
  195. rgblight_config.mode = 1;
  196. } else if (mode > RGBLIGHT_MODES) {
  197. rgblight_config.mode = RGBLIGHT_MODES;
  198. } else {
  199. rgblight_config.mode = mode;
  200. }
  201. eeconfig_update_rgblight(rgblight_config.raw);
  202. xprintf("rgblight mode: %u\n", rgblight_config.mode);
  203. if (rgblight_config.mode == 1) {
  204. #ifdef RGBLIGHT_ANIMATIONS
  205. rgblight_timer_disable();
  206. #endif
  207. } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 24) {
  208. // MODE 2-5, breathing
  209. // MODE 6-8, rainbow mood
  210. // MODE 9-14, rainbow swirl
  211. // MODE 15-20, snake
  212. // MODE 21-23, knight
  213. // MODE 24, xmas
  214. // MODE 25-34, static rainbow
  215. #ifdef RGBLIGHT_ANIMATIONS
  216. rgblight_timer_enable();
  217. #endif
  218. } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
  219. // MODE 25-34, static gradient
  220. #ifdef RGBLIGHT_ANIMATIONS
  221. rgblight_timer_disable();
  222. #endif
  223. }
  224. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  225. }
  226. void rgblight_toggle(void) {
  227. rgblight_config.enable ^= 1;
  228. eeconfig_update_rgblight(rgblight_config.raw);
  229. xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
  230. if (rgblight_config.enable) {
  231. rgblight_mode(rgblight_config.mode);
  232. } else {
  233. #ifdef RGBLIGHT_ANIMATIONS
  234. rgblight_timer_disable();
  235. #endif
  236. _delay_ms(50);
  237. rgblight_set();
  238. }
  239. }
  240. void rgblight_enable(void) {
  241. rgblight_config.enable = 1;
  242. eeconfig_update_rgblight(rgblight_config.raw);
  243. xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable);
  244. rgblight_mode(rgblight_config.mode);
  245. }
  246. void rgblight_increase_hue(void) {
  247. uint16_t hue;
  248. hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
  249. rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
  250. }
  251. void rgblight_decrease_hue(void) {
  252. uint16_t hue;
  253. if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
  254. hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
  255. } else {
  256. hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
  257. }
  258. rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
  259. }
  260. void rgblight_increase_sat(void) {
  261. uint8_t sat;
  262. if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
  263. sat = 255;
  264. } else {
  265. sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
  266. }
  267. rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
  268. }
  269. void rgblight_decrease_sat(void) {
  270. uint8_t sat;
  271. if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
  272. sat = 0;
  273. } else {
  274. sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
  275. }
  276. rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
  277. }
  278. void rgblight_increase_val(void) {
  279. uint8_t val;
  280. if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
  281. val = 255;
  282. } else {
  283. val = rgblight_config.val + RGBLIGHT_VAL_STEP;
  284. }
  285. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
  286. }
  287. void rgblight_decrease_val(void) {
  288. uint8_t val;
  289. if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
  290. val = 0;
  291. } else {
  292. val = rgblight_config.val - RGBLIGHT_VAL_STEP;
  293. }
  294. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
  295. }
  296. void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  297. inmem_config.raw = rgblight_config.raw;
  298. if (rgblight_config.enable) {
  299. LED_TYPE tmp_led;
  300. sethsv(hue, sat, val, &tmp_led);
  301. inmem_config.hue = hue;
  302. inmem_config.sat = sat;
  303. inmem_config.val = val;
  304. // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
  305. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  306. }
  307. }
  308. void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  309. if (rgblight_config.enable) {
  310. if (rgblight_config.mode == 1) {
  311. // same static color
  312. rgblight_sethsv_noeeprom(hue, sat, val);
  313. } else {
  314. // all LEDs in same color
  315. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  316. // breathing mode, ignore the change of val, use in memory value instead
  317. val = rgblight_config.val;
  318. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
  319. // rainbow mood and rainbow swirl, ignore the change of hue
  320. hue = rgblight_config.hue;
  321. } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
  322. // static gradient
  323. uint16_t _hue;
  324. int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1;
  325. uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]);
  326. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  327. _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
  328. dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
  329. sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
  330. }
  331. rgblight_set();
  332. }
  333. }
  334. rgblight_config.hue = hue;
  335. rgblight_config.sat = sat;
  336. rgblight_config.val = val;
  337. eeconfig_update_rgblight(rgblight_config.raw);
  338. xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  339. }
  340. }
  341. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  342. // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b);
  343. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  344. led[i].r = r;
  345. led[i].g = g;
  346. led[i].b = b;
  347. }
  348. rgblight_set();
  349. }
  350. #ifndef RGBLIGHT_CUSTOM_DRIVER
  351. void rgblight_set(void) {
  352. if (rgblight_config.enable) {
  353. #ifdef RGBW
  354. ws2812_setleds_rgbw(led, RGBLED_NUM);
  355. #else
  356. ws2812_setleds(led, RGBLED_NUM);
  357. #endif
  358. } else {
  359. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  360. led[i].r = 0;
  361. led[i].g = 0;
  362. led[i].b = 0;
  363. }
  364. #ifdef RGBW
  365. ws2812_setleds_rgbw(led, RGBLED_NUM);
  366. #else
  367. ws2812_setleds(led, RGBLED_NUM);
  368. #endif
  369. }
  370. }
  371. #endif
  372. #ifdef RGBLIGHT_ANIMATIONS
  373. // Animation timer -- AVR Timer3
  374. void rgblight_timer_init(void) {
  375. // static uint8_t rgblight_timer_is_init = 0;
  376. // if (rgblight_timer_is_init) {
  377. // return;
  378. // }
  379. // rgblight_timer_is_init = 1;
  380. // /* Timer 3 setup */
  381. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  382. // | _BV(CS30); // Clock selelct: clk/1
  383. // /* Set TOP value */
  384. // uint8_t sreg = SREG;
  385. // cli();
  386. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  387. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  388. // SREG = sreg;
  389. rgblight_timer_enabled = true;
  390. }
  391. void rgblight_timer_enable(void) {
  392. rgblight_timer_enabled = true;
  393. dprintf("TIMER3 enabled.\n");
  394. }
  395. void rgblight_timer_disable(void) {
  396. rgblight_timer_enabled = false;
  397. dprintf("TIMER3 disabled.\n");
  398. }
  399. void rgblight_timer_toggle(void) {
  400. rgblight_timer_enabled ^= rgblight_timer_enabled;
  401. dprintf("TIMER3 toggled.\n");
  402. }
  403. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  404. rgblight_enable();
  405. rgblight_mode(1);
  406. rgblight_setrgb(r, g, b);
  407. }
  408. void rgblight_task(void) {
  409. if (rgblight_timer_enabled) {
  410. // mode = 1, static light, do nothing here
  411. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  412. // mode = 2 to 5, breathing mode
  413. rgblight_effect_breathing(rgblight_config.mode - 2);
  414. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
  415. // mode = 6 to 8, rainbow mood mod
  416. rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
  417. } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
  418. // mode = 9 to 14, rainbow swirl mode
  419. rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
  420. } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
  421. // mode = 15 to 20, snake mode
  422. rgblight_effect_snake(rgblight_config.mode - 15);
  423. } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
  424. // mode = 21 to 23, knight mode
  425. rgblight_effect_knight(rgblight_config.mode - 21);
  426. } else if (rgblight_config.mode == 24) {
  427. // mode = 24, christmas mode
  428. rgblight_effect_christmas();
  429. }
  430. }
  431. }
  432. // Effects
  433. void rgblight_effect_breathing(uint8_t interval) {
  434. static uint8_t pos = 0;
  435. static uint16_t last_timer = 0;
  436. float val;
  437. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
  438. return;
  439. }
  440. last_timer = timer_read();
  441. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  442. val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
  443. rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, val);
  444. pos = (pos + 1) % 256;
  445. }
  446. void rgblight_effect_rainbow_mood(uint8_t interval) {
  447. static uint16_t current_hue = 0;
  448. static uint16_t last_timer = 0;
  449. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
  450. return;
  451. }
  452. last_timer = timer_read();
  453. rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
  454. current_hue = (current_hue + 1) % 360;
  455. }
  456. void rgblight_effect_rainbow_swirl(uint8_t interval) {
  457. static uint16_t current_hue = 0;
  458. static uint16_t last_timer = 0;
  459. uint16_t hue;
  460. uint8_t i;
  461. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
  462. return;
  463. }
  464. last_timer = timer_read();
  465. for (i = 0; i < RGBLED_NUM; i++) {
  466. hue = (360 / RGBLED_NUM * i + current_hue) % 360;
  467. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  468. }
  469. rgblight_set();
  470. if (interval % 2) {
  471. current_hue = (current_hue + 1) % 360;
  472. } else {
  473. if (current_hue - 1 < 0) {
  474. current_hue = 359;
  475. } else {
  476. current_hue = current_hue - 1;
  477. }
  478. }
  479. }
  480. void rgblight_effect_snake(uint8_t interval) {
  481. static uint8_t pos = 0;
  482. static uint16_t last_timer = 0;
  483. uint8_t i, j;
  484. int8_t k;
  485. int8_t increment = 1;
  486. if (interval % 2) {
  487. increment = -1;
  488. }
  489. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
  490. return;
  491. }
  492. last_timer = timer_read();
  493. for (i = 0; i < RGBLED_NUM; i++) {
  494. led[i].r = 0;
  495. led[i].g = 0;
  496. led[i].b = 0;
  497. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  498. k = pos + j * increment;
  499. if (k < 0) {
  500. k = k + RGBLED_NUM;
  501. }
  502. if (i == k) {
  503. sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), (LED_TYPE *)&led[i]);
  504. }
  505. }
  506. }
  507. rgblight_set();
  508. if (increment == 1) {
  509. if (pos - 1 < 0) {
  510. pos = RGBLED_NUM - 1;
  511. } else {
  512. pos -= 1;
  513. }
  514. } else {
  515. pos = (pos + 1) % RGBLED_NUM;
  516. }
  517. }
  518. void rgblight_effect_knight(uint8_t interval) {
  519. static uint16_t last_timer = 0;
  520. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
  521. return;
  522. }
  523. last_timer = timer_read();
  524. static int8_t low_bound = 0;
  525. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  526. static int8_t increment = 1;
  527. uint8_t i, cur;
  528. // Set all the LEDs to 0
  529. for (i = 0; i < RGBLED_NUM; i++) {
  530. led[i].r = 0;
  531. led[i].g = 0;
  532. led[i].b = 0;
  533. }
  534. // Determine which LEDs should be lit up
  535. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  536. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
  537. if (i >= low_bound && i <= high_bound) {
  538. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  539. } else {
  540. led[cur].r = 0;
  541. led[cur].g = 0;
  542. led[cur].b = 0;
  543. }
  544. }
  545. rgblight_set();
  546. // Move from low_bound to high_bound changing the direction we increment each
  547. // time a boundary is hit.
  548. low_bound += increment;
  549. high_bound += increment;
  550. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  551. increment = -increment;
  552. }
  553. }
  554. void rgblight_effect_christmas(void) {
  555. static uint16_t current_offset = 0;
  556. static uint16_t last_timer = 0;
  557. uint16_t hue;
  558. uint8_t i;
  559. if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
  560. return;
  561. }
  562. last_timer = timer_read();
  563. current_offset = (current_offset + 1) % 2;
  564. for (i = 0; i < RGBLED_NUM; i++) {
  565. hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
  566. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  567. }
  568. rgblight_set();
  569. }
  570. #endif