rgblight.c 46 KB

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