rgblight.c 22 KB

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