Fading and gpio override finally work

This commit is contained in:
whowechina 2024-02-16 23:29:22 +09:00
parent f3355cc1fa
commit 4d71b7e11d
7 changed files with 64 additions and 36 deletions

Binary file not shown.

View File

@ -47,7 +47,7 @@ uint8_t button_num()
return BUTTON_NUM;
}
uint8_t button_gpio(int id)
uint8_t button_real_gpio(int id)
{
if (id >= BUTTON_NUM) {
return 0xff;
@ -55,6 +55,14 @@ uint8_t button_gpio(int id)
return gpio_real[id];
}
uint8_t button_default_gpio(int id)
{
if (id >= BUTTON_NUM) {
return 0xff;
}
return gpio_def[id];
}
static uint16_t button_reading;
/* If a switch flips, it freezes for a while */

View File

@ -14,6 +14,7 @@ void button_init();
uint8_t button_num();
void button_update();
uint16_t button_read();
uint8_t button_gpio(int id);
uint8_t button_real_gpio(int id);
uint8_t button_default_gpio(int id);
#endif

View File

@ -11,7 +11,7 @@
#include "save.h"
#define MAX_COMMANDS 32
#define MAX_PARAMETERS 6
#define MAX_PARAMETERS 10
#define MAX_PARAMETER_LENGTH 20
const char *cli_prompt = "cli>";

View File

@ -66,10 +66,11 @@ static void disp_gpio()
printf("[GPIO]\n");
printf(" Main buttons:");
for (int i = 0; i < 8; i++) {
printf(" %d:%d", i + 1, button_gpio(i));
printf(" %d:%d", i + 1, button_real_gpio(i));
}
printf("\n Test:%d, Service:%d, Navigate:%d, Coin:%d\n",
button_gpio(8), button_gpio(9), button_gpio(10), button_gpio(11));
button_real_gpio(8), button_real_gpio(9),
button_real_gpio(10), button_real_gpio(11));
}
void handle_display(int argc, char *argv[])
@ -386,9 +387,18 @@ static void handle_save()
static void handle_gpio(int argc, char *argv[])
{
const char *usage = "Usage: gpio main <gpio1> <gpio2> ... <gpio8>\n"
" or: gpio <test|service|navigate|coin> <gpio>\n"
" gpio: 0..29\n";
if (argc == 9) {
" gpio <test|service|navigate|coin> <gpio>\n"
" gpio reset\n"
" gpio: 0..29\n";
if (argc == 1) {
if (strcasecmp(argv[0], "reset") != 0) {
printf(usage);
return;
}
for (int i = 0; i < sizeof(mai_cfg->alt.buttons); i++) {
mai_cfg->alt.buttons[i] = 0xff;
}
} else if (argc == 9) {
const char *choices[] = {"main"};
if (cli_match_prefix(choices, 1, argv[0]) < 0) {
printf(usage);
@ -403,7 +413,11 @@ static void handle_gpio(int argc, char *argv[])
}
gpio_main[i] = gpio;
}
memcpy(mai_cfg->alt.buttons, gpio_main, 8);
for (int i = 0; i < 8; i++) {
int gpio = gpio_main[i];
bool is_default = (gpio == button_default_gpio(i));
mai_cfg->alt.buttons[i] = is_default ? 0xff : gpio;
}
} else if (argc == 2) {
const char *choices[] = {"test", "service", "navigate", "coin"};
const uint8_t button_pos[] = {8, 9, 10, 11};
@ -413,13 +427,16 @@ static void handle_gpio(int argc, char *argv[])
printf(usage);
return;
}
mai_cfg->alt.buttons[button_pos[match]] = gpio;
int index = button_pos[match];
bool is_default = (gpio == button_default_gpio(index));
mai_cfg->alt.buttons[index] = is_default ? 0xff : gpio;
} else {
printf(usage);
return;
}
config_changed();
button_init(); // Re-init the buttons
disp_gpio();
}
void commands_init()

View File

