Acitve-high button switches as a tweak options

This commit is contained in:
whowechina 2024-06-18 15:55:19 +09:00
parent 1a3306f8cb
commit 2a2969e04c
5 changed files with 103 additions and 37 deletions

Binary file not shown.

View File

@ -42,10 +42,19 @@ void button_init()
}
}
static inline bool button_pressed(int id)
{
bool reading = gpio_get(gpio_real[id]);
bool active_level = id < 8 ? mai_cfg->tweak.main_button_active_high :
mai_cfg->tweak.aux_button_active_high;
return reading == active_level;
}
bool button_is_stuck()
{
for (int i = 0; i < BUTTON_NUM; i++) {
if (!gpio_get(gpio_real[i])) {
if (button_pressed(i)) {
return true;
}
}
@ -83,7 +92,7 @@ void button_update()
uint16_t buttons = 0;
for (int i = BUTTON_NUM - 1; i >= 0; i--) {
bool sw_pressed = !gpio_get(gpio_real[i]);
bool sw_pressed = button_pressed(i);
if (now >= sw_freeze_time[i]) {
if (sw_pressed != sw_val[i]) {

View File

@ -61,7 +61,7 @@ static void disp_hid()
{
printf("[HID]\n");
const char *nkro[] = {"off", "key1", "key2"};
printf(" Joy: %s, NKRO: %s\n", mai_cfg->hid.joy ? "on" : "off",
printf(" Joy: %s, NKRO: %s\n", mai_cfg->hid.joy ? "ON" : "OFF",
mai_cfg->hid.nkro <= 2 ? nkro[mai_cfg->hid.nkro] : "key1");
if (mai_runtime.key_stuck) {
printf(" !!! Button stuck, force JOY only !!!\n");
@ -71,9 +71,9 @@ static void disp_hid()
static void disp_aime()
{
printf("[AIME]\n");
printf(" NFC Module: %s\n", nfc_module_name());
printf(" Virtual AIC: %s\n", mai_cfg->aime.virtual_aic ? "ON" : "OFF");
printf(" Mode: %d\n", mai_cfg->aime.mode);
printf(" NFC Module: %s\n", nfc_module_name());
printf(" Virtual AIC: %s\n", mai_cfg->aime.virtual_aic ? "ON" : "OFF");
printf(" Protocol Mode: %d\n", mai_cfg->aime.mode);
}
static void disp_gpio()
@ -102,48 +102,57 @@ static void disp_touch()
}
}
static void disp_tweak()
{
printf("[Tweak]\n");
printf(" Main Buttons Active-High: %s\n",
mai_cfg->tweak.main_button_active_high ? "ON" : "OFF");
printf(" Aux Buttons Active-High: %s\n",
mai_cfg->tweak.aux_button_active_high ? "ON" : "OFF");
}
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))
void handle_display(int argc, char *argv[])
{
const char *usage = "Usage: display [rgb|sense|hid|gpio|aime]\n";
const char *usage = "Usage: display [rgb|sense|hid|gpio|touch|aime|tweak]\n";
if (argc > 1) {
printf(usage);
return;
}
const char *choices[] = {"rgb", "sense", "hid", "gpio", "touch", "aime", "tweak"};
static void (*disp_funcs[])() = {
disp_rgb,
disp_sense,
disp_hid,
disp_gpio,
disp_touch,
disp_aime,
disp_tweak,
};
static_assert(ARRAYSIZE(choices) == ARRAYSIZE(disp_funcs),
"Choices and disp_funcs arrays must have the same number of elements");
if (argc == 0) {
disp_rgb();
disp_sense();
disp_hid();
disp_gpio();
disp_touch();
disp_aime();
for (int i = 0; i < ARRAYSIZE(disp_funcs); i++) {
disp_funcs[i]();
}
return;
}
const char *choices[] = {"rgb", "sense", "hid", "gpio", "touch", "aime"};
switch (cli_match_prefix(choices, 6, argv[0])) {
case 0:
disp_rgb();
break;
case 1:
disp_sense();
break;
case 2:
disp_hid();
break;
case 3:
disp_gpio();
break;
case 4:
disp_touch();
break;
case 5:
disp_aime();
break;
default:
printf(usage);
break;
int choice = cli_match_prefix(choices, ARRAYSIZE(choices), argv[0]);
if (choice < 0) {
printf(usage);
return;
}
if (choice > ARRAYSIZE(disp_funcs)) {
return;
}
disp_funcs[choice]();
}
static void handle_rgb(int argc, char *argv[])
@ -593,6 +602,42 @@ static void handle_aime(int argc, char *argv[])
}
}
static void handle_tweak(int argc, char *argv[])
{
const char *usage = "Usage: tweak <option> <on|off>\n"
"Options:\n"
" main_button_active_high\n"
" aux_button_active_high\n";
if (argc != 2) {
printf(usage);
return;
}
const char *options[] = {
"main_button_active_high",
"aux_button_active_high",
};
const char *switches[] = { "on", "off" };
int option = cli_match_prefix(options, 2, argv[0]);
int on_off = cli_match_prefix(switches, 2, argv[1]);
if ((option < 0) || (on_off < 0)) {
printf(usage);
return;
}
bool active = on_off == 0 ? true : false;
if (option == 0) {
mai_cfg->tweak.main_button_active_high = active;
} else if (option == 1) {
mai_cfg->tweak.aux_button_active_high = active;
}
config_changed();
disp_tweak();
}
void commands_init()
{
cli_register("display", handle_display, "Display all config.");
@ -608,6 +653,7 @@ void commands_init()
cli_register("save", handle_save, "Save config to flash.");
cli_register("gpio", handle_gpio, "Set GPIO pins for buttons.");
cli_register("touch", handle_touch, "Custimze touch mapping.");
cli_register("tweak", handle_tweak, "Miscellaneous tweak options.");
cli_register("factory", config_factory_reset, "Reset everything to default.");
cli_register("aime", handle_aime, "AIME settings.");
}

View File

@ -40,7 +40,11 @@ static mai_cfg_t default_cfg = {
.aime = {
.mode = 0,
.virtual_aic = 0,
}
},
.tweak = {
.main_button_active_high = 0,
.aux_button_active_high = 0,
},
};
mai_runtime_t mai_runtime;

View File

@ -40,6 +40,13 @@ typedef struct __attribute__((packed)) {
uint8_t mode : 4;
uint8_t virtual_aic : 4;
} aime;
struct {
uint8_t main_button_active_high : 1;
uint8_t aux_button_active_high : 1;
uint8_t unused_bits : 6;
uint8_t reserved[3];
} tweak;
uint8_t reserved[8];
} mai_cfg_t;
typedef struct {