rgblight.c 35 KB

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