Button GPIOs can be re-assigned thru CLI

This commit is contained in:
whowe 2024-02-13 21:06:39 +08:00
parent daddf813ce
commit 3d6661d6e7
6 changed files with 82 additions and 13 deletions

View File

@ -12,9 +12,9 @@
#define RGB_PIN 13
#define RGB_ORDER GRB // or RGB
#define RGB_BUTTON_MAP { 5, 4, 3, 2, 1, 0, 7, 6, 8, 9, 10 }
#define RGB_BUTTON_MAP { 5, 4, 3, 2, 1, 0, 7, 6, 8, 9, 10, 11 }
#define BUTTON_DEF { 1, 0, 4, 5, 8, 9, 3, 2, 12, 10, 11}
#define BUTTON_DEF { 1, 0, 4, 5, 8, 9, 3, 2, 12, 10, 11, 13 }
/* HID Keycode: https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h */
// P1: WEDCXZAQ34 P2: (Numpad)89632147*-

View File

@ -16,9 +16,10 @@
#include "config.h"
#include "board_defs.h"
static const uint8_t button_gpio[] = BUTTON_DEF;
static const uint8_t gpio_def[] = BUTTON_DEF;
static uint8_t gpio_real[] = BUTTON_DEF;
#define BUTTON_NUM (sizeof(button_gpio))
#define BUTTON_NUM (sizeof(gpio_def))
static bool sw_val[BUTTON_NUM]; /* true if pressed */
static uint64_t sw_freeze_time[BUTTON_NUM];
@ -29,7 +30,11 @@ void button_init()
{
sw_val[i] = false;
sw_freeze_time[i] = 0;
int8_t gpio = button_gpio[i];
uint8_t gpio = mai_cfg->alt.buttons[i];
if (gpio > 29) {
gpio = gpio_def[i];
}
gpio_real[i] = gpio;
gpio_init(gpio);
gpio_set_function(gpio, GPIO_FUNC_SIO);
gpio_set_dir(gpio, GPIO_IN);
@ -42,6 +47,14 @@ uint8_t button_num()
return BUTTON_NUM;
}
uint8_t button_gpio(int id)
{
if (id >= BUTTON_NUM) {
return 0xff;
}
return gpio_real[id];
}
static uint16_t button_reading;
/* If a switch flips, it freezes for a while */
@ -52,7 +65,7 @@ void button_update()
uint16_t buttons = 0;
for (int i = BUTTON_NUM - 1; i >= 0; i--) {
bool sw_pressed = !gpio_get(button_gpio[i]);
bool sw_pressed = !gpio_real[i];
if (now >= sw_freeze_time[i]) {
if (sw_pressed != sw_val[i]) {

View File

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

View File

@ -61,9 +61,20 @@ static void disp_hid()
mai_cfg->hid.nkro <= 2 ? nkro[mai_cfg->hid.nkro] : "key1");
}
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("\n Test:%d, Service:%d, Navigate:%d, Coin:%d\n",
button_gpio(8), button_gpio(9), button_gpio(10), button_gpio(11));
}
void handle_display(int argc, char *argv[])
{
const char *usage = "Usage: display [rgb|sense|hid]\n";
const char *usage = "Usage: display [rgb|sense|hid|gpio]\n";
if (argc > 1) {
printf(usage);
return;
@ -73,11 +84,12 @@ void handle_display(int argc, char *argv[])
disp_rgb();
disp_sense();
disp_hid();
disp_gpio();
return;
}
const char *choices[] = {"rgb", "sense", "hid"};
switch (cli_match_prefix(choices, 3, argv[0])) {
const char *choices[] = {"rgb", "sense", "hid", "gpio"};
switch (cli_match_prefix(choices, 4, argv[0])) {
case 0:
disp_rgb();
break;
@ -87,6 +99,9 @@ void handle_display(int argc, char *argv[])
case 2:
disp_hid();
break;
case 3:
disp_gpio();
break;
default:
printf(usage);
break;
@ -368,10 +383,43 @@ static void handle_save()
save_request(true);
}
static void handle_joy_reset()
static void handle_gpio(int argc, char *argv[])
{
config_factory_reset();
printf("Factory reset done.\n");
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) {
const char *choices[] = {"main"};
if (cli_match_prefix(choices, 1, argv[0]) < 0) {
printf(usage);
return;
}
uint8_t gpio_main[8];
for (int i = 0; i < 8; i++) {
int gpio = cli_extract_non_neg_int(argv[i + 1], 0);
if (gpio > 29) {
printf(usage);
return;
}
gpio_main[i] = gpio;
}
memcpy(mai_cfg->alt.buttons, gpio_main, 8);
} else if (argc == 2) {
const char *choices[] = {"test", "service", "navigate", "coin"};
const uint8_t button_pos[] = {8, 9, 10, 11};
int match = cli_match_prefix(choices, 4, argv[0]);
uint8_t gpio = cli_extract_non_neg_int(argv[1], 0);
if ((match < 0) || (gpio > 29)) {
printf(usage);
return;
}
mai_cfg->alt.buttons[button_pos[match]] = gpio;
} else {
printf(usage);
return;
}
config_changed();
button_init(); // Re-init the buttons
}
void commands_init()
@ -387,5 +435,6 @@ void commands_init()
cli_register("raw", handle_raw, "Show key raw readings.");
cli_register("whoami", handle_whoami, "Identify each com port.");
cli_register("save", handle_save, "Save config to flash.");
cli_register("gpio", handle_gpio, "Set GPIO pins for buttons.");
cli_register("factory", config_factory_reset, "Reset everything to default.");
}

View File

@ -29,7 +29,10 @@ static mai_cfg_t default_cfg = {
.rgb = {
.per_button = 1,
.per_aux = 1,
}
},
.alt = {
.buttons = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
},
};
mai_runtime_t *mai_runtime;

View File

@ -30,6 +30,9 @@ typedef struct __attribute__((packed)) {
uint8_t per_button;
uint8_t per_aux;
} rgb;
struct {
uint8_t buttons[12];
} alt;
} mai_cfg_t;
typedef struct {