2
0

rgblight.c 36 KB

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