rgblight.c 39 KB

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