EmlaLockSafe
LockedView.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "../Tools.h"
8#include "../emlalock/EmlaLockApi.h"
9
10#include <RotaryEncoder.h>
11#include <ViewBase.h>
12
13namespace views {
17class LockedView : public lcd::ViewBase {
18protected:
22 RotaryEncoder* encoder;
23
24protected:
29
30public:
37 LockedView(LiquidCrystal_PCF8574* display, RotaryEncoder* encoder)
38 : lcd::ViewBase(display, "LockedView")
39 , encoder(encoder) {
40 temperatureStrings[0] = "very cold";
41 temperatureStrings[1] = "cold";
42 temperatureStrings[2] = "warm";
43 temperatureStrings[3] = "very warm";
44 temperatureStrings[4] = "hot";
45 temperatureStrings[5] = "very hot";
46 }
47
48public:
52 LockedView(const LockedView& other) = delete;
53
54public:
58 LockedView(LockedView&& other) noexcept = delete;
59
60protected:
64 virtual void activate() {
65 initializeSpecialCharacters();
66
67 tick(true);
68 }
69
70public:
76 virtual void tick(const bool& forceRedraw) {
77 auto direction = encoder->getDirection();
78 auto click = encoder->getNewClick();
79 char buf[21];
80 tm tmBuf;
81
82 if (click || (direction != RotaryEncoder::Direction::NOROTATION)) {
83 if (!getBacklightTimeoutManager().delayTimeout()) {
84 return;
85 }
86 }
87 getBacklightTimeoutManager().tick(display);
88
89 // click reloads the data from emlalock
90 if (click) {
92 }
93
94 // just to be sure...
95 digitalWrite(COIL_PIN, SAFE_COIL_LOCKED);
96
97 // Stuff which never changes
98 if (forceRedraw) {
99 display->clear();
100 display->setCursor(0, 0);
101 display->print("Safe Locked");
102
103 display->setCursor(0, 1);
104 display->print("Passed: ***:**:**:**");
105 display->setCursor(0, 2);
106 display->print("Left: ***:**:**:**");
107
109 strftime(buf, 21, " %d.%m.%Y %T", localtime_r(&LockState::getEndDate(), &tmBuf));
110 display->setCursor(0, 3);
111 display->print(buf);
112 }
113 }
114
115 // current time
116 static time_t lastDisplayedCurrentTime = 0;
117 time_t currentTime = time(NULL);
118 if (forceRedraw || (lastDisplayedCurrentTime / 60 != currentTime / 60)) {
119 lastDisplayedCurrentTime = currentTime;
120 strftime(buf, 6, "%R", localtime_r(&currentTime, &tmBuf));
121 display->setCursor(13, 0);
122 display->print(buf);
123 }
124
125 // draw wifi signal strength
126 Tools::tickWifiSymbol(display, forceRedraw);
127
128 // should the passed time be displayed?
130 static time_t lastDisplayedTime = 0;
131 time_t timePassed = time(NULL) - LockState::getStartDate();
132
133 // redraw required
134 if (forceRedraw || (lastDisplayedTime != timePassed)) {
135 lastDisplayedTime = timePassed;
136
137 int sec = timePassed % 60;
138 timePassed = timePassed / 60;
139 int min = timePassed % 60;
140 timePassed = timePassed / 60;
141 int hour = timePassed % 24;
142 int day = std::min((int)timePassed / 24, 999);
143
144 sprintf(buf, "Passed: %03d:%02d:%02d:%02d", day, hour, min, sec);
145 display->setCursor(0, 1);
146 display->print(buf);
147 }
148 }
149
150 // should the time left be displayed? If yes, how
153 static time_t lastDisplayedTime = 0;
154 time_t timeLeft = LockState::getEndDate() - time(NULL);
155
156 // redraw required?
157 if (forceRedraw || (lastDisplayedTime != timeLeft)) {
158 lastDisplayedTime = timeLeft;
159
160 int sec = timeLeft % 60;
161 timeLeft = timeLeft / 60;
162 int min = timeLeft % 60;
163 timeLeft = timeLeft / 60;
164 int hour = timeLeft % 24;
165 int day = std::min((int)timeLeft / 24, 999);
166
167 sprintf(buf, "Left: %03d:%02d:%02d:%02d", day, hour, min, sec);
168 display->setCursor(0, 2);
169 display->print(buf);
170 }
171
172 break;
173 }
175 static String lastString = "";
177
178 // The string does not come from the Emlalock API. Just generate it
179 if (s.isEmpty()) {
180 int idx = std::min(
181 (int)(((time(NULL) - LockState::getStartDate()) * 6) / (LockState::getEndDate() - LockState::getStartDate())),
182 5);
183
184 s = temperatureStrings[idx];
185 }
186
187 // redraw required?
188 if (forceRedraw || (s != lastString)) {
189 display->setCursor(0, 2);
190 display->print("Left: ");
191 display->setCursor(6, 2);
192 display->print(s);
193 lastString = s;
194 }
195 break;
196 }
197 default:
198 break;
199 }
200 }
201};
202} // namespace views
#define COIL_PIN
GPIO the coil of the safe is connected to.
Definition: HardwareConfiguration.h:10
#define SAFE_COIL_LOCKED
Value of the COIL_PIN if the coil should be locked.
Definition: HardwareConfiguration.h:25
static const time_t & getEndDate()
Get the End Date.
Definition: LockState.h:325
static const DisplayTimePassed & getDisplayTimePassed()
Get how the time passed should be displayed.
Definition: LockState.h:256
static const String & getTemperatureString()
Get the temperature string.
Definition: LockState.h:371
static const time_t & getStartDate()
Get the Start Date.
Definition: LockState.h:302
static const DisplayTimeLeft & getDisplayTimeLeft()
Get how the time left should be displayed.
Definition: LockState.h:279
static void tickWifiSymbol(LiquidCrystal_PCF8574 *display, bool forceRedraw)
tick routine updating the wifi signal strength icon
Definition: Tools.h:23
void triggerRefresh()
Triggers the client to reload the current state from the EmlaLock server.
Definition: EmlaLockApi.h:105
static EmlaLockApi & getSingleton(bool offlineMode=false)
Get the singleton instance of the API handler.
Definition: EmlaLockApi.h:71
View used when the safe is locked.
Definition: LockedView.h:17
virtual void tick(const bool &forceRedraw)
called during the loop function
Definition: LockedView.h:76
RotaryEncoder * encoder
pointer to the encoder instance
Definition: LockedView.h:22
virtual void activate()
called as soon as the view becomes active
Definition: LockedView.h:64
LockedView(LockedView &&other) noexcept=delete
Move constructor.
LockedView(const LockedView &other)=delete
Copy constructor - not available.
String temperatureStrings[6]
temperature strings if they are not provided by the emlalock API
Definition: LockedView.h:28
LockedView(LiquidCrystal_PCF8574 *display, RotaryEncoder *encoder)
Construct the locked view.
Definition: LockedView.h:37
views::ConfigurationServerView configurationServerView & display
Definition: main.cpp:57
Definition: ConfigurationServerView.h:10