rgblight.c 47 KB

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