1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2024-12-01 00:17:16 +01:00

Lighting cleanup and fix HID light color

This commit is contained in:
whowechina 2024-05-12 22:25:21 +08:00
parent 8558f6b250
commit 661e609dbd
4 changed files with 13 additions and 50 deletions

Binary file not shown.

View File

@ -27,7 +27,7 @@ static uint8_t led_gpio[] = LED_DEF;
#define LED_NUM (sizeof(led_gpio)) #define LED_NUM (sizeof(led_gpio))
static uint8_t led_buf[LED_NUM]; static uint8_t led_buf[LED_NUM];
static inline uint32_t _rgb32(uint32_t c1, uint32_t c2, uint32_t c3, bool gamma_fix) static inline uint32_t rgb32(uint32_t c1, uint32_t c2, uint32_t c3, bool gamma_fix)
{ {
if (gamma_fix) { if (gamma_fix) {
c1 = ((c1 + 1) * (c1 + 1) - 1) >> 8; c1 = ((c1 + 1) * (c1 + 1) - 1) >> 8;
@ -39,21 +39,16 @@ static inline uint32_t _rgb32(uint32_t c1, uint32_t c2, uint32_t c3, bool gamma_
} }
/* translate RGB to local RGB channel order */ /* translate RGB to local RGB channel order */
static inline uint32_t rgb2local(uint32_t color) static inline uint32_t rgb2local(uint32_t color, bool gamma_fix)
{ {
uint8_t r = (color >> 16) & 0xff; uint8_t r = (color >> 16) & 0xff;
uint8_t g = (color >> 8) & 0xff; uint8_t g = (color >> 8) & 0xff;
uint8_t b = color & 0xff; uint8_t b = color & 0xff;
return rgb32(r, g, b, false);
}
uint32_t rgb32(uint32_t r, uint32_t g, uint32_t b, bool gamma_fix)
{
#if BUTTON_RGB_ORDER == GRB #if BUTTON_RGB_ORDER == GRB
return _rgb32(g, r, b, gamma_fix); return rgb32(g, r, b, gamma_fix);
#else #else
return _rgb32(r, g, b, gamma_fix); return rgb32(r, g, b, gamma_fix);
#endif #endif
} }
@ -153,7 +148,7 @@ void light_fade_n(int repeat, int count, ...)
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
fading.steps[i].from = i == 0 ? fading.color : fading.steps[i - 1].to; fading.steps[i].from = i == 0 ? fading.color : fading.steps[i - 1].to;
fading.steps[i].to = rgb2local(va_arg(args, uint32_t)); fading.steps[i].to = rgb2local(va_arg(args, uint32_t), true);
fading.steps[i].duration = va_arg(args, int); fading.steps[i].duration = va_arg(args, int);
} }
@ -230,7 +225,7 @@ void light_fade_s(const char *pattern)
fading.step_num = (param_num - 1) / 2; fading.step_num = (param_num - 1) / 2;
for (int i = 0; i < fading.step_num; i++) { for (int i = 0; i < fading.step_num; i++) {
fading.steps[i].from = i == 0 ? fading.color : fading.steps[i - 1].to; fading.steps[i].from = i == 0 ? fading.color : fading.steps[i - 1].to;
fading.steps[i].to = rgb2local(param[i * 2 + 1]); fading.steps[i].to = rgb2local(param[i * 2 + 1], true);
fading.steps[i].duration = param[i * 2 + 2]; fading.steps[i].duration = param[i * 2 + 2];
} }
fading.curr_step = 0; fading.curr_step = 0;
@ -239,7 +234,7 @@ void light_fade_s(const char *pattern)
light_mode = MODE_FADE; light_mode = MODE_FADE;
} }
static void color_control(uint32_t delta_ms) static void fade_control(uint32_t delta_ms)
{ {
if (fading.repeat == 0) { if (fading.repeat == 0) {
return; return;
@ -268,7 +263,7 @@ static void color_control(uint32_t delta_ms)
fading.color = color; fading.color = color;
} }
static void color_render() static void fade_render()
{ {
uint32_t color = apply_level(fading.color, aic_cfg->light.max); uint32_t color = apply_level(fading.color, aic_cfg->light.max);
@ -282,7 +277,7 @@ static void color_render()
} }
} }
static void color_update(uint32_t delta_ms) static void fade_update(uint32_t delta_ms)
{ {
if (light_mode != MODE_FADE) { if (light_mode != MODE_FADE) {
fading.color = 0x000000; fading.color = 0x000000;
@ -290,8 +285,8 @@ static void color_update(uint32_t delta_ms)
return; return;
} }
color_control(delta_ms); fade_control(delta_ms);
color_render(); fade_render();
} }
static struct { static struct {
@ -423,37 +418,6 @@ static void drive_led()
} }
} }
void light_set_color(uint32_t color)
{
for (int i = 0; i < RGB_NUM; i++) {
rgb_buf[i] = apply_level(color, aic_cfg->light.max);
}
}
static uint64_t last_hid = 0;
void light_hid_light(uint8_t r, uint8_t g, uint8_t b)
{
light_set_color(rgb32(r, g, b, false));
last_hid = time_us_64();
}
void light_set_brg(unsigned index, const uint8_t *brg_array, size_t num)
{
if (index >= RGB_NUM) {
return;
}
if (index + num > RGB_NUM) {
num = RGB_NUM - index;
}
for (int i = 0; i < num; i++) {
uint8_t b = brg_array[i * 3 + 0];
uint8_t r = brg_array[i * 3 + 1];
uint8_t g = brg_array[i * 3 + 2];
uint32_t color = apply_level(rgb32(r, g, b, false), aic_cfg->light.max);
rgb_buf[index + i] = color;
}
}
void light_init() void light_init()
{ {
uint pio0_offset = pio_add_program(pio0, &ws2812_program); uint pio0_offset = pio_add_program(pio0, &ws2812_program);
@ -485,7 +449,7 @@ void light_update()
uint32_t delta_ms = (now - last_time) / 1000; uint32_t delta_ms = (now - last_time) / 1000;
last_time = now; last_time = now;
color_update(delta_ms); fade_update(delta_ms);
rainbow_update(delta_ms); rainbow_update(delta_ms);
drive_led(); drive_led();

View File

@ -15,7 +15,6 @@
void light_init(); void light_init();
void light_update(); void light_update();
uint32_t rgb32(uint32_t r, uint32_t g, uint32_t b, bool gamma_fix);
uint32_t rgb32_from_hsv(uint8_t h, uint8_t s, uint8_t v); uint32_t rgb32_from_hsv(uint8_t h, uint8_t s, uint8_t v);
void light_fade(uint32_t color, uint32_t fading_ms); void light_fade(uint32_t color, uint32_t fading_ms);

View File

@ -421,7 +421,7 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
(report_type == HID_REPORT_TYPE_OUTPUT)) { (report_type == HID_REPORT_TYPE_OUTPUT)) {
if (bufsize >= 3) { if (bufsize >= 3) {
last_hid_time = time_us_64(); last_hid_time = time_us_64();
light_fade(rgb32(buffer[0], buffer[1], buffer[2], false), 0); light_fade(buffer[0] << 16 | buffer[1] << 8 | buffer[2], 0);
} }
} }
} }