rgblight.c 41 KB

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