rgblight.c 46 KB

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