rgblight.c 49 KB

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