EmlaLockSafe
WifiConnectingView.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "../Tools.h"
8#include "../configuration/Configuration.h"
9
10#include <MenuView.h>
11#include <ViewBase.h>
12#include <WiFi.h>
13#include <time.h>
14
15namespace views {
19class WifiConnectingView : public lcd::ViewBase {
20public:
26 WifiConnectingView(LiquidCrystal_PCF8574* display)
27 : lcd::ViewBase(display, "WifiConnectingView") {}
28
29public:
33 WifiConnectingView(const WifiConnectingView& other) = delete;
34
35public:
39 WifiConnectingView(WifiConnectingView&& other) noexcept = delete;
40
41protected:
45 virtual void activate() {
46 display->clear();
47 display->setCursor(0, 0);
48 display->print("Connecting to wifi:");
49
50 display->setCursor(0, 1);
52
53 // Start connecting
54 WiFi.disconnect();
55 WiFi.setHostname("EmlalockSafe");
56 WiFi.mode(WIFI_STA);
57 WiFi.begin(configuration::Configuration::getSingleton().getSsid().c_str(),
59 WiFi.setAutoReconnect(true);
60
61 // Play an animation while the connection is established
62 int counter = 0;
63 Tools::waitAnimation(display, [&counter]() -> bool {
64 counter++;
65 if (counter % 25 == 0) {
66 // if we've waited for over 2.5s (25 loops), restart wifi....
67 // this is required because of an bug in the ESP32 arduino core.
68 WiFi.disconnect();
69 WiFi.setHostname("EmlalockSafe");
70 WiFi.mode(WIFI_STA);
71 WiFi.begin(configuration::Configuration::getSingleton().getSsid().c_str(),
73 WiFi.setAutoReconnect(true);
74 }
75 return WiFi.status() != WL_CONNECTED;
76 });
77
78 display->clear();
79 display->setCursor(0, 0);
80 display->print("Setting Timezone:");
81 // configure NTP
82 configTzTime(configuration::Configuration::getSingleton().getTimezone().c_str(),
83 "pool.ntp.org",
84 "time.nist.gov",
85 "time.google.com");
86
87 // go back to previous view
88 activatePreviousView();
89 }
90
91public:
97 virtual void tick(const bool& forceRedraw) {
98 // not used since activate blocks and immediately returns to the previous
99 // view
100 }
101}; // namespace views
102} // namespace views
static void waitAnimation(LiquidCrystal_PCF8574 *display, std::function< bool()> stillActive)
Shows the animation while waiting until the callback returns false.
Definition: Tools.h:69
static Configuration & getSingleton()
Get the Singleton object.
Definition: Configuration.h:162
View displayed while connection to the access point.
Definition: WifiConnectingView.h:19
virtual void tick(const bool &forceRedraw)
called during the loop function
Definition: WifiConnectingView.h:97
WifiConnectingView(LiquidCrystal_PCF8574 *display)
Construct the view object.
Definition: WifiConnectingView.h:26
WifiConnectingView(WifiConnectingView &&other) noexcept=delete
Move constructor - not available.
WifiConnectingView(const WifiConnectingView &other)=delete
Copy constructor - not available.
virtual void activate()
called as soon as the view becomes active
Definition: WifiConnectingView.h:45
views::ConfigurationServerView configurationServerView & display
Definition: main.cpp:57
Definition: ConfigurationServerView.h:10