rgblight.c 49 KB

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