rgblight.c 23 KB

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