rgblight.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. #ifdef __AVR__
  19. #include <avr/eeprom.h>
  20. #include <avr/interrupt.h>
  21. #endif
  22. #ifdef STM32_EEPROM_ENABLE
  23. #include "hal.h"
  24. #include "eeprom.h"
  25. #include "eeprom_stm32.h"
  26. #endif
  27. #include "wait.h"
  28. #include "progmem.h"
  29. #include "timer.h"
  30. #include "rgblight.h"
  31. #include "color.h"
  32. #include "debug.h"
  33. #include "led_tables.h"
  34. #include "lib/lib8tion/lib8tion.h"
  35. #ifdef VELOCIKEY_ENABLE
  36. #include "velocikey.h"
  37. #endif
  38. #ifdef RGBLIGHT_SPLIT
  39. /* for split keyboard */
  40. #define RGBLIGHT_SPLIT_SET_CHANGE_MODE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_MODE
  41. #define RGBLIGHT_SPLIT_SET_CHANGE_HSVS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_HSVS
  42. #define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS rgblight_status.change_flags |= (RGBLIGHT_STATUS_CHANGE_MODE|RGBLIGHT_STATUS_CHANGE_HSVS)
  43. #define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_TIMER
  44. #define RGBLIGHT_SPLIT_ANIMATION_TICK rgblight_status.change_flags |= RGBLIGHT_STATUS_ANIMATION_TICK
  45. #else
  46. #define RGBLIGHT_SPLIT_SET_CHANGE_MODE
  47. #define RGBLIGHT_SPLIT_SET_CHANGE_HSVS
  48. #define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
  49. #define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
  50. #define RGBLIGHT_SPLIT_ANIMATION_TICK
  51. #endif
  52. #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
  53. #define _RGBM_SINGLE_DYNAMIC(sym)
  54. #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
  55. #define _RGBM_MULTI_DYNAMIC(sym)
  56. #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_ ## sym,
  57. #define _RGBM_TMP_DYNAMIC(sym, msym)
  58. static uint8_t static_effect_table [] = {
  59. #include "rgblight_modes.h"
  60. };
  61. #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
  62. #define _RGBM_SINGLE_DYNAMIC(sym) RGBLIGHT_MODE_ ## sym,
  63. #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
  64. #define _RGBM_MULTI_DYNAMIC(sym) RGBLIGHT_MODE_ ## sym,
  65. #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_ ## msym,
  66. #define _RGBM_TMP_DYNAMIC(sym, msym) RGBLIGHT_MODE_ ## msym,
  67. static uint8_t mode_base_table [] = {
  68. 0, // RGBLIGHT_MODE_zero
  69. #include "rgblight_modes.h"
  70. };
  71. static inline int is_static_effect(uint8_t mode) {
  72. return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
  73. }
  74. #ifdef RGBLIGHT_LED_MAP
  75. const uint8_t led_map[] PROGMEM = RGBLIGHT_LED_MAP;
  76. #endif
  77. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  78. __attribute__ ((weak))
  79. const uint8_t RGBLED_GRADIENT_RANGES[] PROGMEM = {255, 170, 127, 85, 64};
  80. #endif
  81. rgblight_config_t rgblight_config;
  82. rgblight_status_t rgblight_status = { .timer_enabled = false };
  83. bool is_rgblight_initialized = false;
  84. #ifdef RGBLIGHT_USE_TIMER
  85. animation_status_t animation_status = {};
  86. #endif
  87. #ifndef LED_ARRAY
  88. LED_TYPE led[RGBLED_NUM];
  89. #define LED_ARRAY led
  90. #endif
  91. static uint8_t clipping_start_pos = 0;
  92. static uint8_t clipping_num_leds = RGBLED_NUM;
  93. static uint8_t effect_start_pos = 0;
  94. static uint8_t effect_end_pos = RGBLED_NUM;
  95. static uint8_t effect_num_leds = RGBLED_NUM;
  96. void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) {
  97. clipping_start_pos = start_pos;
  98. clipping_num_leds = num_leds;
  99. }
  100. void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds) {
  101. if (start_pos >= RGBLED_NUM) return;
  102. if (start_pos + num_leds > RGBLED_NUM) return;
  103. effect_start_pos = start_pos;
  104. effect_end_pos = start_pos + num_leds;
  105. effect_num_leds = num_leds;
  106. }
  107. void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
  108. HSV hsv = { hue, sat, val };
  109. RGB rgb = hsv_to_rgb(hsv);
  110. setrgb(rgb.r, rgb.g, rgb.b, led1);
  111. }
  112. void sethsv(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
  113. sethsv_raw( hue, sat,
  114. val > RGBLIGHT_LIMIT_VAL ? RGBLIGHT_LIMIT_VAL : val,
  115. led1);
  116. }
  117. void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
  118. (*led1).r = r;
  119. (*led1).g = g;
  120. (*led1).b = b;
  121. }
  122. void rgblight_check_config(void) {
  123. /* Add some out of bound checks for RGB light config */
  124. if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
  125. rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
  126. }
  127. else if (rgblight_config.mode > RGBLIGHT_MODES) {
  128. rgblight_config.mode = RGBLIGHT_MODES;
  129. }
  130. if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
  131. rgblight_config.val = RGBLIGHT_LIMIT_VAL;
  132. }
  133. }
  134. uint32_t eeconfig_read_rgblight(void) {
  135. #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
  136. return eeprom_read_dword(EECONFIG_RGBLIGHT);
  137. #else
  138. return 0;
  139. #endif
  140. }
  141. void eeconfig_update_rgblight(uint32_t val) {
  142. #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
  143. rgblight_check_config();
  144. eeprom_update_dword(EECONFIG_RGBLIGHT, val);
  145. #endif
  146. }
  147. void eeconfig_update_rgblight_default(void) {
  148. rgblight_config.enable = 1;
  149. rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
  150. rgblight_config.hue = 0;
  151. rgblight_config.sat = UINT8_MAX;
  152. rgblight_config.val = RGBLIGHT_LIMIT_VAL;
  153. rgblight_config.speed = 0;
  154. RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
  155. eeconfig_update_rgblight(rgblight_config.raw);
  156. }
  157. void eeconfig_debug_rgblight(void) {
  158. dprintf("rgblight_config EEPROM:\n");
  159. dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
  160. dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
  161. dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
  162. dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
  163. dprintf("rgblight_config.val = %d\n", rgblight_config.val);
  164. dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
  165. }
  166. void rgblight_init(void) {
  167. /* if already initialized, don't do it again.
  168. If you must do it again, extern this and set to false, first.
  169. This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
  170. if (is_rgblight_initialized) { return; }
  171. debug_enable = 1; // Debug ON!
  172. dprintf("rgblight_init called.\n");
  173. dprintf("rgblight_init start!\n");
  174. if (!eeconfig_is_enabled()) {
  175. dprintf("rgblight_init eeconfig is not enabled.\n");
  176. eeconfig_init();
  177. eeconfig_update_rgblight_default();
  178. }
  179. rgblight_config.raw = eeconfig_read_rgblight();
  180. RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
  181. if (!rgblight_config.mode) {
  182. dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
  183. eeconfig_update_rgblight_default();
  184. rgblight_config.raw = eeconfig_read_rgblight();
  185. }
  186. rgblight_check_config();
  187. eeconfig_debug_rgblight(); // display current eeprom values
  188. #ifdef RGBLIGHT_USE_TIMER
  189. rgblight_timer_init(); // setup the timer
  190. #endif
  191. if (rgblight_config.enable) {
  192. rgblight_mode_noeeprom(rgblight_config.mode);
  193. }
  194. is_rgblight_initialized = true;
  195. }
  196. uint32_t rgblight_read_dword(void) {
  197. return rgblight_config.raw;
  198. }
  199. void rgblight_update_dword(uint32_t dword) {
  200. RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
  201. rgblight_config.raw = dword;
  202. if (rgblight_config.enable)
  203. rgblight_mode_noeeprom(rgblight_config.mode);
  204. else {
  205. #ifdef RGBLIGHT_USE_TIMER
  206. rgblight_timer_disable();
  207. #endif
  208. rgblight_set();
  209. }
  210. }
  211. void rgblight_increase(void) {
  212. uint8_t mode = 0;
  213. if (rgblight_config.mode < RGBLIGHT_MODES) {
  214. mode = rgblight_config.mode + 1;
  215. }
  216. rgblight_mode(mode);
  217. }
  218. void rgblight_decrease(void) {
  219. uint8_t mode = 0;
  220. // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
  221. if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
  222. mode = rgblight_config.mode - 1;
  223. }
  224. rgblight_mode(mode);
  225. }
  226. void rgblight_step_helper(bool write_to_eeprom) {
  227. uint8_t mode = 0;
  228. mode = rgblight_config.mode + 1;
  229. if (mode > RGBLIGHT_MODES) {
  230. mode = 1;
  231. }
  232. rgblight_mode_eeprom_helper(mode, write_to_eeprom);
  233. }
  234. void rgblight_step_noeeprom(void) {
  235. rgblight_step_helper(false);
  236. }
  237. void rgblight_step(void) {
  238. rgblight_step_helper(true);
  239. }
  240. void rgblight_step_reverse_helper(bool write_to_eeprom) {
  241. uint8_t mode = 0;
  242. mode = rgblight_config.mode - 1;
  243. if (mode < 1) {
  244. mode = RGBLIGHT_MODES;
  245. }
  246. rgblight_mode_eeprom_helper(mode, write_to_eeprom);
  247. }
  248. void rgblight_step_reverse_noeeprom(void) {
  249. rgblight_step_reverse_helper(false);
  250. }
  251. void rgblight_step_reverse(void) {
  252. rgblight_step_reverse_helper(true);
  253. }
  254. uint8_t rgblight_get_mode(void) {
  255. if (!rgblight_config.enable) {
  256. return false;
  257. }
  258. return rgblight_config.mode;
  259. }
  260. void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
  261. if (!rgblight_config.enable) {
  262. return;
  263. }
  264. if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
  265. rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
  266. } else if (mode > RGBLIGHT_MODES) {
  267. rgblight_config.mode = RGBLIGHT_MODES;
  268. } else {
  269. rgblight_config.mode = mode;
  270. }
  271. RGBLIGHT_SPLIT_SET_CHANGE_MODE;
  272. if (write_to_eeprom) {
  273. eeconfig_update_rgblight(rgblight_config.raw);
  274. dprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
  275. } else {
  276. dprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
  277. }
  278. if( is_static_effect(rgblight_config.mode) ) {
  279. #ifdef RGBLIGHT_USE_TIMER
  280. rgblight_timer_disable();
  281. #endif
  282. } else {
  283. #ifdef RGBLIGHT_USE_TIMER
  284. rgblight_timer_enable();
  285. #endif
  286. }
  287. #ifdef RGBLIGHT_USE_TIMER
  288. animation_status.restart = true;
  289. #endif
  290. rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  291. }
  292. void rgblight_mode(uint8_t mode) {
  293. rgblight_mode_eeprom_helper(mode, true);
  294. }
  295. void rgblight_mode_noeeprom(uint8_t mode) {
  296. rgblight_mode_eeprom_helper(mode, false);
  297. }
  298. void rgblight_toggle(void) {
  299. dprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
  300. if (rgblight_config.enable) {
  301. rgblight_disable();
  302. }
  303. else {
  304. rgblight_enable();
  305. }
  306. }
  307. void rgblight_toggle_noeeprom(void) {
  308. dprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
  309. if (rgblight_config.enable) {
  310. rgblight_disable_noeeprom();
  311. }
  312. else {
  313. rgblight_enable_noeeprom();
  314. }
  315. }
  316. void rgblight_enable(void) {
  317. rgblight_config.enable = 1;
  318. // No need to update EEPROM here. rgblight_mode() will do that, actually
  319. //eeconfig_update_rgblight(rgblight_config.raw);
  320. dprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  321. rgblight_mode(rgblight_config.mode);
  322. }
  323. void rgblight_enable_noeeprom(void) {
  324. rgblight_config.enable = 1;
  325. dprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  326. rgblight_mode_noeeprom(rgblight_config.mode);
  327. }
  328. void rgblight_disable(void) {
  329. rgblight_config.enable = 0;
  330. eeconfig_update_rgblight(rgblight_config.raw);
  331. dprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
  332. #ifdef RGBLIGHT_USE_TIMER
  333. rgblight_timer_disable();
  334. #endif
  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. #ifdef RGBLIGHT_USE_TIMER
  343. rgblight_timer_disable();
  344. #endif
  345. RGBLIGHT_SPLIT_SET_CHANGE_MODE;
  346. wait_ms(50);
  347. rgblight_set();
  348. }
  349. void rgblight_increase_hue_helper(bool write_to_eeprom) {
  350. uint8_t hue = rgblight_config.hue + RGBLIGHT_HUE_STEP;
  351. rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
  352. }
  353. void rgblight_increase_hue_noeeprom(void) {
  354. rgblight_increase_hue_helper(false);
  355. }
  356. void rgblight_increase_hue(void) {
  357. rgblight_increase_hue_helper(true);
  358. }
  359. void rgblight_decrease_hue_helper(bool write_to_eeprom) {
  360. uint8_t hue = rgblight_config.hue - RGBLIGHT_HUE_STEP;
  361. rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
  362. }
  363. void rgblight_decrease_hue_noeeprom(void) {
  364. rgblight_decrease_hue_helper(false);
  365. }
  366. void rgblight_decrease_hue(void) {
  367. rgblight_decrease_hue_helper(true);
  368. }
  369. void rgblight_increase_sat_helper(bool write_to_eeprom) {
  370. uint8_t sat = qadd8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
  371. rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
  372. }
  373. void rgblight_increase_sat_noeeprom(void) {
  374. rgblight_increase_sat_helper(false);
  375. }
  376. void rgblight_increase_sat(void) {
  377. rgblight_increase_sat_helper(true);
  378. }
  379. void rgblight_decrease_sat_helper(bool write_to_eeprom) {
  380. uint8_t sat = qsub8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
  381. rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
  382. }
  383. void rgblight_decrease_sat_noeeprom(void) {
  384. rgblight_decrease_sat_helper(false);
  385. }
  386. void rgblight_decrease_sat(void) {
  387. rgblight_decrease_sat_helper(true);
  388. }
  389. void rgblight_increase_val_helper(bool write_to_eeprom) {
  390. uint8_t val = qadd8(rgblight_config.val, RGBLIGHT_VAL_STEP);
  391. rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
  392. }
  393. void rgblight_increase_val_noeeprom(void) {
  394. rgblight_increase_val_helper(false);
  395. }
  396. void rgblight_increase_val(void) {
  397. rgblight_increase_val_helper(true);
  398. }
  399. void rgblight_decrease_val_helper(bool write_to_eeprom) {
  400. uint8_t val = qsub8(rgblight_config.val, RGBLIGHT_VAL_STEP);
  401. rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
  402. }
  403. void rgblight_decrease_val_noeeprom(void) {
  404. rgblight_decrease_val_helper(false);
  405. }
  406. void rgblight_decrease_val(void) {
  407. rgblight_decrease_val_helper(true);
  408. }
  409. void rgblight_increase_speed(void) {
  410. if (rgblight_config.speed < 3)
  411. rgblight_config.speed++;
  412. //RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?
  413. eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
  414. }
  415. void rgblight_decrease_speed(void) {
  416. if (rgblight_config.speed > 0)
  417. rgblight_config.speed--;
  418. //RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED??
  419. eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
  420. }
  421. void rgblight_sethsv_noeeprom_old(uint8_t hue, uint8_t sat, uint8_t val) {
  422. if (rgblight_config.enable) {
  423. LED_TYPE tmp_led;
  424. sethsv(hue, sat, val, &tmp_led);
  425. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  426. }
  427. }
  428. void rgblight_sethsv_eeprom_helper(uint8_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
  429. if (rgblight_config.enable) {
  430. rgblight_status.base_mode = mode_base_table[rgblight_config.mode];
  431. if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
  432. // same static color
  433. LED_TYPE tmp_led;
  434. sethsv(hue, sat, val, &tmp_led);
  435. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  436. } else {
  437. // all LEDs in same color
  438. if ( 1 == 0 ) { //dummy
  439. }
  440. #ifdef RGBLIGHT_EFFECT_BREATHING
  441. else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING ) {
  442. // breathing mode, ignore the change of val, use in memory value instead
  443. val = rgblight_config.val;
  444. }
  445. #endif
  446. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  447. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
  448. // rainbow mood, ignore the change of hue
  449. hue = rgblight_config.hue;
  450. }
  451. #endif
  452. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  453. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
  454. // rainbow swirl, ignore the change of hue
  455. hue = rgblight_config.hue;
  456. }
  457. #endif
  458. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  459. else if (rgblight_status.base_mode == RGBLIGHT_MODE_STATIC_GRADIENT) {
  460. // static gradient
  461. uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
  462. bool direction = (delta % 2) == 0;
  463. #ifdef __AVR__
  464. // probably due to how pgm_read_word is defined for ARM, but the ARM compiler really hates this line
  465. uint8_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[delta / 2]);
  466. #else
  467. uint8_t range = RGBLED_GRADIENT_RANGES[delta / 2];
  468. #endif
  469. for (uint8_t i = 0; i < effect_num_leds; i++) {
  470. uint8_t _hue = ((uint16_t)i * (uint16_t)range) / effect_num_leds;
  471. if (direction) {
  472. _hue = hue + _hue;
  473. } else {
  474. _hue = hue - _hue;
  475. }
  476. dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i, _hue, direction, range);
  477. sethsv(_hue, sat, val, (LED_TYPE *)&led[i + effect_start_pos]);
  478. }
  479. rgblight_set();
  480. }
  481. #endif
  482. }
  483. #ifdef RGBLIGHT_SPLIT
  484. if( rgblight_config.hue != hue ||
  485. rgblight_config.sat != sat ||
  486. rgblight_config.val != val ) {
  487. RGBLIGHT_SPLIT_SET_CHANGE_HSVS;
  488. }
  489. #endif
  490. rgblight_config.hue = hue;
  491. rgblight_config.sat = sat;
  492. rgblight_config.val = val;
  493. if (write_to_eeprom) {
  494. eeconfig_update_rgblight(rgblight_config.raw);
  495. dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  496. } else {
  497. dprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  498. }
  499. }
  500. }
  501. void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val) {
  502. rgblight_sethsv_eeprom_helper(hue, sat, val, true);
  503. }
  504. void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val) {
  505. rgblight_sethsv_eeprom_helper(hue, sat, val, false);
  506. }
  507. uint8_t rgblight_get_hue(void) {
  508. return rgblight_config.hue;
  509. }
  510. uint8_t rgblight_get_sat(void) {
  511. return rgblight_config.sat;
  512. }
  513. uint8_t rgblight_get_val(void) {
  514. return rgblight_config.val;
  515. }
  516. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  517. if (!rgblight_config.enable) { return; }
  518. for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
  519. led[i].r = r;
  520. led[i].g = g;
  521. led[i].b = b;
  522. }
  523. rgblight_set();
  524. }
  525. void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
  526. if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
  527. led[index].r = r;
  528. led[index].g = g;
  529. led[index].b = b;
  530. rgblight_set();
  531. }
  532. void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index) {
  533. if (!rgblight_config.enable) { return; }
  534. LED_TYPE tmp_led;
  535. sethsv(hue, sat, val, &tmp_led);
  536. rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
  537. }
  538. #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) \
  539. || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
  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) { return; }
  550. for (uint8_t i = start; i < end; i++) {
  551. led[i].r = r;
  552. led[i].g = g;
  553. led[i].b = b;
  554. }
  555. rgblight_set();
  556. wait_ms(1);
  557. }
  558. void rgblight_sethsv_range(uint8_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
  559. if (!rgblight_config.enable) { return; }
  560. LED_TYPE tmp_led;
  561. sethsv(hue, sat, val, &tmp_led);
  562. rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
  563. }
  564. #ifndef RGBLIGHT_SPLIT
  565. void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) {
  566. rgblight_setrgb_range(r, g, b, 0 , (uint8_t) RGBLED_NUM/2);
  567. }
  568. void rgblight_setrgb_slave(uint8_t r, uint8_t g, uint8_t b) {
  569. rgblight_setrgb_range(r, g, b, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
  570. }
  571. void rgblight_sethsv_master(uint8_t hue, uint8_t sat, uint8_t val) {
  572. rgblight_sethsv_range(hue, sat, val, 0, (uint8_t) RGBLED_NUM/2);
  573. }
  574. void rgblight_sethsv_slave(uint8_t hue, uint8_t sat, uint8_t val) {
  575. rgblight_sethsv_range(hue, sat, val, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
  576. }
  577. #endif // ifndef RGBLIGHT_SPLIT
  578. #ifndef RGBLIGHT_CUSTOM_DRIVER
  579. void rgblight_set(void) {
  580. LED_TYPE *start_led;
  581. uint16_t num_leds = clipping_num_leds;
  582. if (!rgblight_config.enable) {
  583. for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
  584. led[i].r = 0;
  585. led[i].g = 0;
  586. led[i].b = 0;
  587. }
  588. }
  589. #ifdef RGBLIGHT_LED_MAP
  590. LED_TYPE led0[RGBLED_NUM];
  591. for(uint8_t i = 0; i < RGBLED_NUM; i++) {
  592. led0[i] = led[pgm_read_byte(&led_map[i])];
  593. }
  594. start_led = led0 + clipping_start_pos;
  595. #else
  596. start_led = led + clipping_start_pos;
  597. #endif
  598. #ifdef RGBW
  599. ws2812_setleds_rgbw(start_led, num_leds);
  600. #else
  601. ws2812_setleds(start_led, num_leds);
  602. #endif
  603. }
  604. #endif
  605. #ifdef RGBLIGHT_SPLIT
  606. /* for split keyboard master side */
  607. uint8_t rgblight_get_change_flags(void) {
  608. return rgblight_status.change_flags;
  609. }
  610. void rgblight_clear_change_flags(void) {
  611. rgblight_status.change_flags = 0;
  612. }
  613. void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) {
  614. syncinfo->config = rgblight_config;
  615. syncinfo->status = rgblight_status;
  616. }
  617. /* for split keyboard slave side */
  618. void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) {
  619. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) {
  620. if (syncinfo->config.enable) {
  621. rgblight_config.enable = 1; // == rgblight_enable_noeeprom();
  622. rgblight_mode_eeprom_helper(syncinfo->config.mode, write_to_eeprom);
  623. } else {
  624. rgblight_disable_noeeprom();
  625. }
  626. }
  627. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_HSVS) {
  628. rgblight_sethsv_eeprom_helper(syncinfo->config.hue, syncinfo->config.sat, syncinfo->config.val, write_to_eeprom);
  629. // rgblight_config.speed = config->speed; // NEED???
  630. }
  631. #ifdef RGBLIGHT_USE_TIMER
  632. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_TIMER) {
  633. if (syncinfo->status.timer_enabled) {
  634. rgblight_timer_enable();
  635. } else {
  636. rgblight_timer_disable();
  637. }
  638. }
  639. #ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
  640. if (syncinfo->status.change_flags & RGBLIGHT_STATUS_ANIMATION_TICK) {
  641. animation_status.restart = true;
  642. }
  643. #endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
  644. #endif /* RGBLIGHT_USE_TIMER */
  645. }
  646. #endif /* RGBLIGHT_SPLIT */
  647. #ifdef RGBLIGHT_USE_TIMER
  648. typedef void (*effect_func_t)(animation_status_t *anim);
  649. // Animation timer -- use system timer (AVR Timer0)
  650. void rgblight_timer_init(void) {
  651. // OLD!!!! Animation timer -- AVR Timer3
  652. // static uint8_t rgblight_timer_is_init = 0;
  653. // if (rgblight_timer_is_init) {
  654. // return;
  655. // }
  656. // rgblight_timer_is_init = 1;
  657. // /* Timer 3 setup */
  658. // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
  659. // | _BV(CS30); // Clock selelct: clk/1
  660. // /* Set TOP value */
  661. // uint8_t sreg = SREG;
  662. // cli();
  663. // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  664. // OCR3AL = RGBLED_TIMER_TOP & 0xff;
  665. // SREG = sreg;
  666. rgblight_status.timer_enabled = false;
  667. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  668. }
  669. void rgblight_timer_enable(void) {
  670. if( !is_static_effect(rgblight_config.mode) ) {
  671. rgblight_status.timer_enabled = true;
  672. }
  673. animation_status.last_timer = timer_read();
  674. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  675. dprintf("rgblight timer enabled.\n");
  676. }
  677. void rgblight_timer_disable(void) {
  678. rgblight_status.timer_enabled = false;
  679. RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
  680. dprintf("rgblight timer disable.\n");
  681. }
  682. void rgblight_timer_toggle(void) {
  683. dprintf("rgblight timer toggle.\n");
  684. if(rgblight_status.timer_enabled) {
  685. rgblight_timer_disable();
  686. } else {
  687. rgblight_timer_enable();
  688. }
  689. }
  690. void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
  691. rgblight_enable();
  692. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  693. rgblight_setrgb(r, g, b);
  694. }
  695. static void rgblight_effect_dummy(animation_status_t *anim) {
  696. // do nothing
  697. /********
  698. dprintf("rgblight_task() what happened?\n");
  699. dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
  700. dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
  701. rgblight_config.mode, rgblight_status.base_mode,
  702. rgblight_status.timer_enabled);
  703. dprintf("last_timer = %d\n",anim->last_timer);
  704. **/
  705. }
  706. void rgblight_task(void) {
  707. if (rgblight_status.timer_enabled) {
  708. effect_func_t effect_func = rgblight_effect_dummy;
  709. uint16_t interval_time = 2000; // dummy interval
  710. uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
  711. animation_status.delta = delta;
  712. // static light mode, do nothing here
  713. if ( 1 == 0 ) { //dummy
  714. }
  715. #ifdef RGBLIGHT_EFFECT_BREATHING
  716. else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
  717. // breathing mode
  718. interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
  719. effect_func = rgblight_effect_breathing;
  720. }
  721. #endif
  722. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  723. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
  724. // rainbow mood mode
  725. interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
  726. effect_func = rgblight_effect_rainbow_mood;
  727. }
  728. #endif
  729. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  730. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
  731. // rainbow swirl mode
  732. interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
  733. effect_func = rgblight_effect_rainbow_swirl;
  734. }
  735. #endif
  736. #ifdef RGBLIGHT_EFFECT_SNAKE
  737. else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
  738. // snake mode
  739. interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
  740. effect_func = rgblight_effect_snake;
  741. }
  742. #endif
  743. #ifdef RGBLIGHT_EFFECT_KNIGHT
  744. else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
  745. // knight mode
  746. interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
  747. effect_func = rgblight_effect_knight;
  748. }
  749. #endif
  750. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  751. else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
  752. // christmas mode
  753. interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
  754. effect_func = (effect_func_t)rgblight_effect_christmas;
  755. }
  756. #endif
  757. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  758. else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
  759. // RGB test mode
  760. interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
  761. effect_func = (effect_func_t)rgblight_effect_rgbtest;
  762. }
  763. #endif
  764. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  765. else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING){
  766. interval_time = 500;
  767. effect_func = (effect_func_t)rgblight_effect_alternating;
  768. }
  769. #endif
  770. if (animation_status.restart) {
  771. animation_status.restart = false;
  772. animation_status.last_timer = timer_read() - interval_time - 1;
  773. animation_status.pos16 = 0; // restart signal to local each effect
  774. }
  775. if (timer_elapsed(animation_status.last_timer) >= interval_time) {
  776. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  777. static uint16_t report_last_timer = 0;
  778. static bool tick_flag = false;
  779. uint16_t oldpos16;
  780. if (tick_flag) {
  781. tick_flag = false;
  782. if (timer_elapsed(report_last_timer) >= 30000) {
  783. report_last_timer = timer_read();
  784. dprintf("rgblight animation tick report to slave\n");
  785. RGBLIGHT_SPLIT_ANIMATION_TICK;
  786. }
  787. }
  788. oldpos16 = animation_status.pos16;
  789. #endif
  790. animation_status.last_timer += interval_time;
  791. effect_func(&animation_status);
  792. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  793. if (animation_status.pos16 == 0 && oldpos16 != 0) {
  794. tick_flag = true;
  795. }
  796. #endif
  797. }
  798. }
  799. }
  800. #endif /* RGBLIGHT_USE_TIMER */
  801. // Effects
  802. #ifdef RGBLIGHT_EFFECT_BREATHING
  803. #ifndef RGBLIGHT_EFFECT_BREATHE_CENTER
  804. #ifndef RGBLIGHT_BREATHE_TABLE_SIZE
  805. #define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256 or 128 or 64
  806. #endif
  807. #include <rgblight_breathe_table.h>
  808. #endif
  809. __attribute__ ((weak))
  810. const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  811. void rgblight_effect_breathing(animation_status_t *anim) {
  812. float val;
  813. // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
  814. #ifdef RGBLIGHT_EFFECT_BREATHE_TABLE
  815. val = pgm_read_byte(&rgblight_effect_breathe_table[anim->pos / table_scale]);
  816. #else
  817. val = (exp(sin((anim->pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
  818. #endif
  819. rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
  820. anim->pos = (anim->pos + 1);
  821. }
  822. #endif
  823. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  824. __attribute__ ((weak))
  825. const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  826. void rgblight_effect_rainbow_mood(animation_status_t *anim) {
  827. rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
  828. anim->current_hue++;
  829. }
  830. #endif
  831. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  832. #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
  833. #define RGBLIGHT_RAINBOW_SWIRL_RANGE 255
  834. #endif
  835. __attribute__ ((weak))
  836. const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  837. void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
  838. uint8_t hue;
  839. uint8_t i;
  840. for (i = 0; i < effect_num_leds; i++) {
  841. hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / effect_num_leds * i + anim->current_hue);
  842. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
  843. }
  844. rgblight_set();
  845. if (anim->delta % 2) {
  846. anim->current_hue++;
  847. } else {
  848. anim->current_hue--;
  849. }
  850. }
  851. #endif
  852. #ifdef RGBLIGHT_EFFECT_SNAKE
  853. __attribute__ ((weak))
  854. const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  855. void rgblight_effect_snake(animation_status_t *anim) {
  856. static uint8_t pos = 0;
  857. uint8_t i, j;
  858. int8_t k;
  859. int8_t increment = 1;
  860. if (anim->delta % 2) {
  861. increment = -1;
  862. }
  863. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  864. if (anim->pos == 0) { // restart signal
  865. if (increment == 1) {
  866. pos = effect_num_leds - 1;
  867. } else {
  868. pos = 0;
  869. }
  870. anim->pos = 1;
  871. }
  872. #endif
  873. for (i = 0; i < effect_num_leds; i++) {
  874. LED_TYPE *ledp = led + i + effect_start_pos;
  875. ledp->r = 0;
  876. ledp->g = 0;
  877. ledp->b = 0;
  878. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  879. k = pos + j * increment;
  880. if (k < 0) {
  881. k = k + effect_num_leds;
  882. }
  883. if (i == k) {
  884. sethsv(rgblight_config.hue, rgblight_config.sat,
  885. (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH),
  886. ledp);
  887. }
  888. }
  889. }
  890. rgblight_set();
  891. if (increment == 1) {
  892. if (pos - 1 < 0) {
  893. pos = effect_num_leds - 1;
  894. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  895. anim->pos = 0;
  896. #endif
  897. } else {
  898. pos -= 1;
  899. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  900. anim->pos = 1;
  901. #endif
  902. }
  903. } else {
  904. pos = (pos + 1) % effect_num_leds;
  905. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  906. anim->pos = pos;
  907. #endif
  908. }
  909. }
  910. #endif
  911. #ifdef RGBLIGHT_EFFECT_KNIGHT
  912. __attribute__ ((weak))
  913. const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
  914. void rgblight_effect_knight(animation_status_t *anim) {
  915. static int8_t low_bound = 0;
  916. static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  917. static int8_t increment = 1;
  918. uint8_t i, cur;
  919. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  920. if (anim->pos == 0) { // restart signal
  921. anim->pos = 1;
  922. low_bound = 0;
  923. high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  924. increment = 1;
  925. }
  926. #endif
  927. // Set all the LEDs to 0
  928. for (i = effect_start_pos; i < effect_end_pos; i++) {
  929. led[i].r = 0;
  930. led[i].g = 0;
  931. led[i].b = 0;
  932. }
  933. // Determine which LEDs should be lit up
  934. for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
  935. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % effect_num_leds + effect_start_pos;
  936. if (i >= low_bound && i <= high_bound) {
  937. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
  938. } else {
  939. led[cur].r = 0;
  940. led[cur].g = 0;
  941. led[cur].b = 0;
  942. }
  943. }
  944. rgblight_set();
  945. // Move from low_bound to high_bound changing the direction we increment each
  946. // time a boundary is hit.
  947. low_bound += increment;
  948. high_bound += increment;
  949. if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
  950. increment = -increment;
  951. #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
  952. if (increment == 1) {
  953. anim->pos = 0;
  954. }
  955. #endif
  956. }
  957. }
  958. #endif
  959. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  960. void rgblight_effect_christmas(animation_status_t *anim) {
  961. uint8_t hue;
  962. uint8_t i;
  963. anim->current_offset = (anim->current_offset + 1) % 2;
  964. for (i = 0; i < effect_num_leds; i++) {
  965. hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85;
  966. sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
  967. }
  968. rgblight_set();
  969. }
  970. #endif
  971. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  972. __attribute__ ((weak))
  973. const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
  974. void rgblight_effect_rgbtest(animation_status_t *anim) {
  975. static uint8_t maxval = 0;
  976. uint8_t g; uint8_t r; uint8_t b;
  977. if( maxval == 0 ) {
  978. LED_TYPE tmp_led;
  979. sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
  980. maxval = tmp_led.r;
  981. }
  982. g = r = b = 0;
  983. switch( anim->pos ) {
  984. case 0: r = maxval; break;
  985. case 1: g = maxval; break;
  986. case 2: b = maxval; break;
  987. }
  988. rgblight_setrgb(r, g, b);
  989. anim->pos = (anim->pos + 1) % 3;
  990. }
  991. #endif
  992. #ifdef RGBLIGHT_EFFECT_ALTERNATING
  993. void rgblight_effect_alternating(animation_status_t *anim) {
  994. for (int i = 0; i < effect_num_leds; i++) {
  995. LED_TYPE *ledp = led + i + effect_start_pos;
  996. if (i<effect_num_leds/2 && anim->pos) {
  997. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
  998. } else if (i>=effect_num_leds/2 && !anim->pos) {
  999. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
  1000. } else {
  1001. sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp);
  1002. }
  1003. }
  1004. rgblight_set();
  1005. anim->pos = (anim->pos + 1) % 2;
  1006. }
  1007. #endif