-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleWatch.ino
319 lines (267 loc) · 9.21 KB
/
SimpleWatch.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
// Please select the model you want to use in config.h
#include "config.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"
#include <soc/rtc.h>
#include "esp_wifi.h"
#include "esp_sleep.h"
#include <driver/rtc_io.h>
#include <WiFi.h>
#include "gui.h"
#define G_EVENT_VBUS_PLUGIN _BV(0)
#define G_EVENT_VBUS_REMOVE _BV(1)
#define G_EVENT_CHARGE_DONE _BV(2)
#define G_EVENT_WIFI_SCAN_START _BV(3)
#define G_EVENT_WIFI_SCAN_DONE _BV(4)
#define G_EVENT_WIFI_CONNECTED _BV(5)
#define G_EVENT_WIFI_BEGIN _BV(6)
#define G_EVENT_WIFI_OFF _BV(7)
enum {
Q_EVENT_WIFI_SCAN_DONE,
Q_EVENT_WIFI_CONNECT,
Q_EVENT_BMA_INT,
Q_EVENT_AXP_INT,
} ;
#define DEFAULT_SCREEN_TIMEOUT 3*1000
#define WATCH_FLAG_SLEEP_MODE _BV(1)
#define WATCH_FLAG_SLEEP_EXIT _BV(2)
#define WATCH_FLAG_BMA_IRQ _BV(3)
#define WATCH_FLAG_AXP_IRQ _BV(4)
QueueHandle_t g_event_queue_handle = NULL;
EventGroupHandle_t g_event_group = NULL;
EventGroupHandle_t isr_group = NULL;
bool lenergy = false;
TTGOClass *ttgo;
void setupNetwork()
{
WiFi.mode(WIFI_STA);
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
xEventGroupClearBits(g_event_group, G_EVENT_WIFI_CONNECTED);
}, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
uint8_t data = Q_EVENT_WIFI_SCAN_DONE;
xQueueSend(g_event_queue_handle, &data, portMAX_DELAY);
}, WiFiEvent_t::SYSTEM_EVENT_SCAN_DONE);
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
xEventGroupSetBits(g_event_group, G_EVENT_WIFI_CONNECTED);
}, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED);
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
wifi_connect_status(true);
}, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
}
void low_energy()
{
if (ttgo->bl->isOn()) {
xEventGroupSetBits(isr_group, WATCH_FLAG_SLEEP_MODE);
ttgo->closeBL();
ttgo->stopLvglTick();
ttgo->bma->enableStepCountInterrupt(false);
ttgo->displaySleep();
if (!WiFi.isConnected()) {
lenergy = true;
WiFi.mode(WIFI_OFF);
// 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 {
//esp_default_wake_deep_sleep();
ttgo->startLvglTick();
ttgo->displayWakeup();
ttgo->rtc->syncToSystem();
updateStepCounter(ttgo->bma->getCounter());
updateBatteryLevel();
updateBatteryIcon(LV_ICON_CALCULATION);
lv_disp_trig_activity(NULL);
ttgo->openBL();
ttgo->bma->enableStepCountInterrupt();
}
}
void setup()
{
Serial.begin(115200);
//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();
ttgo = TTGOClass::getWatch();
//Initialize TWatch
ttgo->begin();
// 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);
//Initialize lvgl
ttgo->lvgl_begin();
//Initialize bma423
ttgo->bma->begin();
//Enable BMA423 interrupt
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);
//Check if the RTC clock matches, if not, use compile time
ttgo->rtc->check();
//Synchronize time to system time
ttgo->rtc->syncToSystem();
#ifdef LILYGO_WATCH_HAS_BUTTON
/*
ttgo->button->setClickHandler([]() {
Serial.println("Button2 Pressed");
});
*/
//Set the user button long press to restart
ttgo->button->setLongClickHandler([]() {
Serial.println("Pressed Restart Button,Restart now ...");
delay(1000);
esp_restart();
});
#endif
//Setting up the network
setupNetwork();
//Execute your own GUI interface
setupGui();
//Clear lvgl counter
lv_disp_trig_activity(NULL);
#ifdef LILYGO_WATCH_HAS_BUTTON
//In lvgl we call the button processing regularly
lv_task_create([](lv_task_t *args) {
ttgo->button->loop();
}, 30, 1, nullptr);
#endif
//When the initialization is complete, turn on the backlight
ttgo->openBL();
}
void loop()
{
bool rlst;
uint8_t data;
static uint8_t CurrentBLvalue;
if(CurrentBLvalue != g_blValue)
{
// Set BackLight to 5 improving battery perdormance
ttgo->setBrightness(g_blValue);
// Change current BL value
CurrentBLvalue = g_blValue;
}
//! 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()) {
updateStepCounter(ttgo->bma->getCounter());
}
break;
case Q_EVENT_AXP_INT:
ttgo->power->readIRQ();
if (ttgo->power->isVbusPlugInIRQ()) {
updateBatteryIcon(LV_ICON_CHARGE);
}
if (ttgo->power->isVbusRemoveIRQ()) {
updateBatteryIcon(LV_ICON_CALCULATION);
}
if (ttgo->power->isChargingDoneIRQ()) {
updateBatteryIcon(LV_ICON_CALCULATION);
}
if (ttgo->power->isPEKShortPressIRQ()) {
ttgo->power->clearIRQ();
low_energy();
return;
}
ttgo->power->clearIRQ();
break;
case Q_EVENT_WIFI_SCAN_DONE: {
int16_t len = WiFi.scanComplete();
for (int i = 0; i < len; ++i) {
wifi_list_add(WiFi.SSID(i).c_str());
}
break;
}
default:
break;
}
}
if (lv_disp_get_inactive_time(NULL) < DEFAULT_SCREEN_TIMEOUT) {
lv_task_handler();
} else {
low_energy();
}
}