rgblight.c 37 KB

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