rgblight.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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_current(void) { eeconfig_update_rgblight(rgblight_config.raw); }
  143. void eeconfig_update_rgblight_default(void) {
  144. rgblight_config.enable = 1;
  145. rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
  146. rgblight_config.hue = 0;
  147. rgblight_config.sat = UINT8_MAX;
  148. rgblight_config.val = RGBLIGHT_LIMIT_VAL;
  149. rgblight_config.speed = 0;
  150. RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
  151. eeconfig_update_rgblight(rgblight_config.raw);
  152. }
  153. void eeconfig_debug_rgblight(void) {
  154. dprintf("rgblight_config EEPROM:\n");
  155. dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
  156. dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
  157. dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
  158. dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
  159. dprintf("rgblight_config.val = %d\n", rgblight_config.val);
  160. dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
  161. }
  162. void rgblight_init(void) {
  163. /* if already initialized, don't do it again.
  164. If you must do it again, extern this and set to false, first.
  165. This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
  166. if (is_rgblight_initialized) {
  167. return;
  168. }
  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_speed(void) { return rgblight_config.speed; }
  457. void rgblight_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  458. rgblight_config.speed = speed;
  459. if (write_to_eeprom) {
  460. eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
  461. dprintf("rgblight set speed [EEPROM]: %u\n", rgblight_config.speed);
  462. } else {
  463. dprintf("rgblight set speed [NOEEPROM]: %u\n", rgblight_config.speed);
  464. }
  465. }
  466. void rgblight_set_speed(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, true); }
  467. void rgblight_set_speed_noeeprom(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, false); }
  468. uint8_t rgblight_get_hue(void) { return rgblight_config.hue; }
  469. uint8_t rgblight_get_sat(void) { return rgblight_config.sat; }
  470. uint8_t rgblight_get_val(void) { return rgblight_config.val; }
  471. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  472. if (!rgblight_config.enable) {
  473. return;
  474. }
  475. for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
  476. led[i].r = r;
  477. led[i].g = g;
  478. led[i].b = b;
  479. #ifdef RGBW
  480. led[i].w = 0;
  481. #endif
  482. }
  483. rgblight_set();
  484. }
  485. void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
  486. if (!rgblight_config.enable || index >= RGBLED_NUM) {
  487. return;
  488. }
  489. led[index].r = r;
  490. led[index].g = g;
  491. led[index].b = b;
  492. #ifdef RGBW
  493. led[index].w = 0;
  494. #endif
  495. rgblight_set();
  496. }
  497. void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index) {
  498. if (!rgblight_config.enable) {
  499. return;
  500. }
  501. LED_TYPE tmp_led;
  502. sethsv(hue, sat, val, &tmp_led);
  503. rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
  504. }
  505. #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
  506. static uint8_t get_interval_time(const uint8_t *default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) {
  507. return
  508. # ifdef VELOCIKEY_ENABLE
  509. velocikey_enabled() ? velocikey_match_speed(velocikey_min, velocikey_max) :
  510. # endif
  511. pgm_read_byte(default_interval_address);
  512. }
  513. #endif
  514. void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
  515. if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) {
  516. return;
  517. }
  518. for (uint8_t i = start; i < end; i++) {
  519. led[i].r = r;
  520. led[i].g = g;
  521. led[i].b = b;
  522. #ifdef RGBW
  523. led[i].w = 0;
  524. #endif
  525. }
  526. rgblight_set();
  527. wait_ms(1);
  528. }
  529. void rgblight_sethsv_range(uint8_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
  530. if (!rgblight_config.enable) {
  531. return;
  532. }
  533. LED_TYPE tmp_led;
  534. sethsv(hue, sat, val, &tmp_led);
  535. rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
  536. }
  537. #ifndef RGBLIGHT_SPLIT
  538. 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); }
  539. 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); }
  540. 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); }
  541. 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); }
  542. #endif // ifndef RGBLIGHT_SPLIT
  543. #ifndef RGBLIGHT_CUSTOM_DRIVER
  544. void rgblight_set(void) {
  545. LED_TYPE *start_led;
  546. uint16_t num_leds = clipping_num_leds;
  547. if (!rgblight_config.enable) {
  548. for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
  549. led[i].r = 0;
  550. led[i].g = 0;
  551. led[i].b = 0;
  552. # ifdef RGBW
  553. led[i].w = 0;
  554. # endif
  555. }
  556. }
  557. # ifdef RGBLIGHT_LED_MAP
  558. LED_TYPE led0[RGBLED_NUM];
  559. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  560. led0[i] = led[pgm_read_byte(&led_map[i])];
  561. }
  562. start_led = led0 + clipping_start_pos;
  563. # else
  564. start_led = led + clipping_start_pos;
  565. # endif
  566. # ifdef RGBW
  567. for (uint8_t i = 0; i < num_leds; i++) {
  568. convert_rgb_to_rgbw(&start_led[i]);
  569. }
  570. # endif
  571. ws2812_setleds(start_led, num_leds);
  572. }
  573. #endif
  574. #ifdef RGBLIGHT_SPLIT
  575. /* for split keyboard master side */
  576. uint8_t rgblight_get_change_flags(void) { return rgblight_status.change_flags; }
  577. void rgblight_clear_change_flags(void) { rgblight_status.change_flags = 0; }
  578. void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) {
  579. syncinfo->config = rgblight_config;
  580. syncinfo->status = rgblight_status;
  581. }
  582. /* for split keyboard slave side */
  583. void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) {
  584. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) {
  585. if (syncinfo->config.enable) {
  586. rgblight_config.enable = 1; // == rgblight_enable_noeeprom();
  587. rgblight_mode_eeprom_helper(syncinfo->config.mode, write_to_eeprom);
  588. } else {
  589. rgblight_disable_noeeprom();
  590. }
  591. }
  592. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_HSVS) {
  593. rgblight_sethsv_eeprom_helper(syncinfo->config.hue, syncinfo->config.sat, syncinfo->config.val, write_to_eeprom);
  594. // rgblight_config.speed = config->speed; // NEED???
  595. }
  596. # ifdef RGBLIGHT_USE_TIMER
  597. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_TIMER) {
  598. if (syncinfo->status.timer_enabled) {
  599. rgblight_timer_enable();
  600. } else {
  601. rgblight_timer_disable();
  602. }
  603. }
  604. # ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
  605. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_ANIMATION_TICK) {
  606. animation_status.restart = true;
  607. }
  608. # endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
  609. # endif /* RGBLIGHT_USE_TIMER */
  610. }
  611. #endif /* RGBLIGHT_SPLIT */
  612. #ifdef RGBLIGHT_USE_TIMER
  613. typedef void (*effect_func_t)(animation_status_t *anim);
  614. // Animation timer -- use system timer (AVR Timer0)
  615. void rgblight_timer_init(void) {
  616. // OLD!!!! Animation timer -- AVR Timer3
  617. // static uint8_t rgblight_timer_is_init = 0;
  618. // if (rgblight_timer_is_init) {
  619. // return;
  620. // }
  621. // rgblight_timer_is_init = 1;
  622. // /* Timer 3 setup */
  623. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  624. // | _BV(CS30); // Clock selelct: clk/1
  625. // /* Set TOP value */
  626. // uint8_t sreg = SREG;
  627. // cli();
  628. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  629. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  630. // SREG = sreg;
  631. rgblight_status.timer_enabled = false;
  632. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  633. }
  634. void rgblight_timer_enable(void) {
  635. if (!is_static_effect(rgblight_config.mode)) {
  636. rgblight_status.timer_enabled = true;
  637. }
  638. animation_status.last_timer = timer_read();
  639. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  640. dprintf("rgblight timer enabled.\n");
  641. }
  642. void rgblight_timer_disable(void) {
  643. rgblight_status.timer_enabled = false;
  644. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  645. dprintf("rgblight timer disable.\n");
  646. }
  647. void rgblight_timer_toggle(void) {
  648. dprintf("rgblight timer toggle.\n");
  649. if (rgblight_status.timer_enabled) {
  650. rgblight_timer_disable();
  651. } else {
  652. rgblight_timer_enable();
  653. }
  654. }
  655. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  656. rgblight_enable();
  657. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  658. rgblight_setrgb(r, g, b);
  659. }
  660. static void rgblight_effect_dummy(animation_status_t *anim) {
  661. // do nothing
  662. /********
  663. dprintf("rgblight_task() what happened?\n");
  664. dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
  665. dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
  666. rgblight_config.mode, rgblight_status.base_mode,
  667. rgblight_status.timer_enabled);
  668. dprintf("last_timer = %d\n",anim->last_timer);
  669. **/
  670. }
  671. void rgblight_task(void) {
  672. if (rgblight_status.timer_enabled) {
  673. effect_func_t effect_func = rgblight_effect_dummy;
  674. uint16_t interval_time = 2000; // dummy interval
  675. uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
  676. animation_status.delta = delta;
  677. // static light mode, do nothing here
  678. if (1 == 0) { // dummy
  679. }
  680. # ifdef RGBLIGHT_EFFECT_BREATHING
  681. else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
  682. // breathing mode
  683. interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
  684. effect_func = rgblight_effect_breathing;
  685. }
  686. # endif
  687. # ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  688. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
  689. // rainbow mood mode
  690. interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
  691. effect_func = rgblight_effect_rainbow_mood;
  692. }
  693. # endif
  694. # ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  695. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
  696. // rainbow swirl mode
  697. interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
  698. effect_func = rgblight_effect_rainbow_swirl;
  699. }
  700. # endif
  701. # ifdef RGBLIGHT_EFFECT_SNAKE
  702. else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
  703. // snake mode
  704. interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
  705. effect_func = rgblight_effect_snake;
  706. }
  707. # endif
  708. # ifdef RGBLIGHT_EFFECT_KNIGHT
  709. else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
  710. // knight mode
  711. interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
  712. effect_func = rgblight_effect_knight;
  713. }
  714. # endif
  715. # ifdef RGBLIGHT_EFFECT_CHRISTMAS
  716. else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
  717. // christmas mode
  718. interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
  719. effect_func = (effect_func_t)rgblight_effect_christmas;
  720. }
  721. # endif
  722. # ifdef RGBLIGHT_EFFECT_RGB_TEST
  723. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
  724. // RGB test mode
  725. interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
  726. effect_func = (effect_func_t)rgblight_effect_rgbtest;
  727. }
  728. # endif
  729. # ifdef RGBLIGHT_EFFECT_ALTERNATING
  730. else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING) {
  731. interval_time = 500;
  732. effect_func = (effect_func_t)rgblight_effect_alternating;
  733. }
  734. # endif
  735. if (animation_status.restart) {
  736. animation_status.restart = false;
  737. animation_status.last_timer = timer_read() - interval_time - 1;
  738. animation_status.pos16 = 0; // restart signal to local each effect
  739. }
  740. if (timer_elapsed(animation_status.last_timer) >= interval_time) {
  741. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  742. static uint16_t report_last_timer = 0;
  743. static bool tick_flag = false;
  744. uint16_t oldpos16;
  745. if (tick_flag) {
  746. tick_flag = false;
  747. if (timer_elapsed(report_last_timer) >= 30000) {
  748. report_last_timer = timer_read();
  749. dprintf("rgblight animation tick report to slave\n");
  750. RGBLIGHT_SPLIT_ANIMATION_TICK;
  751. }
  752. }
  753. oldpos16 = animation_status.pos16;
  754. # endif
  755. animation_status.last_timer += interval_time;
  756. effect_func(&animation_status);
  757. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  758. if (animation_status.pos16 == 0 && oldpos16 != 0) {
  759. tick_flag = true;
  760. }
  761. # endif
  762. }
  763. }
  764. }
  765. #endif /* RGBLIGHT_USE_TIMER */
  766. // Effects
  767. #ifdef RGBLIGHT_EFFECT_BREATHING
  768. # ifndef RGBLIGHT_EFFECT_BREATHE_CENTER
  769. # ifndef RGBLIGHT_BREATHE_TABLE_SIZE
  770. # define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256 or 128 or 64
  771. # endif
  772. # include <rgblight_breathe_table.h>
  773. # endif
  774. __attribute__((weak)) const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  775. void rgblight_effect_breathing(animation_status_t *anim) {
  776. float val;
  777. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  778. # ifdef RGBLIGHT_EFFECT_BREATHE_TABLE
  779. val = pgm_read_byte(&rgblight_effect_breathe_table[anim->pos / table_scale]);
  780. # else
  781. val = (exp(sin((anim->pos / 255.0) * M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER / M_E) * (RGBLIGHT_EFFECT_BREATHE_MAX / (M_E - 1 / M_E));
  782. # endif
  783. rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
  784. anim->pos = (anim->pos + 1);
  785. }
  786. #endif
  787. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  788. __attribute__((weak)) const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  789. void rgblight_effect_rainbow_mood(animation_status_t *anim) {
  790. rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
  791. anim->current_hue++;
  792. }
  793. #endif
  794. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  795. # ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
  796. # define RGBLIGHT_RAINBOW_SWIRL_RANGE 255
  797. # endif
  798. __attribute__((weak)) const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  799. void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
  800. uint8_t hue;
  801. uint8_t i;
  802. for (i = 0; i < effect_num_leds; i++) {
  803. hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / effect_num_leds * i + anim->current_hue);
  804. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
  805. }
  806. rgblight_set();
  807. if (anim->delta % 2) {
  808. anim->current_hue++;
  809. } else {
  810. anim->current_hue--;
  811. }
  812. }
  813. #endif
  814. #ifdef RGBLIGHT_EFFECT_SNAKE
  815. __attribute__((weak)) const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  816. void rgblight_effect_snake(animation_status_t *anim) {
  817. static uint8_t pos = 0;
  818. uint8_t i, j;
  819. int8_t k;
  820. int8_t increment = 1;
  821. if (anim->delta % 2) {
  822. increment = -1;
  823. }
  824. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  825. if (anim->pos == 0) { // restart signal
  826. if (increment == 1) {
  827. pos = effect_num_leds - 1;
  828. } else {
  829. pos = 0;
  830. }
  831. anim->pos = 1;
  832. }
  833. # endif
  834. for (i = 0; i < effect_num_leds; i++) {
  835. LED_TYPE *ledp = led + i + effect_start_pos;
  836. ledp->r = 0;
  837. ledp->g = 0;
  838. ledp->b = 0;
  839. # ifdef RGBW
  840. ledp->w = 0;
  841. # endif
  842. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  843. k = pos + j * increment;
  844. if (k > RGBLED_NUM) {
  845. k = k % RGBLED_NUM;
  846. }
  847. if (k < 0) {
  848. k = k + effect_num_leds;
  849. }
  850. if (i == k) {
  851. sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val * (RGBLIGHT_EFFECT_SNAKE_LENGTH - j) / RGBLIGHT_EFFECT_SNAKE_LENGTH), ledp);
  852. }
  853. }
  854. }
  855. rgblight_set();
  856. if (increment == 1) {
  857. if (pos - 1 < 0) {
  858. pos = effect_num_leds - 1;
  859. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  860. anim->pos = 0;
  861. # endif
  862. } else {
  863. pos -= 1;
  864. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  865. anim->pos = 1;
  866. # endif
  867. }
  868. } else {
  869. pos = (pos + 1) % effect_num_leds;
  870. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  871. anim->pos = pos;
  872. # endif
  873. }
  874. }
  875. #endif
  876. #ifdef RGBLIGHT_EFFECT_KNIGHT
  877. __attribute__((weak)) const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
  878. void rgblight_effect_knight(animation_status_t *anim) {
  879. static int8_t low_bound = 0;
  880. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  881. static int8_t increment = 1;
  882. uint8_t i, cur;
  883. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  884. if (anim->pos == 0) { // restart signal
  885. anim->pos = 1;
  886. low_bound = 0;
  887. high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  888. increment = 1;
  889. }
  890. # endif
  891. // Set all the LEDs to 0
  892. for (i = effect_start_pos; i < effect_end_pos; i++) {
  893. led[i].r = 0;
  894. led[i].g = 0;
  895. led[i].b = 0;
  896. # ifdef RGBW
  897. led[i].w = 0;
  898. # endif
  899. }
  900. // Determine which LEDs should be lit up
  901. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  902. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % effect_num_leds + effect_start_pos;
  903. if (i >= low_bound && i <= high_bound) {
  904. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  905. } else {
  906. led[cur].r = 0;
  907. led[cur].g = 0;
  908. led[cur].b = 0;
  909. # ifdef RGBW
  910. led[cur].w = 0;
  911. # endif
  912. }
  913. }
  914. rgblight_set();
  915. // Move from low_bound to high_bound changing the direction we increment each
  916. // time a boundary is hit.
  917. low_bound += increment;
  918. high_bound += increment;
  919. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  920. increment = -increment;
  921. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  922. if (increment == 1) {
  923. anim->pos = 0;
  924. }
  925. # endif
  926. }
  927. }
  928. #endif
  929. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  930. void rgblight_effect_christmas(animation_status_t *anim) {
  931. uint8_t hue;
  932. uint8_t i;
  933. anim->current_offset = (anim->current_offset + 1) % 2;
  934. for (i = 0; i < effect_num_leds; i++) {
  935. hue = 0 + ((i / RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85;
  936. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
  937. }
  938. rgblight_set();
  939. }
  940. #endif
  941. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  942. __attribute__((weak)) const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
  943. void rgblight_effect_rgbtest(animation_status_t *anim) {
  944. static uint8_t maxval = 0;
  945. uint8_t g;
  946. uint8_t r;
  947. uint8_t b;
  948. if (maxval == 0) {
  949. LED_TYPE tmp_led;
  950. sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
  951. maxval = tmp_led.r;
  952. }
  953. g = r = b = 0;
  954. switch (anim->pos) {
  955. case 0:
  956. r = maxval;
  957. break;
  958. case 1:
  959. g = maxval;
  960. break;
  961. case 2:
  962. b = maxval;
  963. break;
  964. }
  965. rgblight_setrgb(r, g, b);
  966. anim->pos = (anim->pos + 1) % 3;
  967. }
  968. #endif
  969. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  970. void rgblight_effect_alternating(animation_status_t *anim) {
  971. for (int i = 0; i < effect_num_leds; i++) {
  972. LED_TYPE *ledp = led + i + effect_start_pos;
  973. if (i < effect_num_leds / 2 && anim->pos) {
  974. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
  975. } else if (i >= effect_num_leds / 2 && !anim->pos) {
  976. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
  977. } else {
  978. sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp);
  979. }
  980. }
  981. rgblight_set();
  982. anim->pos = (anim->pos + 1) % 2;
  983. }
  984. #endif