From 4e71dd066b9fc123f0cd814573319806072101eb Mon Sep 17 00:00:00 2001 From: joshvito Date: Thu, 20 Aug 2020 22:35:07 -0400 Subject: [PATCH] adds a toggle to choose temperature units; --- src/app/weather/weather.cpp | 2 ++ src/app/weather/weather.h | 1 + src/app/weather/weather_fetch.cpp | 18 +++++++++++------- src/app/weather/weather_setup.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/app/weather/weather.cpp b/src/app/weather/weather.cpp index 1acd99e..b2de1af 100644 --- a/src/app/weather/weather.cpp +++ b/src/app/weather/weather.cpp @@ -236,6 +236,7 @@ void weather_save_config( void ) { doc["lon"] = weather_config.lon; doc["autosync"] = weather_config.autosync; doc["showWind"] = weather_config.showWind; + doc["imperial"] = weather_config.imperial; if ( serializeJsonPretty( doc, file ) == 0) { 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 ) ); weather_config.autosync = doc["autosync"].as(); weather_config.showWind = doc["showWind"].as(); + weather_config.imperial = doc["imperial"].as(); } doc.clear(); } diff --git a/src/app/weather/weather.h b/src/app/weather/weather.h index 0c40354..cdd475d 100644 --- a/src/app/weather/weather.h +++ b/src/app/weather/weather.h @@ -36,6 +36,7 @@ char lat[16] = ""; bool autosync = true; bool showWind = false; + bool imperial = false; } weather_config_t; typedef struct { diff --git a/src/app/weather/weather_fetch.cpp b/src/app/weather/weather_fetch.cpp index f7493d1..d36ee32 100644 --- a/src/app/weather/weather_fetch.cpp +++ b/src/app/weather/weather_fetch.cpp @@ -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 ) { char url[512]=""; int httpcode = -1; - - 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_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/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; @@ -45,7 +47,7 @@ int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *we httpcode = today_client.GET(); if ( httpcode != 200 ) { - log_e("HTTPClient error %d", httpcode ); + log_e("HTTPClient error %d", httpcode, url ); today_client.end(); return( -1 ); } @@ -63,7 +65,7 @@ int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *we today_client.end(); weather_today->valide = true; - snprintf( weather_today->temp, sizeof( weather_today->temp ),"%0.1f°C", doc["main"]["temp"].as() - 273.15 ); + snprintf( weather_today->temp, sizeof( weather_today->temp ), "%0.1f°%s", doc["main"]["temp"].as(), weather_units_symbol); snprintf( weather_today->humidity, sizeof( weather_today->humidity ),"%f%%", doc["main"]["humidity"].as() ); snprintf( weather_today->pressure, sizeof( weather_today->pressure ),"%fpha", doc["main"]["pressure"].as() ); 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 ) { char url[512]=""; 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; @@ -90,7 +94,7 @@ int weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t httpcode = forecast_client.GET(); if ( httpcode != 200 ) { - log_e("HTTPClient error %d", httpcode ); + log_e("HTTPClient error %d", httpcode, url ); forecast_client.end(); return( -1 ); } @@ -110,7 +114,7 @@ int weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t weather_forecast[0].valide = true; for ( int i = 0 ; i < WEATHER_MAX_FORECAST ; i++ ) { weather_forecast[ i ].timestamp = doc["list"][i]["dt"].as(); - snprintf( weather_forecast[ i ].temp, sizeof( weather_forecast[ i ].temp ),"%0.1f°C", doc["list"][i]["main"]["temp"].as() - 273.15 ); + snprintf( weather_forecast[ i ].temp, sizeof( weather_forecast[ i ].temp ),"%0.1f°%s", doc["list"][i]["main"]["temp"].as(), weather_units_symbol ); snprintf( weather_forecast[ i ].humidity, sizeof( weather_forecast[ i ].humidity ),"%f%%", doc["list"][i]["main"]["humidity"].as() ); snprintf( weather_forecast[ i ].pressure, sizeof( weather_forecast[ i ].pressure ),"%fpha", doc["list"][i]["main"]["pressure"].as() ); strcpy( weather_forecast[ i ].icon, doc["list"][i]["weather"][0]["icon"] ); diff --git a/src/app/weather/weather_setup.cpp b/src/app/weather/weather_setup.cpp index d3b167d..520e7af 100644 --- a/src/app/weather/weather_setup.cpp +++ b/src/app/weather/weather_setup.cpp @@ -44,6 +44,7 @@ lv_obj_t *weather_lat_textfield = NULL; lv_obj_t *weather_lon_textfield = NULL; lv_obj_t *weather_autosync_onoff = NULL; lv_obj_t *weather_wind_onoff = NULL; +lv_obj_t *weather_imperial_onoff = NULL; lv_style_t weather_widget_setup_style; 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 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_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_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_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) lv_switch_on(weather_autosync_onoff, LV_ANIM_OFF); else @@ -189,6 +206,11 @@ void weather_setup_tile_setup( uint32_t tile_num ) { else 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 ); } @@ -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 ) { switch( event ) { case( LV_EVENT_CLICKED ): keyboard_hide();