rgblight.c 30 KB

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