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.

745 lines
21 KiB

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