You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

827 lines
23 KiB

  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. uint8_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. fled_hs[0].hue = fled_hs[1].hue = hue;
  389. fled_hs[0].sat = fled_hs[1].sat = sat;
  390. // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
  391. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  392. }
  393. }
  394. void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
  395. if (rgblight_config.enable) {
  396. if (rgblight_config.mode == 1) {
  397. // same static color
  398. LED_TYPE tmp_led;
  399. sethsv(hue, sat, val, &tmp_led);
  400. fled_hs[0].hue = fled_hs[1].hue = hue;
  401. fled_hs[0].sat = fled_hs[1].sat = sat;
  402. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  403. } else {
  404. // all LEDs in same color
  405. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  406. // breathing mode, ignore the change of val, use in memory value instead
  407. val = rgblight_config.val;
  408. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
  409. // rainbow mood and rainbow swirl, ignore the change of hue
  410. hue = rgblight_config.hue;
  411. } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
  412. // static gradient
  413. uint16_t _hue;
  414. int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1;
  415. uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]);
  416. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  417. _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
  418. dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
  419. sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
  420. }
  421. rgblight_set();
  422. }
  423. }
  424. rgblight_config.hue = hue;
  425. rgblight_config.sat = sat;
  426. rgblight_config.val = val;
  427. if (write_to_eeprom) {
  428. eeconfig_update_rgblight(rgblight_config.raw);
  429. xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  430. } else {
  431. xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  432. }
  433. }
  434. }
  435. void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  436. rgblight_sethsv_eeprom_helper(hue, sat, val, true);
  437. }
  438. void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  439. rgblight_sethsv_eeprom_helper(hue, sat, val, false);
  440. }
  441. uint16_t rgblight_get_hue(void) {
  442. return rgblight_config.hue;
  443. }
  444. uint8_t rgblight_get_sat(void) {
  445. return rgblight_config.sat;
  446. }
  447. uint8_t rgblight_get_val(void) {
  448. return rgblight_config.val;
  449. }
  450. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  451. if (!rgblight_config.enable) { return; }
  452. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  453. led[i].r = r;
  454. led[i].g = g;
  455. led[i].b = b;
  456. }
  457. rgblight_set();
  458. }
  459. void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
  460. if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
  461. led[index].r = r;
  462. led[index].g = g;
  463. led[index].b = b;
  464. rgblight_set();
  465. }
  466. void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
  467. if (!rgblight_config.enable) { return; }
  468. LED_TYPE tmp_led;
  469. sethsv(hue, sat, val, &tmp_led);
  470. rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
  471. }
  472. void rgblight_set(void) {
  473. if (!rgblight_config.enable) {
  474. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  475. if (i == RGBLIGHT_FLED1 && i == RGBLIGHT_FLED2)
  476. continue;
  477. led[i].r = 0;
  478. led[i].g = 0;
  479. led[i].b = 0;
  480. }
  481. }
  482. switch (fled_mode) {
  483. case FLED_OFF:
  484. setrgb(0, 0, 0, &led[RGBLIGHT_FLED1]);
  485. setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]);
  486. break;
  487. case FLED_INDI:
  488. copyrgb(&fleds[0], &led[RGBLIGHT_FLED1]);
  489. copyrgb(&fleds[1], &led[RGBLIGHT_FLED2]);
  490. break;
  491. case FLED_RGB:
  492. if (fled_hs[0].hue == 0 && fled_hs[0].hue == 0 && (rgblight_config.mode >= 15 && rgblight_config.mode <= 23))
  493. setrgb(0, 0, 0, &led[RGBLIGHT_FLED1]);
  494. else
  495. sethsv(fled_hs[0].hue, fled_hs[0].sat, fled_val, &led[RGBLIGHT_FLED1]);
  496. if (fled_hs[1].hue == 0 && fled_hs[1].hue == 0 && (rgblight_config.mode >= 15 && rgblight_config.mode <= 23))
  497. setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]);
  498. else
  499. sethsv(fled_hs[1].hue, fled_hs[1].sat, fled_val, &led[RGBLIGHT_FLED2]);
  500. break;
  501. default:
  502. break;
  503. }
  504. ws2812_setleds(led, RGBLED_NUM);
  505. }
  506. #ifdef RGBLIGHT_ANIMATIONS
  507. // Animation timer -- AVR Timer3
  508. void rgblight_timer_init(void) {
  509. // static uint8_t rgblight_timer_is_init = 0;
  510. // if (rgblight_timer_is_init) {
  511. // return;
  512. // }
  513. // rgblight_timer_is_init = 1;
  514. // /* Timer 3 setup */
  515. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  516. // | _BV(CS30); // Clock selelct: clk/1
  517. // /* Set TOP value */
  518. // uint8_t sreg = SREG;
  519. // cli();
  520. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  521. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  522. // SREG = sreg;
  523. rgblight_timer_enabled = true;
  524. }
  525. void rgblight_timer_enable(void) {
  526. rgblight_timer_enabled = true;
  527. dprintf("TIMER3 enabled.\n");
  528. }
  529. void rgblight_timer_disable(void) {
  530. rgblight_timer_enabled = false;
  531. dprintf("TIMER3 disabled.\n");
  532. }
  533. void rgblight_timer_toggle(void) {
  534. rgblight_timer_enabled ^= rgblight_timer_enabled;
  535. dprintf("TIMER3 toggled.\n");
  536. }
  537. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  538. rgblight_enable();
  539. rgblight_mode(1);
  540. rgblight_setrgb(r, g, b);
  541. }
  542. void rgblight_task(void) {
  543. if (rgblight_timer_enabled) {
  544. // mode = 1, static light, do nothing here
  545. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  546. // mode = 2 to 5, breathing mode
  547. rgblight_effect_breathing(rgblight_config.mode - 2);
  548. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
  549. // mode = 6 to 8, rainbow mood mod
  550. rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
  551. } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
  552. // mode = 9 to 14, rainbow swirl mode
  553. rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
  554. } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
  555. // mode = 15 to 20, snake mode
  556. rgblight_effect_snake(rgblight_config.mode - 15);
  557. } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
  558. // mode = 21 to 23, knight mode
  559. rgblight_effect_knight(rgblight_config.mode - 21);
  560. } else if (rgblight_config.mode == 24) {
  561. // mode = 24, christmas mode
  562. rgblight_effect_christmas();
  563. } else if (rgblight_config.mode == 35) {
  564. // mode = 35, RGB test
  565. rgblight_effect_rgbtest();
  566. }
  567. }
  568. }
  569. // Effects
  570. void rgblight_effect_breathing(uint8_t interval) {
  571. static uint8_t pos = 0;
  572. static uint16_t last_timer = 0;
  573. float val;
  574. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
  575. return;
  576. }
  577. last_timer = timer_read();
  578. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  579. val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
  580. rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
  581. pos = (pos + 1) % 256;
  582. }
  583. void rgblight_effect_rainbow_mood(uint8_t interval) {
  584. static uint16_t current_hue = 0;
  585. static uint16_t last_timer = 0;
  586. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
  587. return;
  588. }
  589. last_timer = timer_read();
  590. rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
  591. current_hue = (current_hue + 1) % 360;
  592. }
  593. void rgblight_effect_rainbow_swirl(uint8_t interval) {
  594. static uint16_t current_hue = 0;
  595. static uint16_t last_timer = 0;
  596. uint16_t hue;
  597. uint8_t i;
  598. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
  599. return;
  600. }
  601. last_timer = timer_read();
  602. for (i = 0; i < RGBLED_NUM; i++) {
  603. hue = (360 / RGBLED_NUM * i + current_hue) % 360;
  604. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  605. }
  606. rgblight_set();
  607. if (interval % 2) {
  608. current_hue = (current_hue + 1) % 360;
  609. } else {
  610. if (current_hue - 1 < 0) {
  611. current_hue = 359;
  612. } else {
  613. current_hue = current_hue - 1;
  614. }
  615. }
  616. }
  617. void rgblight_effect_snake(uint8_t interval) {
  618. static uint8_t pos = 0;
  619. static uint16_t last_timer = 0;
  620. uint8_t i, j;
  621. int8_t k;
  622. int8_t increment = 1;
  623. if (interval % 2) {
  624. increment = -1;
  625. }
  626. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
  627. return;
  628. }
  629. last_timer = timer_read();
  630. fled_hs[0].hue = fled_hs[1].hue = 0;
  631. fled_hs[0].sat = fled_hs[1].sat = 0;
  632. for (i = 0; i < RGBLED_NUM; i++) {
  633. led[i].r = 0;
  634. led[i].g = 0;
  635. led[i].b = 0;
  636. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  637. k = pos + j * increment;
  638. if (k < 0) {
  639. k = k + RGBLED_NUM;
  640. }
  641. if (i == k) {
  642. 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]);
  643. }
  644. }
  645. }
  646. rgblight_set();
  647. if (increment == 1) {
  648. if (pos - 1 < 0) {
  649. pos = RGBLED_NUM - 1;
  650. } else {
  651. pos -= 1;
  652. }
  653. } else {
  654. pos = (pos + 1) % RGBLED_NUM;
  655. }
  656. }
  657. void rgblight_effect_knight(uint8_t interval) {
  658. static uint16_t last_timer = 0;
  659. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
  660. return;
  661. }
  662. last_timer = timer_read();
  663. static int8_t low_bound = 0;
  664. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  665. static int8_t increment = 1;
  666. uint8_t i, cur;
  667. // Set all the LEDs to 0
  668. for (i = 0; i < RGBLED_NUM; i++) {
  669. led[i].r = 0;
  670. led[i].g = 0;
  671. led[i].b = 0;
  672. }
  673. // Determine which LEDs should be lit up
  674. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  675. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
  676. if (i >= low_bound && i <= high_bound) {
  677. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  678. } else {
  679. if (i == RGBLIGHT_FLED1 || i == RGBLIGHT_FLED2) {
  680. fled_hs[0].hue = fled_hs[1].hue = 0;
  681. fled_hs[0].sat = fled_hs[1].sat = 0;
  682. }
  683. led[cur].r = 0;
  684. led[cur].g = 0;
  685. led[cur].b = 0;
  686. }
  687. }
  688. rgblight_set();
  689. // Move from low_bound to high_bound changing the direction we increment each
  690. // time a boundary is hit.
  691. low_bound += increment;
  692. high_bound += increment;
  693. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  694. increment = -increment;
  695. }
  696. }
  697. void rgblight_effect_christmas(void) {
  698. static uint16_t current_offset = 0;
  699. static uint16_t last_timer = 0;
  700. uint16_t hue;
  701. uint8_t i;
  702. if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
  703. return;
  704. }
  705. last_timer = timer_read();
  706. current_offset = (current_offset + 1) % 2;
  707. for (i = 0; i < RGBLED_NUM; i++) {
  708. hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
  709. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  710. }
  711. rgblight_set();
  712. }
  713. void rgblight_effect_rgbtest(void) {
  714. static uint8_t pos = 0;
  715. static uint16_t last_timer = 0;
  716. static uint8_t maxval = 0;
  717. uint8_t g; uint8_t r; uint8_t b;
  718. if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
  719. return;
  720. }
  721. if( maxval == 0 ) {
  722. LED_TYPE tmp_led;
  723. sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
  724. maxval = tmp_led.r;
  725. }
  726. last_timer = timer_read();
  727. g = r = b = 0;
  728. switch( pos ) {
  729. case 0: r = maxval; break;
  730. case 1: g = maxval; break;
  731. case 2: b = maxval; break;
  732. }
  733. rgblight_setrgb(r, g, b);
  734. pos = (pos + 1) % 3;
  735. }
  736. #endif /* RGBLIGHT_ANIMATIONS */