From 07e83e7557c80678ddc7d95c28edf1699f30b184 Mon Sep 17 00:00:00 2001 From: Lewis Jackson <> Date: Thu, 1 Jun 2023 17:17:57 +0300 Subject: [PATCH] Move wifi code into a watch feature --- src/WatchFace.cpp | 17 +++++++++++------ src/WatchFeatures/WatchFeatures.h | 2 ++ src/WatchFeatures/Wifi.cpp | 28 ++++++++++++++++++++++++++++ src/WatchFeatures/Wifi.h | 12 ++++++++++++ src/Watchy.cpp | 20 -------------------- 5 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 src/WatchFeatures/Wifi.cpp create mode 100644 src/WatchFeatures/Wifi.h diff --git a/src/WatchFace.cpp b/src/WatchFace.cpp index e17494c..009d909 100644 --- a/src/WatchFace.cpp +++ b/src/WatchFace.cpp @@ -13,9 +13,10 @@ void WatchFace::InitBoot() SetupVolatileMenuStuff(); m_menu.Reset(); - ConnectWiFi(); - SyncNTPTime(); - DisconnectWiFi(); + if(m_features.wifi.Connect()) { + SyncNTPTime(); + m_features.wifi.Disconnect(); + } for (auto & page : m_pages) { page->InitBoot(); @@ -235,16 +236,20 @@ void WatchFace::MenuExited() void WatchFace::MenuNTPSyncSelected() { - ConnectWiFi(); + if (!m_features.wifi.Connect()) { + return; + } + SyncNTPTime(); - DisconnectWiFi(); - m_features.rtc.Resync(); + m_features.wifi.Disconnect(); if (m_inMenu) { m_inMenu = false; m_menu.Reset(); DrawWatchFace(false); } + + m_features.rtc.Resync(); } void WatchFace::MenuTimeZoneSelected(int tzOffset) diff --git a/src/WatchFeatures/WatchFeatures.h b/src/WatchFeatures/WatchFeatures.h index e5049ed..e14b5c6 100644 --- a/src/WatchFeatures/WatchFeatures.h +++ b/src/WatchFeatures/WatchFeatures.h @@ -4,6 +4,7 @@ #include "StepCounter.h" #include "RTC.h" #include "Storage.h" +#include "Wifi.h" namespace WatchFeatures { @@ -21,4 +22,5 @@ struct WatchFeatures::WatchFeatures StepCounter stepCounter; RTC rtc; Storage storage; + Wifi wifi; }; \ No newline at end of file diff --git a/src/WatchFeatures/Wifi.cpp b/src/WatchFeatures/Wifi.cpp new file mode 100644 index 0000000..8eff1e0 --- /dev/null +++ b/src/WatchFeatures/Wifi.cpp @@ -0,0 +1,28 @@ +#include +#include "Wifi.h" +#include "config.h" + +bool WatchFeatures::Wifi::Connect() +{ + if (WiFi.begin(WIFI_SSID, WIFI_PASS) == WL_CONNECT_FAILED) { + WiFi.mode(WIFI_OFF); + return false; + } + + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + WiFi.mode(WIFI_OFF); + return false; + } + + return true; +} + +void WatchFeatures::Wifi::Disconnect() +{ + +} + +bool WatchFeatures::Wifi::IsConnected() +{ + return true; +} \ No newline at end of file diff --git a/src/WatchFeatures/Wifi.h b/src/WatchFeatures/Wifi.h new file mode 100644 index 0000000..f173389 --- /dev/null +++ b/src/WatchFeatures/Wifi.h @@ -0,0 +1,12 @@ +#pragma once + +namespace WatchFeatures +{ + class Wifi + { + public: + bool Connect(); + void Disconnect(); + bool IsConnected(); + }; +} \ No newline at end of file diff --git a/src/Watchy.cpp b/src/Watchy.cpp index 7e49b8b..8c3796b 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -112,21 +112,6 @@ void Watchy::VibeMotor(uint8_t intervalMs, uint8_t length) } } -void Watchy::ConnectWiFi() -{ - if(WiFi.begin(WIFI_SSID, WIFI_PASS) == WL_CONNECT_FAILED) { - Serial.begin(9600); - Serial.println("Failed to connect to WiFi"); - return; - } - - if(WiFi.waitForConnectResult() != WL_CONNECTED) { - Serial.begin(9600); - Serial.println("Failed to connect to WiFi"); - return; - } -} - void Watchy::SyncNTPTime() { WiFiUDP ntpUDP; @@ -144,11 +129,6 @@ void Watchy::SyncNTPTime() } } -void Watchy::DisconnectWiFi() -{ - WiFi.mode(WIFI_OFF); -} - void Watchy::ShowWatchFace(bool partialRefresh) { DrawWatchFace(partialRefresh);