rgblight.c 50 KB

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