rgblight.c 37 KB

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