From 2116a41a44299d4f885db1b4a89d4013f4785911 Mon Sep 17 00:00:00 2001 From: chrismcna Date: Thu, 20 Aug 2020 19:04:18 +0100 Subject: [PATCH] Weather App - configure via bluetooth https://www.my-ttgo-watch.co.uk/device/weather --- src/app/weather/weather_setup.cpp | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/app/weather/weather_setup.cpp b/src/app/weather/weather_setup.cpp index e44dd02..d3b167d 100644 --- a/src/app/weather/weather_setup.cpp +++ b/src/app/weather/weather_setup.cpp @@ -30,6 +30,10 @@ #include "gui/statusbar.h" #include "gui/keyboard.h" +#include "hardware/blectl.h" +#include "hardware/motor.h" +#include "hardware/json_psram_allocator.h" + lv_obj_t *weather_setup_tile = NULL; lv_style_t weather_setup_style; uint32_t weather_setup_tile_num; @@ -49,6 +53,9 @@ 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 bluetooth_message_event_cb( EventBits_t event, char* msg ); +static void bluetooth_message_msg_pharse( char* msg ); + void weather_setup_tile_setup( uint32_t tile_num ) { weather_config_t *weather_config = weather_get_config(); @@ -181,6 +188,8 @@ void weather_setup_tile_setup( uint32_t tile_num ) { lv_switch_on( weather_wind_onoff, LV_ANIM_OFF ); else lv_switch_off( weather_wind_onoff, LV_ANIM_OFF ); + + blectl_register_cb( BLECTL_MSG, bluetooth_message_event_cb ); } static void weather_textarea_event_cb( lv_obj_t * obj, lv_event_t event ) { @@ -218,3 +227,36 @@ static void exit_weather_widget_setup_event_cb( lv_obj_t * obj, lv_event_t event break; } } + + +static void bluetooth_message_event_cb( EventBits_t event, char* msg ) { + switch( event ) { + case BLECTL_MSG: bluetooth_message_msg_pharse( msg ); + break; + } +} + +void bluetooth_message_msg_pharse( char* msg ) { + + SpiRamJsonDocument doc( strlen( msg ) * 2 ); + + DeserializationError error = deserializeJson( doc, msg ); + if ( error ) { + log_e("bluetooth message deserializeJson() failed: %s", error.c_str() ); + } + else { + if( !strcmp( doc["t"], "conf" ) ) { + if ( !strcmp( doc["app"], "weather" ) ) { + + weather_config_t *weather_config = weather_get_config(); + strcpy( weather_config->apikey, doc["apikey"] ); + strcpy( weather_config->lat, doc["lat"] ); + strcpy( weather_config->lon, doc["lon"] ); + weather_save_config(); + motor_vibe(100); + } + + } + } + doc.clear(); +}