rgblight.c 18 KB

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