rgblight.c 45 KB

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