forked from Xinyuan-LilyGO/TTGO-T-Watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTGO-T-Watch-mod.ino
562 lines (496 loc) · 16.4 KB
/
TTGO-T-Watch-mod.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include "board_def.h"
#include "lv_dirver.h"
#include "lv_filesys.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"
#include "struct_def.h"
#include <WiFi.h>
#include <lvgl.h>
#include "lv_swatch.h"
#include "gps_task.h"
#include "motion_task.h"
#include <Ticker.h>
#include "axp20x.h"
#include <Button2.h>
#include <SPI.h>
#include <pcf8563.h>
#include <soc/rtc.h>
#include "lv_ble.h"
#include "wifi_setting.h"
#include <time.h> // requried for settimeofday
#include <sys/time.h>// requried for timeval
/*********************
* DEFINES
*********************/
#define TASK_QUEUE_MESSAGE_LEN 10
/**********************
*
* STATIC VARIABLES
**********************/
AXP20X_Class axp;
PCF8563_Class rtc;
EventGroupHandle_t g_sync_event_group = NULL;
QueueHandle_t g_event_queue_handle = NULL;
static Ticker *wifiTicker = nullptr;
static Ticker btnTicker;
Button2 btn(USER_BUTTON);
Ticker pwmTicker;
power_data_t data;
static void time_task(void *param);
void wifi_event_setup()
{
WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.disconnected.reason);
}, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.println("Completed scan for access points");
int16_t len = WiFi.scanComplete();
for (int i = 0; i < len; ++i) {
lv_wifi_list_add(WiFi.SSID(i).c_str(), WiFi.RSSI(i), WiFi.channel(i));
}
WiFi.scanDelete();
}, WiFiEvent_t::SYSTEM_EVENT_SCAN_DONE);
eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.println("Connected to access point");
}, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED);
eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.print("Obtained IP address: ");
Serial.println(WiFi.localIP());
task_event_data_t event_data;
event_data.type = MESS_EVENT_WIFI;
event_data.wifi.event = LVGL_WIFI_CONFIG_CONNECT_SUCCESS;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
}, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
}
bool syncRtcBySystemTime()
{
struct tm timeinfo;
bool ret = false;
int retry = 0;
configTzTime("JST-9", "pool.ntp.org");
do {
ret = getLocalTime(&timeinfo);
if (!ret) {
Serial.printf("get ntp fail,retry : %d \n", retry++);
}
} while (!ret && retry < 3);
if (ret) {
char format[256];
Serial.print("NTP Time is : ");
Serial.println(&timeinfo, " %A, %B %d %Y %H:%M:%S");
Serial.print("NTP Time is : ");
snprintf(format, sizeof(format), "%d-%d-%d/%d:%d:%d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
Serial.println(format);
rtc.setDateTime(timeinfo.tm_year, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
RTC_Date d = rtc.getDateTime();
if (d.year != (timeinfo.tm_year + 1900) || d.month != timeinfo.tm_mon + 1 || d.day != timeinfo.tm_mday
|| d.hour != timeinfo.tm_hour ||
d.minute != timeinfo.tm_min) {
Serial.println("Write RTC Fail");
Serial.println(rtc.formatDateTime(PCF_TIMEFORMAT_YYYY_MM_DD_H_M_S));
} else {
Serial.print("Write RTC PASS : ");
Serial.print("Read RTC :");
Serial.println(rtc.formatDateTime(PCF_TIMEFORMAT_YYYY_MM_DD_H_M_S));
}
}
return true;
}
void syncSystemTimeByRtc()
{
Serial.print("Read RTC :");
Serial.println(rtc.formatDateTime(PCF_TIMEFORMAT_YYYY_MM_DD_H_M_S));
struct tm dt;
getLocalTime(&dt);
Serial.printf("getLocalTime is %d:%d:%d\n",dt.tm_hour,dt.tm_min,dt.tm_sec);
RTC_Date d = rtc.getDateTime();
Serial.printf(" %d,%d,%d,%d,%d,%d\n",d.hour,d.minute,d.second,d.day,d.month,d.year);
dt.tm_hour = d.hour;
dt.tm_min = d.minute;
dt.tm_sec = d.second;
dt.tm_mday = d.day;
dt.tm_mon = d.month-1;
dt.tm_year = d.year-1900;
time_t timertc = mktime(&dt);
Serial.print("RTC unixtime is ");
Serial.print(timertc);
Serial.print(" ,system unixtime is ");
time_t timesys = time(NULL);
Serial.println(timesys);
struct timeval tv ={
.tv_sec = timertc
};
settimeofday(&tv,NULL);
getLocalTime(&dt);
Serial.printf("getLocalTime override RTC clock, %d:%d:%d\n",dt.tm_hour,dt.tm_min,dt.tm_sec);
}
void setup()
{
Serial.begin(115200);
g_sync_event_group = xEventGroupCreate();
g_event_queue_handle = xQueueCreate(TASK_QUEUE_MESSAGE_LEN, sizeof(task_event_data_t));
SPI.begin(TFT_SCLK, TFT_MISO, TFT_MOSI, -1);
lv_filesystem_init();
display_init();
Wire1.begin(SEN_SDA, SEN_SCL);
axp.begin(Wire1);
backlight_init();
//BL Power
axp.setPowerOutPut(AXP202_LDO2, AXP202_ON);
lv_create_ttgo();
int level = backlight_getLevel();
for (int level = 0; level < 255; level += 25) {
backlight_adjust(level);
delay(100);
}
axp.enableIRQ(AXP202_ALL_IRQ, AXP202_OFF);
axp.adc1Enable(0xFF, AXP202_OFF);
axp.adc2Enable(0xFF, AXP202_OFF);
axp.adc1Enable(AXP202_BATT_VOL_ADC1 | AXP202_BATT_CUR_ADC1 | AXP202_VBUS_VOL_ADC1 | AXP202_VBUS_CUR_ADC1, AXP202_ON);
axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_FINISHED_IRQ, AXP202_ON);
axp.clearIRQ();
rtc.begin(Wire1);
//syncSystemTimeByRtc();
wifi_event_setup();
WiFi.begin(ssid,password);
syncRtcBySystemTime();
WiFi.disconnect(true);
motion_task_init();
#if defined(UBOX_GPS_MODULE) || defined(ACSIP_S7XG_MODULE)
gps_task_init();
#endif
lv_main();
btn.setLongClickHandler([](Button2 & b) {
task_event_data_t event_data;
event_data.type = MESS_EVENT_POWER;
event_data.power.event = LVGL_POWER_ENTER_SLEEP;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
});
btnTicker.attach_ms(30, [] {
btn.loop();
});
pinMode(AXP202_INT, INPUT_PULLUP);
attachInterrupt(AXP202_INT, [] {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
task_event_data_t event_data;
event_data.type = MESS_EVENT_POWER;
event_data.power.event = LVGL_POWER_IRQ;
xQueueSendFromISR(g_event_queue_handle, &event_data, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken)
{
portYIELD_FROM_ISR ();
}
}, FALLING);
if (axp.isChargeing()) {
charging_anim_start();
}
xTaskCreate(time_task, "time", 2048, NULL, 20, NULL);
#ifdef ENABLE_BLE
ble_init();
#endif
xEventGroupSetBits(g_sync_event_group, BIT0);
}
void wifi_handle(void *data)
{
int16_t len;
wifi_struct_t *p = (wifi_struct_t *)data;
switch (p->event) {
case LVGL_WIFI_CONFIG_SCAN:
Serial.println("[WIFI] WIFI Scan Start ...");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
len = WiFi.scanNetworks();
Serial.printf("[WIFI] WIFI Scan Done ...Scan %d len\n", len);
break;
case LVGL_WIFI_CONFIG_SCAN_DONE:
break;
case LVGL_WIFI_CONFIG_CONNECT_SUCCESS:
Serial.println("Connect Success");
wifiTicker->detach();
delete wifiTicker;
lv_wifi_connect_pass();
task_event_data_t event_data;
event_data.type = MESS_EVENT_TIME;
event_data.time.event = LVGL_TIME_SYNC;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
break;
case LVGL_WIFI_CONFIG_CONNECT_FAIL:
Serial.println("Connect Fail,Power OFF WiFi");
WiFi.disconnect(true);
wifiTicker->detach();
delete wifiTicker;
lv_wifi_connect_fail();
break;
case LVGL_WIFI_CONFIG_TRY_CONNECT: {
Serial.println("LVGL_WIFI_CONFIG_TRY_CONNECT event");
wifi_auth_t *auth = (wifi_auth_t *)p->ctx;
wl_status_t ret = WiFi.begin(auth->ssid, auth->password);
wifiTicker = new Ticker();
wifiTicker->once_ms(10000, [] {
task_event_data_t event_data;
event_data.type = MESS_EVENT_WIFI;
event_data.wifi.event = LVGL_WIFI_CONFIG_CONNECT_FAIL;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
});
}
break;
default:
break;
}
}
void power_handle(void *param)
{
power_struct_t *p = (power_struct_t *)param;
switch (p->event) {
case LVGL_POWER_GET_MOINITOR:
Serial.println("LVGL_POWER_GET_MOINITOR event");
pwmTicker.attach_ms(1000, [] {
data.vbus_vol = axp.getVbusVoltage();
data.vbus_cur = axp.getVbusCurrent();
data.batt_vol = axp.getBattVoltage();
data.batt_cur = axp.isChargeing() ? data.batt_cur = axp.getBattChargeCurrent() : axp.getBattDischargeCurrent();
lv_update_power_info(&data);
});
break;
case LVGL_POWER_MOINITOR_STOP:
Serial.println("LVGL_POWER_MOINITOR_STOP event");
pwmTicker.detach();
break;
case LVGL_POWER_IRQ:
Serial.println("");
Serial.println("LVGL_POWER_IRQ event");
axp.readIRQ();
if (axp.isVbusPlugInIRQ()) {
charging_anim_start();
}
if (axp.isVbusRemoveIRQ()) {
charging_anim_stop();
}
if (axp.isChargingDoneIRQ()) {
charging_anim_stop();
}
if (axp.isPEKShortPressIRQ()) {
if (isBacklightOn()) {
// lv_task_enable(false);
Serial.println("");
Serial.println("PEKShortPressIRQ to off");
backlight_off();
display_sleep();
axp.setPowerOutPut(AXP202_LDO2, AXP202_OFF);
bma423_disable_interrupt();
xEventGroupClearBits(g_sync_event_group, BIT0);
// rtc_clk_cpu_freq_set(RTC_CPU_FREQ_80M);
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_2M);
} else {
Serial.println("PEKShortPressIRQ to on");
rtc_clk_cpu_freq_set(RTC_CPU_FREQ_240M);
// lv_task_enable(true);
bma423_enable_interrupt();
axp.setPowerOutPut(AXP202_LDO2, AXP202_ON);
backlight_on();
display_wakeup();
touch_timer_create();
syncSystemTimeByRtc();
xEventGroupSetBits(g_sync_event_group, BIT0);
}
}
axp.clearIRQ();
break;
case LVGL_POWER_ENTER_SLEEP: {
Serial.println("LVGL_POWER_ENTER_SLEEP event");
lv_create_ttgo();
int level = backlight_getLevel();
for (; level > 0; level -= 25) {
backlight_adjust(level);
delay(100);
}
display_off();
axp.setPowerOutPut(AXP202_LDO2, AXP202_OFF);
esp_sleep_enable_ext1_wakeup( ((uint64_t)(((uint64_t)1) << USER_BUTTON)), ESP_EXT1_WAKEUP_ALL_LOW);
esp_deep_sleep_start();
}
break;
default:
break;
}
}
#define DEFAULT_BATT_POLL 5
void time_handle(void *param)
{
bool rslt;
char time_buf[64];
char date_buf[64];
uint8_t batteryLoop = 0;
time_struct_t *p = (time_struct_t *)param;
switch (p->event) {
{
case LVGL_TIME_UPDATE:
if (lv_main_in()) {
strftime(time_buf, sizeof(time_buf), "%H:%M", &(p->time));
strftime(date_buf, sizeof(date_buf), "%a %d %b ", &(p->time));
lv_main_time_update(time_buf, date_buf);
if (++batteryLoop >= DEFAULT_BATT_POLL) {
batteryLoop = 0;
lv_update_battery_percent(axp.getBattPercentage());
}
}
break;
case LVGL_TIME_ALARM:
break;
case LVGL_TIME_SYNC:
do {
rslt = syncRtcBySystemTime();
} while (!rslt);
break;
default:
break;
}
}
}
void loop()
{
task_event_data_t event_data;
for (;;) {
if (xQueueReceive(g_event_queue_handle, &event_data, portMAX_DELAY) == pdPASS) {
switch (event_data.type) {
case MESS_EVENT_GPS:
#if defined(UBOX_GPS_MODULE)
gps_handle(&event_data.gps);
#elif defined(ACSIP_S7XG_MODULE)
Serial.println("MESS_EVENT_GPS event");
event_data.lora.event = (lora_event_t)event_data.gps.event;
s7xg_handle(&event_data.lora);
#endif
break;
case MESS_EVENT_FILE:
file_handle(&event_data.file);
break;
case MESS_EVENT_VOLT:
break;
case MESS_EVENT_SPEK:
break;
case MESS_EVENT_WIFI:
wifi_handle(&event_data.wifi);
break;
case MESS_EVENT_MOTI:
motion_handle(&event_data.motion);
break;
case MESS_EVENT_TIME:
time_handle(&event_data.time);
break;
case MESS_EVENT_POWER:
power_handle(&event_data.power);
break;
case MESS_EVENT_LORA:
#if defined(ACSIP_S7XG_MODULE)
s7xg_handle(&event_data.lora);
#endif
break;
case MESS_EVENT_BLE:
#ifdef ENABLE_BLE
ble_handle(&event_data.ble);
#endif
break;
default:
Serial.println("Error event");
break;
}
}
}
}
static void time_task(void *param)
{
struct tm time;
task_event_data_t event_data;
for (;;) {
EventBits_t bits = xEventGroupWaitBits(g_sync_event_group,
BIT0,
pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & BIT0) {
struct tm dt;
getLocalTime(&dt);
Serial.print("RTC time is ");
Serial.println(rtc.formatDateTime(PCF_TIMEFORMAT_YYYY_MM_DD_H_M_S));
Serial.printf("getLocalTime is %d:%d:%d\n",dt.tm_hour,dt.tm_min,dt.tm_sec);
time.tm_year = dt.tm_year - 1900;
time.tm_mon = dt.tm_mon;
time.tm_mday = dt.tm_mday;
time.tm_hour = dt.tm_hour;
time.tm_min = dt.tm_min;
event_data.type = MESS_EVENT_TIME;
event_data.time.event = LVGL_TIME_UPDATE;
event_data.time.time = time;
xQueueSend(g_event_queue_handle, &event_data, portMAX_DELAY);
}
delay(10000);
}
}
extern "C" int get_batt_percentage()
{
return axp.getBattPercentage();
}
extern "C" int get_ld1_status()
{
return 1;
}
extern "C" int get_ld2_status()
{
return axp.isLDO2Enable();
}
extern "C" int get_ld3_status()
{
return axp.isLDO3Enable();
}
extern "C" int get_ld4_status()
{
return axp.isLDO4Enable();
}
extern "C" int get_dc2_status()
{
return axp.isDCDC2Enable();
}
extern "C" int get_dc3_status()
{
return axp.isDCDC3Enable();
}
extern "C" const char *get_wifi_ssid()
{
return WiFi.SSID() == "" ? "None" : WiFi.SSID().c_str();
}
extern "C" const char *get_wifi_rssi()
{
return String(WiFi.RSSI()).c_str();
}
extern "C" const char *get_wifi_channel()
{
return String(WiFi.channel()).c_str();
}
extern "C" const char *get_wifi_address()
{
return WiFi.localIP().toString().c_str();
}
extern "C" const char *get_wifi_mac()
{
return WiFi.macAddress().c_str();
}
void gps_power_on()
{
axp.setLDO3Mode(1);
axp.setPowerOutPut(AXP202_LDO3, AXP202_ON);
}
void gps_power_off()
{
axp.setPowerOutPut(AXP202_LDO3, AXP202_OFF);
}
void s7xg_power_on()
{
axp.setLDO4Voltage(AXP202_LDO4_1800MV);
axp.setPowerOutPut(AXP202_LDO4, AXP202_ON);
}
void s7xg_power_off()
{
axp.setPowerOutPut(AXP202_LDO4, AXP202_OFF);
}