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

558 lines
16 KiB

  1. #include <avr/eeprom.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4. #include "progmem.h"
  5. #include "timer.h"
  6. #include "rgblight.h"
  7. #include "debug.h"
  8. // Lightness curve using the CIE 1931 lightness formula
  9. //Generated by the python script provided in http://jared.geek.nz/2013/feb/linear-led-pwm
  10. const uint8_t DIM_CURVE[] PROGMEM = {
  11. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  12. 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
  13. 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
  14. 3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
  15. 5, 5, 6, 6, 6, 6, 6, 7, 7, 7,
  16. 7, 8, 8, 8, 8, 9, 9, 9, 10, 10,
  17. 10, 10, 11, 11, 11, 12, 12, 12, 13, 13,
  18. 13, 14, 14, 15, 15, 15, 16, 16, 17, 17,
  19. 17, 18, 18, 19, 19, 20, 20, 21, 21, 22,
  20. 22, 23, 23, 24, 24, 25, 25, 26, 26, 27,
  21. 28, 28, 29, 29, 30, 31, 31, 32, 32, 33,
  22. 34, 34, 35, 36, 37, 37, 38, 39, 39, 40,
  23. 41, 42, 43, 43, 44, 45, 46, 47, 47, 48,
  24. 49, 50, 51, 52, 53, 54, 54, 55, 56, 57,
  25. 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
  26. 68, 70, 71, 72, 73, 74, 75, 76, 77, 79,
  27. 80, 81, 82, 83, 85, 86, 87, 88, 90, 91,
  28. 92, 94, 95, 96, 98, 99, 100, 102, 103, 105,
  29. 106, 108, 109, 110, 112, 113, 115, 116, 118, 120,
  30. 121, 123, 124, 126, 128, 129, 131, 132, 134, 136,
  31. 138, 139, 141, 143, 145, 146, 148, 150, 152, 154,
  32. 155, 157, 159, 161, 163, 165, 167, 169, 171, 173,
  33. 175, 177, 179, 181, 183, 185, 187, 189, 191, 193,
  34. 196, 198, 200, 202, 204, 207, 209, 211, 214, 216,
  35. 218, 220, 223, 225, 228, 230, 232, 235, 237, 240,
  36. 242, 245, 247, 250, 252, 255,
  37. };
  38. const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {
  39. 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9,
  40. 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
  41. 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
  42. 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124,
  43. 127, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173,
  44. 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 213, 215,
  45. 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, 241, 243, 244,
  46. 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
  47. 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 250, 250, 249, 248, 246,
  48. 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 230, 228, 226, 224, 222, 220,
  49. 218, 215, 213, 211, 208, 206, 203, 201, 198, 196, 193, 190, 188, 185, 182, 179,
  50. 176, 173, 170, 167, 165, 162, 158, 155, 152, 149, 146, 143, 140, 137, 134, 131,
  51. 128, 124, 121, 118, 115, 112, 109, 106, 103, 100, 97, 93, 90, 88, 85, 82,
  52. 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
  53. 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
  54. 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0
  55. };
  56. __attribute__ ((weak))
  57. const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  58. __attribute__ ((weak))
  59. const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  60. __attribute__ ((weak))
  61. const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  62. __attribute__ ((weak))
  63. const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  64. __attribute__ ((weak))
  65. const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {100, 50, 20};
  66. rgblight_config_t rgblight_config;
  67. rgblight_config_t inmem_config;
  68. struct cRGB led[RGBLED_NUM];
  69. uint8_t rgblight_inited = 0;
  70. void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
  71. uint8_t r = 0, g = 0, b = 0, base, color;
  72. if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
  73. r = val;
  74. g = val;
  75. b = val;
  76. } else {
  77. base = ((255 - sat) * val) >> 8;
  78. color = (val - base) * (hue % 60) / 60;
  79. switch (hue / 60) {
  80. case 0:
  81. r = val;
  82. g = base + color;
  83. b = base;
  84. break;
  85. case 1:
  86. r = val - color;
  87. g = val;
  88. b = base;
  89. break;
  90. case 2:
  91. r = base;
  92. g = val;
  93. b = base + color;
  94. break;
  95. case 3:
  96. r = base;
  97. g = val - color;
  98. b = val;
  99. break;
  100. case 4:
  101. r = base + color;
  102. g = base;
  103. b = val;
  104. break;
  105. case 5:
  106. r = val;
  107. g = base;
  108. b = val - color;
  109. break;
  110. }
  111. }
  112. r = pgm_read_byte(&DIM_CURVE[r]);
  113. g = pgm_read_byte(&DIM_CURVE[g]);
  114. b = pgm_read_byte(&DIM_CURVE[b]);
  115. setrgb(r, g, b, led1);
  116. }
  117. void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) {
  118. (*led1).r = r;
  119. (*led1).g = g;
  120. (*led1).b = b;
  121. }
  122. uint32_t eeconfig_read_rgblight(void) {
  123. return eeprom_read_dword(EECONFIG_RGBLIGHT);
  124. }
  125. void eeconfig_update_rgblight(uint32_t val) {
  126. eeprom_update_dword(EECONFIG_RGBLIGHT, val);
  127. }
  128. void eeconfig_update_rgblight_default(void) {
  129. dprintf("eeconfig_update_rgblight_default\n");
  130. rgblight_config.enable = 1;
  131. rgblight_config.mode = 1;
  132. rgblight_config.hue = 200;
  133. rgblight_config.sat = 204;
  134. rgblight_config.val = 204;
  135. eeconfig_update_rgblight(rgblight_config.raw);
  136. }
  137. void eeconfig_debug_rgblight(void) {
  138. dprintf("rgblight_config eprom\n");
  139. dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
  140. dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
  141. dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
  142. dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
  143. dprintf("rgblight_config.val = %d\n", rgblight_config.val);
  144. }
  145. void rgblight_init(void) {
  146. debug_enable = 1; // Debug ON!
  147. dprintf("rgblight_init called.\n");
  148. rgblight_inited = 1;
  149. dprintf("rgblight_init start!\n");
  150. if (!eeconfig_is_enabled()) {
  151. dprintf("rgblight_init eeconfig is not enabled.\n");
  152. eeconfig_init();
  153. eeconfig_update_rgblight_default();
  154. }
  155. rgblight_config.raw = eeconfig_read_rgblight();
  156. if (!rgblight_config.mode) {
  157. dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
  158. eeconfig_update_rgblight_default();
  159. rgblight_config.raw = eeconfig_read_rgblight();
  160. }
  161. eeconfig_debug_rgblight(); // display current eeprom values
  162. #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
  163. rgblight_timer_init(); // setup the timer
  164. #endif
  165. if (rgblight_config.enable) {
  166. rgblight_mode(rgblight_config.mode);
  167. }
  168. }
  169. void rgblight_increase(void) {
  170. uint8_t mode = 0;
  171. if (rgblight_config.mode < RGBLIGHT_MODES) {
  172. mode = rgblight_config.mode + 1;
  173. }
  174. rgblight_mode(mode);
  175. }
  176. void rgblight_decrease(void) {
  177. uint8_t mode = 0;
  178. // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
  179. if (rgblight_config.mode > 1) {
  180. mode = rgblight_config.mode - 1;
  181. }
  182. rgblight_mode(mode);
  183. }
  184. void rgblight_step(void) {
  185. uint8_t mode = 0;
  186. mode = rgblight_config.mode + 1;
  187. if (mode > RGBLIGHT_MODES) {
  188. mode = 1;
  189. }
  190. rgblight_mode(mode);
  191. }
  192. void rgblight_mode(uint8_t mode) {
  193. if (!rgblight_config.enable) {
  194. return;
  195. }
  196. if (mode < 1) {
  197. rgblight_config.mode = 1;
  198. } else if (mode > RGBLIGHT_MODES) {
  199. rgblight_config.mode = RGBLIGHT_MODES;
  200. } else {
  201. rgblight_config.mode = mode;
  202. }
  203. eeconfig_update_rgblight(rgblight_config.raw);
  204. xprintf("rgblight mode: %u\n", rgblight_config.mode);
  205. if (rgblight_config.mode == 1) {
  206. #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
  207. rgblight_timer_disable();
  208. #endif
  209. } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) {
  210. // MODE 2-5, breathing
  211. // MODE 6-8, rainbow mood
  212. // MODE 9-14, rainbow swirl
  213. // MODE 15-20, snake
  214. // MODE 21-23, knight
  215. #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
  216. rgblight_timer_enable();
  217. #endif
  218. }
  219. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  220. }
  221. void rgblight_toggle(void) {
  222. rgblight_config.enable ^= 1;
  223. eeconfig_update_rgblight(rgblight_config.raw);
  224. xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
  225. if (rgblight_config.enable) {
  226. rgblight_mode(rgblight_config.mode);
  227. } else {
  228. #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
  229. rgblight_timer_disable();
  230. #endif
  231. _delay_ms(50);
  232. rgblight_set();
  233. }
  234. }
  235. void rgblight_increase_hue(void) {
  236. uint16_t hue;
  237. hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
  238. rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
  239. }
  240. void rgblight_decrease_hue(void) {
  241. uint16_t hue;
  242. if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
  243. hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
  244. } else {
  245. hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
  246. }
  247. rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
  248. }
  249. void rgblight_increase_sat(void) {
  250. uint8_t sat;
  251. if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
  252. sat = 255;
  253. } else {
  254. sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
  255. }
  256. rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
  257. }
  258. void rgblight_decrease_sat(void) {
  259. uint8_t sat;
  260. if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
  261. sat = 0;
  262. } else {
  263. sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
  264. }
  265. rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
  266. }
  267. void rgblight_increase_val(void) {
  268. uint8_t val;
  269. if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
  270. val = 255;
  271. } else {
  272. val = rgblight_config.val + RGBLIGHT_VAL_STEP;
  273. }
  274. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
  275. }
  276. void rgblight_decrease_val(void) {
  277. uint8_t val;
  278. if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
  279. val = 0;
  280. } else {
  281. val = rgblight_config.val - RGBLIGHT_VAL_STEP;
  282. }
  283. rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
  284. }
  285. void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
  286. inmem_config.raw = rgblight_config.raw;
  287. if (rgblight_config.enable) {
  288. struct cRGB tmp_led;
  289. sethsv(hue, sat, val, &tmp_led);
  290. inmem_config.hue = hue;
  291. inmem_config.sat = sat;
  292. inmem_config.val = val;
  293. // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
  294. rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
  295. }
  296. }
  297. void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
  298. if (rgblight_config.enable) {
  299. if (rgblight_config.mode == 1) {
  300. // same static color
  301. rgblight_sethsv_noeeprom(hue, sat, val);
  302. } else {
  303. // all LEDs in same color
  304. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  305. // breathing mode, ignore the change of val, use in memory value instead
  306. val = rgblight_config.val;
  307. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
  308. // rainbow mood and rainbow swirl, ignore the change of hue
  309. hue = rgblight_config.hue;
  310. }
  311. }
  312. rgblight_config.hue = hue;
  313. rgblight_config.sat = sat;
  314. rgblight_config.val = val;
  315. eeconfig_update_rgblight(rgblight_config.raw);
  316. xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
  317. }
  318. }
  319. void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
  320. // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b);
  321. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  322. led[i].r = r;
  323. led[i].g = g;
  324. led[i].b = b;
  325. }
  326. rgblight_set();
  327. }
  328. void rgblight_set(void) {
  329. if (rgblight_config.enable) {
  330. ws2812_setleds(led, RGBLED_NUM);
  331. } else {
  332. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  333. led[i].r = 0;
  334. led[i].g = 0;
  335. led[i].b = 0;
  336. }
  337. ws2812_setleds(led, RGBLED_NUM);
  338. }
  339. }
  340. #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER)
  341. // Animation timer -- AVR Timer3
  342. void rgblight_timer_init(void) {
  343. static uint8_t rgblight_timer_is_init = 0;
  344. if (rgblight_timer_is_init) {
  345. return;
  346. }
  347. rgblight_timer_is_init = 1;
  348. /* Timer 3 setup */
  349. TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP
  350. | _BV(CS30); //Clock selelct: clk/1
  351. /* Set TOP value */
  352. uint8_t sreg = SREG;
  353. cli();
  354. OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
  355. OCR3AL = RGBLED_TIMER_TOP & 0xff;
  356. SREG = sreg;
  357. }
  358. void rgblight_timer_enable(void) {
  359. TIMSK3 |= _BV(OCIE3A);
  360. dprintf("TIMER3 enabled.\n");
  361. }
  362. void rgblight_timer_disable(void) {
  363. TIMSK3 &= ~_BV(OCIE3A);
  364. dprintf("TIMER3 disabled.\n");
  365. }
  366. void rgblight_timer_toggle(void) {
  367. TIMSK3 ^= _BV(OCIE3A);
  368. dprintf("TIMER3 toggled.\n");
  369. }
  370. ISR(TIMER3_COMPA_vect) {
  371. // mode = 1, static light, do nothing here
  372. if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
  373. // mode = 2 to 5, breathing mode
  374. rgblight_effect_breathing(rgblight_config.mode - 2);
  375. } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
  376. // mode = 6 to 8, rainbow mood mod
  377. rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
  378. } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
  379. // mode = 9 to 14, rainbow swirl mode
  380. rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
  381. } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
  382. // mode = 15 to 20, snake mode
  383. rgblight_effect_snake(rgblight_config.mode - 15);
  384. } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
  385. // mode = 21 to 23, knight mode
  386. rgblight_effect_knight(rgblight_config.mode - 21);
  387. }
  388. }
  389. // Effects
  390. void rgblight_effect_breathing(uint8_t interval) {
  391. static uint8_t pos = 0;
  392. static uint16_t last_timer = 0;
  393. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
  394. return;
  395. }
  396. last_timer = timer_read();
  397. rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos]));
  398. pos = (pos + 1) % 256;
  399. }
  400. void rgblight_effect_rainbow_mood(uint8_t interval) {
  401. static uint16_t current_hue = 0;
  402. static uint16_t last_timer = 0;
  403. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
  404. return;
  405. }
  406. last_timer = timer_read();
  407. rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
  408. current_hue = (current_hue + 1) % 360;
  409. }
  410. void rgblight_effect_rainbow_swirl(uint8_t interval) {
  411. static uint16_t current_hue = 0;
  412. static uint16_t last_timer = 0;
  413. uint16_t hue;
  414. uint8_t i;
  415. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval / 2])) {
  416. return;
  417. }
  418. last_timer = timer_read();
  419. for (i = 0; i < RGBLED_NUM; i++) {
  420. hue = (360 / RGBLED_NUM * i + current_hue) % 360;
  421. sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]);
  422. }
  423. rgblight_set();
  424. if (interval % 2) {
  425. current_hue = (current_hue + 1) % 360;
  426. } else {
  427. if (current_hue - 1 < 0) {
  428. current_hue = 359;
  429. } else {
  430. current_hue = current_hue - 1;
  431. }
  432. }
  433. }
  434. void rgblight_effect_snake(uint8_t interval) {
  435. static uint8_t pos = 0;
  436. static uint16_t last_timer = 0;
  437. uint8_t i, j;
  438. int8_t k;
  439. int8_t increment = 1;
  440. if (interval % 2) {
  441. increment = -1;
  442. }
  443. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
  444. return;
  445. }
  446. last_timer = timer_read();
  447. for (i = 0; i < RGBLED_NUM; i++) {
  448. led[i].r = 0;
  449. led[i].g = 0;
  450. led[i].b = 0;
  451. for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
  452. k = pos + j * increment;
  453. if (k < 0) {
  454. k = k + RGBLED_NUM;
  455. }
  456. if (i == k) {
  457. sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]);
  458. }
  459. }
  460. }
  461. rgblight_set();
  462. if (increment == 1) {
  463. if (pos - 1 < 0) {
  464. pos = RGBLED_NUM - 1;
  465. } else {
  466. pos -= 1;
  467. }
  468. } else {
  469. pos = (pos + 1) % RGBLED_NUM;
  470. }
  471. }
  472. void rgblight_effect_knight(uint8_t interval) {
  473. static int8_t pos = 0;
  474. static uint16_t last_timer = 0;
  475. uint8_t i, j, cur;
  476. int8_t k;
  477. struct cRGB preled[RGBLED_NUM];
  478. static int8_t increment = -1;
  479. if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
  480. return;
  481. }
  482. last_timer = timer_read();
  483. for (i = 0; i < RGBLED_NUM; i++) {
  484. preled[i].r = 0;
  485. preled[i].g = 0;
  486. preled[i].b = 0;
  487. for (j = 0; j < RGBLIGHT_EFFECT_KNIGHT_LENGTH; j++) {
  488. k = pos + j * increment;
  489. if (k < 0) {
  490. k = 0;
  491. }
  492. if (k >= RGBLED_NUM) {
  493. k = RGBLED_NUM - 1;
  494. }
  495. if (i == k) {
  496. sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]);
  497. }
  498. }
  499. }
  500. if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
  501. for (i = 0; i < RGBLED_NUM; i++) {
  502. cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
  503. led[i].r = preled[cur].r;
  504. led[i].g = preled[cur].g;
  505. led[i].b = preled[cur].b;
  506. }
  507. }
  508. rgblight_set();
  509. if (increment == 1) {
  510. if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
  511. pos = 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH;
  512. increment = -1;
  513. } else {
  514. pos -= 1;
  515. }
  516. } else {
  517. if (pos + 1 > RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
  518. pos = RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
  519. increment = 1;
  520. } else {
  521. pos += 1;
  522. }
  523. }
  524. }
  525. #endif