rgblight.c 19 KB

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