rgblight.c 43 KB

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