Merge pull request #59 from joshvito/feature/add_imperial_units_selector

adds a toggle to choose temperature units;
This commit is contained in:
Dirk Broßwick
2020-08-21 09:58:13 +02:00
committed by GitHub
4 changed files with 45 additions and 7 deletions

View File

@@ -236,6 +236,7 @@ void weather_save_config( void ) {
doc["lon"] = weather_config.lon; doc["lon"] = weather_config.lon;
doc["autosync"] = weather_config.autosync; doc["autosync"] = weather_config.autosync;
doc["showWind"] = weather_config.showWind; doc["showWind"] = weather_config.showWind;
doc["imperial"] = weather_config.imperial;
if ( serializeJsonPretty( doc, file ) == 0) { if ( serializeJsonPretty( doc, file ) == 0) {
log_e("Failed to write config file"); log_e("Failed to write config file");
@@ -268,6 +269,7 @@ void weather_load_config( void ) {
strlcpy( weather_config.lon, doc["lon"], sizeof( weather_config.lon ) ); strlcpy( weather_config.lon, doc["lon"], sizeof( weather_config.lon ) );
weather_config.autosync = doc["autosync"].as<bool>(); weather_config.autosync = doc["autosync"].as<bool>();
weather_config.showWind = doc["showWind"].as<bool>(); weather_config.showWind = doc["showWind"].as<bool>();
weather_config.imperial = doc["imperial"].as<bool>();
} }
doc.clear(); doc.clear();
} }

View File

