rgblight.c 19 KB

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