rgblight.c 22 KB

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