rgblight.c 49 KB

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