@@ -36,6 +36,7 @@
char lat[16] = ""; char lat[16] = "";
bool autosync = true; bool autosync = true;
bool showWind = false; bool showWind = false;
bool imperial = false;
} weather_config_t; } weather_config_t;
typedef struct { typedef struct {

View File

@@ -35,8 +35,10 @@ static void weather_wind_to_string( weather_forcast_t* container, int speed, int
int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *weather_today ) { int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *weather_today ) {
char url[512]=""; char url[512]="";
int httpcode = -1; int httpcode = -1;
const char* weather_units_symbol = weather_config->imperial ? "F" : "C";
snprintf( url, sizeof( url ), "http://%s/data/2.5/weather?lat=%s&lon=%s&appid=%s", OWM_HOST, weather_config->lat, weather_config->lon, weather_config->apikey); const char* weather_units_char = weather_config->imperial ? "imperial" : "metric";
snprintf( url, sizeof( url ), "http://%s/data/2.5/weather?lat=%s&lon=%s&appid=%s&units=%s", OWM_HOST, weather_config->lat, weather_config->lon, weather_config->apikey, weather_units_char);
HTTPClient today_client; HTTPClient today_client;
@@ -45,7 +47,7 @@ int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *we
httpcode = today_client.GET(); httpcode = today_client.GET();
if ( httpcode != 200 ) { if ( httpcode != 200 ) {
log_e("HTTPClient error %d", httpcode ); log_e("HTTPClient error %d", httpcode, url );
today_client.end(); today_client.end();
return( -1 ); return( -1 );
} }
@@ -63,7 +65,7 @@ int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *we
today_client.end(); today_client.end();
weather_today->valide = true; 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->temp, sizeof( weather_today->temp ), "%0.1f°%s", doc["main"]["temp"].as<float>(), weather_units_symbol);
snprintf( weather_today->humidity, sizeof( weather_today->humidity ),"%f%%", doc["main"]["humidity"].as<float>() ); snprintf( weather_today->humidity, sizeof( weather_today->humidity ),"%f%%", doc["main"]["humidity"].as<float>() );
snprintf( weather_today->pressure, sizeof( weather_today->pressure ),"%fpha", doc["main"]["pressure"].as<float>() ); snprintf( weather_today->pressure, sizeof( weather_today->pressure ),"%fpha", doc["main"]["pressure"].as<float>() );
strcpy( weather_today->icon, doc["weather"][0]["icon"] ); strcpy( weather_today->icon, doc["weather"][0]["icon"] );
@@ -80,8 +82,10 @@ int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *we
int weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t * weather_forecast ) { int weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t * weather_forecast ) {
char url[512]=""; char url[512]="";
int httpcode = -1; int httpcode = -1;
const char* weather_units_symbol = weather_config->imperial ? "F" : "C";
const char* weather_units_char = weather_config->imperial ? "imperial" : "metric";
snprintf( url, sizeof( url ), "http://%s/data/2.5/forecast?cnt=%d&lat=%s&lon=%s&appid=%s", OWM_HOST, WEATHER_MAX_FORECAST, weather_config->lat, weather_config->lon, weather_config->apikey); snprintf( url, sizeof( url ), "http://%s/data/2.5/forecast?cnt=%d&lat=%s&lon=%s&appid=%s&units=%s", OWM_HOST, WEATHER_MAX_FORECAST, weather_config->lat, weather_config->lon, weather_config->apikey, weather_units_char);
HTTPClient forecast_client; HTTPClient forecast_client;
@@ -90,7 +94,7 @@ int weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t
httpcode = forecast_client.GET(); httpcode = forecast_client.GET();
if ( httpcode != 200 ) { if ( httpcode != 200 ) {
log_e("HTTPClient error %d", httpcode ); log_e("HTTPClient error %d", httpcode, url );
forecast_client.end(); forecast_client.end();
return( -1 ); return( -1 );
} }
@@ -110,7 +114,7 @@ int weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t
weather_forecast[0].valide = true; weather_forecast[0].valide = true;
for ( int i = 0 ; i < WEATHER_MAX_FORECAST ; i++ ) { for ( int i = 0 ; i < WEATHER_MAX_FORECAST ; i++ ) {
weather_forecast[ i ].timestamp = doc["list"][i]["dt"].as<long>(); weather_forecast[ i ].timestamp = doc["list"][i]["dt"].as<long>();
snprintf( weather_forecast[ i ].temp, sizeof( weather_forecast[ i ].temp ),"%0.1f°C", doc["list"][i]["main"]["temp"].as<float>() - 273.15 ); snprintf( weather_forecast[ i ].temp, sizeof( weather_forecast[ i ].temp ),"%0.1f°%s", doc["list"][i]["main"]["temp"].as<float>(), weather_units_symbol );
snprintf( weather_forecast[ i ].humidity, sizeof( weather_forecast[ i ].humidity ),"%f%%", doc["list"][i]["main"]["humidity"].as<float>() ); snprintf( weather_forecast[ i ].humidity, sizeof( weather_forecast[ i ].humidity ),"%f%%", doc["list"][i]["main"]["humidity"].as<float>() );
snprintf( weather_forecast[ i ].pressure, sizeof( weather_forecast[ i ].pressure ),"%fpha", doc["list"][i]["main"]["pressure"].as<float>() ); snprintf( weather_forecast[ i ].pressure, sizeof( weather_forecast[ i ].pressure ),"%fpha", doc["list"][i]["main"]["pressure"].as<float>() );
strcpy( weather_forecast[ i ].icon, doc["list"][i]["weather"][0]["icon"] ); strcpy( weather_forecast[ i ].icon, doc["list"][i]["weather"][0]["icon"] );

View File

@@ -44,6 +44,7 @@ lv_obj_t *weather_lat_textfield = NULL;
lv_obj_t *weather_lon_textfield = NULL; lv_obj_t *weather_lon_textfield = NULL;
lv_obj_t *weather_autosync_onoff = NULL; lv_obj_t *weather_autosync_onoff = NULL;
lv_obj_t *weather_wind_onoff = NULL; lv_obj_t *weather_wind_onoff = NULL;
lv_obj_t *weather_imperial_onoff = NULL;
lv_style_t weather_widget_setup_style; lv_style_t weather_widget_setup_style;
LV_IMG_DECLARE(exit_32px); LV_IMG_DECLARE(exit_32px);
@@ -52,6 +53,7 @@ static void weather_textarea_event_cb( lv_obj_t * obj, lv_event_t event );
static void exit_weather_widget_setup_event_cb( lv_obj_t * obj, lv_event_t event ); static void exit_weather_widget_setup_event_cb( lv_obj_t * obj, lv_event_t event );
static void weather_autosync_onoff_event_handler( lv_obj_t * obj, lv_event_t event ); static void weather_autosync_onoff_event_handler( lv_obj_t * obj, lv_event_t event );
static void weather_wind_onoff_event_handler( lv_obj_t *obj, lv_event_t event ); static void weather_wind_onoff_event_handler( lv_obj_t *obj, lv_event_t event );
static void weather_imperial_onoff_event_handler( lv_obj_t *obj, lv_event_t event );
static void bluetooth_message_event_cb( EventBits_t event, char* msg ); static void bluetooth_message_event_cb( EventBits_t event, char* msg );
static void bluetooth_message_msg_pharse( char* msg ); static void bluetooth_message_msg_pharse( char* msg );
@@ -179,6 +181,21 @@ void weather_setup_tile_setup( uint32_t tile_num ) {
lv_label_set_text( weather_wind_label, "Display wind"); lv_label_set_text( weather_wind_label, "Display wind");
lv_obj_align( weather_wind_label, weather_wind_cont, LV_ALIGN_IN_LEFT_MID, 5, 0); lv_obj_align( weather_wind_label, weather_wind_cont, LV_ALIGN_IN_LEFT_MID, 5, 0);
lv_obj_t *weather_imperial_cont = lv_obj_create( weather_setup_tile, NULL);
lv_obj_set_size( weather_imperial_cont, LV_HOR_RES_MAX, 32);
lv_obj_add_style( weather_imperial_cont, LV_OBJ_PART_MAIN, &weather_setup_style );
lv_obj_align( weather_imperial_cont, weather_autosync_cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 0 );
weather_imperial_onoff = lv_switch_create( weather_imperial_cont, NULL);
lv_obj_add_protect( weather_imperial_onoff, LV_PROTECT_CLICK_FOCUS);
lv_obj_add_style( weather_imperial_onoff, LV_SWITCH_PART_INDIC, mainbar_get_switch_style() );
lv_switch_off( weather_imperial_onoff, LV_ANIM_ON);
lv_obj_align( weather_imperial_onoff, weather_imperial_cont, LV_ALIGN_IN_RIGHT_MID, -5, 0);
lv_obj_set_event_cb( weather_imperial_onoff, weather_imperial_onoff_event_handler);
lv_obj_t *weather_imperial_label = lv_label_create(weather_imperial_cont, NULL);
lv_obj_add_style( weather_imperial_label, LV_OBJ_PART_MAIN, &weather_setup_style );
lv_label_set_text( weather_imperial_label, "Use Imperial");
lv_obj_align( weather_imperial_label, weather_imperial_cont, LV_ALIGN_IN_LEFT_MID, 5, 0);
if ( weather_config->autosync) if ( weather_config->autosync)
lv_switch_on(weather_autosync_onoff, LV_ANIM_OFF); lv_switch_on(weather_autosync_onoff, LV_ANIM_OFF);
else else
@@ -189,6 +206,11 @@ void weather_setup_tile_setup( uint32_t tile_num ) {
else else
lv_switch_off( weather_wind_onoff, LV_ANIM_OFF ); lv_switch_off( weather_wind_onoff, LV_ANIM_OFF );
if ( weather_config->imperial )
lv_switch_on( weather_imperial_onoff, LV_ANIM_OFF );
else
lv_switch_off( weather_imperial_onoff, LV_ANIM_OFF );
blectl_register_cb( BLECTL_MSG, bluetooth_message_event_cb ); blectl_register_cb( BLECTL_MSG, bluetooth_message_event_cb );
} }
@@ -215,6 +237,15 @@ static void weather_wind_onoff_event_handler(lv_obj_t *obj, lv_event_t event)
} }
} }
static void weather_imperial_onoff_event_handler(lv_obj_t *obj, lv_event_t event)
{
switch (event) {
case ( LV_EVENT_VALUE_CHANGED ): weather_config_t *weather_config = weather_get_config();
weather_config->imperial = lv_switch_get_state( obj );
break;
}
}
static void exit_weather_widget_setup_event_cb( lv_obj_t * obj, lv_event_t event ) { static void exit_weather_widget_setup_event_cb( lv_obj_t * obj, lv_event_t event ) {
switch( event ) { switch( event ) {
case( LV_EVENT_CLICKED ): keyboard_hide(); case( LV_EVENT_CLICKED ): keyboard_hide();