rgblight.c 30 KB

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