rgblight.c 19 KB

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