@ -126,35 +126,35 @@ static void led_cmd(cdc_t *cdc)
switch (cdc->hdr.cmd) {
case 0x31:
printf("8b\n");
//printf("C %d:1: %02x%02x%02x\n", cdc->led.index, cdc->led.r, cdc->led.g, cdc->led.b);
rgb_set_button(cdc->led.index, color, 0);
break;
case 0x32:
printf("8bM\n");
//printf("C %d:%d: %02x%02x%02x\n", cdc->led.start, cdc->led.len, cdc->led.r, cdc->led.g, cdc->led.b);
for (int i = 0; i < cdc->led.len; i++) {
rgb_set_button(i + cdc->led.start, color, 0);
}
break;
case 0x33:
printf("8bMF\n");
//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);
for (int i = 0; i < cdc->led.len; i++) {
rgb_set_button(i + cdc->led.start, color, cdc->led.speed);
}
break;
case 0x39:
printf("Fet\n");
//printf("Fet\n");
rgb_set_cab(0, gray32(cdc->led.body, false));
rgb_set_cab(1, gray32(cdc->led.ext, false));
rgb_set_cab(2, gray32(cdc->led.side, false));
break;
case 0x3C:
printf("Upd\n");
//printf("Upd\n");
break;
case 0x82:
printf("Dir\n");
//printf("Dir\n");
break;
default:
printf("LEDUnk\n");
printf("Unknown LED Cmd %02x\n", cdc->hdr.cmd);
return;
}
}

View File

@ -22,12 +22,13 @@
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
uint32_t rgb_buf[20];
static struct {
uint32_t color;
uint32_t color; // current color
uint32_t target; // target color
uint16_t duration;
uint16_t elapsed;
} rgb_ctrl[20];
} fade_ctx[20];
static const uint8_t button_led_map[] = RGB_BUTTON_MAP;
#define _MAP_LED(x) _MAKE_MAPPER(x)
@ -111,10 +112,10 @@ static void drive_led()
}
last = now;
for (int i = 0; i < ARRAY_SIZE(rgb_ctrl); i++) {
for (int i = 0; i < ARRAY_SIZE(rgb_buf); i++) {
int num = (i < 8) ? mai_cfg->rgb.per_button : mai_cfg->rgb.per_aux;
for (int j = 0; j < num; j++) {
pio_sm_put_blocking(pio0, 0, rgb_ctrl[i].color << 8u);
pio_sm_put_blocking(pio0, 0, rgb_buf[i] << 8u);
}
}
}
@ -125,24 +126,24 @@ static void fade_ctrl()
uint64_t now = time_us_64();
uint32_t delta_ms = (now - last) / 1000;
if (delta_ms == 0) {
if (delta_ms < 4) { // no faster than 250Hz
return;
}
for (int i = 0; i < ARRAY_SIZE(rgb_ctrl); i++) {
if (rgb_ctrl[i].duration == 0) {
for (int i = 0; i < ARRAY_SIZE(fade_ctx); i++) {
if (fade_ctx[i].duration == 0) {
continue;
}
rgb_ctrl[i].elapsed += delta_ms;
if (rgb_ctrl[i].elapsed >= rgb_ctrl[i].duration) {
rgb_ctrl[i].color = rgb_ctrl[i].target;
rgb_ctrl[i].duration = 0;
fade_ctx[i].elapsed += delta_ms;
if (fade_ctx[i].elapsed >= fade_ctx[i].duration) {
fade_ctx[i].duration = 0;
rgb_buf[i] = fade_ctx[i].target;
continue;
}
uint8_t progress = rgb_ctrl[i].elapsed * 255 / rgb_ctrl[i].duration;
rgb_ctrl->color = lerp(rgb_ctrl->color, rgb_ctrl->target, progress);
uint8_t progress = fade_ctx[i].elapsed * 255 / fade_ctx[i].duration;
rgb_buf[i] = lerp(fade_ctx->color, fade_ctx->target, progress);
}
last = now;
@ -163,17 +164,18 @@ static inline uint32_t apply_level(uint32_t color)
static void set_color(unsigned index, uint32_t color, uint8_t speed)
{
if (index >= ARRAY_SIZE(rgb_ctrl)) {
if (index >= ARRAY_SIZE(fade_ctx)) {
return;
}
if (speed > 0) {
rgb_ctrl[index].target = apply_level(color);
rgb_ctrl[index].duration = 32767 / speed;
rgb_ctrl[index].elapsed = 0;
fade_ctx[index].color= rgb_buf[index];
fade_ctx[index].target = apply_level(color);
fade_ctx[index].duration = 32767 / speed;
fade_ctx[index].elapsed = 0;
} else {
rgb_ctrl[index].color = apply_level(color);
rgb_ctrl[index].duration = 0;
rgb_buf[index] = apply_level(color);
fade_ctx[index].duration = 0;
}
}