rgblight.c 26 KB

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