Adding json weather functions

pull/156/head
Michael-Paul Moore 2022-04-10 09:43:32 -07:00
parent 66a4c9b84e
commit 2b96e25364
2 changed files with 119 additions and 63 deletions

View File

@ -66,44 +66,44 @@ void WatchyExpanded::deepSleep()
} }
void WatchyExpanded::handleButtonPress(){ void WatchyExpanded::handleButtonPress(){
uint64_t wakeupBit = esp_sleep_get_ext1_wakeup_status(); uint64_t wakeupBit = esp_sleep_get_ext1_wakeup_status();
//Menu Button //Menu Button
if (wakeupBit & MENU_BTN_MASK){ if (wakeupBit & MENU_BTN_MASK){
if(guiState == WATCHFACE_STATE){//enter menu state if coming from watch face if(guiState == WATCHFACE_STATE){//enter menu state if coming from watch face
showMenu(menuIndex, false); showMenu(menuIndex, false);
}else if(guiState == MAIN_MENU_STATE){//if already in menu, then select menu item }else if(guiState == MAIN_MENU_STATE){//if already in menu, then select menu item
switch(menuIndex) switch(menuIndex)
{ {
case 0: case 0:
showAbout(); showAbout();
break; break;
case 1: case 1:
showBuzz(); showBuzz();
break; break;
case 2: case 2:
showAccelerometer(); showAccelerometer();
break; break;
case 3: case 3:
setTime(); setTime();
break; break;
case 4: case 4:
setupWifi(); setupWifi();
break; break;
case 5: case 5:
showUpdateFW(); showUpdateFW();
break; break;
case 6: case 6:
showSyncNTP(); showSyncNTP();
break; break;
default: default:
break; break;
} }
}else if(guiState == FW_UPDATE_STATE){ }else if(guiState == FW_UPDATE_STATE){
updateFWBegin(); updateFWBegin();
} }
} }
//Back Button //Back Button
else if (wakeupBit & BACK_BTN_MASK){ else if (wakeupBit & BACK_BTN_MASK){
if(guiState == MAIN_MENU_STATE){//exit to watch face if already in menu if(guiState == MAIN_MENU_STATE){//exit to watch face if already in menu
RTC.read(currentTime); RTC.read(currentTime);
showWatchFace(false); showWatchFace(false);
@ -114,44 +114,44 @@ void WatchyExpanded::handleButtonPress(){
}else if(guiState == WATCHFACE_STATE){ }else if(guiState == WATCHFACE_STATE){
return; return;
} }
} }
//Up Button //Up Button
else if (wakeupBit & UP_BTN_MASK){ else if (wakeupBit & UP_BTN_MASK){
if(guiState == MAIN_MENU_STATE){//increment menu index if(guiState == MAIN_MENU_STATE){//increment menu index
menuIndex--; menuIndex--;
if(menuIndex < 0){ if(menuIndex < 0){
menuIndex = MENU_LENGTH - 1; menuIndex = MENU_LENGTH - 1;
} }
showMenu(menuIndex, true); showMenu(menuIndex, true);
}else if(guiState == WATCHFACE_STATE){ }else if(guiState == WATCHFACE_STATE){
return; return;
} }
} }
//Down Button //Down Button
else if (wakeupBit & DOWN_BTN_MASK){ else if (wakeupBit & DOWN_BTN_MASK){
if(guiState == MAIN_MENU_STATE){//decrement menu index if(guiState == MAIN_MENU_STATE){//decrement menu index
menuIndex++; menuIndex++;
if(menuIndex > MENU_LENGTH - 1){ if(menuIndex > MENU_LENGTH - 1){
menuIndex = 0; menuIndex = 0;
} }
showMenu(menuIndex, true); showMenu(menuIndex, true);
}else if(guiState == WATCHFACE_STATE){ }else if(guiState == WATCHFACE_STATE){
return; return;
} }
} }
/***************** fast menu *****************/ /***************** fast menu *****************/
bool timeout = false; bool timeout = false;
long lastTimeout = millis(); long lastTimeout = millis();
pinMode(MENU_BTN_PIN, INPUT); pinMode(MENU_BTN_PIN, INPUT);
pinMode(BACK_BTN_PIN, INPUT); pinMode(BACK_BTN_PIN, INPUT);
pinMode(UP_BTN_PIN, INPUT); pinMode(UP_BTN_PIN, INPUT);
pinMode(DOWN_BTN_PIN, INPUT); pinMode(DOWN_BTN_PIN, INPUT);
while(!timeout){ while(!timeout){
if(millis() - lastTimeout > 5000){ if(millis() - lastTimeout > 5000){
timeout = true; timeout = true;
}else{ }else{
if(digitalRead(MENU_BTN_PIN) == 1){ if(digitalRead(MENU_BTN_PIN) == 1){
lastTimeout = millis(); lastTimeout = millis();
if(guiState == MAIN_MENU_STATE){//if already in menu, then select menu item if(guiState == MAIN_MENU_STATE){//if already in menu, then select menu item
switch(menuIndex) switch(menuIndex)
@ -183,7 +183,7 @@ void WatchyExpanded::handleButtonPress(){
}else if(guiState == FW_UPDATE_STATE){ }else if(guiState == FW_UPDATE_STATE){
updateFWBegin(); updateFWBegin();
} }
}else if(digitalRead(BACK_BTN_PIN) == 1){ }else if(digitalRead(BACK_BTN_PIN) == 1){
lastTimeout = millis(); lastTimeout = millis();
if(guiState == MAIN_MENU_STATE){//exit to watch face if already in menu if(guiState == MAIN_MENU_STATE){//exit to watch face if already in menu
RTC.read(currentTime); RTC.read(currentTime);
@ -194,7 +194,7 @@ void WatchyExpanded::handleButtonPress(){
}else if(guiState == FW_UPDATE_STATE){ }else if(guiState == FW_UPDATE_STATE){
showMenu(menuIndex, false);//exit to menu if already in app showMenu(menuIndex, false);//exit to menu if already in app
} }
}else if(digitalRead(UP_BTN_PIN) == 1){ }else if(digitalRead(UP_BTN_PIN) == 1){
lastTimeout = millis(); lastTimeout = millis();
if(guiState == MAIN_MENU_STATE){//increment menu index if(guiState == MAIN_MENU_STATE){//increment menu index
menuIndex--; menuIndex--;
@ -203,7 +203,7 @@ void WatchyExpanded::handleButtonPress(){
} }
showFastMenu(menuIndex); showFastMenu(menuIndex);
} }
}else if(digitalRead(DOWN_BTN_PIN) == 1){ }else if(digitalRead(DOWN_BTN_PIN) == 1){
lastTimeout = millis(); lastTimeout = millis();
if(guiState == MAIN_MENU_STATE){//decrement menu index if(guiState == MAIN_MENU_STATE){//decrement menu index
menuIndex++; menuIndex++;
@ -212,9 +212,9 @@ void WatchyExpanded::handleButtonPress(){
} }
showFastMenu(menuIndex); showFastMenu(menuIndex);
} }
} }
} }
} }
} }
void WatchyExpanded::showMenu(byte menuIndex, bool partialRefresh){ void WatchyExpanded::showMenu(byte menuIndex, bool partialRefresh){
@ -549,10 +549,10 @@ void WatchyExpanded::showAccelerometer(){
} }
void WatchyExpanded::showWatchFace(bool partialRefresh){ void WatchyExpanded::showWatchFace(bool partialRefresh){
display.setFullWindow(); display.setFullWindow();
drawWatchFace(); drawWatchFace();
display.display(partialRefresh); //partial refresh display.display(partialRefresh); //partial refresh
guiState = WATCHFACE_STATE; guiState = WATCHFACE_STATE;
} }
void WatchyExpanded::drawWatchFace(){ void WatchyExpanded::drawWatchFace(){
@ -957,3 +957,54 @@ bool WatchyExpanded::syncNTP(long gmt, int dst, String ntpServer){ //NTP sync -
RTC.set(tm); RTC.set(tm);
return true; return true;
} }
JSONVar& WatchyExpanded::getWeatherJSON()
{
getWeatherJSON(settings.cityID, settings.weatherUnit, settings.weatherLang, settings.weatherURL,
settings.weatherAPIKey, settings.weatherUpdateInterval);
return m_responseObject;
}
JSONVar& WatchyExpanded::getWeatherJSON(String cityID, String units, String lang, String url, String apiKey, uint8_t updateInterval)
{
if(weatherIntervalCounter < 0) //-1 on first run, set to updateInterval
weatherIntervalCounter = updateInterval;
if(weatherIntervalCounter >= updateInterval) //only update if WEATHER_UPDATE_INTERVAL has elapsed i.e. 30 minutes
{
if(connectWiFi())
{
HTTPClient http; //Use Weather API for live data if WiFi is connected
http.setConnectTimeout(3000);//3 second max timeout
const String& weatherQueryURL = url + cityID + String("&units=") + units + String("&lang=") + lang + String("&appid=") + apiKey;
http.begin(weatherQueryURL.c_str());
int httpResponseCode = http.GET();
if(httpResponseCode == 200)
{
String payload = http.getString();
const JSONVar& jsonNew = JSON.parse(payload);
const JSONVar jsonOld = m_responseObject;
m_responseObject = jsonNew;
if (!json.hasOwnProperty("weather"))
m_responseObject["weather"] = jsonOld["weather"];
}
else
{
//http error
}
http.end();
//turn off radios
WiFi.mode(WIFI_OFF);
btStop();
}
weatherIntervalCounter = 0;
}
else
{
++weatherIntervalCounter;
}
return m_responseObject;
}

View File

@ -71,6 +71,11 @@ class WatchyExpanded
void showWatchFace(bool partialRefresh); void showWatchFace(bool partialRefresh);
virtual void drawWatchFace(); //override this method for different watch faces virtual void drawWatchFace(); //override this method for different watch faces
// Expanded
JSONVar& getWeatherJSON();
JSONVar& getWeatherJSON(String cityID, String units, String lang, String url, String apiKey,
uint8_t updateInterval);
private: private:
void _bmaConfig(); void _bmaConfig();
static void _configModeCallback(WiFiManager *myWiFiManager); static void _configModeCallback(WiFiManager *myWiFiManager);