2
0

rgblight.c 23 KB

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