rgblight.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. uint16_t rgblight_get_hue(void) {
  347. return rgblight_config.hue;
  348. }
  349. uint8_t rgblight_get_sat(void) {
  350. return rgblight_config.sat;
  351. }
  352. uint8_t rgblight_get_val(void) {
  353. return rgblight_config.val;
  354. }
  355. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  356. if (!rgblight_config.enable) { return; }
  357. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  358. led[i].r = r;
  359. led[i].g = g;
  360. led[i].b = b;
  361. }
  362. rgblight_set();
  363. }
  364. void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
  365. if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
  366. led[index].r = r;
  367. led[index].g = g;
  368. led[index].b = b;
  369. rgblight_set();
  370. }
  371. void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
  372. if (!rgblight_config.enable) { return; }
  373. LED_TYPE tmp_led;
  374. sethsv(hue, sat, val, &tmp_led);
  375. rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
  376. }
  377. #ifndef RGBLIGHT_CUSTOM_DRIVER
  378. void rgblight_set(void) {
  379. if (rgblight_config.enable) {
  380. #ifdef RGBW
  381. ws2812_setleds_rgbw(led, RGBLED_NUM);
  382. #else
  383. ws2812_setleds(led, RGBLED_NUM);
  384. #endif
  385. } else {
  386. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  387. led[i].r = 0;
  388. led[i].g = 0;
  389. led[i].b = 0;
  390. }
  391. #ifdef RGBW
  392. ws2812_setleds_rgbw(led, RGBLED_NUM);
  393. #else
  394. ws2812_setleds(led, RGBLED_NUM);
  395. #endif
  396. }
  397. }
  398. #endif
  399. #ifdef RGBLIGHT_ANIMATIONS
  400. // Animation timer -- AVR Timer3
  401. void rgblight_timer_init(void) {
  402. // static uint8_t rgblight_timer_is_init = 0;
  403. // if (rgblight_timer_is_init) {
  404. // return;
  405. // }
  406. // rgblight_timer_is_init = 1;
  407. // /* Timer 3 setup */
  408. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  409. // | _BV(CS30); // Clock selelct: clk/1
  410. // /* Set TOP value */
  411. // uint8_t sreg = SREG;
  412. // cli();
  413. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  414. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  415. // SREG = sreg;
  416. rgblight_timer_enabled = true;
  417. }
  418. void rgblight_timer_enable(void) {
  419. rgblight_timer_enabled = true;
  420. dprintf("TIMER3 enabled.\n");
  421. }
  422. void rgblight_timer_disable(void) {
  423. rgblight_timer_enabled = false;
  424. dprintf("TIMER3 disabled.\n");
  425. }
  426. void rgblight_timer_toggle(void) {
  427. rgblight_timer_enabled ^= rgblight_timer_enabled;
  428. dprintf("TIMER3 toggled.\n");
  429. }
  430. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  431. rgblight_enable();
  432. rgblight_mode(1);
  433. rgblight_setrgb(r, g, b);
  434. }
  435. void rgblight_task(void) {
  436. if (rgblight_timer_enabled) {
  437. // mode = 1, static light, do nothing here
  438. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  439. // mode = 2 to 5, breathing mode
  440. rgblight_effect_breathing(rgblight_config.mode - 2);
  441. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
  442. // mode = 6 to 8, rainbow mood mod
  443. rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
  444. } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
  445. // mode = 9 to 14, rainbow swirl mode
  446. rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
  447. } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
  448. // mode = 15 to 20, snake mode
  449. rgblight_effect_snake(rgblight_config.mode - 15);
  450. } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
  451. // mode = 21 to 23, knight mode
  452. rgblight_effect_knight(rgblight_config.mode - 21);
  453. } else if (rgblight_config.mode == 24) {
  454. // mode = 24, christmas mode
  455. rgblight_effect_christmas();
  456. }
  457. }
  458. }
  459. // Effects
  460. void rgblight_effect_breathing(uint8_t interval) {
  461. static uint8_t pos = 0;
  462. static uint16_t last_timer = 0;
  463. float val;
  464. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
  465. return;
  466. }
  467. last_timer = timer_read();
  468. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  469. val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
  470. rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, val);
  471. pos = (pos + 1) % 256;
  472. }
  473. void rgblight_effect_rainbow_mood(uint8_t interval) {
  474. static uint16_t current_hue = 0;
  475. static uint16_t last_timer = 0;
  476. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
  477. return;
  478. }
  479. last_timer = timer_read();
  480. rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
  481. current_hue = (current_hue + 1) % 360;
  482. }
  483. void rgblight_effect_rainbow_swirl(uint8_t interval) {
  484. static uint16_t current_hue = 0;
  485. static uint16_t last_timer = 0;
  486. uint16_t hue;
  487. uint8_t i;
  488. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
  489. return;
  490. }
  491. last_timer = timer_read();
  492. for (i = 0; i < RGBLED_NUM; i++) {
  493. hue = (360 / RGBLED_NUM * i + current_hue) % 360;
  494. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  495. }
  496. rgblight_set();
  497. if (interval % 2) {
  498. current_hue = (current_hue + 1) % 360;
  499. } else {
  500. if (current_hue - 1 < 0) {
  501. current_hue = 359;
  502. } else {
  503. current_hue = current_hue - 1;
  504. }
  505. }
  506. }
  507. void rgblight_effect_snake(uint8_t interval) {
  508. static uint8_t pos = 0;
  509. static uint16_t last_timer = 0;
  510. uint8_t i, j;
  511. int8_t k;
  512. int8_t increment = 1;
  513. if (interval % 2) {
  514. increment = -1;
  515. }
  516. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
  517. return;
  518. }
  519. last_timer = timer_read();
  520. for (i = 0; i < RGBLED_NUM; i++) {
  521. led[i].r = 0;
  522. led[i].g = 0;
  523. led[i].b = 0;
  524. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  525. k = pos + j * increment;
  526. if (k < 0) {
  527. k = k + RGBLED_NUM;
  528. }
  529. if (i == k) {
  530. 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]);
  531. }
  532. }
  533. }
  534. rgblight_set();
  535. if (increment == 1) {
  536. if (pos - 1 < 0) {
  537. pos = RGBLED_NUM - 1;
  538. } else {
  539. pos -= 1;
  540. }
  541. } else {
  542. pos = (pos + 1) % RGBLED_NUM;
  543. }
  544. }
  545. void rgblight_effect_knight(uint8_t interval) {
  546. static uint16_t last_timer = 0;
  547. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
  548. return;
  549. }
  550. last_timer = timer_read();
  551. static int8_t low_bound = 0;
  552. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  553. static int8_t increment = 1;
  554. uint8_t i, cur;
  555. // Set all the LEDs to 0
  556. for (i = 0; i < RGBLED_NUM; i++) {
  557. led[i].r = 0;
  558. led[i].g = 0;
  559. led[i].b = 0;
  560. }
  561. // Determine which LEDs should be lit up
  562. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  563. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
  564. if (i >= low_bound && i <= high_bound) {
  565. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  566. } else {
  567. led[cur].r = 0;
  568. led[cur].g = 0;
  569. led[cur].b = 0;
  570. }
  571. }
  572. rgblight_set();
  573. // Move from low_bound to high_bound changing the direction we increment each
  574. // time a boundary is hit.
  575. low_bound += increment;
  576. high_bound += increment;
  577. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  578. increment = -increment;
  579. }
  580. }
  581. void rgblight_effect_christmas(void) {
  582. static uint16_t current_offset = 0;
  583. static uint16_t last_timer = 0;
  584. uint16_t hue;
  585. uint8_t i;
  586. if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
  587. return;
  588. }
  589. last_timer = timer_read();
  590. current_offset = (current_offset + 1) % 2;
  591. for (i = 0; i < RGBLED_NUM; i++) {
  592. hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
  593. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  594. }
  595. rgblight_set();
  596. }
  597. #endif