rgblight.c 25 KB

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