rgblight.c 37 KB

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