Move wifi code into a watch feature
All checks were successful
Compile / Compile (push) Successful in 1m25s

This commit is contained in:
Lewis Jackson 2023-06-01 17:17:57 +03:00
parent 93602ebb77
commit 07e83e7557
5 changed files with 53 additions and 26 deletions

View file

@ -13,9 +13,10 @@ void WatchFace::InitBoot()
SetupVolatileMenuStuff(); SetupVolatileMenuStuff();
m_menu.Reset(); m_menu.Reset();
ConnectWiFi(); if(m_features.wifi.Connect()) {
SyncNTPTime(); SyncNTPTime();
DisconnectWiFi(); m_features.wifi.Disconnect();
}
for (auto & page : m_pages) { for (auto & page : m_pages) {
page->InitBoot(); page->InitBoot();
@ -235,16 +236,20 @@ void WatchFace::MenuExited()
void WatchFace::MenuNTPSyncSelected() void WatchFace::MenuNTPSyncSelected()
{ {
ConnectWiFi(); if (!m_features.wifi.Connect()) {
return;
}
SyncNTPTime(); SyncNTPTime();
DisconnectWiFi(); m_features.wifi.Disconnect();
m_features.rtc.Resync();
if (m_inMenu) { if (m_inMenu) {
m_inMenu = false; m_inMenu = false;
m_menu.Reset(); m_menu.Reset();
DrawWatchFace(false); DrawWatchFace(false);
} }
m_features.rtc.Resync();
} }
void WatchFace::MenuTimeZoneSelected(int tzOffset) void WatchFace::MenuTimeZoneSelected(int tzOffset)

View file

@ -4,6 +4,7 @@
#include "StepCounter.h" #include "StepCounter.h"
#include "RTC.h" #include "RTC.h"
#include "Storage.h" #include "Storage.h"
#include "Wifi.h"
namespace WatchFeatures namespace WatchFeatures
{ {
@ -21,4 +22,5 @@ struct WatchFeatures::WatchFeatures
StepCounter stepCounter; StepCounter stepCounter;
RTC rtc; RTC rtc;
Storage storage; Storage storage;
Wifi wifi;
}; };

View file

@ -0,0 +1,28 @@
#include <WiFi.h>
#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;
}

12
src/WatchFeatures/Wifi.h Normal file
View file

@ -0,0 +1,12 @@
#pragma once
namespace WatchFeatures
{
class Wifi
{
public:
bool Connect();
void Disconnect();
bool IsConnected();
};
}

View file

@ -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() void Watchy::SyncNTPTime()
{ {
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
@ -144,11 +129,6 @@ void Watchy::SyncNTPTime()
} }
} }
void Watchy::DisconnectWiFi()
{
WiFi.mode(WIFI_OFF);
}
void Watchy::ShowWatchFace(bool partialRefresh) void Watchy::ShowWatchFace(bool partialRefresh)
{ {
DrawWatchFace(partialRefresh); DrawWatchFace(partialRefresh);