rgblight.c 39 KB

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