logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

rgblight.c (49924B)


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