rgblight.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. #ifndef RGBLIGHT_CUSTOM_DRIVER
  552. void rgblight_set(void) {
  553. if (rgblight_config.enable) {
  554. #ifdef RGBW
  555. ws2812_setleds_rgbw(led, RGBLED_NUM);
  556. #else
  557. ws2812_setleds(led, RGBLED_NUM);
  558. #endif
  559. } else {
  560. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  561. led[i].r = 0;
  562. led[i].g = 0;
  563. led[i].b = 0;
  564. }
  565. #ifdef RGBW
  566. ws2812_setleds_rgbw(led, RGBLED_NUM);
  567. #else
  568. ws2812_setleds(led, RGBLED_NUM);
  569. #endif
  570. }
  571. }
  572. #endif
  573. #ifdef RGBLIGHT_USE_TIMER
  574. // Animation timer -- AVR Timer3
  575. void rgblight_timer_init(void) {
  576. // static uint8_t rgblight_timer_is_init = 0;
  577. // if (rgblight_timer_is_init) {
  578. // return;
  579. // }
  580. // rgblight_timer_is_init = 1;
  581. // /* Timer 3 setup */
  582. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  583. // | _BV(CS30); // Clock selelct: clk/1
  584. // /* Set TOP value */
  585. // uint8_t sreg = SREG;
  586. // cli();
  587. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  588. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  589. // SREG = sreg;
  590. rgblight_timer_enabled = true;
  591. }
  592. void rgblight_timer_enable(void) {
  593. rgblight_timer_enabled = true;
  594. dprintf("TIMER3 enabled.\n");
  595. }
  596. void rgblight_timer_disable(void) {
  597. rgblight_timer_enabled = false;
  598. dprintf("TIMER3 disabled.\n");
  599. }
  600. void rgblight_timer_toggle(void) {
  601. rgblight_timer_enabled ^= rgblight_timer_enabled;
  602. dprintf("TIMER3 toggled.\n");
  603. }
  604. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  605. rgblight_enable();
  606. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  607. rgblight_setrgb(r, g, b);
  608. }
  609. void rgblight_task(void) {
  610. if (rgblight_timer_enabled) {
  611. // static light mode, do nothing here
  612. if ( 1 == 0 ) { //dummy
  613. }
  614. #ifdef RGBLIGHT_EFFECT_BREATHING
  615. else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
  616. rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
  617. // breathing mode
  618. rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
  619. }
  620. #endif
  621. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  622. else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
  623. rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
  624. // rainbow mood mode
  625. rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
  626. }
  627. #endif
  628. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  629. else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
  630. rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
  631. // rainbow swirl mode
  632. rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
  633. }
  634. #endif
  635. #ifdef RGBLIGHT_EFFECT_SNAKE
  636. else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
  637. rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
  638. // snake mode
  639. rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
  640. }
  641. #endif
  642. #ifdef RGBLIGHT_EFFECT_KNIGHT
  643. else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
  644. rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
  645. // knight mode
  646. rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
  647. }
  648. #endif
  649. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  650. else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
  651. // christmas mode
  652. rgblight_effect_christmas();
  653. }
  654. #endif
  655. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  656. else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
  657. // RGB test mode
  658. rgblight_effect_rgbtest();
  659. }
  660. #endif
  661. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  662. else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
  663. rgblight_effect_alternating();
  664. }
  665. #endif
  666. }
  667. }
  668. #endif /* RGBLIGHT_USE_TIMER */
  669. // Effects
  670. #ifdef RGBLIGHT_EFFECT_BREATHING
  671. __attribute__ ((weak))
  672. const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  673. void rgblight_effect_breathing(uint8_t interval) {
  674. static uint8_t pos = 0;
  675. static uint16_t last_timer = 0;
  676. float val;
  677. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
  678. return;
  679. }
  680. last_timer = timer_read();
  681. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  682. val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
  683. rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
  684. pos = (pos + 1) % 256;
  685. }
  686. #endif
  687. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  688. __attribute__ ((weak))
  689. const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  690. void rgblight_effect_rainbow_mood(uint8_t interval) {
  691. static uint16_t current_hue = 0;
  692. static uint16_t last_timer = 0;
  693. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
  694. return;
  695. }
  696. last_timer = timer_read();
  697. rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
  698. current_hue = (current_hue + 1) % 360;
  699. }
  700. #endif
  701. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  702. #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
  703. #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
  704. #endif
  705. __attribute__ ((weak))
  706. const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  707. void rgblight_effect_rainbow_swirl(uint8_t interval) {
  708. static uint16_t current_hue = 0;
  709. static uint16_t last_timer = 0;
  710. uint16_t hue;
  711. uint8_t i;
  712. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
  713. return;
  714. }
  715. last_timer = timer_read();
  716. for (i = 0; i < RGBLED_NUM; i++) {
  717. hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360;
  718. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  719. }
  720. rgblight_set();
  721. if (interval % 2) {
  722. current_hue = (current_hue + 1) % 360;
  723. } else {
  724. if (current_hue - 1 < 0) {
  725. current_hue = 359;
  726. } else {
  727. current_hue = current_hue - 1;
  728. }
  729. }
  730. }
  731. #endif
  732. #ifdef RGBLIGHT_EFFECT_SNAKE
  733. __attribute__ ((weak))
  734. const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  735. void rgblight_effect_snake(uint8_t interval) {
  736. static uint8_t pos = 0;
  737. static uint16_t last_timer = 0;
  738. uint8_t i, j;
  739. int8_t k;
  740. int8_t increment = 1;
  741. if (interval % 2) {
  742. increment = -1;
  743. }
  744. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
  745. return;
  746. }
  747. last_timer = timer_read();
  748. for (i = 0; i < RGBLED_NUM; i++) {
  749. led[i].r = 0;
  750. led[i].g = 0;
  751. led[i].b = 0;
  752. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  753. k = pos + j * increment;
  754. if (k < 0) {
  755. k = k + RGBLED_NUM;
  756. }
  757. if (i == k) {
  758. 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]);
  759. }
  760. }
  761. }
  762. rgblight_set();
  763. if (increment == 1) {
  764. if (pos - 1 < 0) {
  765. pos = RGBLED_NUM - 1;
  766. } else {
  767. pos -= 1;
  768. }
  769. } else {
  770. pos = (pos + 1) % RGBLED_NUM;
  771. }
  772. }
  773. #endif
  774. #ifdef RGBLIGHT_EFFECT_KNIGHT
  775. __attribute__ ((weak))
  776. const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
  777. void rgblight_effect_knight(uint8_t interval) {
  778. static uint16_t last_timer = 0;
  779. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
  780. return;
  781. }
  782. last_timer = timer_read();
  783. static int8_t low_bound = 0;
  784. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  785. static int8_t increment = 1;
  786. uint8_t i, cur;
  787. // Set all the LEDs to 0
  788. for (i = 0; i < RGBLED_NUM; i++) {
  789. led[i].r = 0;
  790. led[i].g = 0;
  791. led[i].b = 0;
  792. }
  793. // Determine which LEDs should be lit up
  794. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  795. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
  796. if (i >= low_bound && i <= high_bound) {
  797. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  798. } else {
  799. led[cur].r = 0;
  800. led[cur].g = 0;
  801. led[cur].b = 0;
  802. }
  803. }
  804. rgblight_set();
  805. // Move from low_bound to high_bound changing the direction we increment each
  806. // time a boundary is hit.
  807. low_bound += increment;
  808. high_bound += increment;
  809. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  810. increment = -increment;
  811. }
  812. }
  813. #endif
  814. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  815. void rgblight_effect_christmas(void) {
  816. static uint16_t current_offset = 0;
  817. static uint16_t last_timer = 0;
  818. uint16_t hue;
  819. uint8_t i;
  820. if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
  821. return;
  822. }
  823. last_timer = timer_read();
  824. current_offset = (current_offset + 1) % 2;
  825. for (i = 0; i < RGBLED_NUM; i++) {
  826. hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
  827. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  828. }
  829. rgblight_set();
  830. }
  831. #endif
  832. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  833. __attribute__ ((weak))
  834. const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
  835. void rgblight_effect_rgbtest(void) {
  836. static uint8_t pos = 0;
  837. static uint16_t last_timer = 0;
  838. static uint8_t maxval = 0;
  839. uint8_t g; uint8_t r; uint8_t b;
  840. if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
  841. return;
  842. }
  843. if( maxval == 0 ) {
  844. LED_TYPE tmp_led;
  845. sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
  846. maxval = tmp_led.r;
  847. }
  848. last_timer = timer_read();
  849. g = r = b = 0;
  850. switch( pos ) {
  851. case 0: r = maxval; break;
  852. case 1: g = maxval; break;
  853. case 2: b = maxval; break;
  854. }
  855. rgblight_setrgb(r, g, b);
  856. pos = (pos + 1) % 3;
  857. }
  858. #endif
  859. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  860. void rgblight_effect_alternating(void){
  861. static uint16_t last_timer = 0;
  862. static uint16_t pos = 0;
  863. if (timer_elapsed(last_timer) < 500) {
  864. return;
  865. }
  866. last_timer = timer_read();
  867. for(int i = 0; i<RGBLED_NUM; i++){
  868. if(i<RGBLED_NUM/2 && pos){
  869. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  870. }else if (i>=RGBLED_NUM/2 && !pos){
  871. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
  872. }else{
  873. sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);
  874. }
  875. }
  876. rgblight_set();
  877. pos = (pos + 1) % 2;
  878. }
  879. #endif