forked from wizche/ttgo-smb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mario.ino
349 lines (308 loc) · 10.2 KB
/
mario.ino
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "config.h"
#include "src/gui.h"
#include <WiFi.h>
#include "src/secret.h"
#include "time.h"
#define DEFAULT_SCREEN_TIMEOUT 10 * 1000
enum
{
Q_EVENT_BMA_INT,
Q_EVENT_AXP_INT,
};
TTGOClass *ttgo = TTGOClass::getWatch();
QueueHandle_t g_event_queue_handle = NULL;
EventGroupHandle_t g_event_group = NULL;
EventGroupHandle_t isr_group = NULL;
bool lenergy = false;
bool tryNTPtime = true;
bool useNTPtime = false;
// Wifi variables
// The credetials are stored in src/secret.h file that doesnt need to be synched with the repo. The followin format is used:
// #define SSID "MY_SSID"
// #define PASSWORD "MY_PASSWORD"
// TODO: store this infos in a crypted way?
const char* ssid = SSID;
const char* password = PASSWORD;
// NTP Settings : for more ionformations, https://lastminuteengineers.com/esp32-ntp-server-date-time-tutorial/
const char* ntpServer = "pool.ntp.org"; // (for worlwide NTP server)
// const char* ntpServer = "europe.pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
Gui *gui;
void initWakeupTriggers()
{
AXP20X_Class *power = ttgo->power;
BMA *sensor = ttgo->bma;
Acfg cfg;
cfg.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
cfg.range = BMA4_ACCEL_RANGE_2G;
cfg.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
cfg.perf_mode = BMA4_CONTINUOUS_MODE;
// Configure the BMA423 accelerometer
sensor->accelConfig(cfg);
// Enable BMA423 accelerometer
sensor->enableAccel();
// Disable BMA423 isStepCounter feature
sensor->enableFeature(BMA423_STEP_CNTR, false);
// Enable BMA423 isTilt feature
sensor->enableFeature(BMA423_TILT, true);
// Enable BMA423 isDoubleClick feature
sensor->enableFeature(BMA423_WAKEUP, true);
// Reset steps
sensor->resetStepCounter();
// Turn off feature interrupt
// sensor->enableStepCountInterrupt();
sensor->enableTiltInterrupt();
// It corresponds to isDoubleClick interrupt
sensor->enableWakeupInterrupt();
}
void low_energy()
{
if (ttgo->bl->isOn())
{
xEventGroupSetBits(isr_group, WATCH_FLAG_SLEEP_MODE);
ttgo->closeBL();
ttgo->stopLvglTick();
ttgo->bma->enableStepCountInterrupt(false);
ttgo->displaySleep();
lenergy = true;
// rtc_clk_cpu_freq_set(RTC_CPU_FREQ_2M);
setCpuFrequencyMhz(20);
Serial.println("ENTER IN LIGHT SLEEEP MODE");
gpio_wakeup_enable((gpio_num_t)AXP202_INT, GPIO_INTR_LOW_LEVEL);
gpio_wakeup_enable((gpio_num_t)BMA423_INT1, GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_gpio_wakeup();
esp_light_sleep_start();
}
else
{
ttgo->startLvglTick();
ttgo->displayWakeup();
ttgo->rtc->syncToSystem();
lv_disp_trig_activity(NULL);
gui->updateTime();
gui->updateBatteryLevel();
gui->updateStepCounter(ttgo->bma->getCounter());
gui->updateWakeupCount();
gui->updateDate();
ttgo->openBL();
ttgo->bma->enableStepCountInterrupt();
}
}
void synchRtc2Ntp()
{
//connect to WiFi
Serial.printf("Connecting to %s \n", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.printf("\nConnected to %s \n", ssid);
//init and get the time from NTP server
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
struct tm timeinfo;
if(!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
// RTC_Date updateRTC =(timeinfo.tm_year,timeinfo.tm_mon,timeinfo.tm_mday,timeinfo.tm_hour,timeinfo.tm_min,timeinfo.tm_sec);
// ttgo->rtc->setDateTime(updateRTC);
RTC_Date updateRTC ;
updateRTC.year = timeinfo.tm_year;
updateRTC.month = timeinfo.tm_mon;
updateRTC.day = timeinfo.tm_mday;
updateRTC.hour = timeinfo.tm_hour;
updateRTC.minute = timeinfo.tm_min;
updateRTC.second = timeinfo.tm_sec;
ttgo->rtc->setDateTime(updateRTC);
// ttgo->rtc->syncToRtc();
Serial.println("RTC time synched with NTP");
useNTPtime = true;
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void setup()
{
Serial.begin(115200);
Serial.println("Woked-up!");
// ttgo = TTGOClass::getWatch();
//initWakeupTriggers();
//Create a program that allows the required message objects and group flags
g_event_queue_handle = xQueueCreate(20, sizeof(uint8_t));
g_event_group = xEventGroupCreate();
isr_group = xEventGroupCreate();
//Initialize TWatch
ttgo->begin();
ttgo->openBL();
// Turn on the IRQ used
ttgo->power->adc1Enable(AXP202_BATT_VOL_ADC1 | AXP202_BATT_CUR_ADC1 | AXP202_VBUS_VOL_ADC1 | AXP202_VBUS_CUR_ADC1, AXP202_ON);
ttgo->power->enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_FINISHED_IRQ, AXP202_ON);
ttgo->power->clearIRQ();
// Turn off unused power
ttgo->power->setPowerOutPut(AXP202_EXTEN, AXP202_OFF);
ttgo->power->setPowerOutPut(AXP202_DCDC2, AXP202_OFF);
ttgo->power->setPowerOutPut(AXP202_LDO3, AXP202_OFF);
ttgo->power->setPowerOutPut(AXP202_LDO4, AXP202_OFF);
// Enable BMA423 interrupt ,
// The default interrupt configuration,
// you need to set the acceleration parameters, please refer to the BMA423_Accel example
ttgo->bma->attachInterrupt();
//Connection interrupted to the specified pin
pinMode(BMA423_INT1, INPUT);
attachInterrupt(
BMA423_INT1, [] {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
EventBits_t bits = xEventGroupGetBitsFromISR(isr_group);
if (bits & WATCH_FLAG_SLEEP_MODE)
{
//! For quick wake up, use the group flag
xEventGroupSetBitsFromISR(isr_group, WATCH_FLAG_SLEEP_EXIT | WATCH_FLAG_BMA_IRQ, &xHigherPriorityTaskWoken);
}
else
{
uint8_t data = Q_EVENT_BMA_INT;
xQueueSendFromISR(g_event_queue_handle, &data, &xHigherPriorityTaskWoken);
}
if (xHigherPriorityTaskWoken)
{
portYIELD_FROM_ISR();
}
},
RISING);
// Connection interrupted to the specified pin
pinMode(AXP202_INT, INPUT);
attachInterrupt(
AXP202_INT, [] {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
EventBits_t bits = xEventGroupGetBitsFromISR(isr_group);
if (bits & WATCH_FLAG_SLEEP_MODE)
{
//! For quick wake up, use the group flag
xEventGroupSetBitsFromISR(isr_group, WATCH_FLAG_SLEEP_EXIT | WATCH_FLAG_AXP_IRQ, &xHigherPriorityTaskWoken);
}
else
{
uint8_t data = Q_EVENT_AXP_INT;
xQueueSendFromISR(g_event_queue_handle, &data, &xHigherPriorityTaskWoken);
}
if (xHigherPriorityTaskWoken)
{
portYIELD_FROM_ISR();
}
},
FALLING);
ttgo->lvgl_begin();
//Check if the RTC clock matches, if not, use compile time
ttgo->rtc->check();
Serial.printf("RTC time: %s\n", ttgo->rtc->formatDateTime());
if (tryNTPtime == false)
{
//Synchronize time to system time
ttgo->rtc->syncToSystem();
Serial.printf("System time: %s\n", ttgo->rtc->formatDateTime());
useNTPtime = false;
} else {
//Synchronize time to NTP
synchRtc2Ntp();
}
gui = new Gui();
gui->setupGui();
}
void loop()
{
bool rlst;
uint8_t data;
//! Fast response wake-up interrupt
EventBits_t bits = xEventGroupGetBits(isr_group);
if (bits & WATCH_FLAG_SLEEP_EXIT)
{
if (lenergy)
{
lenergy = false;
// rtc_clk_cpu_freq_set(RTC_CPU_FREQ_160M);
setCpuFrequencyMhz(160);
}
low_energy();
if (bits & WATCH_FLAG_BMA_IRQ)
{
do
{
rlst = ttgo->bma->readInterrupt();
} while (!rlst);
xEventGroupClearBits(isr_group, WATCH_FLAG_BMA_IRQ);
}
if (bits & WATCH_FLAG_AXP_IRQ)
{
ttgo->power->readIRQ();
ttgo->power->clearIRQ();
//TODO: Only accept axp power pek key short press
xEventGroupClearBits(isr_group, WATCH_FLAG_AXP_IRQ);
}
xEventGroupClearBits(isr_group, WATCH_FLAG_SLEEP_EXIT);
xEventGroupClearBits(isr_group, WATCH_FLAG_SLEEP_MODE);
}
if ((bits & WATCH_FLAG_SLEEP_MODE))
{
//! No event processing after entering the information screen
return;
}
//! Normal polling
if (xQueueReceive(g_event_queue_handle, &data, 5 / portTICK_RATE_MS) == pdPASS)
{
switch (data)
{
case Q_EVENT_BMA_INT:
do
{
rlst = ttgo->bma->readInterrupt();
} while (!rlst);
//! setp counter
if (ttgo->bma->isStepCounter())
{
Serial.printf("Stepcounter: %d\n", ttgo->bma->getCounter());
gui->updateStepCounter(ttgo->bma->getCounter());
}
break;
case Q_EVENT_AXP_INT:
ttgo->power->readIRQ();
if (ttgo->power->isVbusPlugInIRQ())
{
Serial.println("Charging");
}
if (ttgo->power->isVbusRemoveIRQ())
{
//updateBatteryIcon(LV_ICON_CALCULATION);
Serial.println("Finished charging");
}
if (ttgo->power->isChargingDoneIRQ())
{
Serial.println("Full charged");
}
if (ttgo->power->isPEKShortPressIRQ())
{
Serial.println("PEK Short press");
Serial.printf("Current time: %s\n", ttgo->rtc->formatDateTime());
ttgo->power->clearIRQ();
low_energy();
return;
}
ttgo->power->clearIRQ();
break;
default:
break;
}
}
if (ttgo->power->isVBUSPlug() || lv_disp_get_inactive_time(NULL) < DEFAULT_SCREEN_TIMEOUT)
{
lv_task_handler();
}
else
{
low_energy();
}
}