rgblight.c 27 KB

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