diff --git a/src/app/weather/weather.cpp b/src/app/weather/weather.cpp index 6f6ff2f..0045ef8 100644 --- a/src/app/weather/weather.cpp +++ b/src/app/weather/weather.cpp @@ -37,6 +37,7 @@ #include "gui/keyboard.h" #include "hardware/motor.h" #include "hardware/powermgm.h" +#include "hardware/json_config_psram_allocator.h" EventGroupHandle_t weather_widget_event_handle = NULL; TaskHandle_t _weather_widget_sync_Task; @@ -211,37 +212,81 @@ void weather_widget_sync_Task( void * pvParameters ) { * */ void weather_save_config( void ) { + if ( SPIFFS.exists( WEATHER_CONFIG_FILE ) ) { + SPIFFS.remove( WEATHER_CONFIG_FILE ); + log_i("remove old binary weather config"); + } - fs::File file = SPIFFS.open( WEATHER_CONFIG_FILE, FILE_WRITE ); + fs::File file = SPIFFS.open( WEATHER_JSON_CONFIG_FILE, FILE_WRITE ); - if ( !file ) { - log_e( "Can't save file: %s\r\n", WEATHER_CONFIG_FILE ); + if (!file) { + log_e("Can't open file: %s!", WEATHER_JSON_CONFIG_FILE ); } else { - file.write( (uint8_t *)&weather_config, sizeof( weather_config ) ); - file.close(); + SpiRamJsonDocument doc( 1000 ); + + doc["apikey"] = weather_config.apikey; + doc["lat"] = weather_config.lat; + doc["lon"] = weather_config.lon; + doc["autosync"] = weather_config.autosync; + doc["showWind"] = weather_config.showWind; + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); } + file.close(); } /* * */ void weather_load_config( void ) { - - fs::File file = SPIFFS.open( WEATHER_CONFIG_FILE, FILE_READ ); - - if (!file) { - log_e( "Can't open file: %s\r\n", WEATHER_CONFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( weather_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + if ( SPIFFS.exists( WEATHER_JSON_CONFIG_FILE ) ) { + fs::File file = SPIFFS.open( WEATHER_JSON_CONFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", WEATHER_JSON_CONFIG_FILE ); } else { - file.read( (uint8_t *)&weather_config, filesize ); + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); + + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + strlcpy( weather_config.apikey, doc["apikey"], sizeof( weather_config.apikey ) ); + strlcpy( weather_config.lat, doc["lat"], sizeof( weather_config.lat ) ); + strlcpy( weather_config.lon, doc["lon"], sizeof( weather_config.lon ) ); + weather_config.autosync = doc["autosync"].as(); + weather_config.showWind = doc["showWind"].as(); + } + doc.clear(); } file.close(); } + else { + log_i("no json config exists, read from binary"); + fs::File file = SPIFFS.open( WEATHER_CONFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", WEATHER_CONFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( weather_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)&weather_config, filesize ); + file.close(); + weather_save_config(); + return; + } + file.close(); + } + } } diff --git a/src/app/weather/weather.h b/src/app/weather/weather.h index 5682391..0c40354 100644 --- a/src/app/weather/weather.h +++ b/src/app/weather/weather.h @@ -25,6 +25,7 @@ #include #define WEATHER_CONFIG_FILE "/weather.cfg" + #define WEATHER_JSON_CONFIG_FILE "/weather.json" #define WEATHER_WIDGET_SYNC_REQUEST _BV(0) diff --git a/src/app/weather/weather_fetch.cpp b/src/app/weather/weather_fetch.cpp index da312d1..0f089b7 100644 --- a/src/app/weather/weather_fetch.cpp +++ b/src/app/weather/weather_fetch.cpp @@ -20,7 +20,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "config.h" -#include "ArduinoJson.h" #include "HTTPClient.h" #include "weather.h" @@ -28,27 +27,7 @@ #include "weather_forecast.h" #include "hardware/powermgm.h" - -// arduinoJson allocator for external PSRAM -// see: https://arduinojson.org/v6/how-to/use-external-ram-on-esp32/ -struct WeatherSpiRamAllocator { - void* allocate( size_t size ) { - void *psram = ps_calloc( size, 1 ); - if ( psram ) { - log_i("allocate %dbytes(%p) json psram", size, psram ); - return( psram ); - } - else { - log_e("allocate %dbytes(%p) json psram failed", size, psram ); - while(1); - } - } - void deallocate( void* pointer ) { - log_i("deallocate (%p) json psram", pointer ); - free( pointer ); - } -}; -using WeatherSpiRamJsonDocument = BasicJsonDocument; +#include "hardware/json_config_psram_allocator.h" /* Utility function to convert numbers to directions */ static void weather_wind_to_string( weather_forcast_t* container, int speed, int directionDegree); @@ -71,7 +50,7 @@ int weather_fetch_today( weather_config_t *weather_config, weather_forcast_t *we return( -1 ); } - WeatherSpiRamJsonDocument doc( today_client.getSize() * 2 ); + SpiRamJsonDocument doc( today_client.getSize() * 2 ); DeserializationError error = deserializeJson( doc, today_client.getStream() ); if (error) { @@ -116,7 +95,7 @@ int weather_fetch_forecast( weather_config_t *weather_config, weather_forcast_t return( -1 ); } - WeatherSpiRamJsonDocument doc( forecast_client.getSize() * 2 ); + SpiRamJsonDocument doc( forecast_client.getSize() * 2 ); DeserializationError error = deserializeJson( doc, forecast_client.getStream() ); if (error) { diff --git a/src/config.h b/src/config.h index 57e2de8..b8cd9b4 100644 --- a/src/config.h +++ b/src/config.h @@ -32,6 +32,6 @@ /* * firmeware version string */ - #define __FIRMWARE__ "2020081102" + #define __FIRMWARE__ "2020081103" #endif // _CONFIG_H diff --git a/src/gui/mainbar/setup_tile/update/update_check_version.cpp b/src/gui/mainbar/setup_tile/update/update_check_version.cpp index aca3a68..147e6e5 100644 --- a/src/gui/mainbar/setup_tile/update/update_check_version.cpp +++ b/src/gui/mainbar/setup_tile/update/update_check_version.cpp @@ -20,31 +20,11 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "config.h" -#include "ArduinoJson.h" #include "HTTPClient.h" #include "update_check_version.h" -// arduinoJson allocator for external PSRAM -// see: https://arduinojson.org/v6/how-to/use-external-ram-on-esp32/ -struct UpdateSpiRamAllocator { - void* allocate( size_t size ) { - void *psram = ps_calloc( size, 1 ); - if ( psram ) { - log_i("allocate %dbytes(%p) json psram", size, psram ); - return( psram ); - } - else { - log_e("allocate %dbytes(%p) json psram failed", size, psram ); - while(1); - } - } - void deallocate( void* pointer ) { - log_i("deallocate (%p) json psram", pointer ); - free( pointer ); - } -}; -using SpiRamJsonDocument = BasicJsonDocument; +#include "hardware/json_config_psram_allocator.h" uint64_t update_check_new_version( void ) { char url[512]=""; diff --git a/src/gui/mainbar/setup_tile/update/update_setup.cpp b/src/gui/mainbar/setup_tile/update/update_setup.cpp index d6ba8a1..28bebe5 100644 --- a/src/gui/mainbar/setup_tile/update/update_setup.cpp +++ b/src/gui/mainbar/setup_tile/update/update_setup.cpp @@ -29,6 +29,8 @@ #include "gui/statusbar.h" #include "gui/keyboard.h" +#include "hardware/json_config_psram_allocator.h" + update_config_t update_config; lv_obj_t *update_setup_tile = NULL; @@ -88,7 +90,7 @@ void update_setup_tile_setup( uint32_t tile_num ) { lv_label_set_text( update_check_autosync_label, "check for updates"); lv_obj_align( update_check_autosync_label, update_check_autosync_cont, LV_ALIGN_IN_LEFT_MID, 5, 0 ); - if ( update_config.autosync) + if ( update_config.autosync ) lv_switch_on( update_check_autosync_onoff, LV_ANIM_OFF); else lv_switch_off( update_check_autosync_onoff, LV_ANIM_OFF); @@ -112,33 +114,71 @@ static void exit_update_check_setup_event_cb( lv_obj_t * obj, lv_event_t event ) } void update_save_config( void ) { - fs::File file = SPIFFS.open( UPDATE_CONFIG_FILE, FILE_WRITE ); + if ( SPIFFS.exists( UPDATE_CONFIG_FILE ) ) { + SPIFFS.remove( UPDATE_CONFIG_FILE ); + log_i("remove old binary update config"); + } - if ( !file ) { - log_e("Can't save file: %s", UPDATE_CONFIG_FILE ); - } - else { - file.write( (uint8_t *)&update_config, sizeof( update_config ) ); + fs::File file = SPIFFS.open( UPDATE_JSON_CONFIG_FILE, FILE_WRITE ); + + if (!file) { + log_e("Can't open file: %s!", UPDATE_JSON_CONFIG_FILE ); + } + else { + SpiRamJsonDocument doc( 1000 ); + + doc["autosync"] = update_config.autosync; + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); + } file.close(); - } } void update_read_config( void ) { - fs::File file = SPIFFS.open( UPDATE_CONFIG_FILE, FILE_READ ); + if ( SPIFFS.exists( UPDATE_JSON_CONFIG_FILE ) ) { + fs::File file = SPIFFS.open( UPDATE_JSON_CONFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", UPDATE_JSON_CONFIG_FILE ); + } + else { + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); - if (!file) { - log_e("Can't open file: %s!", UPDATE_CONFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( update_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + update_config.autosync = doc["autosync"].as(); + } + doc.clear(); + } + file.close(); } else { - file.read( (uint8_t *)&update_config, filesize ); + log_i("no json config exists, read from binary"); + fs::File file = SPIFFS.open( UPDATE_CONFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", UPDATE_CONFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( update_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)&update_config, filesize ); + file.close(); + update_save_config(); + return; + } + file.close(); + } } - file.close(); - } } bool update_setup_get_autosync( void ) { diff --git a/src/gui/mainbar/setup_tile/update/update_setup.h b/src/gui/mainbar/setup_tile/update/update_setup.h index a603798..176ea2d 100644 --- a/src/gui/mainbar/setup_tile/update/update_setup.h +++ b/src/gui/mainbar/setup_tile/update/update_setup.h @@ -25,6 +25,7 @@ #include #define UPDATE_CONFIG_FILE "/update.cfg" + #define UPDATE_JSON_CONFIG_FILE "/update.json" typedef struct { bool autosync = true; diff --git a/src/gui/screenshot.cpp b/src/gui/screenshot.cpp index 07a2321..5434f42 100644 --- a/src/gui/screenshot.cpp +++ b/src/gui/screenshot.cpp @@ -32,6 +32,7 @@ void screenshot_setup( void ) { log_e("error memory alloc"); while(1); } + SPIFFS.remove( SCREENSHOT_FILE_NAME ); } void screenshot_take( void ) { diff --git a/src/hardware/bma.cpp b/src/hardware/bma.cpp index 5646b12..eb98a62 100644 --- a/src/hardware/bma.cpp +++ b/src/hardware/bma.cpp @@ -25,6 +25,7 @@ #include "bma.h" #include "powermgm.h" +#include "json_config_psram_allocator.h" #include "gui/statusbar.h" @@ -145,36 +146,76 @@ void bma_loop( TTGOClass *ttgo ) { * */ void bma_save_config( void ) { - fs::File file = SPIFFS.open( BMA_COFIG_FILE, FILE_WRITE ); + if ( SPIFFS.exists( BMA_COFIG_FILE ) ) { + SPIFFS.remove( BMA_COFIG_FILE ); + log_i("remove old binary bma config"); + } - if ( !file ) { - log_e("Can't save file: %s", BMA_COFIG_FILE ); - } - else { - file.write( (uint8_t *)bma_config, sizeof( bma_config ) ); + fs::File file = SPIFFS.open( BMA_JSON_COFIG_FILE, FILE_WRITE ); + + if (!file) { + log_e("Can't open file: %s!", BMA_JSON_COFIG_FILE ); + } + else { + SpiRamJsonDocument doc( 1000 ); + + doc["stepcounter"] = bma_config[ BMA_STEPCOUNTER ].enable; + doc["doubleclick"] = bma_config[ BMA_DOUBLECLICK ].enable; + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); + } file.close(); - } } /* * */ void bma_read_config( void ) { - fs::File file = SPIFFS.open( BMA_COFIG_FILE, FILE_READ ); + if ( SPIFFS.exists( BMA_JSON_COFIG_FILE ) ) { + fs::File file = SPIFFS.open( BMA_JSON_COFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", BMA_JSON_COFIG_FILE ); + } + else { + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); - if (!file) { - log_e("Can't open file: %s!", BMA_COFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( bma_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + bma_config[ BMA_STEPCOUNTER ].enable = doc["stepcounter"].as(); + bma_config[ BMA_DOUBLECLICK ].enable = doc["doubleclick"].as(); + } + doc.clear(); + } + file.close(); } else { - file.read( (uint8_t *)bma_config, filesize ); + log_i("no json config exists, read from binary"); + fs::File file = SPIFFS.open( BMA_COFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", BMA_COFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( bma_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)bma_config, filesize ); + file.close(); + bma_save_config(); + return; + } + file.close(); + } } - file.close(); - } } /* diff --git a/src/hardware/bma.h b/src/hardware/bma.h index cc4fe58..2461966 100644 --- a/src/hardware/bma.h +++ b/src/hardware/bma.h @@ -22,7 +22,10 @@ #ifndef _BMA_H #define _BMA_H - #define BMA_EVENT_INT _BV(0) + #define BMA_EVENT_INT _BV(0) + + #define BMA_COFIG_FILE "/bma.cfg" + #define BMA_JSON_COFIG_FILE "/bma.json" typedef struct { bool enable=true; @@ -34,8 +37,6 @@ BMA_CONFIG_NUM }; - #define BMA_COFIG_FILE "/bma.cfg" - /* * @brief setup bma activity measurement * diff --git a/src/hardware/display.cpp b/src/hardware/display.cpp index f32cd8b..a0a8391 100644 --- a/src/hardware/display.cpp +++ b/src/hardware/display.cpp @@ -26,6 +26,8 @@ #include "powermgm.h" #include "motor.h" +#include "json_config_psram_allocator.h" + display_config_t display_config; static uint8_t dest_brightness = 0; @@ -106,36 +108,78 @@ void display_wakeup( void ) { * */ void display_save_config( void ) { - fs::File file = SPIFFS.open( DISPLAY_CONFIG_FILE, FILE_WRITE ); + if ( SPIFFS.exists( DISPLAY_CONFIG_FILE ) ) { + SPIFFS.remove( DISPLAY_CONFIG_FILE ); + log_i("remove old binary display config"); + } - if ( !file ) { - log_e("Can't save file: %s", DISPLAY_CONFIG_FILE ); - } - else { - file.write( (uint8_t *)&display_config, sizeof( display_config ) ); + fs::File file = SPIFFS.open( DISPLAY_JSON_CONFIG_FILE, FILE_WRITE ); + + if (!file) { + log_e("Can't open file: %s!", DISPLAY_JSON_CONFIG_FILE ); + } + else { + SpiRamJsonDocument doc( 1000 ); + + doc["brightness"] = display_config.brightness; + doc["rotation"] = display_config.rotation; + doc["timeout"] = display_config.timeout; + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); + } file.close(); - } } /* * */ void display_read_config( void ) { - fs::File file = SPIFFS.open( DISPLAY_CONFIG_FILE, FILE_READ ); + if ( SPIFFS.exists( DISPLAY_JSON_CONFIG_FILE ) ) { + fs::File file = SPIFFS.open( DISPLAY_JSON_CONFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", DISPLAY_JSON_CONFIG_FILE ); + } + else { + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); - if (!file) { - log_e("Can't open file: %s!", DISPLAY_CONFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( display_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + display_config.brightness = doc["brightness"].as(); + display_config.rotation = doc["rotation"].as(); + display_config.timeout = doc["timeout"].as(); + } + doc.clear(); + } + file.close(); } else { - file.read( (uint8_t *)&display_config, filesize ); + log_i("no json config exists, read from binary"); + fs::File file = SPIFFS.open( DISPLAY_CONFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", DISPLAY_CONFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( display_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)&display_config, filesize ); + file.close(); + display_save_config(); + return; + } + file.close(); + } } - file.close(); - } } uint32_t display_get_timeout( void ) { diff --git a/src/hardware/display.h b/src/hardware/display.h index a9ae294..3649b45 100644 --- a/src/hardware/display.h +++ b/src/hardware/display.h @@ -32,12 +32,13 @@ #define DISPLAY_MAX_ROTATE 270 typedef struct { - uint32_t brightness = DISPLAY_MIN_BRIGHTNESS; + uint32_t brightness = DISPLAY_MAX_BRIGHTNESS; uint32_t timeout = DISPLAY_MIN_TIMEOUT; uint32_t rotation = 0; } display_config_t; - #define DISPLAY_CONFIG_FILE "/display.cfg" + #define DISPLAY_CONFIG_FILE "/display.cfg" + #define DISPLAY_JSON_CONFIG_FILE "/display.json" /* * @brief setup display diff --git a/src/hardware/json_config_psram_allocator.h b/src/hardware/json_config_psram_allocator.h new file mode 100644 index 0000000..8a96cd8 --- /dev/null +++ b/src/hardware/json_config_psram_allocator.h @@ -0,0 +1,23 @@ +#include "config.h" +#include "ArduinoJson.h" + +// arduinoJson allocator for external PSRAM +// see: https://arduinojson.org/v6/how-to/use-external-ram-on-esp32/ +struct SpiRamAllocator { + void* allocate( size_t size ) { + void *psram = ps_calloc( size, 1 ); + if ( psram ) { + log_i("allocate %dbytes(%p) json psram", size, psram ); + return( psram ); + } + else { + log_e("allocate %dbytes(%p) json psram failed", size, psram ); + while(1); + } + } + void deallocate( void* pointer ) { + log_i("deallocate (%p) json psram", pointer ); + free( pointer ); + } +}; +using SpiRamJsonDocument = BasicJsonDocument; \ No newline at end of file diff --git a/src/hardware/motor.cpp b/src/hardware/motor.cpp index c57a37a..fd676f0 100644 --- a/src/hardware/motor.cpp +++ b/src/hardware/motor.cpp @@ -21,6 +21,7 @@ */ #include "config.h" #include +#include "json_config_psram_allocator.h" #include "motor.h" #include "powermgm.h" @@ -55,6 +56,8 @@ void motor_setup( void ) { if ( motor_init == true ) return; + motor_read_config(); + pinMode(GPIO_NUM_4, OUTPUT); timer = timerBegin(0, 80, true); timerAttachInterrupt(timer, &onTimer, true); @@ -92,34 +95,72 @@ void motor_set_vibe_config( bool enable ) { * */ void motor_save_config( void ) { - fs::File file = SPIFFS.open( MOTOR_CONFIG_FILE, FILE_WRITE ); + if ( SPIFFS.exists( MOTOR_CONFIG_FILE ) ) { + SPIFFS.remove( MOTOR_CONFIG_FILE ); + log_i("remove old binary motor config"); + } - if ( !file ) { - log_e("Can't save file: %s", MOTOR_CONFIG_FILE ); - } - else { - file.write( (uint8_t *)&motor_config, sizeof( motor_config ) ); + fs::File file = SPIFFS.open( MOTOR_JSON_CONFIG_FILE, FILE_WRITE ); + + if (!file) { + log_e("Can't open file: %s!", MOTOR_JSON_CONFIG_FILE ); + } + else { + SpiRamJsonDocument doc( 1000 ); + + doc["motor"] = motor_config.vibe; + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); + } file.close(); - } } /* * */ void motor_read_config( void ) { - fs::File file = SPIFFS.open( MOTOR_CONFIG_FILE, FILE_READ ); + if ( SPIFFS.exists( MOTOR_JSON_CONFIG_FILE ) ) { + fs::File file = SPIFFS.open( MOTOR_JSON_CONFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", MOTOR_JSON_CONFIG_FILE ); + } + else { + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); - if (!file) { - log_e("Can't open file: %s!", MOTOR_CONFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( motor_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + motor_config.vibe = doc["motor"].as(); + } + doc.clear(); + } + file.close(); } else { - file.read( (uint8_t *)&motor_config, filesize ); + log_i("no json config exists, read from binary"); + fs::File file = SPIFFS.open( MOTOR_CONFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", MOTOR_CONFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( motor_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)&motor_config, filesize ); + file.close(); + motor_save_config(); + return; + } + file.close(); + } } - file.close(); - } } \ No newline at end of file diff --git a/src/hardware/motor.h b/src/hardware/motor.h index 9a1122d..8685871 100644 --- a/src/hardware/motor.h +++ b/src/hardware/motor.h @@ -25,6 +25,7 @@ #include "TTGO.h" #define MOTOR_CONFIG_FILE "/motor.cfg" + #define MOTOR_JSON_CONFIG_FILE "/motor.json" typedef struct { bool vibe = true; diff --git a/src/hardware/pmu.cpp b/src/hardware/pmu.cpp index a7b3064..95a8d01 100644 --- a/src/hardware/pmu.cpp +++ b/src/hardware/pmu.cpp @@ -1,6 +1,7 @@ #include "config.h" #include #include +#include "json_config_psram_allocator.h" #include "display.h" #include "pmu.h" @@ -112,36 +113,78 @@ void pmu_wakeup( void ) { * */ void pmu_save_config( void ) { - fs::File file = SPIFFS.open( PMU_CONFIG_FILE, FILE_WRITE ); + if ( SPIFFS.exists( PMU_CONFIG_FILE ) ) { + SPIFFS.remove( PMU_CONFIG_FILE ); + log_i("remove old binary pmu config"); + } + + fs::File file = SPIFFS.open( PMU_JSON_CONFIG_FILE, FILE_WRITE ); - if ( !file ) { - log_e("Can't save file: %s", PMU_CONFIG_FILE ); - } - else { - file.write( (uint8_t *)&pmu_config, sizeof( pmu_config ) ); + if (!file) { + log_e("Can't open file: %s!", PMU_JSON_CONFIG_FILE ); + } + else { + SpiRamJsonDocument doc( 1000 ); + + doc["silence_wakeup"] = pmu_config.silence_wakeup; + doc["experimental_power_save"] = pmu_config.experimental_power_save; + doc["compute_percent"] = pmu_config.compute_percent; + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); + } file.close(); - } } /* * */ void pmu_read_config( void ) { - fs::File file = SPIFFS.open( PMU_CONFIG_FILE, FILE_READ ); + if ( SPIFFS.exists( PMU_JSON_CONFIG_FILE ) ) { + fs::File file = SPIFFS.open( PMU_JSON_CONFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", PMU_JSON_CONFIG_FILE ); + } + else { + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); - if (!file) { - log_e("Can't open file: %s!", PMU_CONFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( pmu_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + pmu_config.silence_wakeup = doc["silence_wakeup"].as(); + pmu_config.experimental_power_save = doc["experimental_power_save"].as(); + pmu_config.compute_percent = doc["compute_percent"].as(); + } + doc.clear(); + } + file.close(); } else { - file.read( (uint8_t *)&pmu_config, filesize ); + log_i("no json config exists, read from binary"); + fs::File file = SPIFFS.open( PMU_CONFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", PMU_CONFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( pmu_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)&pmu_config, filesize ); + file.close(); + pmu_save_config(); + return; + } + file.close(); + } } - file.close(); - } } bool pmu_get_silence_wakeup( void ) { diff --git a/src/hardware/pmu.h b/src/hardware/pmu.h index fa9938c..33a3518 100644 --- a/src/hardware/pmu.h +++ b/src/hardware/pmu.h @@ -27,6 +27,7 @@ #define PMU_BATTERY_CAP 300 #define PMU_CONFIG_FILE "/pmu.cfg" + #define PMU_JSON_CONFIG_FILE "/pmu.json" typedef struct { bool compute_percent = false; diff --git a/src/hardware/timesync.cpp b/src/hardware/timesync.cpp index f6b6354..4a1cd8c 100644 --- a/src/hardware/timesync.cpp +++ b/src/hardware/timesync.cpp @@ -24,6 +24,7 @@ #include "config.h" #include "timesync.h" #include "powermgm.h" +#include "json_config_psram_allocator.h" EventGroupHandle_t time_event_handle = NULL; TaskHandle_t _timesync_Task; @@ -57,33 +58,75 @@ void timesync_setup( TTGOClass *ttgo ) { } void timesync_save_config( void ) { - fs::File file = SPIFFS.open( TIMESYNC_CONFIG_FILE, FILE_WRITE ); + if ( SPIFFS.exists( TIMESYNC_CONFIG_FILE ) ) { + SPIFFS.remove( TIMESYNC_CONFIG_FILE ); + log_i("remove old binary timesync config"); + } - if ( !file ) { - log_e("Can't save file: %s", TIMESYNC_CONFIG_FILE ); - } - else { - file.write( (uint8_t *)×ync_config, sizeof( timesync_config ) ); + fs::File file = SPIFFS.open( TIMESYNC_JSON_CONFIG_FILE, FILE_WRITE ); + + if (!file) { + log_e("Can't open file: %s!", TIMESYNC_JSON_CONFIG_FILE ); + } + else { + SpiRamJsonDocument doc( 1000 ); + + doc["daylightsave"] = timesync_config.daylightsave; + doc["timesync"] = timesync_config.timesync; + doc["timezone"] = timesync_config.timezone; + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); + } file.close(); - } } void timesync_read_config( void ) { - fs::File file = SPIFFS.open( TIMESYNC_CONFIG_FILE, FILE_READ ); + if ( SPIFFS.exists( TIMESYNC_JSON_CONFIG_FILE ) ) { + fs::File file = SPIFFS.open( TIMESYNC_JSON_CONFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", TIMESYNC_JSON_CONFIG_FILE ); + } + else { + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); - if (!file) { - log_e("Can't open file: %s!", TIMESYNC_CONFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( timesync_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + timesync_config.daylightsave = doc["daylightsave"].as(); + timesync_config.timesync = doc["timesync"].as(); + timesync_config.timezone = doc["timezone"].as(); + } + doc.clear(); + } + file.close(); } else { - file.read( (uint8_t *)×ync_config, filesize ); + log_i("no json config exists, read from binary"); + fs::File file = SPIFFS.open( TIMESYNC_CONFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", TIMESYNC_CONFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( timesync_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)×ync_config, filesize ); + file.close(); + timesync_save_config(); + return; + } + file.close(); + } } - file.close(); - } } bool timesync_get_timesync( void ) { diff --git a/src/hardware/timesync.h b/src/hardware/timesync.h index 2fe5f6a..3d76503 100644 --- a/src/hardware/timesync.h +++ b/src/hardware/timesync.h @@ -24,9 +24,10 @@ #include - #define TIME_SYNC_REQUEST _BV(0) + #define TIME_SYNC_REQUEST _BV(0) - #define TIMESYNC_CONFIG_FILE "/timesync.cfg" + #define TIMESYNC_CONFIG_FILE "/timesync.cfg" + #define TIMESYNC_JSON_CONFIG_FILE "/timesync.json" typedef struct { bool timesync = true; diff --git a/src/hardware/wifictl.cpp b/src/hardware/wifictl.cpp index ee196b1..b8ce20e 100644 --- a/src/hardware/wifictl.cpp +++ b/src/hardware/wifictl.cpp @@ -27,6 +27,7 @@ #include "powermgm.h" #include "wifictl.h" +#include "json_config_psram_allocator.h" #include "gui/statusbar.h" #include "webserver/webserver.h" @@ -67,8 +68,7 @@ void wifictl_setup( void ) { wifictl_networklist[ entry ].password[ 0 ] = '\0'; } - // load network list from spiff - wifictl_load_network(); + // load config from spiff wifictl_load_config(); // register WiFi events @@ -185,36 +185,91 @@ void wifictl_setup( void ) { * */ void wifictl_save_config( void ) { - fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_WRITE ); + if ( SPIFFS.exists( WIFICTL_CONFIG_FILE ) ) { + SPIFFS.remove( WIFICTL_CONFIG_FILE ); + log_i("remove old binary wificfg config"); + } + if ( SPIFFS.exists( WIFICTL_LIST_FILE ) ) { + SPIFFS.remove( WIFICTL_LIST_FILE ); + log_i("remove old binary wifilist config"); + } - if ( !file ) { - log_e("Can't save file: %s", WIFICTL_CONFIG_FILE ); - } - else { - file.write( (uint8_t *)&wifictl_config, sizeof( wifictl_config ) ); + fs::File file = SPIFFS.open( WIFICTL_JSON_CONFIG_FILE, FILE_WRITE ); + + if (!file) { + log_e("Can't open file: %s!", WIFICTL_JSON_CONFIG_FILE ); + } + else { + SpiRamJsonDocument doc( 10000 ); + + doc["autoon"] = wifictl_config.autoon; + doc["webserver"] = wifictl_config.webserver; + for ( int i = 0 ; i < NETWORKLIST_ENTRYS ; i++ ) { + doc["networklist"][ i ]["ssid"] = wifictl_networklist[ i ].ssid; + doc["networklist"][ i ]["psk"] = wifictl_networklist[ i ].password; + } + + if ( serializeJsonPretty( doc, file ) == 0) { + log_e("Failed to write config file"); + } + doc.clear(); + } file.close(); - } } /* * */ void wifictl_load_config( void ) { - fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_READ ); + if ( SPIFFS.exists( WIFICTL_JSON_CONFIG_FILE ) ) { + fs::File file = SPIFFS.open( WIFICTL_JSON_CONFIG_FILE, FILE_READ ); + if (!file) { + log_e("Can't open file: %s!", WIFICTL_JSON_CONFIG_FILE ); + } + else { + int filesize = file.size(); + SpiRamJsonDocument doc( filesize * 2 ); - if (!file) { - log_e("Can't open file: %s", WIFICTL_CONFIG_FILE ); - } - else { - int filesize = file.size(); - if ( filesize > sizeof( wifictl_config ) ) { - log_e("Failed to read configfile. Wrong filesize!" ); + DeserializationError error = deserializeJson( doc, file ); + if ( error ) { + log_e("update check deserializeJson() failed: %s", error.c_str() ); + } + else { + wifictl_config.autoon = doc["autoon"].as(); + wifictl_config.webserver = doc["webserver"].as(); + for ( int i = 0 ; i < NETWORKLIST_ENTRYS ; i++ ) { + strlcpy( wifictl_networklist[ i ].ssid , doc["networklist"][ i ]["ssid"], sizeof( wifictl_networklist[ i ].ssid ) ); + strlcpy( wifictl_networklist[ i ].password, doc["networklist"][ i ]["psk"], sizeof( wifictl_networklist[ i ].password ) ); + } + } + doc.clear(); + } + file.close(); } else { - file.read( (uint8_t *)&wifictl_config, filesize ); + log_i("no json config exists, read from binary"); + + wifictl_load_network(); + + fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_READ ); + + if (!file) { + log_e("Can't open file: %s!", WIFICTL_CONFIG_FILE ); + } + else { + int filesize = file.size(); + if ( filesize > sizeof( wifictl_config ) ) { + log_e("Failed to read configfile. Wrong filesize!" ); + } + else { + file.read( (uint8_t *)&wifictl_config, filesize ); + file.close(); + wifictl_save_config(); + return; + } + file.close(); + } } - file.close(); - } } bool wifictl_get_autoon( void ) { @@ -234,20 +289,6 @@ void wifictl_set_webserver( bool webserver ) { wifictl_config.webserver = webserver; wifictl_save_config(); } -/* - * - */ -void wifictl_save_network( void ) { - fs::File file = SPIFFS.open( WIFICTL_LIST_FILE, FILE_WRITE ); - - if ( !file ) { - log_e("Can't save file: %s", WIFICTL_LIST_FILE ); - } - else { - file.write( (uint8_t *)wifictl_networklist, sizeof( wifictl_networklist ) ); - file.close(); - } -} /* * @@ -296,7 +337,7 @@ bool wifictl_delete_network( const char *ssid ) { if( !strcmp( ssid, wifictl_networklist[ entry ].ssid ) ) { wifictl_networklist[ entry ].ssid[ 0 ] = '\0'; wifictl_networklist[ entry ].password[ 0 ] = '\0'; - wifictl_save_network(); + wifictl_save_config(); return( true ); } } @@ -314,7 +355,7 @@ bool wifictl_insert_network( const char *ssid, const char *password ) { for( int entry = 0 ; entry < NETWORKLIST_ENTRYS; entry++ ) { if( !strcmp( ssid, wifictl_networklist[ entry ].ssid ) ) { strncpy( wifictl_networklist[ entry ].password, password, sizeof( wifictl_networklist[ entry ].password ) ); - wifictl_save_network(); + wifictl_save_config(); WiFi.scanNetworks(); powermgm_set_event( POWERMGM_WIFI_SCAN ); return( true ); @@ -325,7 +366,7 @@ bool wifictl_insert_network( const char *ssid, const char *password ) { if( strlen( wifictl_networklist[ entry ].ssid ) == 0 ) { strncpy( wifictl_networklist[ entry ].ssid, ssid, sizeof( wifictl_networklist[ entry ].ssid ) ); strncpy( wifictl_networklist[ entry ].password, password, sizeof( wifictl_networklist[ entry ].password ) ); - wifictl_save_network(); + wifictl_save_config(); WiFi.scanNetworks(); powermgm_set_event( POWERMGM_WIFI_SCAN ); return( true ); diff --git a/src/hardware/wifictl.h b/src/hardware/wifictl.h index d1fe7f4..2eb5756 100644 --- a/src/hardware/wifictl.h +++ b/src/hardware/wifictl.h @@ -22,16 +22,17 @@ #ifndef _WIFICTL_H #define _WIFICTL_H - #define WIFICTL_DELAY 10 - #define NETWORKLIST_ENTRYS 20 - #define WIFICTL_LIST_FILE "/wifilist.cfg" - #define WIFICTL_CONFIG_FILE "/wificfg.cfg" + #define WIFICTL_DELAY 10 + #define NETWORKLIST_ENTRYS 20 + #define WIFICTL_LIST_FILE "/wifilist.cfg" + #define WIFICTL_CONFIG_FILE "/wificfg.cfg" + #define WIFICTL_JSON_CONFIG_FILE "/wificfg.json" - #define ESP_WPS_MODE WPS_TYPE_PBC - #define ESP_MANUFACTURER "ESPRESSIF" - #define ESP_MODEL_NUMBER "ESP32" - #define ESP_MODEL_NAME "LILYGO T-WATCH2020 V1" - #define ESP_DEVICE_NAME "ESP STATION" + #define ESP_WPS_MODE WPS_TYPE_PBC + #define ESP_MANUFACTURER "ESPRESSIF" + #define ESP_MODEL_NUMBER "ESP32" + #define ESP_MODEL_NAME "LILYGO T-WATCH2020 V1" + #define ESP_DEVICE_NAME "ESP STATION" struct networklist { char ssid[64]=""; diff --git a/src/my-ttgo-watch.ino b/src/my-ttgo-watch.ino index 73c474c..28c32bd 100644 --- a/src/my-ttgo-watch.ino +++ b/src/my-ttgo-watch.ino @@ -40,13 +40,15 @@ TTGOClass *ttgo = TTGOClass::getWatch(); void setup() { - motor_setup(); Serial.begin(115200); Serial.printf("starting t-watch V1, version: " __FIRMWARE__ "\r\n"); ttgo->begin(); ttgo->lvgl_begin(); SPIFFS.begin(); + + motor_setup(); + display_setup( ttgo ); screenshot_setup(); diff --git a/ttgo-t-watch2020_v1.ino.bin b/ttgo-t-watch2020_v1.ino.bin index c4b4ae1..ec402ed 100644 Binary files a/ttgo-t-watch2020_v1.ino.bin and b/ttgo-t-watch2020_v1.ino.bin differ diff --git a/ttgo-t-watch2020_v1.version.json b/ttgo-t-watch2020_v1.version.json index 4ea9328..8c90158 100644 --- a/ttgo-t-watch2020_v1.version.json +++ b/ttgo-t-watch2020_v1.version.json @@ -1 +1 @@ -{"version":"2020081102","host":"http://www.neo-guerillaz.de","file":"ttgo-t-watch2020_v1.ino.bin"} +{"version":"2020081103","host":"http://www.neo-guerillaz.de","file":"ttgo-t-watch2020_v1.ino.bin"}