rgblight.c 22 KB

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