2
0

rgblight.c 21 KB

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