From 813e7ee7122b43915fd8fc0e0b2b5b2263e2df3a Mon Sep 17 00:00:00 2001 From: Philippe G Date: Thu, 19 Aug 2021 19:11:39 -0700 Subject: [PATCH] use polling for gpio 36 & 39 - release --- components/platform_console/cmd_system.c | 9 +--- components/services/buttons.c | 58 +++++++++++++++++++----- components/services/globdefs.h | 1 - components/wifi-manager/wifi_manager.c | 12 ----- 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/components/platform_console/cmd_system.c b/components/platform_console/cmd_system.c index 51443dee..248cae21 100644 --- a/components/platform_console/cmd_system.c +++ b/components/platform_console/cmd_system.c @@ -40,7 +40,6 @@ #endif static struct { struct arg_str *scanmode; - //disable_ps struct arg_lit *disable_power_save; struct arg_end *end; } wifi_parms_arg; static struct { @@ -610,7 +609,6 @@ static int do_configure_wifi(int argc, char **argv){ cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream."); return 1; } - // disable_ps nerrors += enable_disable(f,"disable_ps",wifi_parms_arg.disable_power_save); if(wifi_parms_arg.scanmode->count>0){ if(strcasecmp(wifi_parms_arg.scanmode->sval[0],"Comprehensive") == 0){ @@ -690,11 +688,7 @@ static int do_set_services(int argc, char **argv) cJSON * configure_wifi_cb(){ cJSON * values = cJSON_CreateObject(); char * p=NULL; - // disable_ps - // if ((p = config_alloc_get(NVS_TYPE_STR, "disable_ps")) != NULL) { - // cJSON_AddBoolToObject(values,"disable_power_save",strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0); - // FREE_AND_NULL(p); - // } + if ((p = config_alloc_get(NVS_TYPE_STR, "wifi_smode")) != NULL) { cJSON_AddStringToObject(values,"scanmode",strcasecmp(p,"a") == 0 ?"Comprehensive":"Fast"); FREE_AND_NULL(p); @@ -757,7 +751,6 @@ static void register_set_services(){ static void register_set_wifi_parms(){ wifi_parms_arg.scanmode = arg_str0(NULL, "scanmode", "Fast|Comprehensive","Sets the WiFi Scan Mode. Use Comprehensive where more than one AP has the same name on different channels. This will ensure that the AP with the strongest signal is chosen."); - //disable_ps wifi_parms_arg.disable_power_save = arg_lit0(NULL, "disable_power_save", "Disable Power Saving. This may help if the wifi connection is unstable."); wifi_parms_arg.end=arg_end(2); const esp_console_cmd_t cmd = { .command = CFG_TYPE_SYST("wifi"), diff --git a/components/services/buttons.c b/components/services/buttons.c index 1fb687fa..8289eed5 100644 --- a/components/services/buttons.c +++ b/components/services/buttons.c @@ -25,8 +25,6 @@ #include "rotary_encoder.h" #include "globdefs.h" -bool gpio36_39_used; - static const char * TAG = "buttons"; static int n_buttons = 0; @@ -49,6 +47,13 @@ static EXT_RAM_ATTR struct button_s { TimerHandle_t timer; } buttons[MAX_BUTTONS]; +static struct { + int gpio, level; + struct button_s *button; +} polled_gpio[] = { {36, -1, NULL}, {39, -1, NULL}, {-1, -1, NULL} }; + +static TimerHandle_t polled_timer; + static struct { QueueHandle_t queue; void *client; @@ -117,6 +122,22 @@ static void buttons_timer( TimerHandle_t xTimer ) { } } +/**************************************************************************************** + * Buttons polling timer + */ +static void buttons_polling( TimerHandle_t xTimer ) { + for (int i = 0; polled_gpio[i].gpio != -1; i++) { + if (!polled_gpio[i].button) continue; + + int level = gpio_get_level(polled_gpio[i].gpio); + + if (level != polled_gpio[i].level) { + polled_gpio[i].level = level; + buttons_timer(polled_gpio[i].button->timer); + } + } +} + /**************************************************************************************** * Tasks that calls the appropriate functions when buttons are pressed */ @@ -252,14 +273,29 @@ void button_create(void *client, int gpio, int type, bool pull, int debounce, bu } } - // nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall used which WiFi does when PS is activated - if (gpio == 36 || gpio == 39) gpio36_39_used = true; - // and initialize level ... buttons[n_buttons].level = gpio_get_level(gpio); - - gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]); - gpio_intr_enable(gpio); + + // nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall used which WiFi does when PS is activated + for (int i = 0; polled_gpio[i].gpio != -1; i++) if (polled_gpio[i].gpio == gpio) { + if (!polled_timer) { + polled_timer = xTimerCreate("buttonsPolling", 100 / portTICK_RATE_MS, pdTRUE, polled_gpio, buttons_polling); + xTimerStart(polled_timer, portMAX_DELAY); + } + + polled_gpio[i].button = buttons + n_buttons; + polled_gpio[i].level = gpio_get_level(gpio); + ESP_LOGW(TAG, "creating polled gpio %u, level %u", gpio, polled_gpio[i].level); + + gpio = -1; + break; + } + + // only create timers and ISR is this is not a polled gpio + if (gpio != -1) { + gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]); + gpio_intr_enable(gpio); + } n_buttons++; } @@ -337,7 +373,8 @@ static void rotary_button_handler(void *id, button_event_e event, button_press_e * Create rotary encoder */ bool create_rotary(void *id, int A, int B, int SW, int long_press, rotary_handler handler) { - if (A == -1 || B == -1) { + // nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall used which WiFi does when PS is activated + if (A == -1 || B == -1 || A == 36 || A == 39 || B == 36 || B == 39) { ESP_LOGI(TAG, "Cannot create rotary %d %d", A, B); return false; } @@ -348,9 +385,6 @@ bool create_rotary(void *id, int A, int B, int SW, int long_press, rotary_handle rotary.client = id; rotary.handler = handler; - // nasty ESP32 bug: fire-up constantly INT on GPIO 36/39 if ADC1, AMP, Hall used which WiFi does when PS is activated - if (A == 36 || A == 39 || B == 36 || B == 39 || SW == 36 || SW == 39) gpio36_39_used = true; - // Initialise the rotary encoder device with the GPIOs for A and B signals rotary_encoder_init(&rotary.info, A, B); diff --git a/components/services/globdefs.h b/components/services/globdefs.h index 377d3019..8b5139a5 100644 --- a/components/services/globdefs.h +++ b/components/services/globdefs.h @@ -17,7 +17,6 @@ extern int i2c_system_port; extern int i2c_system_speed; extern int spi_system_host; extern int spi_system_dc_gpio; -extern bool gpio36_39_used; typedef struct { int timer, base_channel, max; } pwm_system_t; diff --git a/components/wifi-manager/wifi_manager.c b/components/wifi-manager/wifi_manager.c index b5bcec6a..033e1405 100644 --- a/components/wifi-manager/wifi_manager.c +++ b/components/wifi-manager/wifi_manager.c @@ -283,18 +283,6 @@ void wifi_manager_init_wifi(){ ESP_LOGD(TAG, "Initializing wifi. Setting WiFi mode to WIFI_MODE_NULL"); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) ); ESP_LOGD(TAG, "Initializing wifi. Starting wifi"); - // char * disable_ps = config_alloc_get_default(NVS_TYPE_STR, "disable_ps", "n", 0); - - // if (gpio36_39_used || (disable_ps && strcasecmp(disable_ps,"y")==0)) { - // if(gpio36_39_used){ - // ESP_LOGW(TAG, "GPIO 36 or 39 are in use, need to disable WiFi PowerSave!"); - // } - // else { - // ESP_LOGW(TAG, "wifi powersave config is disabled. Disabling WiFi PowerSave!"); - // } - // esp_wifi_set_ps(WIFI_PS_NONE); - // } - // FREE_AND_NULL(disable_ps); ESP_ERROR_CHECK( esp_wifi_start() ); taskYIELD();