Fix LED bahavior

This commit is contained in:
whowechina 2024-02-18 00:07:33 +09:00
parent 1d823b8d0f
commit bbc51f2a4d
3 changed files with 44 additions and 27 deletions

Binary file not shown.

View File

@ -122,21 +122,32 @@ static void led_cmd(cdc_t *cdc)
cdc->len = 0; cdc->len = 0;
ctx.last_io_time = time_us_64(); ctx.last_io_time = time_us_64();
uint32_t color = rgb32(cdc->led.r, cdc->led.g, cdc->led.b, false); uint32_t now = (ctx.last_io_time / 1000) % 10000;
uint32_t color;
switch (cdc->hdr.cmd) { switch (cdc->hdr.cmd) {
case 0x10:
for (int i = 0; i < 8; i++) {
rgb_set_button(i, 0, 0);
}
for (int i = 0; i < 3; i++) {
rgb_set_cab(i, 0);
}
break;
case 0x31: case 0x31:
//printf("C %d:1: %02x%02x%02x\n", cdc->led.index, cdc->led.r, cdc->led.g, cdc->led.b); color = rgb32(cdc->led.r, cdc->led.g, cdc->led.b, false);
//printf("%4d:c %d:1: %06x\n", now, cdc->led.index, color);
rgb_set_button(cdc->led.index, color, 0); rgb_set_button(cdc->led.index, color, 0);
break; break;
case 0x32: case 0x32:
//printf("C %d:%d: %02x%02x%02x\n", cdc->led.start, cdc->led.len, cdc->led.r, cdc->led.g, cdc->led.b); color = rgb32(cdc->led.mr, cdc->led.mg, cdc->led.mb, false);
//printf("%4d:C %d:%d: %06x\n", now, cdc->led.start, cdc->led.len, color, cdc->led.mb);
for (int i = 0; i < cdc->led.len; i++) { for (int i = 0; i < cdc->led.len; i++) {
rgb_set_button(i + cdc->led.start, color, 0); rgb_set_button(i + cdc->led.start, color, 0);
} }
break; break;
case 0x33: case 0x33:
//printf("F %d:%d: %02x%02x%02x %d\n", cdc->led.start, cdc->led.len, cdc->led.r, cdc->led.g, cdc->led.b, cdc->led.speed); color = rgb32(cdc->led.mr, cdc->led.mg, cdc->led.mb, false);
//printf("%4d:F %d:%d: %06x %d\n", now, cdc->led.start, cdc->led.len, color, cdc->led.speed);
for (int i = 0; i < cdc->led.len; i++) { for (int i = 0; i < cdc->led.len; i++) {
rgb_set_button(i + cdc->led.start, color, cdc->led.speed); rgb_set_button(i + cdc->led.start, color, cdc->led.speed);
} }

View File

@ -95,12 +95,17 @@ uint32_t rgb32_from_hsv(uint8_t h, uint8_t s, uint8_t v)
} }
} }
static inline uint8_t lerp8b(uint8_t a, uint8_t b, uint8_t t)
{
return a + (b - a) * t / 255;
}
static uint32_t lerp(uint32_t a, uint32_t b, uint8_t t) static uint32_t lerp(uint32_t a, uint32_t b, uint8_t t)
{ {
uint32_t c1 = ((a & 0xff0000) * (255 - t) + (b & 0xff0000) * t) & 0xff000000; uint32_t c1 = lerp8b((a >> 16) & 0xff, (b >> 16) & 0xff, t);
uint32_t c2 = ((a & 0xff00) * (255 - t) + (b & 0xff00) * t) & 0xff0000; uint32_t c2 = lerp8b((a >> 8) & 0xff, (b >> 8) & 0xff, t);
uint32_t c3 = ((a & 0xff) * (255 - t) + (b & 0xff) * t) & 0xff00; uint32_t c3 = lerp8b(a & 0xff, b & 0xff, t);
return c1 | c2 | c3; return c1 << 16 | c2 << 8 | c3;
} }
static void drive_led() static void drive_led()
@ -120,6 +125,19 @@ static void drive_led()
} }
} }
static inline uint32_t apply_level(uint32_t color)
{
unsigned r = (color >> 16) & 0xff;
unsigned g = (color >> 8) & 0xff;
unsigned b = color & 0xff;
r = r * mai_cfg->color.level / 255;
g = g * mai_cfg->color.level / 255;
b = b * mai_cfg->color.level / 255;
return r << 16 | g << 8 | b;
}
static void fade_ctrl() static void fade_ctrl()
{ {
static uint64_t last = 0; static uint64_t last = 0;
@ -143,25 +161,13 @@ static void fade_ctrl()
} }
uint8_t progress = fade_ctx[i].elapsed * 255 / fade_ctx[i].duration; uint8_t progress = fade_ctx[i].elapsed * 255 / fade_ctx[i].duration;
rgb_buf[i] = lerp(fade_ctx->color, fade_ctx->target, progress); uint32_t color = lerp(fade_ctx[i].color, fade_ctx[i].target, progress);
rgb_buf[i] = apply_level(color);
} }
last = now; last = now;
} }
static inline uint32_t apply_level(uint32_t color)
{
unsigned r = (color >> 16) & 0xff;
unsigned g = (color >> 8) & 0xff;
unsigned b = color & 0xff;
r = r * mai_cfg->color.level / 255;
g = g * mai_cfg->color.level / 255;
b = b * mai_cfg->color.level / 255;
return r << 16 | g << 8 | b;
}
static void set_color(unsigned index, uint32_t color, uint8_t speed) static void set_color(unsigned index, uint32_t color, uint8_t speed)
{ {
if (index >= ARRAY_SIZE(fade_ctx)) { if (index >= ARRAY_SIZE(fade_ctx)) {
@ -169,13 +175,13 @@ static void set_color(unsigned index, uint32_t color, uint8_t speed)
} }
if (speed > 0) { if (speed > 0) {
fade_ctx[index].color= rgb_buf[index]; fade_ctx[index].target = color;
fade_ctx[index].target = apply_level(color); fade_ctx[index].duration = 4095 / speed * 8;
fade_ctx[index].duration = 32767 / speed;
fade_ctx[index].elapsed = 0; fade_ctx[index].elapsed = 0;
} else { } else {
rgb_buf[index] = apply_level(color); fade_ctx[index].color= color;
fade_ctx[index].duration = 0; fade_ctx[index].duration = 0;
rgb_buf[index] = apply_level(color);
} }
} }