rgblight.c 40 KB

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