rgblight.c 49 KB

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