rgblight.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. #include <stdlib.h>
  19. #ifdef __AVR__
  20. # include <avr/eeprom.h>
  21. # include <avr/interrupt.h>
  22. #endif
  23. #ifdef EEPROM_ENABLE
  24. # include "eeprom.h"
  25. #endif
  26. #include "wait.h"
  27. #include "progmem.h"
  28. #include "timer.h"
  29. #include "rgblight.h"
  30. #include "color.h"
  31. #include "debug.h"
  32. #include "led_tables.h"
  33. #include <lib/lib8tion/lib8tion.h>
  34. #ifdef VELOCIKEY_ENABLE
  35. # include "velocikey.h"
  36. #endif
  37. // MxSS custom
  38. #include "mxss_frontled.h"
  39. #ifndef MIN
  40. # define MIN(a, b) (((a) < (b)) ? (a) : (b))
  41. #endif
  42. #ifdef RGBLIGHT_SPLIT
  43. /* for split keyboard */
  44. # define RGBLIGHT_SPLIT_SET_CHANGE_MODE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_MODE
  45. # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_HSVS
  46. # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS rgblight_status.change_flags |= (RGBLIGHT_STATUS_CHANGE_MODE | RGBLIGHT_STATUS_CHANGE_HSVS)
  47. # define RGBLIGHT_SPLIT_SET_CHANGE_LAYERS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_LAYERS
  48. # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_TIMER
  49. # define RGBLIGHT_SPLIT_ANIMATION_TICK rgblight_status.change_flags |= RGBLIGHT_STATUS_ANIMATION_TICK
  50. #else
  51. # define RGBLIGHT_SPLIT_SET_CHANGE_MODE
  52. # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS
  53. # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
  54. # define RGBLIGHT_SPLIT_SET_CHANGE_LAYERS
  55. # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
  56. # define RGBLIGHT_SPLIT_ANIMATION_TICK
  57. #endif
  58. #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
  59. #define _RGBM_SINGLE_DYNAMIC(sym)
  60. #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
  61. #define _RGBM_MULTI_DYNAMIC(sym)
  62. #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##sym,
  63. #define _RGBM_TMP_DYNAMIC(sym, msym)
  64. static uint8_t static_effect_table[] = {
  65. #include "rgblight_modes.h"
  66. };
  67. #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
  68. #define _RGBM_SINGLE_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
  69. #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
  70. #define _RGBM_MULTI_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
  71. #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##msym,
  72. #define _RGBM_TMP_DYNAMIC(sym, msym) RGBLIGHT_MODE_##msym,
  73. static uint8_t mode_base_table[] = {
  74. 0, // RGBLIGHT_MODE_zero
  75. #include "rgblight_modes.h"
  76. };
  77. static inline int is_static_effect(uint8_t mode) { return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL; }
  78. #ifdef RGBLIGHT_LED_MAP
  79. const uint8_t led_map[] PROGMEM = RGBLIGHT_LED_MAP;
  80. #endif
  81. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  82. __attribute__((weak)) const uint8_t RGBLED_GRADIENT_RANGES[] PROGMEM = {255, 170, 127, 85, 64};
  83. #endif
  84. rgblight_config_t rgblight_config;
  85. rgblight_status_t rgblight_status = {.timer_enabled = false};
  86. bool is_rgblight_initialized = false;
  87. #ifdef RGBLIGHT_USE_TIMER
  88. animation_status_t animation_status = {};
  89. #endif
  90. #ifndef LED_ARRAY
  91. LED_TYPE led[RGBLED_NUM];
  92. # define LED_ARRAY led
  93. #endif
  94. #ifdef RGBLIGHT_LAYERS
  95. rgblight_segment_t const *const *rgblight_layers = NULL;
  96. #endif
  97. rgblight_ranges_t rgblight_ranges = {0, RGBLED_NUM, 0, RGBLED_NUM, RGBLED_NUM};
  98. // MxSS custom
  99. extern uint8_t fled_mode;
  100. extern uint8_t fled_val;
  101. extern LED_TYPE fleds[2];
  102. hs_set fled_hs[2];
  103. void copyrgb(LED_TYPE *src, LED_TYPE *dst) {
  104. dst->r = src->r;
  105. dst->g = src->g;
  106. dst->b = src->b;
  107. }
  108. void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) {
  109. rgblight_ranges.clipping_start_pos = start_pos;
  110. rgblight_ranges.clipping_num_leds = num_leds;
  111. }
  112. void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds) {
  113. if (start_pos >= RGBLED_NUM) return;
  114. if (start_pos + num_leds > RGBLED_NUM) return;
  115. rgblight_ranges.effect_start_pos = start_pos;
  116. rgblight_ranges.effect_end_pos = start_pos + num_leds;
  117. rgblight_ranges.effect_num_leds = num_leds;
  118. }
  119. __attribute__((weak)) RGB rgblight_hsv_to_rgb(HSV hsv) { return hsv_to_rgb(hsv); }
  120. void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
  121. HSV hsv = {hue, sat, val};
  122. // MxSS custom
  123. // if led is front leds, cache the hue and sat values
  124. if (led1 == &led[RGBLIGHT_FLED1]) {
  125. fled_hs[0].hue = hue;
  126. fled_hs[0].sat = sat;
  127. } else if (led1 == &led[RGBLIGHT_FLED2]) {
  128. fled_hs[1].hue = hue;
  129. fled_hs[1].sat = sat;
  130. }
  131. RGB rgb = rgblight_hsv_to_rgb(hsv);
  132. setrgb(rgb.r, rgb.g, rgb.b, led1);
  133. }
  134. 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); }
  135. void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
  136. led1->r = r;
  137. led1->g = g;
  138. led1->b = b;
  139. #ifdef RGBW
  140. led1->w = 0;
  141. #endif
  142. }
  143. void rgblight_check_config(void) {
  144. /* Add some out of bound checks for RGB light config */
  145. if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
  146. rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
  147. } else if (rgblight_config.mode > RGBLIGHT_MODES) {
  148. rgblight_config.mode = RGBLIGHT_MODES;
  149. }
  150. if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
  151. rgblight_config.val = RGBLIGHT_LIMIT_VAL;
  152. }
  153. }
  154. uint64_t eeconfig_read_rgblight(void) {
  155. #ifdef EEPROM_ENABLE
  156. return (uint64_t)((eeprom_read_dword(EECONFIG_RGBLIGHT)) | ((uint64_t)eeprom_read_byte(EECONFIG_RGBLIGHT_EXTENDED) << 32));
  157. #else
  158. return 0;
  159. #endif
  160. }
  161. void eeconfig_update_rgblight(uint64_t val) {
  162. #ifdef EEPROM_ENABLE
  163. rgblight_check_config();
  164. eeprom_update_dword(EECONFIG_RGBLIGHT, val & 0xFFFFFFFF);
  165. eeprom_update_byte(EECONFIG_RGBLIGHT_EXTENDED, (val >> 32) & 0xFF);
  166. #endif
  167. }
  168. void eeconfig_update_rgblight_current(void) { eeconfig_update_rgblight(rgblight_config.raw); }
  169. void eeconfig_update_rgblight_default(void) {
  170. rgblight_config.enable = 1;
  171. rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
  172. rgblight_config.hue = 0;
  173. rgblight_config.sat = UINT8_MAX;
  174. rgblight_config.val = RGBLIGHT_LIMIT_VAL;
  175. rgblight_config.speed = 0;
  176. RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
  177. eeconfig_update_rgblight(rgblight_config.raw);
  178. }
  179. void eeconfig_debug_rgblight(void) {
  180. dprintf("rgblight_config EEPROM:\n");
  181. dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
  182. dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
  183. dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
  184. dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
  185. dprintf("rgblight_config.val = %d\n", rgblight_config.val);
  186. dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
  187. }
  188. void rgblight_init(void) {
  189. /* if already initialized, don't do it again.
  190. If you must do it again, extern this and set to false, first.
  191. This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
  192. if (is_rgblight_initialized) {
  193. return;
  194. }
  195. dprintf("rgblight_init called.\n");
  196. dprintf("rgblight_init start!\n");
  197. if (!eeconfig_is_enabled()) {
  198. dprintf("rgblight_init eeconfig is not enabled.\n");
  199. eeconfig_init();
  200. eeconfig_update_rgblight_default();
  201. }
  202. rgblight_config.raw = eeconfig_read_rgblight();
  203. RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
  204. if (!rgblight_config.mode) {
  205. dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
  206. eeconfig_update_rgblight_default();
  207. rgblight_config.raw = eeconfig_read_rgblight();
  208. }
  209. rgblight_check_config();
  210. eeconfig_debug_rgblight(); // display current eeprom values
  211. rgblight_timer_init(); // setup the timer
  212. if (rgblight_config.enable) {
  213. rgblight_mode_noeeprom(rgblight_config.mode);
  214. }
  215. is_rgblight_initialized = true;
  216. }
  217. uint32_t rgblight_read_dword(void) { return rgblight_config.raw; }
  218. void rgblight_update_dword(uint32_t dword) {
  219. RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
  220. rgblight_config.raw = dword;
  221. if (rgblight_config.enable)
  222. rgblight_mode_noeeprom(rgblight_config.mode);
  223. else {
  224. rgblight_timer_disable();
  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. rgblight_timer_disable();
  289. } else {
  290. rgblight_timer_enable();
  291. }
  292. #ifdef RGBLIGHT_USE_TIMER
  293. animation_status.restart = true;
  294. #endif
  295. rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  296. }
  297. void rgblight_mode(uint8_t mode) { rgblight_mode_eeprom_helper(mode, true); }
  298. void rgblight_mode_noeeprom(uint8_t mode) { rgblight_mode_eeprom_helper(mode, false); }
  299. void rgblight_toggle(void) {
  300. dprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
  301. if (rgblight_config.enable) {
  302. rgblight_disable();
  303. } else {
  304. rgblight_enable();
  305. }
  306. }
  307. void rgblight_toggle_noeeprom(void) {
  308. dprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
  309. if (rgblight_config.enable) {
  310. rgblight_disable_noeeprom();
  311. } else {
  312. rgblight_enable_noeeprom();
  313. }
  314. }
  315. void rgblight_enable(void) {
  316. rgblight_config.enable = 1;
  317. // No need to update EEPROM here. rgblight_mode() will do that, actually
  318. // eeconfig_update_rgblight(rgblight_config.raw);
  319. dprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  320. rgblight_mode(rgblight_config.mode);
  321. }
  322. void rgblight_enable_noeeprom(void) {
  323. rgblight_config.enable = 1;
  324. dprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  325. rgblight_mode_noeeprom(rgblight_config.mode);
  326. }
  327. void rgblight_disable(void) {
  328. rgblight_config.enable = 0;
  329. eeconfig_update_rgblight(rgblight_config.raw);
  330. dprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  331. rgblight_timer_disable();
  332. RGBLIGHT_SPLIT_SET_CHANGE_MODE;
  333. wait_ms(50);
  334. rgblight_set();
  335. }
  336. void rgblight_disable_noeeprom(void) {
  337. rgblight_config.enable = 0;
  338. dprintf("rgblight disable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  339. rgblight_timer_disable();
  340. RGBLIGHT_SPLIT_SET_CHANGE_MODE;
  341. wait_ms(50);
  342. rgblight_set();
  343. }
  344. bool rgblight_is_enabled(void) { return rgblight_config.enable; }
  345. void rgblight_increase_hue_helper(bool write_to_eeprom) {
  346. uint8_t hue = rgblight_config.hue + RGBLIGHT_HUE_STEP;
  347. rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
  348. }
  349. void rgblight_increase_hue_noeeprom(void) { rgblight_increase_hue_helper(false); }
  350. void rgblight_increase_hue(void) { rgblight_increase_hue_helper(true); }
  351. void rgblight_decrease_hue_helper(bool write_to_eeprom) {
  352. uint8_t hue = rgblight_config.hue - RGBLIGHT_HUE_STEP;
  353. rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
  354. }
  355. void rgblight_decrease_hue_noeeprom(void) { rgblight_decrease_hue_helper(false); }
  356. void rgblight_decrease_hue(void) { rgblight_decrease_hue_helper(true); }
  357. void rgblight_increase_sat_helper(bool write_to_eeprom) {
  358. uint8_t sat = qadd8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
  359. rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
  360. }
  361. void rgblight_increase_sat_noeeprom(void) { rgblight_increase_sat_helper(false); }
  362. void rgblight_increase_sat(void) { rgblight_increase_sat_helper(true); }
  363. void rgblight_decrease_sat_helper(bool write_to_eeprom) {
  364. uint8_t sat = qsub8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
  365. rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
  366. }
  367. void rgblight_decrease_sat_noeeprom(void) { rgblight_decrease_sat_helper(false); }
  368. void rgblight_decrease_sat(void) { rgblight_decrease_sat_helper(true); }
  369. void rgblight_increase_val_helper(bool write_to_eeprom) {
  370. uint8_t val = qadd8(rgblight_config.val, RGBLIGHT_VAL_STEP);
  371. rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
  372. }
  373. void rgblight_increase_val_noeeprom(void) { rgblight_increase_val_helper(false); }
  374. void rgblight_increase_val(void) { rgblight_increase_val_helper(true); }
  375. void rgblight_decrease_val_helper(bool write_to_eeprom) {
  376. uint8_t val = qsub8(rgblight_config.val, RGBLIGHT_VAL_STEP);
  377. rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
  378. }
  379. void rgblight_decrease_val_noeeprom(void) { rgblight_decrease_val_helper(false); }
  380. void rgblight_decrease_val(void) { rgblight_decrease_val_helper(true); }
  381. void rgblight_increase_speed_helper(bool write_to_eeprom) {
  382. if (rgblight_config.speed < 3) rgblight_config.speed++;
  383. // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?
  384. if (write_to_eeprom) {
  385. eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
  386. }
  387. }
  388. void rgblight_increase_speed(void) { rgblight_increase_speed_helper(true); }
  389. void rgblight_increase_speed_noeeprom(void) { rgblight_increase_speed_helper(false); }
  390. void rgblight_decrease_speed_helper(bool write_to_eeprom) {
  391. if (rgblight_config.speed > 0) rgblight_config.speed--;
  392. // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED??
  393. if (write_to_eeprom) {
  394. eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
  395. }
  396. }
  397. void rgblight_decrease_speed(void) { rgblight_decrease_speed_helper(true); }
  398. void rgblight_decrease_speed_noeeprom(void) { rgblight_decrease_speed_helper(false); }
  399. void rgblight_sethsv_noeeprom_old(uint8_t hue, uint8_t sat, uint8_t val) {
  400. if (rgblight_config.enable) {
  401. // MxSS custom code
  402. fled_hs[0].hue = fled_hs[1].hue = hue;
  403. fled_hs[0].sat = fled_hs[1].sat = sat;
  404. LED_TYPE tmp_led;
  405. sethsv(hue, sat, val, &tmp_led);
  406. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  407. }
  408. }
  409. void rgblight_sethsv_eeprom_helper(uint8_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
  410. if (rgblight_config.enable) {
  411. rgblight_status.base_mode = mode_base_table[rgblight_config.mode];
  412. if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
  413. // same static color
  414. LED_TYPE tmp_led;
  415. sethsv(hue, sat, val, &tmp_led);
  416. // MxSS custom
  417. // Cache hue/sat for rgb
  418. fled_hs[0].hue = fled_hs[1].hue = hue;
  419. fled_hs[0].sat = fled_hs[1].sat = sat;
  420. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  421. } else {
  422. // all LEDs in same color
  423. if (1 == 0) { // dummy
  424. }
  425. #ifdef RGBLIGHT_EFFECT_BREATHING
  426. else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
  427. // breathing mode, ignore the change of val, use in memory value instead
  428. val = rgblight_config.val;
  429. }
  430. #endif
  431. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  432. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
  433. // rainbow mood, ignore the change of hue
  434. hue = rgblight_config.hue;
  435. }
  436. #endif
  437. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  438. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
  439. // rainbow swirl, ignore the change of hue
  440. hue = rgblight_config.hue;
  441. }
  442. #endif
  443. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  444. else if (rgblight_status.base_mode == RGBLIGHT_MODE_STATIC_GRADIENT) {
  445. // static gradient
  446. uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
  447. bool direction = (delta % 2) == 0;
  448. # ifdef __AVR__
  449. // probably due to how pgm_read_word is defined for ARM, but the ARM compiler really hates this line
  450. uint8_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[delta / 2]);
  451. # else
  452. uint8_t range = RGBLED_GRADIENT_RANGES[delta / 2];
  453. # endif
  454. for (uint8_t i = 0; i < rgblight_ranges.effect_num_leds; i++) {
  455. uint8_t _hue = ((uint16_t)i * (uint16_t)range) / rgblight_ranges.effect_num_leds;
  456. if (direction) {
  457. _hue = hue + _hue;
  458. } else {
  459. _hue = hue - _hue;
  460. }
  461. dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i, _hue, direction, range);
  462. sethsv(_hue, sat, val, (LED_TYPE *)&led[i + rgblight_ranges.effect_start_pos]);
  463. }
  464. rgblight_set();
  465. }
  466. #endif
  467. }
  468. #ifdef RGBLIGHT_SPLIT
  469. if (rgblight_config.hue != hue || rgblight_config.sat != sat || rgblight_config.val != val) {
  470. RGBLIGHT_SPLIT_SET_CHANGE_HSVS;
  471. }
  472. #endif
  473. rgblight_config.hue = hue;
  474. rgblight_config.sat = sat;
  475. rgblight_config.val = val;
  476. if (write_to_eeprom) {
  477. eeconfig_update_rgblight(rgblight_config.raw);
  478. dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  479. } else {
  480. dprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  481. }
  482. }
  483. }
  484. void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, true); }
  485. void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, false); }
  486. uint8_t rgblight_get_speed(void) { return rgblight_config.speed; }
  487. void rgblight_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
  488. rgblight_config.speed = speed;
  489. if (write_to_eeprom) {
  490. eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
  491. dprintf("rgblight set speed [EEPROM]: %u\n", rgblight_config.speed);
  492. } else {
  493. dprintf("rgblight set speed [NOEEPROM]: %u\n", rgblight_config.speed);
  494. }
  495. }
  496. void rgblight_set_speed(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, true); }
  497. void rgblight_set_speed_noeeprom(uint8_t speed) { rgblight_set_speed_eeprom_helper(speed, false); }
  498. uint8_t rgblight_get_hue(void) { return rgblight_config.hue; }
  499. uint8_t rgblight_get_sat(void) { return rgblight_config.sat; }
  500. uint8_t rgblight_get_val(void) { return rgblight_config.val; }
  501. HSV rgblight_get_hsv(void) { return (HSV){rgblight_config.hue, rgblight_config.sat, rgblight_config.val}; }
  502. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  503. if (!rgblight_config.enable) {
  504. return;
  505. }
  506. for (uint8_t i = rgblight_ranges.effect_start_pos; i < rgblight_ranges.effect_end_pos; i++) {
  507. led[i].r = r;
  508. led[i].g = g;
  509. led[i].b = b;
  510. #ifdef RGBW
  511. led[i].w = 0;
  512. #endif
  513. }
  514. rgblight_set();
  515. }
  516. void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
  517. if (!rgblight_config.enable || index >= RGBLED_NUM) {
  518. return;
  519. }
  520. led[index].r = r;
  521. led[index].g = g;
  522. led[index].b = b;
  523. #ifdef RGBW
  524. led[index].w = 0;
  525. #endif
  526. rgblight_set();
  527. }
  528. void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index) {
  529. if (!rgblight_config.enable) {
  530. return;
  531. }
  532. LED_TYPE tmp_led;
  533. sethsv(hue, sat, val, &tmp_led);
  534. rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
  535. }
  536. #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT) || defined(RGBLIGHT_EFFECT_TWINKLE)
  537. static uint8_t get_interval_time(const uint8_t *default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) {
  538. return
  539. # ifdef VELOCIKEY_ENABLE
  540. velocikey_enabled() ? velocikey_match_speed(velocikey_min, velocikey_max) :
  541. # endif
  542. pgm_read_byte(default_interval_address);
  543. }
  544. #endif
  545. void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
  546. if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) {
  547. return;
  548. }
  549. for (uint8_t i = start; i < end; i++) {
  550. led[i].r = r;
  551. led[i].g = g;
  552. led[i].b = b;
  553. #ifdef RGBW
  554. led[i].w = 0;
  555. #endif
  556. }
  557. rgblight_set();
  558. wait_ms(1);
  559. }
  560. void rgblight_sethsv_range(uint8_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
  561. if (!rgblight_config.enable) {
  562. return;
  563. }
  564. LED_TYPE tmp_led;
  565. sethsv(hue, sat, val, &tmp_led);
  566. rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
  567. }
  568. #ifndef RGBLIGHT_SPLIT
  569. 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); }
  570. 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); }
  571. 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); }
  572. 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); }
  573. #endif // ifndef RGBLIGHT_SPLIT
  574. #ifdef RGBLIGHT_LAYERS
  575. void rgblight_set_layer_state(uint8_t layer, bool enabled) {
  576. rgblight_layer_mask_t mask = 1 << layer;
  577. if (enabled) {
  578. rgblight_status.enabled_layer_mask |= mask;
  579. } else {
  580. rgblight_status.enabled_layer_mask &= ~mask;
  581. }
  582. RGBLIGHT_SPLIT_SET_CHANGE_LAYERS;
  583. // Static modes don't have a ticker running to update the LEDs
  584. if (rgblight_status.timer_enabled == false) {
  585. rgblight_mode_noeeprom(rgblight_config.mode);
  586. }
  587. # ifdef RGBLIGHT_LAYERS_OVERRIDE_RGB_OFF
  588. // If not enabled, then nothing else will actually set the LEDs...
  589. if (!rgblight_config.enable) {
  590. rgblight_set();
  591. }
  592. # endif
  593. }
  594. bool rgblight_get_layer_state(uint8_t layer) {
  595. rgblight_layer_mask_t mask = 1 << layer;
  596. return (rgblight_status.enabled_layer_mask & mask) != 0;
  597. }
  598. // Write any enabled LED layers into the buffer
  599. static void rgblight_layers_write(void) {
  600. uint8_t i = 0;
  601. // For each layer
  602. for (const rgblight_segment_t *const *layer_ptr = rgblight_layers; i < RGBLIGHT_MAX_LAYERS; layer_ptr++, i++) {
  603. if (!rgblight_get_layer_state(i)) {
  604. continue; // Layer is disabled
  605. }
  606. const rgblight_segment_t *segment_ptr = pgm_read_ptr(layer_ptr);
  607. if (segment_ptr == NULL) {
  608. break; // No more layers
  609. }
  610. // For each segment
  611. while (1) {
  612. rgblight_segment_t segment;
  613. memcpy_P(&segment, segment_ptr, sizeof(rgblight_segment_t));
  614. if (segment.index == RGBLIGHT_END_SEGMENT_INDEX) {
  615. break; // No more segments
  616. }
  617. // Write segment.count LEDs
  618. LED_TYPE *const limit = &led[MIN(segment.index + segment.count, RGBLED_NUM)];
  619. for (LED_TYPE *led_ptr = &led[segment.index]; led_ptr < limit; led_ptr++) {
  620. sethsv(segment.hue, segment.sat, segment.val, led_ptr);
  621. }
  622. segment_ptr++;
  623. }
  624. }
  625. }
  626. # ifdef RGBLIGHT_LAYER_BLINK
  627. rgblight_layer_mask_t _blinked_layer_mask = 0;
  628. uint16_t _blink_duration = 0;
  629. static uint16_t _blink_timer;
  630. void rgblight_blink_layer(uint8_t layer, uint16_t duration_ms) {
  631. rgblight_set_layer_state(layer, true);
  632. _blinked_layer_mask |= 1 << layer;
  633. _blink_timer = timer_read();
  634. _blink_duration = duration_ms;
  635. }
  636. void rgblight_unblink_layers(void) {
  637. if (_blinked_layer_mask != 0 && timer_elapsed(_blink_timer) > _blink_duration) {
  638. for (uint8_t layer = 0; layer < RGBLIGHT_MAX_LAYERS; layer++) {
  639. if ((_blinked_layer_mask & 1 << layer) != 0) {
  640. rgblight_set_layer_state(layer, false);
  641. }
  642. }
  643. _blinked_layer_mask = 0;
  644. }
  645. }
  646. # endif
  647. #endif
  648. __attribute__((weak)) void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds) { ws2812_setleds(start_led, num_leds); }
  649. #ifndef RGBLIGHT_CUSTOM_DRIVER
  650. void rgblight_set(void) {
  651. LED_TYPE *start_led;
  652. uint8_t num_leds = rgblight_ranges.clipping_num_leds;
  653. if (!rgblight_config.enable) {
  654. for (uint8_t i = rgblight_ranges.effect_start_pos; i < rgblight_ranges.effect_end_pos; i++) {
  655. if (i == RGBLIGHT_FLED1 && i == RGBLIGHT_FLED2)
  656. continue;
  657. led[i].r = 0;
  658. led[i].g = 0;
  659. led[i].b = 0;
  660. # ifdef RGBW
  661. led[i].w = 0;
  662. # endif
  663. }
  664. }
  665. # ifdef RGBLIGHT_LAYERS
  666. if (rgblight_layers != NULL
  667. # ifndef RGBLIGHT_LAYERS_OVERRIDE_RGB_OFF
  668. && rgblight_config.enable
  669. # endif
  670. ) {
  671. rgblight_layers_write();
  672. }
  673. # endif
  674. # ifdef RGBLIGHT_LED_MAP
  675. LED_TYPE led0[RGBLED_NUM];
  676. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  677. led0[i] = led[pgm_read_byte(&led_map[i])];
  678. }
  679. start_led = led0 + rgblight_ranges.clipping_start_pos;
  680. # else
  681. start_led = led + rgblight_ranges.clipping_start_pos;
  682. # endif
  683. # ifdef RGBW
  684. for (uint8_t i = 0; i < num_leds; i++) {
  685. convert_rgb_to_rgbw(&start_led[i]);
  686. }
  687. # endif
  688. // MxSS custom
  689. switch (fled_mode) {
  690. case FLED_OFF:
  691. setrgb(0, 0, 0, &led[RGBLIGHT_FLED1]);
  692. setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]);
  693. break;
  694. case FLED_INDI:
  695. copyrgb(&fleds[0], &led[RGBLIGHT_FLED1]);
  696. copyrgb(&fleds[1], &led[RGBLIGHT_FLED2]);
  697. break;
  698. case FLED_RGB:
  699. if (fled_hs[0].hue == 0 && fled_hs[0].hue == 0 &&
  700. (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE ||
  701. rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT))
  702. setrgb(0, 0, 0, &led[RGBLIGHT_FLED1]);
  703. else
  704. sethsv(fled_hs[0].hue, fled_hs[0].sat, fled_val, &led[RGBLIGHT_FLED1]);
  705. if (fled_hs[1].hue == 0 && fled_hs[1].hue == 0 &&
  706. (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE ||
  707. rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT))
  708. setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]);
  709. else
  710. sethsv(fled_hs[1].hue, fled_hs[1].sat, fled_val, &led[RGBLIGHT_FLED2]);
  711. break;
  712. default:
  713. break;
  714. }
  715. rgblight_call_driver(start_led, num_leds);
  716. }
  717. #endif
  718. #ifdef RGBLIGHT_SPLIT
  719. /* for split keyboard master side */
  720. uint8_t rgblight_get_change_flags(void) { return rgblight_status.change_flags; }
  721. void rgblight_clear_change_flags(void) { rgblight_status.change_flags = 0; }
  722. void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) {
  723. syncinfo->config = rgblight_config;
  724. syncinfo->status = rgblight_status;
  725. }
  726. /* for split keyboard slave side */
  727. void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) {
  728. # ifdef RGBLIGHT_LAYERS
  729. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_LAYERS) {
  730. rgblight_status.enabled_layer_mask = syncinfo->status.enabled_layer_mask;
  731. }
  732. # endif
  733. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) {
  734. if (syncinfo->config.enable) {
  735. rgblight_config.enable = 1; // == rgblight_enable_noeeprom();
  736. rgblight_mode_eeprom_helper(syncinfo->config.mode, write_to_eeprom);
  737. } else {
  738. rgblight_disable_noeeprom();
  739. }
  740. }
  741. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_HSVS) {
  742. rgblight_sethsv_eeprom_helper(syncinfo->config.hue, syncinfo->config.sat, syncinfo->config.val, write_to_eeprom);
  743. // rgblight_config.speed = config->speed; // NEED???
  744. }
  745. # ifdef RGBLIGHT_USE_TIMER
  746. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_TIMER) {
  747. if (syncinfo->status.timer_enabled) {
  748. rgblight_timer_enable();
  749. } else {
  750. rgblight_timer_disable();
  751. }
  752. }
  753. # ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
  754. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_ANIMATION_TICK) {
  755. animation_status.restart = true;
  756. }
  757. # endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
  758. # endif /* RGBLIGHT_USE_TIMER */
  759. }
  760. #endif /* RGBLIGHT_SPLIT */
  761. #ifdef RGBLIGHT_USE_TIMER
  762. typedef void (*effect_func_t)(animation_status_t *anim);
  763. // Animation timer -- use system timer (AVR Timer0)
  764. void rgblight_timer_init(void) {
  765. // OLD!!!! Animation timer -- AVR Timer3
  766. // static uint8_t rgblight_timer_is_init = 0;
  767. // if (rgblight_timer_is_init) {
  768. // return;
  769. // }
  770. // rgblight_timer_is_init = 1;
  771. // /* Timer 3 setup */
  772. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  773. // | _BV(CS30); // Clock selelct: clk/1
  774. // /* Set TOP value */
  775. // uint8_t sreg = SREG;
  776. // cli();
  777. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  778. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  779. // SREG = sreg;
  780. rgblight_status.timer_enabled = false;
  781. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  782. }
  783. void rgblight_timer_enable(void) {
  784. if (!is_static_effect(rgblight_config.mode)) {
  785. rgblight_status.timer_enabled = true;
  786. }
  787. animation_status.last_timer = timer_read();
  788. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  789. dprintf("rgblight timer enabled.\n");
  790. }
  791. void rgblight_timer_disable(void) {
  792. // MxSS custom code
  793. if (fled_mode != FLED_RGB) {
  794. rgblight_status.timer_enabled = false;
  795. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  796. dprintf("rgblight timer disable.\n");
  797. }
  798. }
  799. void rgblight_timer_toggle(void) {
  800. dprintf("rgblight timer toggle.\n");
  801. if (rgblight_status.timer_enabled) {
  802. rgblight_timer_disable();
  803. } else {
  804. rgblight_timer_enable();
  805. }
  806. }
  807. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  808. rgblight_enable();
  809. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  810. rgblight_setrgb(r, g, b);
  811. }
  812. static void rgblight_effect_dummy(animation_status_t *anim) {
  813. // do nothing
  814. /********
  815. dprintf("rgblight_task() what happened?\n");
  816. dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
  817. dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
  818. rgblight_config.mode, rgblight_status.base_mode,
  819. rgblight_status.timer_enabled);
  820. dprintf("last_timer = %d\n",anim->last_timer);
  821. **/
  822. }
  823. void rgblight_task(void) {
  824. if (rgblight_status.timer_enabled) {
  825. effect_func_t effect_func = rgblight_effect_dummy;
  826. uint16_t interval_time = 2000; // dummy interval
  827. uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
  828. animation_status.delta = delta;
  829. // static light mode, do nothing here
  830. if (1 == 0) { // dummy
  831. }
  832. # ifdef RGBLIGHT_EFFECT_BREATHING
  833. else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
  834. // breathing mode
  835. interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
  836. effect_func = rgblight_effect_breathing;
  837. }
  838. # endif
  839. # ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  840. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
  841. // rainbow mood mode
  842. interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
  843. effect_func = rgblight_effect_rainbow_mood;
  844. }
  845. # endif
  846. # ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  847. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
  848. // rainbow swirl mode
  849. interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
  850. effect_func = rgblight_effect_rainbow_swirl;
  851. }
  852. # endif
  853. # ifdef RGBLIGHT_EFFECT_SNAKE
  854. else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
  855. // snake mode
  856. interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
  857. effect_func = rgblight_effect_snake;
  858. }
  859. # endif
  860. # ifdef RGBLIGHT_EFFECT_KNIGHT
  861. else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
  862. // knight mode
  863. interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
  864. effect_func = rgblight_effect_knight;
  865. }
  866. # endif
  867. # ifdef RGBLIGHT_EFFECT_CHRISTMAS
  868. else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
  869. // christmas mode
  870. interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
  871. effect_func = (effect_func_t)rgblight_effect_christmas;
  872. }
  873. # endif
  874. # ifdef RGBLIGHT_EFFECT_RGB_TEST
  875. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
  876. // RGB test mode
  877. interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
  878. effect_func = (effect_func_t)rgblight_effect_rgbtest;
  879. }
  880. # endif
  881. # ifdef RGBLIGHT_EFFECT_ALTERNATING
  882. else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING) {
  883. interval_time = 500;
  884. effect_func = (effect_func_t)rgblight_effect_alternating;
  885. }
  886. # endif
  887. # ifdef RGBLIGHT_EFFECT_TWINKLE
  888. else if (rgblight_status.base_mode == RGBLIGHT_MODE_TWINKLE) {
  889. interval_time = get_interval_time(&RGBLED_TWINKLE_INTERVALS[delta % 3], 5, 50);
  890. effect_func = (effect_func_t)rgblight_effect_twinkle;
  891. }
  892. # endif
  893. if (animation_status.restart) {
  894. animation_status.restart = false;
  895. animation_status.last_timer = timer_read() - interval_time - 1;
  896. animation_status.pos16 = 0; // restart signal to local each effect
  897. }
  898. if (timer_elapsed(animation_status.last_timer) >= interval_time) {
  899. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  900. static uint16_t report_last_timer = 0;
  901. static bool tick_flag = false;
  902. uint16_t oldpos16;
  903. if (tick_flag) {
  904. tick_flag = false;
  905. if (timer_elapsed(report_last_timer) >= 30000) {
  906. report_last_timer = timer_read();
  907. dprintf("rgblight animation tick report to slave\n");
  908. RGBLIGHT_SPLIT_ANIMATION_TICK;
  909. }
  910. }
  911. oldpos16 = animation_status.pos16;
  912. # endif
  913. animation_status.last_timer += interval_time;
  914. effect_func(&animation_status);
  915. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  916. if (animation_status.pos16 == 0 && oldpos16 != 0) {
  917. tick_flag = true;
  918. }
  919. # endif
  920. }
  921. }
  922. # ifdef RGBLIGHT_LAYER_BLINK
  923. rgblight_unblink_layers();
  924. # endif
  925. }
  926. #endif /* RGBLIGHT_USE_TIMER */
  927. // Effects
  928. #ifdef RGBLIGHT_EFFECT_BREATHING
  929. # ifndef RGBLIGHT_EFFECT_BREATHE_CENTER
  930. # ifndef RGBLIGHT_BREATHE_TABLE_SIZE
  931. # define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256 or 128 or 64
  932. # endif
  933. # include <rgblight_breathe_table.h>
  934. # endif
  935. __attribute__((weak)) const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  936. void rgblight_effect_breathing(animation_status_t *anim) {
  937. float val;
  938. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  939. # ifdef RGBLIGHT_EFFECT_BREATHE_TABLE
  940. val = pgm_read_byte(&rgblight_effect_breathe_table[anim->pos / table_scale]);
  941. # else
  942. val = (exp(sin((anim->pos / 255.0) * M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER / M_E) * (RGBLIGHT_EFFECT_BREATHE_MAX / (M_E - 1 / M_E));
  943. # endif
  944. rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
  945. anim->pos = (anim->pos + 1);
  946. }
  947. #endif
  948. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  949. __attribute__((weak)) const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  950. void rgblight_effect_rainbow_mood(animation_status_t *anim) {
  951. rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
  952. anim->current_hue++;
  953. }
  954. #endif
  955. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  956. # ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
  957. # define RGBLIGHT_RAINBOW_SWIRL_RANGE 255
  958. # endif
  959. __attribute__((weak)) const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  960. void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
  961. uint8_t hue;
  962. uint8_t i;
  963. for (i = 0; i < rgblight_ranges.effect_num_leds; i++) {
  964. hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / rgblight_ranges.effect_num_leds * i + anim->current_hue);
  965. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + rgblight_ranges.effect_start_pos]);
  966. }
  967. rgblight_set();
  968. if (anim->delta % 2) {
  969. anim->current_hue++;
  970. } else {
  971. anim->current_hue--;
  972. }
  973. }
  974. #endif
  975. #ifdef RGBLIGHT_EFFECT_SNAKE
  976. __attribute__((weak)) const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  977. void rgblight_effect_snake(animation_status_t *anim) {
  978. static uint8_t pos = 0;
  979. uint8_t i, j;
  980. int8_t k;
  981. int8_t increment = 1;
  982. if (anim->delta % 2) {
  983. increment = -1;
  984. }
  985. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  986. if (anim->pos == 0) { // restart signal
  987. if (increment == 1) {
  988. pos = rgblight_ranges.effect_num_leds - 1;
  989. } else {
  990. pos = 0;
  991. }
  992. anim->pos = 1;
  993. }
  994. # endif
  995. // MxSS custom
  996. fled_hs[0].hue = fled_hs[1].hue = 0;
  997. fled_hs[0].sat = fled_hs[1].sat = 0;
  998. for (i = 0; i < rgblight_ranges.effect_num_leds; i++) {
  999. LED_TYPE *ledp = led + i + rgblight_ranges.effect_start_pos;
  1000. ledp->r = 0;
  1001. ledp->g = 0;
  1002. ledp->b = 0;
  1003. # ifdef RGBW
  1004. ledp->w = 0;
  1005. # endif
  1006. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  1007. k = pos + j * increment;
  1008. if (k > RGBLED_NUM) {
  1009. k = k % RGBLED_NUM;
  1010. }
  1011. if (k < 0) {
  1012. k = k + rgblight_ranges.effect_num_leds;
  1013. }
  1014. if (i == k) {
  1015. sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val * (RGBLIGHT_EFFECT_SNAKE_LENGTH - j) / RGBLIGHT_EFFECT_SNAKE_LENGTH), ledp);
  1016. }
  1017. }
  1018. }
  1019. rgblight_set();
  1020. if (increment == 1) {
  1021. if (pos - 1 < 0) {
  1022. pos = rgblight_ranges.effect_num_leds - 1;
  1023. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  1024. anim->pos = 0;
  1025. # endif
  1026. } else {
  1027. pos -= 1;
  1028. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  1029. anim->pos = 1;
  1030. # endif
  1031. }
  1032. } else {
  1033. pos = (pos + 1) % rgblight_ranges.effect_num_leds;
  1034. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  1035. anim->pos = pos;
  1036. # endif
  1037. }
  1038. }
  1039. #endif
  1040. #ifdef RGBLIGHT_EFFECT_KNIGHT
  1041. __attribute__((weak)) const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
  1042. void rgblight_effect_knight(animation_status_t *anim) {
  1043. static int8_t low_bound = 0;
  1044. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  1045. static int8_t increment = 1;
  1046. uint8_t i, cur;
  1047. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  1048. if (anim->pos == 0) { // restart signal
  1049. anim->pos = 1;
  1050. low_bound = 0;
  1051. high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  1052. increment = 1;
  1053. }
  1054. # endif
  1055. // Set all the LEDs to 0
  1056. for (i = rgblight_ranges.effect_start_pos; i < rgblight_ranges.effect_end_pos; i++) {
  1057. led[i].r = 0;
  1058. led[i].g = 0;
  1059. led[i].b = 0;
  1060. # ifdef RGBW
  1061. led[i].w = 0;
  1062. # endif
  1063. }
  1064. // Determine which LEDs should be lit up
  1065. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  1066. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % rgblight_ranges.effect_num_leds + rgblight_ranges.effect_start_pos;
  1067. if (i >= low_bound && i <= high_bound) {
  1068. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  1069. } else {
  1070. // MxSS custom code
  1071. if (cur == RGBLIGHT_FLED1) {
  1072. fled_hs[0].hue = fled_hs[0].sat = 0;
  1073. } else if (cur == RGBLIGHT_FLED2) {
  1074. fled_hs[1].hue = fled_hs[1].sat = 0;
  1075. }
  1076. led[cur].r = 0;
  1077. led[cur].g = 0;
  1078. led[cur].b = 0;
  1079. # ifdef RGBW
  1080. led[cur].w = 0;
  1081. # endif
  1082. }
  1083. }
  1084. rgblight_set();
  1085. // Move from low_bound to high_bound changing the direction we increment each
  1086. // time a boundary is hit.
  1087. low_bound += increment;
  1088. high_bound += increment;
  1089. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  1090. increment = -increment;
  1091. # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  1092. if (increment == 1) {
  1093. anim->pos = 0;
  1094. }
  1095. # endif
  1096. }
  1097. }
  1098. #endif
  1099. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  1100. # define CUBED(x) ((x) * (x) * (x))
  1101. /**
  1102. * Christmas lights effect, with a smooth animation between red & green.
  1103. */
  1104. void rgblight_effect_christmas(animation_status_t *anim) {
  1105. static int8_t increment = 1;
  1106. const uint8_t max_pos = 32;
  1107. const uint8_t hue_green = 85;
  1108. uint32_t xa;
  1109. uint8_t hue, val;
  1110. uint8_t i;
  1111. // The effect works by animating anim->pos from 0 to 32 and back to 0.
  1112. // The pos is used in a cubic bezier formula to ease-in-out between red and green, leaving the interpolated colors visible as short as possible.
  1113. xa = CUBED((uint32_t)anim->pos);
  1114. hue = ((uint32_t)hue_green) * xa / (xa + CUBED((uint32_t)(max_pos - anim->pos)));
  1115. // Additionally, these interpolated colors get shown with a slightly darker value, to make them less prominent than the main colors.
  1116. val = 255 - (3 * (hue < hue_green / 2 ? hue : hue_green - hue) / 2);
  1117. for (i = 0; i < rgblight_ranges.effect_num_leds; i++) {
  1118. uint8_t local_hue = (i / RGBLIGHT_EFFECT_CHRISTMAS_STEP) % 2 ? hue : hue_green - hue;
  1119. sethsv(local_hue, rgblight_config.sat, val, (LED_TYPE *)&led[i + rgblight_ranges.effect_start_pos]);
  1120. }
  1121. rgblight_set();
  1122. if (anim->pos == 0) {
  1123. increment = 1;
  1124. } else if (anim->pos == max_pos) {
  1125. increment = -1;
  1126. }
  1127. anim->pos += increment;
  1128. }
  1129. #endif
  1130. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  1131. __attribute__((weak)) const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
  1132. void rgblight_effect_rgbtest(animation_status_t *anim) {
  1133. static uint8_t maxval = 0;
  1134. uint8_t g;
  1135. uint8_t r;
  1136. uint8_t b;
  1137. if (maxval == 0) {
  1138. LED_TYPE tmp_led;
  1139. sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
  1140. maxval = tmp_led.r;
  1141. }
  1142. g = r = b = 0;
  1143. switch (anim->pos) {
  1144. // MxSS custom code
  1145. case 0:
  1146. r = maxval;
  1147. fled_hs[0].hue = 0;
  1148. fled_hs[0].sat = 255;
  1149. fled_hs[1].hue = 0;
  1150. fled_hs[1].sat = 255;
  1151. break;
  1152. case 1:
  1153. g = maxval;
  1154. fled_hs[0].hue = 85;
  1155. fled_hs[0].sat = 255;
  1156. fled_hs[1].hue = 85;
  1157. fled_hs[1].sat = 255;
  1158. break;
  1159. case 2:
  1160. b = maxval;
  1161. fled_hs[0].hue = 170;
  1162. fled_hs[0].sat = 255;
  1163. fled_hs[1].hue = 170;
  1164. fled_hs[1].sat = 255;
  1165. break;
  1166. }
  1167. rgblight_setrgb(r, g, b);
  1168. anim->pos = (anim->pos + 1) % 3;
  1169. }
  1170. #endif
  1171. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  1172. void rgblight_effect_alternating(animation_status_t *anim) {
  1173. for (int i = 0; i < rgblight_ranges.effect_num_leds; i++) {
  1174. LED_TYPE *ledp = led + i + rgblight_ranges.effect_start_pos;
  1175. if (i < rgblight_ranges.effect_num_leds / 2 && anim->pos) {
  1176. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
  1177. } else if (i >= rgblight_ranges.effect_num_leds / 2 && !anim->pos) {
  1178. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
  1179. } else {
  1180. sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp);
  1181. }
  1182. }
  1183. rgblight_set();
  1184. anim->pos = (anim->pos + 1) % 2;
  1185. }
  1186. #endif
  1187. #ifdef RGBLIGHT_EFFECT_TWINKLE
  1188. __attribute__((weak)) const uint8_t RGBLED_TWINKLE_INTERVALS[] PROGMEM = {50, 25, 10};
  1189. typedef struct PACKED {
  1190. HSV hsv;
  1191. uint8_t life;
  1192. bool up;
  1193. } TwinkleState;
  1194. static TwinkleState led_twinkle_state[RGBLED_NUM];
  1195. void rgblight_effect_twinkle(animation_status_t *anim) {
  1196. bool random_color = anim->delta / 3;
  1197. bool restart = anim->pos == 0;
  1198. anim->pos = 1;
  1199. for (uint8_t i = 0; i < rgblight_ranges.effect_num_leds; i++) {
  1200. TwinkleState *t = &(led_twinkle_state[i]);
  1201. HSV * c = &(t->hsv);
  1202. if (restart) {
  1203. // Restart
  1204. t->life = 0;
  1205. t->hsv.v = 0;
  1206. } else if (t->life) {
  1207. // This LED is already on, either brightening or dimming
  1208. t->life--;
  1209. uint8_t on = t->up ? RGBLIGHT_EFFECT_TWINKLE_LIFE - t->life : t->life;
  1210. c->v = (uint16_t)rgblight_config.val * on / RGBLIGHT_EFFECT_TWINKLE_LIFE;
  1211. if (t->life == 0 && t->up) {
  1212. t->up = false;
  1213. t->life = RGBLIGHT_EFFECT_TWINKLE_LIFE;
  1214. }
  1215. if (!random_color) {
  1216. c->h = rgblight_config.hue;
  1217. c->s = rgblight_config.sat;
  1218. }
  1219. } else if (rand() < RAND_MAX * RGBLIGHT_EFFECT_TWINKLE_PROBABILITY) {
  1220. // This LED is off, but was randomly selected to start brightening
  1221. c->h = random_color ? rand() % 0xFF : rgblight_config.hue;
  1222. c->s = random_color ? (rand() % (rgblight_config.sat / 2)) + (rgblight_config.sat / 2) : rgblight_config.sat;
  1223. c->v = 0;
  1224. t->life = RGBLIGHT_EFFECT_TWINKLE_LIFE;
  1225. t->up = true;
  1226. } else {
  1227. // This LED is off, and was NOT selected to start brightening
  1228. }
  1229. LED_TYPE *ledp = led + i + rgblight_ranges.effect_start_pos;
  1230. sethsv(c->h, c->s, c->v, ledp);
  1231. }
  1232. rgblight_set();
  1233. }
  1234. #endif