This repository has been archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lifecycle.cpp
181 lines (139 loc) · 4.24 KB
/
Lifecycle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Lifecycle.h"
#include <driver/rtc_io.h>
#include "Constants.h"
#include "RecordStorage.h"
#include "SpreadSheetGateway.h"
#include "Logger.h"
void Lifecycle::enableRTCGPIOHold()
{
rtc_gpio_hold_en(gpio_num_t(Constants::Pins::EPAPER_RST));
rtc_gpio_hold_en(gpio_num_t(Constants::Pins::CCS811_HW_RESET));
rtc_gpio_hold_en(gpio_num_t(Constants::Pins::CCS811_WAKE));
rtc_gpio_hold_en(gpio_num_t(Constants::Pins::EPAPER_KEY4));
}
void Lifecycle::disableRTCGPIOHold()
{
rtc_gpio_hold_dis(gpio_num_t(Constants::Pins::EPAPER_RST));
rtc_gpio_hold_dis(gpio_num_t(Constants::Pins::CCS811_HW_RESET));
rtc_gpio_hold_dis(gpio_num_t(Constants::Pins::CCS811_WAKE));
rtc_gpio_hold_dis(gpio_num_t(Constants::Pins::EPAPER_KEY4));
}
void Lifecycle::sync()
{
const auto recordExists = RecordStorage::NumberOfRecords() > 0;
if (!recordExists)
Println("No record found. Skipping some processes...");
if (recordExists)
{
Println("Drawing sync page...");
ePaper.drawSyncPage();
}
Println("Connecting to the Internet...");
if (!WiFiManager::connect(10000)) {
Println("Failed to connect to the Internet.");
return;
}
syncClockWithNTP();
Record latestRecord;
if (recordExists)
{
Println("Loading latest record...");
latestRecord = RecordStorage::ReadLatestRecord();
postRecordsOnStorage();
}
Println("Disconnecting from Wi-Fi...");
WiFiManager::disconnect();
if (recordExists)
{
Println("Drawing status page...");
ePaper.drawStatusPage(latestRecord.dateTime, latestRecord.value, timeProvider.isRTCBatteryLow());
}
}
void Lifecycle::syncClockWithNTP()
{
Println("Syncing with NTP...");
timeProvider.syncWithNTP();
}
void Lifecycle::postRecordsOnStorage()
{
Println("Sending records...");
const auto postSucceeded = SpreadSheetGateway::PostRecords();
if (postSucceeded)
{
Println("Post succeeded.");
RecordStorage::RemoveAllRecords();
}
else
{
Println("Post failed.");
}
}
void Lifecycle::startDeepSleep()
{
Println("Disconnecting from Wi-Fi...");
WiFiManager::disconnect();
Println("Putting e-Paper to deep-sleep...");
ePaper.sleep();
enableRTCGPIOHold();
const auto syncKey = gpio_num_t(Constants::Pins::EPAPER_KEY4);
rtc_gpio_init(syncKey);
rtc_gpio_set_direction(syncKey, RTC_GPIO_MODE_INPUT_ONLY);
rtc_gpio_pullup_en(syncKey);
rtc_gpio_pulldown_dis(syncKey);
esp_sleep_enable_ext0_wakeup(syncKey, LOW);
const auto now = timeProvider.fetchRTCDateTime();
auto latestRecordTime = RecordStorage::GetLatestRecordTime();
auto sleepUntil = latestRecordTime + TimeSpan(60);
const auto sleepFor = (sleepUntil - now).totalseconds();
delay(100);
esp_sleep_enable_timer_wakeup(sleepFor * 1000 * 1000);
esp_deep_sleep_start();
}
void Lifecycle::handleSetup()
{
Serial.begin(115200);
RecordStorage::Begin();
disableRTCGPIOHold();
Wire.begin(Constants::Pins::SDA, Constants::Pins::SCL);
Println("Initializing e-Paper...");
ePaper.begin();
const auto wakeupCause = esp_sleep_get_wakeup_cause();
if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED)
{
Println("Cold boot.");
Println("Drawing boot page...");
ePaper.drawBootPage();
Println("Initializing sensors...");
sensors.begin();
}
else
{
Println("Wake up from deep-sleep.");
sensors.beginWithoutHardwareInit();
}
if (wakeupCause == ESP_SLEEP_WAKEUP_EXT0 || !timeProvider.isRTCRunning())
{
Println("Syncing...");
sync();
}
Println("Fetching time from RTC...");
auto currentRecordTime = timeProvider.fetchRTCDateTime();
const auto latestRecordTime = RecordStorage::GetLatestRecordTime();
const auto elapsedSecondsFromLatestRecord = (currentRecordTime - latestRecordTime).totalseconds();
if (elapsedSecondsFromLatestRecord < 60)
{
Println("Going into deep-sleep...");
startDeepSleep();
return;
}
Println("Checking RTC's battery status...");
const auto isBatteryLow = timeProvider.isRTCBatteryLow();
Println("Measuring...");
const auto value = sensors.measure();
Println("Saving record on storage...");
RecordStorage::SaveRecord({ currentRecordTime, value });
Println("Drawing status page...");
ePaper.drawStatusPage(currentRecordTime, value, isBatteryLow);
Println("Going into deep-sleep...");
startDeepSleep();
}