rgblight.c 47 KB

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