Skip to content

Commit

Permalink
gpio test, wifi reconnect fix
Browse files Browse the repository at this point in the history
  • Loading branch information
NonPIayerCharacter committed Sep 15, 2024
1 parent 79fd139 commit 4056bd7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
31 changes: 27 additions & 4 deletions src/hal/espidf/hal_pins_espidf.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,16 @@ int GetLedcChannelForPin(gpio_num_t pin)
return -1;
}

int PIN_GetPWMIndexForPinIndex(int pin)
int PIN_GetPWMIndexForPinIndex(int index)
{
if(index >= g_numPins)
return -1;
espPinMapping_t* pin = g_pins + index;
int ch = GetLedcChannelForPin(pin->pin);
if(ch >= 0)
{
return ch;
}
return -1;
}

Expand Down Expand Up @@ -373,6 +381,7 @@ void HAL_PIN_Setup_Input(int index)
return;
espPinMapping_t* pin = g_pins + index;
gpio_set_direction(pin->pin, GPIO_MODE_INPUT);
gpio_set_pull_mode(pin->pin, GPIO_FLOATING);
}

void HAL_PIN_Setup_Output(int index)
Expand All @@ -381,6 +390,7 @@ void HAL_PIN_Setup_Output(int index)
return;
espPinMapping_t* pin = g_pins + index;
gpio_set_direction(pin->pin, GPIO_MODE_OUTPUT);
gpio_set_pull_mode(pin->pin, GPIO_FLOATING);
}

void HAL_PIN_PWM_Stop(int index)
Expand All @@ -399,9 +409,9 @@ void HAL_PIN_PWM_Stop(int index)

void HAL_PIN_PWM_Start(int index)
{
InitLEDC();
if(index >= g_numPins)
return;
InitLEDC();
espPinMapping_t* pin = g_pins + index;
int freecha = GetAvailableLedcChannel();
if(freecha >= 0)
Expand All @@ -424,8 +434,21 @@ void HAL_PIN_PWM_Update(int index, float value)
int ch = GetLedcChannelForPin(pin->pin);
if(ch >= 0)
{
ledc_set_duty(LEDC_LOW_SPEED_MODE, ch, value * 80.92);
ledc_update_duty(LEDC_LOW_SPEED_MODE, ch);
uint32_t curduty = ledc_get_duty(LEDC_LOW_SPEED_MODE, ch);
uint32_t propduty = value * 81.92;
if(propduty != curduty)
{
ledc_set_duty(LEDC_LOW_SPEED_MODE, ch, value * 81.92);
ledc_update_duty(LEDC_LOW_SPEED_MODE, ch);
if(value == 100.0f)
{
ledc_stop(LEDC_LOW_SPEED_MODE, ch, 1);
}
else if(value <= 0.01f)
{
ledc_stop(LEDC_LOW_SPEED_MODE, ch, 0);
}
}
}
}

Expand Down
52 changes: 27 additions & 25 deletions src/hal/espidf/hal_wifi_espidf.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ void event_handler(void* arg, esp_event_base_t event_base,
}
else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
esp_wifi_restore();
if(g_wifiStatusCallback != NULL)
{
g_wifiStatusCallback(WIFI_STA_DISCONNECTED);
Expand All @@ -136,12 +135,15 @@ void event_handler(void* arg, esp_event_base_t event_base,

void HAL_ConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticIP_t* ip)
{
if(sta_netif == NULL)
if(sta_netif != NULL)
{
esp_wifi_stop();
esp_netif_destroy(sta_netif);
esp_wifi_deinit();
delay_ms(10);
}
else
{
sta_netif = esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);

esp_event_handler_instance_t instance_any_id, instance_got_ip;

esp_event_handler_instance_register(WIFI_EVENT,
Expand All @@ -154,29 +156,29 @@ void HAL_ConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticI
&event_handler,
NULL,
&instance_got_ip);
}

wifi_config_t wifi_config;
esp_wifi_get_config(WIFI_IF_STA, &wifi_config);
if(strcmp((char*)wifi_config.sta.ssid, oob_ssid) != 0 || strcmp((char*)wifi_config.sta.password, connect_key) != 0)
{
ADDLOG_ERROR(LOG_FEATURE_MAIN, "WiFi saved ssid/pass != current, resetting");
memset(&wifi_config.sta, 0, sizeof(wifi_sta_config_t));
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
strncpy((char*)wifi_config.sta.ssid, (char*)oob_ssid, 32);
strncpy((char*)wifi_config.sta.password, (char*)connect_key, 64);
}

esp_netif_set_hostname(sta_netif, CFG_GetDeviceName());

esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
esp_wifi_set_mode(WIFI_MODE_STA);
sta_netif = esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);

esp_wifi_start();
}
else
wifi_config_t wifi_config;
esp_wifi_get_config(WIFI_IF_STA, &wifi_config);
if(strcmp((char*)wifi_config.sta.ssid, oob_ssid) != 0 || strcmp((char*)wifi_config.sta.password, connect_key) != 0)
{
esp_wifi_connect();
ADDLOG_ERROR(LOG_FEATURE_MAIN, "WiFi saved ssid/pass != current, resetting");
memset(&wifi_config.sta, 0, sizeof(wifi_sta_config_t));
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
strncpy((char*)wifi_config.sta.ssid, (char*)oob_ssid, 32);
strncpy((char*)wifi_config.sta.password, (char*)connect_key, 64);
}

esp_netif_set_hostname(sta_netif, CFG_GetDeviceName());

esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
esp_wifi_set_mode(WIFI_MODE_STA);

esp_wifi_start();
}

void HAL_DisconnectFromWifi()
Expand Down

0 comments on commit 4056bd7

Please sign in to comment.