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.

571 lines
17 KiB

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