rgblight.c 45 KB

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