rgblight.c 50 KB

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