Skip to content

Commit

Permalink
use polling for gpio 36 & 39 - release
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe44 committed Aug 20, 2021
1 parent ac9ad2e commit 813e7ee
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
9 changes: 1 addition & 8 deletions components/platform_console/cmd_system.c
Expand Up @@ -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 {
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"),
Expand Down
58 changes: 46 additions & 12 deletions components/services/buttons.c
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down
1 change: 0 additions & 1 deletion components/services/globdefs.h
Expand Up @@ -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;
Expand Down
12 changes: 0 additions & 12 deletions components/wifi-manager/wifi_manager.c
Expand Up @@ -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();
Expand Down

0 comments on commit 813e7ee

Please sign in to comment.