rgblight.c 22 KB

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