rgblight.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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_helper(bool write_to_eeprom) {
  189. uint8_t mode = 0;
  190. mode = rgblight_config.mode + 1;
  191. if (mode > RGBLIGHT_MODES) {
  192. mode = 1;
  193. }
  194. rgblight_mode_eeprom_helper(mode, write_to_eeprom);
  195. }
  196. void rgblight_step_noeeprom(void) {
  197. rgblight_step_helper(false);
  198. }
  199. void rgblight_step(void) {
  200. rgblight_step_helper(true);
  201. }
  202. void rgblight_step_reverse_helper(bool write_to_eeprom) {
  203. uint8_t mode = 0;
  204. mode = rgblight_config.mode - 1;
  205. if (mode < 1) {
  206. mode = RGBLIGHT_MODES;
  207. }
  208. rgblight_mode_eeprom_helper(mode, write_to_eeprom);
  209. }
  210. void rgblight_step_reverse_noeeprom(void) {
  211. rgblight_step_reverse_helper(false);
  212. }
  213. void rgblight_step_reverse(void) {
  214. rgblight_step_reverse_helper(true);
  215. }
  216. uint8_t rgblight_get_mode(void) {
  217. if (!rgblight_config.enable) {
  218. return false;
  219. }
  220. return rgblight_config.mode;
  221. }
  222. void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  223. if (!rgblight_config.enable) {
  224. return;
  225. }
  226. if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
  227. rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
  228. } else if (mode > RGBLIGHT_MODES) {
  229. rgblight_config.mode = RGBLIGHT_MODES;
  230. } else {
  231. rgblight_config.mode = mode;
  232. }
  233. if (write_to_eeprom) {
  234. eeconfig_update_rgblight(rgblight_config.raw);
  235. xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
  236. } else {
  237. xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
  238. }
  239. if( is_static_effect(rgblight_config.mode) ) {
  240. #ifdef RGBLIGHT_USE_TIMER
  241. rgblight_timer_disable();
  242. #endif
  243. } else {
  244. #ifdef RGBLIGHT_USE_TIMER
  245. rgblight_timer_enable();
  246. #endif
  247. }
  248. rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  249. }
  250. void rgblight_mode(uint8_t mode) {
  251. rgblight_mode_eeprom_helper(mode, true);
  252. }
  253. void rgblight_mode_noeeprom(uint8_t mode) {
  254. rgblight_mode_eeprom_helper(mode, false);
  255. }
  256. void rgblight_toggle(void) {
  257. xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
  258. if (rgblight_config.enable) {
  259. rgblight_disable();
  260. }
  261. else {
  262. rgblight_enable();
  263. }
  264. }
  265. void rgblight_toggle_noeeprom(void) {
  266. xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
  267. if (rgblight_config.enable) {
  268. rgblight_disable_noeeprom();
  269. }
  270. else {
  271. rgblight_enable_noeeprom();
  272. }
  273. }
  274. void rgblight_enable(void) {
  275. rgblight_config.enable = 1;
  276. // No need to update EEPROM here. rgblight_mode() will do that, actually
  277. //eeconfig_update_rgblight(rgblight_config.raw);
  278. xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  279. rgblight_mode(rgblight_config.mode);
  280. }
  281. void rgblight_enable_noeeprom(void) {
  282. rgblight_config.enable = 1;
  283. xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  284. rgblight_mode_noeeprom(rgblight_config.mode);
  285. }
  286. void rgblight_disable(void) {
  287. rgblight_config.enable = 0;
  288. eeconfig_update_rgblight(rgblight_config.raw);
  289. xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  290. #ifdef RGBLIGHT_USE_TIMER
  291. rgblight_timer_disable();
  292. #endif
  293. wait_ms(50);
  294. rgblight_set();
  295. }
  296. void rgblight_disable_noeeprom(void) {
  297. rgblight_config.enable = 0;
  298. xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  299. #ifdef RGBLIGHT_USE_TIMER
  300. rgblight_timer_disable();
  301. #endif
  302. _delay_ms(50);
  303. rgblight_set();
  304. }
  305. // Deals with the messy details of incrementing an integer
  306. static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  307. int16_t new_value = value;
  308. new_value += step;
  309. return MIN( MAX( new_value, min ), max );
  310. }
  311. static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
  312. int16_t new_value = value;
  313. new_value -= step;
  314. return MIN( MAX( new_value, min ), max );
  315. }
  316. void rgblight_increase_hue_helper(bool write_to_eeprom) {
  317. uint16_t hue;
  318. hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
  319. rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
  320. }
  321. void rgblight_increase_hue_noeeprom(void) {
  322. rgblight_increase_hue_helper(false);
  323. }
  324. void rgblight_increase_hue(void) {
  325. rgblight_increase_hue_helper(true);
  326. }
  327. void rgblight_decrease_hue_helper(bool write_to_eeprom) {
  328. uint16_t hue;
  329. if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
  330. hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
  331. } else {
  332. hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
  333. }
  334. rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
  335. }
  336. void rgblight_decrease_hue_noeeprom(void) {
  337. rgblight_decrease_hue_helper(false);
  338. }
  339. void rgblight_decrease_hue(void) {
  340. rgblight_decrease_hue_helper(true);
  341. }
  342. void rgblight_increase_sat_helper(bool write_to_eeprom) {
  343. uint8_t sat;
  344. if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
  345. sat = 255;
  346. } else {
  347. sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
  348. }
  349. rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
  350. }
  351. void rgblight_increase_sat_noeeprom(void) {
  352. rgblight_increase_sat_helper(false);
  353. }
  354. void rgblight_increase_sat(void) {
  355. rgblight_increase_sat_helper(true);
  356. }
  357. void rgblight_decrease_sat_helper(bool write_to_eeprom) {
  358. uint8_t sat;
  359. if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
  360. sat = 0;
  361. } else {
  362. sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
  363. }
  364. rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
  365. }
  366. void rgblight_decrease_sat_noeeprom(void) {
  367. rgblight_decrease_sat_helper(false);
  368. }
  369. void rgblight_decrease_sat(void) {
  370. rgblight_decrease_sat_helper(true);
  371. }
  372. void rgblight_increase_val_helper(bool write_to_eeprom) {
  373. uint8_t val;
  374. if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
  375. val = RGBLIGHT_LIMIT_VAL;
  376. } else {
  377. val = rgblight_config.val + RGBLIGHT_VAL_STEP;
  378. }
  379. rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
  380. }
  381. void rgblight_increase_val_noeeprom(void) {
  382. rgblight_increase_val_helper(false);
  383. }
  384. void rgblight_increase_val(void) {
  385. rgblight_increase_val_helper(true);
  386. }
  387. void rgblight_decrease_val_helper(bool write_to_eeprom) {
  388. uint8_t val;
  389. if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
  390. val = 0;
  391. } else {
  392. val = rgblight_config.val - RGBLIGHT_VAL_STEP;
  393. }
  394. rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
  395. }
  396. void rgblight_decrease_val_noeeprom(void) {
  397. rgblight_decrease_val_helper(false);
  398. }
  399. void rgblight_decrease_val(void) {
  400. rgblight_decrease_val_helper(true);
  401. }
  402. void rgblight_increase_speed(void) {
  403. rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
  404. eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
  405. }
  406. void rgblight_decrease_speed(void) {
  407. rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
  408. eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
  409. }
  410. void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
  411. if (rgblight_config.enable) {
  412. LED_TYPE tmp_led;
  413. sethsv(hue, sat, val, &tmp_led);
  414. // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
  415. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  416. }
  417. }
  418. void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
  419. if (rgblight_config.enable) {
  420. if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
  421. // same static color
  422. LED_TYPE tmp_led;
  423. sethsv(hue, sat, val, &tmp_led);
  424. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  425. } else {
  426. // all LEDs in same color
  427. if ( 1 == 0 ) { //dummy
  428. }
  429. #ifdef RGBLIGHT_EFFECT_BREATHING
  430. else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
  431. rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
  432. // breathing mode, ignore the change of val, use in memory value instead
  433. val = rgblight_config.val;
  434. }
  435. #endif
  436. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  437. else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
  438. rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
  439. // rainbow mood, ignore the change of hue
  440. hue = rgblight_config.hue;
  441. }
  442. #endif
  443. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  444. else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
  445. rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
  446. // rainbow swirl, ignore the change of hue
  447. hue = rgblight_config.hue;
  448. }
  449. #endif
  450. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  451. else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT &&
  452. rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) {
  453. // static gradient
  454. uint16_t _hue;
  455. int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1;
  456. uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]);
  457. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  458. _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
  459. dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
  460. sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
  461. }
  462. rgblight_set();
  463. }
  464. #endif
  465. }
  466. rgblight_config.hue = hue;
  467. rgblight_config.sat = sat;
  468. rgblight_config.val = val;
  469. if (write_to_eeprom) {
  470. eeconfig_update_rgblight(rgblight_config.raw);
  471. xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  472. } else {
  473. xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  474. }
  475. }
  476. }
  477. void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  478. rgblight_sethsv_eeprom_helper(hue, sat, val, true);
  479. }
  480. void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  481. rgblight_sethsv_eeprom_helper(hue, sat, val, false);
  482. }
  483. uint16_t rgblight_get_hue(void) {
  484. return rgblight_config.hue;
  485. }
  486. uint8_t rgblight_get_sat(void) {
  487. return rgblight_config.sat;
  488. }
  489. uint8_t rgblight_get_val(void) {
  490. return rgblight_config.val;
  491. }
  492. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  493. if (!rgblight_config.enable) { return; }
  494. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  495. led[i].r = r;
  496. led[i].g = g;
  497. led[i].b = b;
  498. }
  499. rgblight_set();
  500. }
  501. void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
  502. if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
  503. led[index].r = r;
  504. led[index].g = g;
  505. led[index].b = b;
  506. rgblight_set();
  507. }
  508. void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
  509. if (!rgblight_config.enable) { return; }
  510. LED_TYPE tmp_led;
  511. sethsv(hue, sat, val, &tmp_led);
  512. rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
  513. }
  514. #ifndef RGBLIGHT_CUSTOM_DRIVER
  515. void rgblight_set(void) {
  516. if (rgblight_config.enable) {
  517. #ifdef RGBW
  518. ws2812_setleds_rgbw(led, RGBLED_NUM);
  519. #else
  520. ws2812_setleds(led, RGBLED_NUM);
  521. #endif
  522. } else {
  523. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  524. led[i].r = 0;
  525. led[i].g = 0;
  526. led[i].b = 0;
  527. }
  528. #ifdef RGBW
  529. ws2812_setleds_rgbw(led, RGBLED_NUM);
  530. #else
  531. ws2812_setleds(led, RGBLED_NUM);
  532. #endif
  533. }
  534. }
  535. #endif
  536. #ifdef RGBLIGHT_USE_TIMER
  537. // Animation timer -- AVR Timer3
  538. void rgblight_timer_init(void) {
  539. // static uint8_t rgblight_timer_is_init = 0;
  540. // if (rgblight_timer_is_init) {
  541. // return;
  542. // }
  543. // rgblight_timer_is_init = 1;
  544. // /* Timer 3 setup */
  545. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  546. // | _BV(CS30); // Clock selelct: clk/1
  547. // /* Set TOP value */
  548. // uint8_t sreg = SREG;
  549. // cli();
  550. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  551. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  552. // SREG = sreg;
  553. rgblight_timer_enabled = true;
  554. }
  555. void rgblight_timer_enable(void) {
  556. rgblight_timer_enabled = true;
  557. dprintf("TIMER3 enabled.\n");
  558. }
  559. void rgblight_timer_disable(void) {
  560. rgblight_timer_enabled = false;
  561. dprintf("TIMER3 disabled.\n");
  562. }
  563. void rgblight_timer_toggle(void) {
  564. rgblight_timer_enabled ^= rgblight_timer_enabled;
  565. dprintf("TIMER3 toggled.\n");
  566. }
  567. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  568. rgblight_enable();
  569. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  570. rgblight_setrgb(r, g, b);
  571. }
  572. void rgblight_task(void) {
  573. if (rgblight_timer_enabled) {
  574. // static light mode, do nothing here
  575. if ( 1 == 0 ) { //dummy
  576. }
  577. #ifdef RGBLIGHT_EFFECT_BREATHING
  578. else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
  579. rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
  580. // breathing mode
  581. rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
  582. }
  583. #endif
  584. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  585. else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
  586. rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
  587. // rainbow mood mode
  588. rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
  589. }
  590. #endif
  591. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  592. else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
  593. rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
  594. // rainbow swirl mode
  595. rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
  596. }
  597. #endif
  598. #ifdef RGBLIGHT_EFFECT_SNAKE
  599. else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
  600. rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
  601. // snake mode
  602. rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
  603. }
  604. #endif
  605. #ifdef RGBLIGHT_EFFECT_KNIGHT
  606. else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
  607. rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
  608. // knight mode
  609. rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
  610. }
  611. #endif
  612. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  613. else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
  614. // christmas mode
  615. rgblight_effect_christmas();
  616. }
  617. #endif
  618. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  619. else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
  620. // RGB test mode
  621. rgblight_effect_rgbtest();
  622. }
  623. #endif
  624. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  625. else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
  626. rgblight_effect_alternating();
  627. }
  628. #endif
  629. }
  630. }
  631. #endif /* RGBLIGHT_USE_TIMER */
  632. // Effects
  633. #ifdef RGBLIGHT_EFFECT_BREATHING
  634. __attribute__ ((weak))
  635. const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  636. void rgblight_effect_breathing(uint8_t interval) {
  637. static uint8_t pos = 0;
  638. static uint16_t last_timer = 0;
  639. float val;
  640. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
  641. return;
  642. }
  643. last_timer = timer_read();
  644. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  645. val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
  646. rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
  647. pos = (pos + 1) % 256;
  648. }
  649. #endif
  650. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  651. __attribute__ ((weak))
  652. const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  653. void rgblight_effect_rainbow_mood(uint8_t interval) {
  654. static uint16_t current_hue = 0;
  655. static uint16_t last_timer = 0;
  656. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
  657. return;
  658. }
  659. last_timer = timer_read();
  660. rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
  661. current_hue = (current_hue + 1) % 360;
  662. }
  663. #endif
  664. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  665. #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
  666. #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
  667. #endif
  668. __attribute__ ((weak))
  669. const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  670. void rgblight_effect_rainbow_swirl(uint8_t interval) {
  671. static uint16_t current_hue = 0;
  672. static uint16_t last_timer = 0;
  673. uint16_t hue;
  674. uint8_t i;
  675. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
  676. return;
  677. }
  678. last_timer = timer_read();
  679. for (i = 0; i < RGBLED_NUM; i++) {
  680. hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360;
  681. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  682. }
  683. rgblight_set();
  684. if (interval % 2) {
  685. current_hue = (current_hue + 1) % 360;
  686. } else {
  687. if (current_hue - 1 < 0) {
  688. current_hue = 359;
  689. } else {
  690. current_hue = current_hue - 1;
  691. }
  692. }
  693. }
  694. #endif
  695. #ifdef RGBLIGHT_EFFECT_SNAKE
  696. __attribute__ ((weak))
  697. const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  698. void rgblight_effect_snake(uint8_t interval) {
  699. static uint8_t pos = 0;
  700. static uint16_t last_timer = 0;
  701. uint8_t i, j;
  702. int8_t k;
  703. int8_t increment = 1;
  704. if (interval % 2) {
  705. increment = -1;
  706. }
  707. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
  708. return;
  709. }
  710. last_timer = timer_read();
  711. for (i = 0; i < RGBLED_NUM; i++) {
  712. led[i].r = 0;
  713. led[i].g = 0;
  714. led[i].b = 0;
  715. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  716. k = pos + j * increment;
  717. if (k < 0) {
  718. k = k + RGBLED_NUM;
  719. }
  720. if (i == k) {
  721. 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]);
  722. }
  723. }
  724. }
  725. rgblight_set();
  726. if (increment == 1) {
  727. if (pos - 1 < 0) {
  728. pos = RGBLED_NUM - 1;
  729. } else {
  730. pos -= 1;
  731. }
  732. } else {
  733. pos = (pos + 1) % RGBLED_NUM;
  734. }
  735. }
  736. #endif
  737. #ifdef RGBLIGHT_EFFECT_KNIGHT
  738. __attribute__ ((weak))
  739. const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
  740. void rgblight_effect_knight(uint8_t interval) {
  741. static uint16_t last_timer = 0;
  742. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
  743. return;
  744. }
  745. last_timer = timer_read();
  746. static int8_t low_bound = 0;
  747. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  748. static int8_t increment = 1;
  749. uint8_t i, cur;
  750. // Set all the LEDs to 0
  751. for (i = 0; i < RGBLED_NUM; i++) {
  752. led[i].r = 0;
  753. led[i].g = 0;
  754. led[i].b = 0;
  755. }
  756. // Determine which LEDs should be lit up
  757. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  758. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
  759. if (i >= low_bound && i <= high_bound) {
  760. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  761. } else {
  762. led[cur].r = 0;
  763. led[cur].g = 0;
  764. led[cur].b = 0;
  765. }
  766. }
  767. rgblight_set();
  768. // Move from low_bound to high_bound changing the direction we increment each
  769. // time a boundary is hit.
  770. low_bound += increment;
  771. high_bound += increment;
  772. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  773. increment = -increment;
  774. }
  775. }
  776. #endif
  777. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  778. void rgblight_effect_christmas(void) {
  779. static uint16_t current_offset = 0;
  780. static uint16_t last_timer = 0;
  781. uint16_t hue;
  782. uint8_t i;
  783. if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
  784. return;
  785. }
  786. last_timer = timer_read();
  787. current_offset = (current_offset + 1) % 2;
  788. for (i = 0; i < RGBLED_NUM; i++) {
  789. hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
  790. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  791. }
  792. rgblight_set();
  793. }
  794. #endif
  795. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  796. __attribute__ ((weak))
  797. const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
  798. void rgblight_effect_rgbtest(void) {
  799. static uint8_t pos = 0;
  800. static uint16_t last_timer = 0;
  801. static uint8_t maxval = 0;
  802. uint8_t g; uint8_t r; uint8_t b;
  803. if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
  804. return;
  805. }
  806. if( maxval == 0 ) {
  807. LED_TYPE tmp_led;
  808. sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
  809. maxval = tmp_led.r;
  810. }
  811. last_timer = timer_read();
  812. g = r = b = 0;
  813. switch( pos ) {
  814. case 0: r = maxval; break;
  815. case 1: g = maxval; break;
  816. case 2: b = maxval; break;
  817. }
  818. rgblight_setrgb(r, g, b);
  819. pos = (pos + 1) % 3;
  820. }
  821. #endif
  822. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  823. void rgblight_effect_alternating(void){
  824. static uint16_t last_timer = 0;
  825. static uint16_t pos = 0;
  826. if (timer_elapsed(last_timer) < 500) {
  827. return;
  828. }
  829. last_timer = timer_read();
  830. for(int i = 0; i<RGBLED_NUM; i++){
  831. if(i<RGBLED_NUM/2 && pos){
  832. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  833. }else if (i>=RGBLED_NUM/2 && !pos){
  834. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  835. }else{
  836. sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);
  837. }
  838. }
  839. rgblight_set();
  840. pos = (pos + 1) % 2;
  841. }
  842. #endif