rgblight.c 29 KB

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