rgblight.c 45 KB

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