rgblight.c 48 KB

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