rgblight.c 46 KB

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