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.

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