button tweaks

This commit is contained in:
philippe44
2020-01-12 20:49:58 -08:00
parent b5dc9ec47a
commit b7f391d909
2 changed files with 23 additions and 13 deletions

View File

@@ -42,10 +42,11 @@ static int n_buttons = 0;
static EXT_RAM_ATTR struct button_s { static EXT_RAM_ATTR struct button_s {
void *id; void *id;
int gpio, index; int gpio;
int debounce; int debounce;
button_handler handler; button_handler handler;
struct button_s *shifter; struct button_s *self, *shifter;
int shifter_gpio; // this one is just for post-creation
int long_press; int long_press;
bool long_timer, shifted, shifting; bool long_timer, shifted, shifting;
int type, level; int type, level;
@@ -126,7 +127,7 @@ static void buttons_task(void* arg) {
(*button.handler)(button.id, BUTTON_RELEASED, press, false); (*button.handler)(button.id, BUTTON_RELEASED, press, false);
} }
// button is a copy, so need to go to real context // button is a copy, so need to go to real context
buttons[button.index].shifting = false; button.self->shifting = false;
} else if (!button.shifting) { } else if (!button.shifting) {
// normal long press and not shifting so don't discard // normal long press and not shifting so don't discard
(*button.handler)(button.id, BUTTON_PRESSED, press, true); (*button.handler)(button.id, BUTTON_PRESSED, press, true);
@@ -135,7 +136,7 @@ static void buttons_task(void* arg) {
// normal press/release of a button or release of a long-press button // normal press/release of a button or release of a long-press button
if (!button.shifting) (*button.handler)(button.id, event, press, button.long_press); if (!button.shifting) (*button.handler)(button.id, event, press, button.long_press);
// button is a copy, so need to go to real context // button is a copy, so need to go to real context
buttons[button.index].shifting = false; button.self->shifting = false;
} }
} }
} }
@@ -173,17 +174,22 @@ void button_create(void *id, int gpio, int type, bool pull, int debounce, button
buttons[n_buttons].handler = handler; buttons[n_buttons].handler = handler;
buttons[n_buttons].long_press = long_press; buttons[n_buttons].long_press = long_press;
buttons[n_buttons].level = -1; buttons[n_buttons].level = -1;
buttons[n_buttons].shifter_gpio = shifter_gpio;
buttons[n_buttons].type = type; buttons[n_buttons].type = type;
buttons[n_buttons].timer = xTimerCreate("buttonTimer", buttons[n_buttons].debounce / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer); buttons[n_buttons].timer = xTimerCreate("buttonTimer", buttons[n_buttons].debounce / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer);
// little trick to find ourselves from queued copy buttons[n_buttons].self = buttons + n_buttons;
buttons[n_buttons].index = n_buttons;
for (int i = 0; i < n_buttons; i++) { for (int i = 0; i < n_buttons; i++) {
// first try to find our shifter
if (buttons[i].gpio == shifter_gpio) { if (buttons[i].gpio == shifter_gpio) {
buttons[n_buttons].shifter = buttons + i; buttons[n_buttons].shifter = buttons + i;
// a shifter must have a long-press handler // a shifter must have a long-press handler
if (!buttons[i].long_press) buttons[i].long_press = -1; if (!buttons[i].long_press) buttons[i].long_press = -1;
break; }
// then try to see if we are a non-assigned shifter
if (buttons[i].shifter_gpio == gpio) {
buttons[i].shifter = buttons + n_buttons;
ESP_LOGI(TAG, "post-assigned shifter gpio %u", buttons[i].gpio);
} }
} }
@@ -195,8 +201,12 @@ void button_create(void *id, int gpio, int type, bool pull, int debounce, button
// do we need pullup or pulldown // do we need pullup or pulldown
if (pull) { if (pull) {
if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY); if (GPIO_IS_VALID_OUTPUT_GPIO(gpio)) {
else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY); if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
} else {
ESP_LOGW(TAG, "cannot set pull up/down for gpio %u", gpio);
}
} }
gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]); gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);

View File

@@ -21,7 +21,7 @@
#ifdef CONFIG_SQUEEZEAMP #ifdef CONFIG_SQUEEZEAMP
#define JACK_GPIO 34 #define JACK_GPIO 34
#define SPKFAULT_GPIO 2 #define SPKFAULT_GPIO 2 // this requires a pull-up, so can't be >34
#endif #endif
#define MONITOR_TIMER (10*1000) #define MONITOR_TIMER (10*1000)
@@ -104,8 +104,8 @@ void monitor_svc_init(void) {
#ifdef SPKFAULT_GPIO #ifdef SPKFAULT_GPIO
gpio_pad_select_gpio(SPKFAULT_GPIO); gpio_pad_select_gpio(SPKFAULT_GPIO);
ESP_LOGI(TAG, "DIR %d", gpio_set_direction(SPKFAULT_GPIO, GPIO_MODE_INPUT)); gpio_set_direction(SPKFAULT_GPIO, GPIO_MODE_INPUT);
ESP_LOGI(TAG, "PULLUP %d", gpio_set_pull_mode(SPKFAULT_GPIO, GPIO_PULLUP_ONLY)); gpio_set_pull_mode(SPKFAULT_GPIO, GPIO_PULLUP_ONLY);
// re-use button management for speaker fault handler, it's a GPIO after all // re-use button management for speaker fault handler, it's a GPIO after all
button_create(NULL, SPKFAULT_GPIO, BUTTON_LOW, true, 0, spkfault_handler_default, 0, -1); button_create(NULL, SPKFAULT_GPIO, BUTTON_LOW, true, 0, spkfault_handler_default, 0, -1);