You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1395 lines
48 KiB

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