You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

611 lines
17 KiB

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