You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

662 lines
20KB

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