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.

613 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
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 = {100, 50, 20};
  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. void rgblight_mode(uint8_t mode) {
  184. if (!rgblight_config.enable) {
  185. return;
  186. }
  187. if (mode < 1) {
  188. rgblight_config.mode = 1;
  189. } else if (mode > RGBLIGHT_MODES) {
  190. rgblight_config.mode = RGBLIGHT_MODES;
  191. } else {
  192. rgblight_config.mode = mode;
  193. }
  194. eeconfig_update_rgblight(rgblight_config.raw);
  195. xprintf("rgblight mode: %u\n", rgblight_config.mode);
  196. if (rgblight_config.mode == 1) {
  197. #ifdef RGBLIGHT_ANIMATIONS
  198. rgblight_timer_disable();
  199. #endif
  200. } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 24) {
  201. // MODE 2-5, breathing
  202. // MODE 6-8, rainbow mood
  203. // MODE 9-14, rainbow swirl
  204. // MODE 15-20, snake
  205. // MODE 21-23, knight
  206. #ifdef RGBLIGHT_ANIMATIONS
  207. rgblight_timer_enable();
  208. #endif
  209. } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
  210. // MODE 25-34, static gradient
  211. #ifdef RGBLIGHT_ANIMATIONS
  212. rgblight_timer_disable();
  213. #endif
  214. }
  215. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  216. }
  217. void rgblight_toggle(void) {
  218. rgblight_config.enable ^= 1;
  219. eeconfig_update_rgblight(rgblight_config.raw);
  220. xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
  221. if (rgblight_config.enable) {
  222. rgblight_mode(rgblight_config.mode);
  223. } else {
  224. #ifdef RGBLIGHT_ANIMATIONS
  225. rgblight_timer_disable();
  226. #endif
  227. _delay_ms(50);
  228. rgblight_set();
  229. }
  230. }
  231. void rgblight_enable(void) {
  232. rgblight_config.enable = 1;
  233. eeconfig_update_rgblight(rgblight_config.raw);
  234. xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable);
  235. rgblight_mode(rgblight_config.mode);
  236. }
  237. void rgblight_increase_hue(void) {
  238. uint16_t hue;
  239. hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
  240. rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
  241. }
  242. void rgblight_decrease_hue(void) {
  243. uint16_t hue;
  244. if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
  245. hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
  246. } else {
  247. hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
  248. }
  249. rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
  250. }
  251. void rgblight_increase_sat(void) {
  252. uint8_t sat;
  253. if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
  254. sat = 255;
  255. } else {
  256. sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
  257. }
  258. rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
  259. }
  260. void rgblight_decrease_sat(void) {
  261. uint8_t sat;
  262. if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
  263. sat = 0;
  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_increase_val(void) {
  270. uint8_t val;
  271. if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
  272. val = 255;
  273. } else {
  274. val = rgblight_config.val + RGBLIGHT_VAL_STEP;
  275. }
  276. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
  277. }
  278. void rgblight_decrease_val(void) {
  279. uint8_t val;
  280. if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
  281. val = 0;
  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_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  288. inmem_config.raw = rgblight_config.raw;
  289. if (rgblight_config.enable) {
  290. LED_TYPE tmp_led;
  291. sethsv(hue, sat, val, &tmp_led);
  292. inmem_config.hue = hue;
  293. inmem_config.sat = sat;
  294. inmem_config.val = val;
  295. // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
  296. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  297. }
  298. }
  299. void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  300. if (rgblight_config.enable) {
  301. if (rgblight_config.mode == 1) {
  302. // same static color
  303. rgblight_sethsv_noeeprom(hue, sat, val);
  304. } else {
  305. // all LEDs in same color
  306. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  307. // breathing mode, ignore the change of val, use in memory value instead
  308. val = rgblight_config.val;
  309. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
  310. // rainbow mood and rainbow swirl, ignore the change of hue
  311. hue = rgblight_config.hue;
  312. } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
  313. // static gradient
  314. uint16_t _hue;
  315. int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1;
  316. uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]);
  317. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  318. _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
  319. dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
  320. sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
  321. }
  322. rgblight_set();
  323. }
  324. }
  325. rgblight_config.hue = hue;
  326. rgblight_config.sat = sat;
  327. rgblight_config.val = val;
  328. eeconfig_update_rgblight(rgblight_config.raw);
  329. xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  330. }
  331. }
  332. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  333. // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b);
  334. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  335. led[i].r = r;
  336. led[i].g = g;
  337. led[i].b = b;
  338. }
  339. rgblight_set();
  340. }
  341. __attribute__ ((weak))
  342. void rgblight_set(void) {
  343. if (rgblight_config.enable) {
  344. #ifdef RGBW
  345. ws2812_setleds_rgbw(led, RGBLED_NUM);
  346. #else
  347. ws2812_setleds(led, RGBLED_NUM);
  348. #endif
  349. } else {
  350. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  351. led[i].r = 0;
  352. led[i].g = 0;
  353. led[i].b = 0;
  354. }
  355. #ifdef RGBW
  356. ws2812_setleds_rgbw(led, RGBLED_NUM);
  357. #else
  358. ws2812_setleds(led, RGBLED_NUM);
  359. #endif
  360. }
  361. }
  362. #ifdef RGBLIGHT_ANIMATIONS
  363. // Animation timer -- AVR Timer3
  364. void rgblight_timer_init(void) {
  365. // static uint8_t rgblight_timer_is_init = 0;
  366. // if (rgblight_timer_is_init) {
  367. // return;
  368. // }
  369. // rgblight_timer_is_init = 1;
  370. // /* Timer 3 setup */
  371. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  372. // | _BV(CS30); // Clock selelct: clk/1
  373. // /* Set TOP value */
  374. // uint8_t sreg = SREG;
  375. // cli();
  376. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  377. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  378. // SREG = sreg;
  379. rgblight_timer_enabled = true;
  380. }
  381. void rgblight_timer_enable(void) {
  382. rgblight_timer_enabled = true;
  383. dprintf("TIMER3 enabled.\n");
  384. }
  385. void rgblight_timer_disable(void) {
  386. rgblight_timer_enabled = false;
  387. dprintf("TIMER3 disabled.\n");
  388. }
  389. void rgblight_timer_toggle(void) {
  390. rgblight_timer_enabled ^= rgblight_timer_enabled;
  391. dprintf("TIMER3 toggled.\n");
  392. }
  393. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  394. rgblight_enable();
  395. rgblight_mode(1);
  396. rgblight_setrgb(r, g, b);
  397. }
  398. void rgblight_task(void) {
  399. if (rgblight_timer_enabled) {
  400. // mode = 1, static light, do nothing here
  401. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  402. // mode = 2 to 5, breathing mode
  403. rgblight_effect_breathing(rgblight_config.mode - 2);
  404. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
  405. // mode = 6 to 8, rainbow mood mod
  406. rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
  407. } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
  408. // mode = 9 to 14, rainbow swirl mode
  409. rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
  410. } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
  411. // mode = 15 to 20, snake mode
  412. rgblight_effect_snake(rgblight_config.mode - 15);
  413. } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
  414. // mode = 21 to 23, knight mode
  415. rgblight_effect_knight(rgblight_config.mode - 21);
  416. } else if (rgblight_config.mode == 24) {
  417. // mode = 24, christmas mode
  418. rgblight_effect_christmas();
  419. }
  420. }
  421. }
  422. // Effects
  423. void rgblight_effect_breathing(uint8_t interval) {
  424. static uint8_t pos = 0;
  425. static uint16_t last_timer = 0;
  426. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
  427. return;
  428. }
  429. last_timer = timer_read();
  430. rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&LED_BREATHING_TABLE[pos]));
  431. pos = (pos + 1) % 256;
  432. }
  433. void rgblight_effect_rainbow_mood(uint8_t interval) {
  434. static uint16_t current_hue = 0;
  435. static uint16_t last_timer = 0;
  436. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
  437. return;
  438. }
  439. last_timer = timer_read();
  440. rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
  441. current_hue = (current_hue + 1) % 360;
  442. }
  443. void rgblight_effect_rainbow_swirl(uint8_t interval) {
  444. static uint16_t current_hue = 0;
  445. static uint16_t last_timer = 0;
  446. uint16_t hue;
  447. uint8_t i;
  448. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval / 2])) {
  449. return;
  450. }
  451. last_timer = timer_read();
  452. for (i = 0; i < RGBLED_NUM; i++) {
  453. hue = (360 / RGBLED_NUM * i + current_hue) % 360;
  454. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  455. }
  456. rgblight_set();
  457. if (interval % 2) {
  458. current_hue = (current_hue + 1) % 360;
  459. } else {
  460. if (current_hue - 1 < 0) {
  461. current_hue = 359;
  462. } else {
  463. current_hue = current_hue - 1;
  464. }
  465. }
  466. }
  467. void rgblight_effect_snake(uint8_t interval) {
  468. static uint8_t pos = 0;
  469. static uint16_t last_timer = 0;
  470. uint8_t i, j;
  471. int8_t k;
  472. int8_t increment = 1;
  473. if (interval % 2) {
  474. increment = -1;
  475. }
  476. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
  477. return;
  478. }
  479. last_timer = timer_read();
  480. for (i = 0; i < RGBLED_NUM; i++) {
  481. led[i].r = 0;
  482. led[i].g = 0;
  483. led[i].b = 0;
  484. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  485. k = pos + j * increment;
  486. if (k < 0) {
  487. k = k + RGBLED_NUM;
  488. }
  489. if (i == k) {
  490. 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]);
  491. }
  492. }
  493. }
  494. rgblight_set();
  495. if (increment == 1) {
  496. if (pos - 1 < 0) {
  497. pos = RGBLED_NUM - 1;
  498. } else {
  499. pos -= 1;
  500. }
  501. } else {
  502. pos = (pos + 1) % RGBLED_NUM;
  503. }
  504. }
  505. void rgblight_effect_knight(uint8_t interval) {
  506. static int8_t pos = 0;
  507. static uint16_t last_timer = 0;
  508. uint8_t i, j, cur;
  509. int8_t k;
  510. LED_TYPE preled[RGBLED_NUM];
  511. static int8_t increment = -1;
  512. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
  513. return;
  514. }
  515. last_timer = timer_read();
  516. for (i = 0; i < RGBLED_NUM; i++) {
  517. preled[i].r = 0;
  518. preled[i].g = 0;
  519. preled[i].b = 0;
  520. for (j = 0; j < RGBLIGHT_EFFECT_KNIGHT_LENGTH; j++) {
  521. k = pos + j * increment;
  522. if (k < 0) {
  523. k = 0;
  524. }
  525. if (k >= RGBLED_NUM) {
  526. k = RGBLED_NUM - 1;
  527. }
  528. if (i == k) {
  529. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&preled[i]);
  530. }
  531. }
  532. }
  533. if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
  534. for (i = 0; i < RGBLED_NUM; i++) {
  535. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
  536. led[i].r = preled[cur].r;
  537. led[i].g = preled[cur].g;
  538. led[i].b = preled[cur].b;
  539. }
  540. }
  541. rgblight_set();
  542. if (increment == 1) {
  543. if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
  544. pos = 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH;
  545. increment = -1;
  546. } else {
  547. pos -= 1;
  548. }
  549. } else {
  550. if (pos + 1 > RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
  551. pos = RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  552. increment = 1;
  553. } else {
  554. pos += 1;
  555. }
  556. }
  557. }
  558. void rgblight_effect_christmas(void) {
  559. static uint16_t current_offset = 0;
  560. static uint16_t last_timer = 0;
  561. uint16_t hue;
  562. uint8_t i;
  563. if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
  564. return;
  565. }
  566. last_timer = timer_read();
  567. current_offset = (current_offset + 1) % 2;
  568. for (i = 0; i < RGBLED_NUM; i++) {
  569. hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
  570. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  571. }
  572. rgblight_set();
  573. }
  574. #endif