rgblight.c 18 KB

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