rgblight.c 23 KB

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