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();
m_menu.Reset();
ConnectWiFi();
if(m_features.wifi.Connect()) {
SyncNTPTime();
DisconnectWiFi();
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)

View file

@ -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;
};

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