rgblight.c 47 KB

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