improved task synchronization
This commit is contained in:
@@ -118,8 +118,13 @@ static void exit_update_setup_event_cb( lv_obj_t * obj, lv_event_t event ) {
|
||||
|
||||
static void update_event_handler(lv_obj_t * obj, lv_event_t event) {
|
||||
if(event == LV_EVENT_CLICKED) {
|
||||
xEventGroupSetBits( update_event_handle, UPDATE_REQUEST );
|
||||
vTaskResume( _update_Task );
|
||||
if ( xEventGroupGetBits( update_event_handle) & UPDATE_REQUEST ) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
xEventGroupSetBits( update_event_handle, UPDATE_REQUEST );
|
||||
vTaskResume( _update_Task );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +138,6 @@ void update_Task( void * pvParameters ) {
|
||||
display_set_timeout( DISPLAY_MAX_TIMEOUT );
|
||||
|
||||
WiFiClient client;
|
||||
client.setTimeout( 10 );
|
||||
|
||||
lv_label_set_text( update_status_label, "start update ..." );
|
||||
lv_obj_align( update_status_label, update_btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 15 );
|
||||
@@ -164,6 +168,7 @@ void update_Task( void * pvParameters ) {
|
||||
}
|
||||
xEventGroupClearBits( update_event_handle, UPDATE_REQUEST );
|
||||
}
|
||||
lv_disp_trig_activity(NULL);
|
||||
vTaskSuspend( _update_Task );
|
||||
}
|
||||
}
|
||||
@@ -224,7 +224,6 @@ static void wifi_onoff_event_handler(lv_obj_t * obj, lv_event_t event) {
|
||||
|
||||
void wifi_settings_enter_pass_event_cb( lv_obj_t * obj, lv_event_t event ) {
|
||||
if(event == LV_EVENT_CLICKED) {
|
||||
// strcpy( ssid, lv_list_get_btn_text(obj) );
|
||||
lv_label_set_text( wlan_password_name_label, lv_list_get_btn_text(obj) );
|
||||
lv_textarea_set_text( wlan_password_pass_textfield, "");
|
||||
mainbar_jump_to_tilenumber( WLAN_PASSWORD_TILE, LV_ANIM_ON );
|
||||
|
||||
@@ -91,7 +91,7 @@ void weather_widget_setup( void ) {
|
||||
xTaskCreate(
|
||||
weather_widget_sync_Task, /* Function to implement the task */
|
||||
"weather sync Task", /* Name of the task */
|
||||
2000, /* Stack size in words */
|
||||
5000, /* Stack size in words */
|
||||
NULL, /* Task input parameter */
|
||||
1, /* Priority of the task */
|
||||
&_weather_widget_sync_Task ); /* Task handle. */
|
||||
@@ -114,8 +114,13 @@ void weather_jump_to_setup( void ) {
|
||||
}
|
||||
|
||||
void weather_widget_sync_request( void ) {
|
||||
xEventGroupSetBits( weather_widget_event_handle, WEATHER_WIDGET_SYNC_REQUEST );
|
||||
vTaskResume( _weather_widget_sync_Task );
|
||||
if ( xEventGroupGetBits( weather_widget_event_handle ) & WEATHER_WIDGET_SYNC_REQUEST ) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
xEventGroupSetBits( weather_widget_event_handle, WEATHER_WIDGET_SYNC_REQUEST );
|
||||
vTaskResume( _weather_widget_sync_Task );
|
||||
}
|
||||
}
|
||||
|
||||
weather_config_t *weather_get_config( void ) {
|
||||
@@ -123,20 +128,22 @@ weather_config_t *weather_get_config( void ) {
|
||||
}
|
||||
|
||||
void weather_widget_sync_Task( void * pvParameters ) {
|
||||
uint32_t retval = -1;
|
||||
|
||||
while( true ) {
|
||||
vTaskDelay( 500 );
|
||||
if ( xEventGroupGetBits( weather_widget_event_handle ) & WEATHER_WIDGET_SYNC_REQUEST ) {
|
||||
if ( weather_config.autosync ) {
|
||||
weather_fetch_today( &weather_config, &weather_today );
|
||||
if ( weather_today.valide ) {
|
||||
uint32_t retval = weather_fetch_today( &weather_config, &weather_today );
|
||||
if ( retval == 200 ) {
|
||||
lv_label_set_text( weather_widget_temperature_label, weather_today.temp );
|
||||
lv_imgbtn_set_src( weather_widget_condition_img, LV_BTN_STATE_RELEASED, resolve_owm_icon( weather_today.icon ) );
|
||||
lv_imgbtn_set_src( weather_widget_condition_img, LV_BTN_STATE_PRESSED, resolve_owm_icon( weather_today.icon ) );
|
||||
lv_imgbtn_set_src( weather_widget_condition_img, LV_BTN_STATE_CHECKED_RELEASED, resolve_owm_icon( weather_today.icon ) );
|
||||
lv_imgbtn_set_src( weather_widget_condition_img, LV_BTN_STATE_CHECKED_PRESSED, resolve_owm_icon( weather_today.icon ) );
|
||||
lv_obj_align( weather_widget_temperature_label, weather_widget_cont, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
|
||||
}
|
||||
motor_vibe( 1 );
|
||||
}
|
||||
}
|
||||
xEventGroupClearBits( weather_widget_event_handle, WEATHER_WIDGET_SYNC_REQUEST );
|
||||
}
|
||||
|
||||
@@ -29,15 +29,14 @@
|
||||
#include "weather_fetch.h"
|
||||
#include "weather_forecast.h"
|
||||
|
||||
void weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *weather_today ) {
|
||||
uint32_t weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *weather_today ) {
|
||||
|
||||
WiFiClient today_client;
|
||||
|
||||
weather_today->valide = false;
|
||||
uint32_t retval = -1;
|
||||
|
||||
if ( !today_client.connect( OWM_HOST, OWM_PORT ) ) {
|
||||
Serial.println("Connection failed");
|
||||
return;
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
today_client.printf( "GET /data/2.5/weather?lat=%s&lon=%s&appid=%s HTTP/1.1\r\n"
|
||||
@@ -50,10 +49,9 @@ void weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *w
|
||||
|
||||
uint64_t startMillis = millis();
|
||||
while ( today_client.available() == 0 ) {
|
||||
yield();
|
||||
if ( millis() - startMillis > 5000 ) {
|
||||
today_client.stop();
|
||||
return;
|
||||
return( retval );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +60,6 @@ void weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *w
|
||||
|
||||
bool data_begin = false;
|
||||
while( today_client.available() ) {
|
||||
yield();
|
||||
if ( data_begin ) {
|
||||
*ptr = today_client.read();
|
||||
ptr++;
|
||||
@@ -75,10 +72,9 @@ void weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *w
|
||||
}
|
||||
*ptr = '\0';
|
||||
if ( data_begin == false ) {
|
||||
return;
|
||||
return( retval );
|
||||
}
|
||||
|
||||
yield();
|
||||
today_client.stop();
|
||||
|
||||
DynamicJsonDocument doc(20000);
|
||||
@@ -89,10 +85,17 @@ void weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *w
|
||||
Serial.println(error.c_str());
|
||||
doc.clear();
|
||||
free( json );
|
||||
return;
|
||||
return( retval );
|
||||
}
|
||||
|
||||
retval = doc["cod"].as<int>();
|
||||
|
||||
if ( retval != 200 ) {
|
||||
doc.clear();
|
||||
free( json );
|
||||
return( retval );
|
||||
}
|
||||
|
||||
yield();
|
||||
weather_today->valide = true;
|
||||
snprintf( weather_today->temp, sizeof( weather_today->temp ),"%0.1f°C", doc["main"]["temp"].as<float>() - 273.15 );
|
||||
snprintf( weather_today->humidity, sizeof( weather_today->humidity ),"%f%%", doc["main"]["humidity"].as<float>() );
|
||||
@@ -102,17 +105,19 @@ void weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *w
|
||||
|
||||
doc.clear();
|
||||
free( json );
|
||||
return( retval );
|
||||
}
|
||||
|
||||
void weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t * weather_forecast ) {
|
||||
uint32_t weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t * weather_forecast ) {
|
||||
|
||||
WiFiClient forecast_client;
|
||||
uint32_t retval = -1;
|
||||
|
||||
weather_forecast[ 0 ].valide = false;
|
||||
|
||||
if ( !forecast_client.connect( OWM_HOST, OWM_PORT ) ) {
|
||||
Serial.println("Connection failed");
|
||||
return;
|
||||
return( retval );
|
||||
}
|
||||
|
||||
forecast_client.printf( "GET /data/2.5/forecast?cnt=%d&lat=%s&lon=%s&appid=%s HTTP/1.1\r\n"
|
||||
@@ -125,10 +130,9 @@ void weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t
|
||||
|
||||
uint64_t startMillis = millis();
|
||||
while ( forecast_client.available() == 0 ) {
|
||||
yield();
|
||||
if ( millis() - startMillis > 5000 ) {
|
||||
forecast_client.stop();
|
||||
return;
|
||||
return( retval );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,9 +157,8 @@ void weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t
|
||||
if ( data_begin == false ) {
|
||||
Serial.printf("No json data\r\n");
|
||||
free( json );
|
||||
return;
|
||||
return( retval );
|
||||
}
|
||||
yield();
|
||||
|
||||
DynamicJsonDocument doc(20000);
|
||||
DeserializationError error = deserializeJson( doc, json );
|
||||
@@ -164,10 +167,17 @@ void weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t
|
||||
Serial.println(error.c_str());
|
||||
doc.clear();
|
||||
free( json );
|
||||
return;
|
||||
return( retval );
|
||||
}
|
||||
|
||||
retval = doc["cod"].as<int>();
|
||||
|
||||
if ( retval != 200 ) {
|
||||
doc.clear();
|
||||
free( json );
|
||||
return( retval );
|
||||
}
|
||||
|
||||
yield();
|
||||
weather_forecast[0].valide = true;
|
||||
for ( int i = 0 ; i < WEATHER_MAX_FORECAST ; i++ ) {
|
||||
snprintf( weather_forecast[ i ].temp, sizeof( weather_forecast[ i ].temp ),"%0.1f°C", doc["list"][i]["main"]["temp"].as<float>() - 273.15 );
|
||||
@@ -179,4 +189,5 @@ void weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t
|
||||
|
||||
doc.clear();
|
||||
free( json );
|
||||
return( 200 );
|
||||
}
|
||||
@@ -25,7 +25,7 @@
|
||||
#define OWM_HOST "api.openweathermap.org"
|
||||
#define OWM_PORT 80
|
||||
|
||||
void weather_fetch_today( weather_config_t * weather_config, weather_forcast_t * weather_today );
|
||||
void weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t * weather_forecast );
|
||||
uint32_t weather_fetch_today( weather_config_t * weather_config, weather_forcast_t * weather_today );
|
||||
uint32_t weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t * weather_forecast );
|
||||
|
||||
#endif // _WEATHER_FETCH_H
|
||||
@@ -126,7 +126,7 @@ void weather_widget_tile_setup( lv_obj_t *tile, lv_style_t *style, lv_coord_t hr
|
||||
xTaskCreate(
|
||||
weather_forecast_sync_Task, /* Function to implement the task */
|
||||
"weather sync Task", /* Name of the task */
|
||||
10000, /* Stack size in words */
|
||||
5000, /* Stack size in words */
|
||||
NULL, /* Task input parameter */
|
||||
1, /* Priority of the task */
|
||||
&_weather_forecast_sync_Task ); /* Task handle. */
|
||||
@@ -164,21 +164,20 @@ void weather_forecast_sync_request( void ) {
|
||||
}
|
||||
else {
|
||||
xEventGroupSetBits( weather_forecast_event_handle, WEATHER_FORECAST_SYNC_REQUEST );
|
||||
vTaskResume( _weather_forecast_sync_Task );
|
||||
}
|
||||
vTaskResume( _weather_forecast_sync_Task );
|
||||
}
|
||||
|
||||
void weather_forecast_sync_Task( void * pvParameters ) {
|
||||
weather_config_t *weather_config = weather_get_config();
|
||||
uint32_t retval = -1;
|
||||
|
||||
while( true ) {
|
||||
vTaskDelay( 500 );
|
||||
if ( xEventGroupGetBits( weather_forecast_event_handle ) & WEATHER_FORECAST_SYNC_REQUEST ) {
|
||||
if ( weather_config->autosync ) {
|
||||
weather_fetch_forecast( weather_get_config() , &weather_forecast[ 0 ] );
|
||||
if ( !weather_forecast[ 0 ].valide )
|
||||
weather_fetch_forecast( weather_get_config() , &weather_forecast[ 0 ] );
|
||||
if ( weather_forecast[ 0 ].valide ) {
|
||||
retval = weather_fetch_forecast( weather_get_config() , &weather_forecast[ 0 ] );
|
||||
if ( retval == 200 ) {
|
||||
for( int i = 0 ; i < WEATHER_MAX_FORECAST / 4 ; i++ ) {
|
||||
lv_label_set_text( weather_forecast_location_label, weather_forecast[ i * 4 ].name );
|
||||
lv_label_set_text( weather_forecast_temperature_label[ i ], weather_forecast[ i * 4 ].temp );
|
||||
@@ -196,7 +195,13 @@ void weather_forecast_sync_Task( void * pvParameters ) {
|
||||
strftime( buf, sizeof(buf), "updated: %d.%b %H:%M", &info );
|
||||
lv_label_set_text( weather_forecast_update_label, buf );
|
||||
}
|
||||
}
|
||||
motor_vibe( 1 );
|
||||
}
|
||||
else {
|
||||
char buf[64];
|
||||
snprintf( buf, sizeof(buf), "Error: %d", retval );
|
||||
lv_label_set_text( weather_forecast_update_label, buf );
|
||||
}
|
||||
}
|
||||
xEventGroupClearBits( weather_forecast_event_handle, WEATHER_FORECAST_SYNC_REQUEST );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user