Peter's keyboard firmware (QMK) https://qmk.fm/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rgblight.c 29KB

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