-
Notifications
You must be signed in to change notification settings - Fork 282
/
TTGO.h
1626 lines (1375 loc) · 45.1 KB
/
TTGO.h
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
_ _ _ ____
| | (_) | | _ _ / ___| ___
| | | | | | | | | | | | _ / _ \
| |___ | | | | | |_| | | |_| | | (_) |
|_____| |_| |_| \__, | \____| \___/
|___/
website:https://github.com/Xinyuan-LilyGO/TTGO_TWatch_Library
Written by Lewis he //https://github.com/lewisxhe
*/
#pragma once
#include <SPI.h>
#if defined(LILYGO_WATCH_HAS_S76_S78G)
#define LILYGO_WATCH_HAS_SDCARD
#endif
#ifdef LILYGO_WATCH_LVGL
#include <Ticker.h>
#endif
#ifdef LILYGO_WATCH_HAS_SDCARD
#include <FS.h>
#include <SD.h>
#endif
#ifndef LILYGO_BLOCK_ILI9481_MODULE
#define LILYGO_TOUCH_DRIVER_FTXXX
#endif
#ifdef LILYGO_WATCH_HAS_TOUCH
#include "drive/fx50xx/focaltech.h"
#if defined(LILYGO_TOUCH_DRIVER_GTXXX)
typedef GT9xx_Class CapacitiveTouch ;
#elif defined(LILYGO_TOUCH_DRIVER_FTXXX)
typedef FocalTech_Class CapacitiveTouch ;
#endif
#endif /*LILYGO_WATCH_HAS_TOUCH*/
#if !defined(EXTERNAL_TFT_ESPI_LIBRARY)
#if defined(LILYGO_WATCH_HAS_DISPLAY) || defined(LILYGO_EINK_TOUCHSCREEN) || defined(LILYGO_WATCH_HAS_EINK)
#include "libraries/TFT_eSPI/TFT_eSPI.h"
#include "libraries/U8g2_for_Adafruit_GFX/src/U8g2_for_Adafruit_GFX.h"
#endif
#else //EXTERNAL_TFT_ESPI_LIBRARY
#include <TFT_eSPI.h>
#endif /*EXTERNAL_TFT_ESPI_LIBRARY*/
#ifdef LILYGO_WATCH_HAS_BMA423
#include "drive/bma423/bma.h"
#endif
#ifdef LILYGO_WATCH_HAS_NFC
#include "libraries/Adafruit-PN532/Adafruit_PN532.h"
#endif
#ifdef LILYGO_WATCH_HAS_BUTTON
#include "drive/button/Button2.h"
#endif
#ifdef LILYGO_WATCH_HAS_PCF8563
#include "drive/rtc/pcf8563.h"
#endif
#if defined(LILYGO_WATCH_AIR530_GPS)
#include "drive/gps/Air530.h"
#elif defined(LILYGO_WATCH_HAS_GPS)
#include "drive/gps/TinyGPS++.h"
#endif
#ifdef LILYGO_WATCH_HAS_S76_S78G
#include "drive/s7xg/s7xg.h"
#endif
#ifdef LILYGO_WATCH_LVGL
#include "lvgl/lvgl.h"
#endif
#ifdef LILYGO_WATCH_LVGL_FS
#include "libraries/lv_fs_if/lv_fs_if.h"
#endif
#ifdef LILYGO_WATCH_LVGL_DECODER
#include "libraries/lv_lib_png/lv_png.h"
#endif
#ifdef LILYGO_WATCH_HAS_AXP202
#include "drive/axp/axp20x.h"
#endif
#ifdef LILYGO_WATCH_HAS_MPU6050
#include "drive/mup6050/MPU6050.h"
#endif
#ifdef LILYGO_WATCH_HAS_ADC
#include "esp_adc_cal.h"
#endif /*LILYGO_WATCH_HAS_ADC*/
#include "drive/i2c/i2c_bus.h"
#include "drive/tft/bl.h"
#ifdef LILYGO_EINK_GDEW0371W7
#define GDEW0371W7_WIDTH 416
#define GDEW0371W7_HEIGHT 240
#include "libraries/ePaperDriverLib/src/ePaperDriver.h"
#endif
#if defined(LILYGO_EINK_GDEP015OC1)
#include "libraries/GxEPD/src/GxEPD.h"
#include "libraries/GxEPD/src/GxIO/GxIO.h"
#include "libraries/GxEPD/src/GxIO/GxIO_SPI/GxIO_SPI.h"
#include "libraries/GxEPD/src/GxGDEP015OC1/GxGDEP015OC1.h"
#elif defined(LILYGO_EINK_DEPG0150BN)
#include "libraries/GxEPD/src/GxEPD.h"
#include "libraries/GxEPD/src/GxIO/GxIO.h"
#include "libraries/GxEPD/src/GxIO/GxIO_SPI/GxIO_SPI.h"
#include "libraries/GxEPD/src/GxDEPG0150BN/GxDEPG0150BN.h"
#elif defined(LILYGO_EINK_GDEH0154D67_TP) || defined(LILYGO_EINK_GDEH0154D67_BL)
#include "libraries/GxEPD/src/GxEPD.h"
#include "libraries/GxEPD/src/GxIO/GxIO.h"
#include "libraries/GxEPD/src/GxIO/GxIO_SPI/GxIO_SPI.h"
#include "libraries/GxEPD/src/GxGDEH0154D67/GxGDEH0154D67.h"
#endif
#if defined(LILYGO_WATCH_DRV2605)
#include "libraries/Adafruit_DRV2605_Library/Adafruit_DRV2605.h"
#endif
#if !defined(EXTERNAL_TFT_ESPI_LIBRARY) && !defined(LILYGO_BLOCK_ILI9488_MODULE) && !defined(TWATCH_USE_PSRAM_ALLOC_LVGL)
// #define ENABLE_LVGL_FLUSH_DMA //Use DMA for transmission by default
#endif
#ifndef LVGL_BUFFER_SIZE
#if defined(LILYGO_BLOCK_ST7796S_MODULE) || defined(LILYGO_BLOCK_ILI9488_MODULE) || defined(LILYGO_BLOCK_ILI9481_MODULE)
#define LVGL_BUFFER_SIZE (320*100)
#else
#define LVGL_BUFFER_SIZE (240*100)
#endif
#endif /*LVGL_BUFFER_SIZE*/
#define TOUCH_IRQ_BIT (_BV(1))
/* Selectively disable some initialisation */
#define NO_HARDWARE (_BV(0))
#define NO_POWER (_BV(1))
#define NO_TFT (_BV(2))
#define NO_TOUCH (_BV(3))
#define NO_SENSOR (_BV(4))
#define NO_BACKLIGHT (_BV(5))
#if defined(LILYGO_WATCH_2020_V1) || defined(LILYGO_WATCH_2019_WITH_TOUCH) || defined(LILYGO_WATCH_2019_NO_TOUCH)
#define WATCH_SCREEN_TOP_EDGE 0
#define WATCH_SCREEN_LEFT_EDGE 1
#define WATCH_SCREEN_BOTTOM_EDGE 2
#define WATCH_SCREEN_RIGHT_EDGE 3
#elif defined(LILYGO_WATCH_2020_V2)
#define WATCH_SCREEN_TOP_EDGE 1
#define WATCH_SCREEN_LEFT_EDGE 2
#define WATCH_SCREEN_BOTTOM_EDGE 3
#define WATCH_SCREEN_RIGHT_EDGE 0
#elif defined(LILYGO_WATCH_2020_V3)
#define WATCH_SCREEN_TOP_EDGE 3
#define WATCH_SCREEN_LEFT_EDGE 0
#define WATCH_SCREEN_BOTTOM_EDGE 1
#define WATCH_SCREEN_RIGHT_EDGE 2
#endif
class TTGOClass
{
public:
static TTGOClass *getWatch()
{
if (_ttgo == nullptr) {
_ttgo = new TTGOClass();
_tpEvent = xEventGroupCreate();
}
return _ttgo;
}
#if defined(EXTERNAL_TFT_ESPI_LIBRARY)
void setTftExternal(TFT_eSPI &handler )
{
tft = &handler;
}
#endif
void begin( uint8_t disable = 0 )
{
i2c = new I2CBus();
#ifdef LILYGO_WATCH_HAS_PCF8563
rtc = new PCF8563_Class(*i2c);
#endif /*LILYGO_WATCH_HAS_PCF8563*/
#ifdef LILYGO_WATCH_HAS_BACKLIGHT
bl = new BackLight(TWATCH_TFT_BL);
#endif /*LILYGO_WATCH_HAS_BACKLIGHT*/
#ifdef LILYGO_WATCH_HAS_BMA423
bma = new BMA(*i2c);
#endif /*LILYGO_WATCH_HAS_BMA423*/
#ifdef LILYGO_WATCH_HAS_BUTTON
//In the 2020 version, Button IO36 is not used.
button = new Button2(USER_BUTTON);
#endif /*LILYGO_WATCH_HAS_BUTTON*/
#ifdef LILYGO_WATCH_HAS_AXP202
power = new AXP20X_Class();
#endif /*LILYGO_WATCH_HAS_AXP202*/
#ifdef LILYGO_WATCH_HAS_MPU6050
mpu = new MPU6050();
#endif /*LILYGO_WATCH_HAS_MPU6050*/
#ifdef LILYGO_WATCH_DRV2605
drv = new Adafruit_DRV2605();
#endif /*LILYGO_WATCH_DRV2605*/
if (!(disable & NO_HARDWARE))
initHardware();
if (!(disable & NO_HARDWARE))
initPower();
if (!(disable & NO_HARDWARE))
initTFT();
if (!(disable & NO_HARDWARE))
initTouch();
if (!(disable & NO_HARDWARE))
initSensor();
if (!(disable & NO_HARDWARE))
initBlacklight();
}
#ifdef LILYGO_WATCH_HAS_BMA423
void bma423AttachInterrupt(void (*handle)(void))
{
pinMode(BMA423_INT1, INPUT_PULLUP);
attachInterrupt(BMA423_INT1, handle, RISING);
}
#endif /*LILYGO_WATCH_HAS_BMA423*/
/******************************************
* TouchPad
* ***************************************/
#ifdef LILYGO_WATCH_HAS_TOUCH
bool touched()
{
#if defined(LILYGO_TOUCH_DRIVER_GTXXX)
return (touch->scanPoint() > 0);
#elif defined(LILYGO_TOUCH_DRIVER_FTXXX)
return (touch->getTouched() > 0);
#endif
}
/**
* @brief Get the touch screen coordinates,
* return true if pressed, false otherwise
*/
bool getTouch(int16_t &x, int16_t &y)
{
uint16_t __x = 0, __y = 0;
if (touch == nullptr) {
return false;
}
#if defined(LILYGO_TOUCH_DRIVER_GTXXX)
if (!touch->scanPoint()) {
return false;
}
touch->getPoint(__x, __y, 0);
#elif defined(LILYGO_TOUCH_DRIVER_FTXXX)
if (!touch->getPoint(__x, __y)) {
return false;
}
#endif /*LILYGO_TOUCH_DRIVER_GTXXX LILYGO_TOUCH_DRIVER_FTXXX*/
#if defined(LILYGO_BLOCK_ILI9481_MODULE)
uint8_t rotation = tft->getRotation();
switch (rotation) {
case 1:
x = __y;
y = tft->height() - __x;
break;
case 2:
x = tft->width() - __x;
y = tft->height() - __y;
break;
case 3:
x = tft->width() - __y;
y = __x;
break;
case 0:
default:
x = __x;
y = __y;
}
#elif defined(LILYGO_BLOCK_ST7796S_MODULE)
uint8_t rotation = tft->getRotation();
switch (rotation) {
case 1:
x = __y;
y = tft->height() - __x;
break;
case 2:
x = tft->width() - __x;
y = tft->height() - __y;
break;
case 3:
x = tft->width() - __y;
y = __x;
break;
case 0:
default:
x = __x;
y = __y;
}
#elif defined(LILYGO_WATCH_2019_WITH_TOUCH)
uint8_t rotation = tft->getRotation();
int16_t _x = map(__x, 0, 320, 0, 240);
int16_t _y = map(__y, 0, 320, 0, 240);
switch (rotation) {
case 1:
x = _y;
y = TFT_HEIGHT - _x;
break;
case 2:
x = TFT_WIDTH - _x;
y = TFT_HEIGHT - _y;
break;
case 3:
x = TFT_WIDTH - _y;
y = _x;
break;
case 0:
default:
x = _x;
y = _y;
}
#elif defined(LILYGO_WATCH_2020_V1) || defined(LILYGO_WATCH_2020_V2) || defined(LILYGO_WATCH_2020_V3)
#ifndef LILYGO_WATCH_2020_PANEL_V1
uint8_t rotation = tft->getRotation();
switch (rotation) {
case 0:
x = TFT_WIDTH - __x;
y = TFT_HEIGHT - __y;
break;
case 1:
x = TFT_WIDTH - __y;
y = __x;
break;
case 3:
x = __y;
y = TFT_HEIGHT - __x;
break;
case 2:
default:
x = __x;
y = __y;
}
#else /*LILYGO_WATCH_2020_PANEL_V1*/
uint8_t rotation = tft->getRotation();
switch (rotation) {
case 2:
x = TFT_WIDTH - __x;
y = TFT_HEIGHT - __y;
break;
case 1:
x = __y;
y = TFT_HEIGHT - __x;
break;
case 3:
x = TFT_HEIGHT - __y;
y = __x;
break;
case 0:
default:
x = __x;
y = __y;
}
#endif /*LILYGO_WATCH_2020_PANEL_V1*/
#elif defined(LILYGO_EINK_GDEW0371W7)
uint8_t r = ePaper->getRotation();
switch (r) {
case 0:
x = __x;
y = __y;
break;
case 1:
x = __y;
y = GDEW0371W7_HEIGHT - __x;
break;
case 2:
x = GDEW0371W7_HEIGHT - __x;
y = GDEW0371W7_WIDTH - __y;
break;
case 3:
x = GDEW0371W7_WIDTH - __y;
y = __x;
break;
default:
x = __x;
y = __y;
}
#else
x = __x;
y = __y;
#endif
return true;
}
/*@brief Cannot wake up after entering sleep mode,
* can only be solved by restarting the power supply
*/
void touchToSleep()
{
#if defined(LILYGO_TOUCH_DRIVER_GTXXX)
//TODO:
#elif defined(LILYGO_TOUCH_DRIVER_FTXXX)
touch->setPowerMode(FOCALTECH_PMODE_DEEPSLEEP);
#endif
}
/*
* @brief Enter the monitoring mode can reduce the touch current,
* when it monitors the touch, it will switch to the normal mode under the monitoring mode
*/
void touchToMonitor()
{
#if defined(LILYGO_TOUCH_DRIVER_GTXXX)
//TODO:
#elif defined(LILYGO_TOUCH_DRIVER_FTXXX)
touch->setPowerMode(FOCALTECH_PMODE_MONITOR);
#endif
}
#if defined(LILYGO_WATCH_2020_V2) || defined(LILYGO_WATCH_2020_V3)
/*
In 2020V2, touch can be set to sleep state, control AXP202_EXTEN can wake up touch
*/
void touchWakup()
{
#ifdef LILYGO_WATCH_2020_V2
power->setPowerOutPut(AXP202_EXTEN, false);
delay(8); //Trst Min = 5ms
power->setPowerOutPut(AXP202_EXTEN, true);
#endif
#ifdef LILYGO_WATCH_2020_V3
digitalWrite(TOUCH_RST, LOW);
delay(8);
digitalWrite(TOUCH_RST, HIGH);
#endif
}
#endif
#endif /*LILYGO_WATCH_HAS_TOUCH*/
/******************************************
* Wire1
* ***************************************/
#if defined(LILYGO_WATCH_2019_WITH_TOUCH) || defined(LILYGO_WATCH_2019_NO_TOUCH) || defined(LILYGO_WATCH_BLOCK)
I2CBus *wire;
//Use the second set of I2C interfaces
void beginWire(uint8_t sda = 25, uint8_t scl = 26)
{
wire = new I2CBus(Wire1, sda, scl);
}
TwoWire &getWire()
{
return (*wire->getHandler());
}
#endif /*TWI1*/
/******************************************
* Power
* ***************************************/
#ifdef LILYGO_WATCH_HAS_AXP202
void enableAudio(void)
{
#if defined(LILYGO_WATCH_2020_V2)
power->setPowerOutPut(AXP202_LDO3, AXP202_ON);
#elif defined(LILYGO_WATCH_2020_V3)
power->setPowerOutPut(AXP202_LDO4, AXP202_ON);
#endif
}
void disableAudio(void)
{
#if defined(LILYGO_WATCH_2020_V2)
power->setPowerOutPut(AXP202_LDO3, AXP202_OFF);
#elif defined(LILYGO_WATCH_2020_V3)
power->setPowerOutPut(AXP202_LDO4, AXP202_OFF);
#endif
}
#ifdef LILYGO_WATCH_DRV2605
void enableDrv2650(bool en = true)
{
#ifdef LILYGO_WATCH_2020_V2
power->setGPIOMode(AXP_GPIO_0, en ? AXP_IO_OUTPUT_HIGH_MODE : AXP_IO_OUTPUT_LOW_MODE);
#else
power->setPowerOutPut(AXP202_EXTEN, en);
#endif
}
#endif
void powerAttachInterrupt(void (*handle)(void))
{
pinMode(AXP202_INT, INPUT_PULLUP);
attachInterrupt(AXP202_INT, handle, FALLING);
}
void trunOnGPS()
{
#ifdef LILYGO_WATCH_2020_V2
// 2020 v2 use axp202 ldo 4
if (power)
power->setPowerOutPut(AXP202_LDO4, true);
#else
if (power)
power->setPowerOutPut(AXP202_LDO3, true);
#endif
}
void turnOffGPS()
{
#ifdef LILYGO_WATCH_2020_V2
// 2020 v2 use axp202 ldo 4
if (power)
power->setPowerOutPut(AXP202_LDO4, false);
#else
if (power)
power->setPowerOutPut(AXP202_LDO3, false);
#endif
}
/*
* @brief It will turn off the power supply of all peripherals except ESP32
* * */
void powerOff()
{
#ifndef LILYGO_WATCH_2020_V1
power->setPowerOutPut(AXP202_EXTEN, false);
power->setPowerOutPut(AXP202_LDO4, false);
power->setPowerOutPut(AXP202_DCDC2, false);
power->setPowerOutPut(AXP202_LDO3, false);
power->setPowerOutPut(AXP202_LDO2, false);
#else
power->setPowerOutPut(AXP202_EXTEN, false);
power->setPowerOutPut(AXP202_LDO4, false);
power->setPowerOutPut(AXP202_DCDC2, false);
power->setPowerOutPut(AXP202_LDO3, false);
// power->setPowerOutPut(AXP202_LDO2, false);
#endif /*LILYGO_WATCH_2020_V1*/
}
/**
* @brief Turn on the peripheral power,
* usually control the power of different backplanes,
* In 2020V1, it controls the power of the audio circuit
*/
void enableLDO3(bool en = true)
{
power->setLDO3Mode(1);
power->setPowerOutPut(AXP202_LDO3, en);
}
void shutdown()
{
power->shutdown();
}
#endif /*LILYGO_WATCH_HAS_AXP202*/
/******************************************
* SIM Series
* ***************************************/
#if defined(LILYGO_WATCH_HAS_SIM800L) || defined(LILYGO_WATCH_HAS_SIM868)
#ifdef LILYGO_WATCH_HAS_SIM868
#undef MOTOR_PIN
#define MOTOR_PIN 4 //SIM868 MOTOR PIN,JUST ONLY SIM868
#define SIM868_MODEM_RI 26
#define SIM868_MODEM_DTR 25
#define SIM868_MODEM_BAUD 115200 //SIM868 BAUD
#define SIM868_MODEM_TX 33 //SIM868 TX PIN
#define SIM868_MODEM_RX 34 //SIM868 RX PIN
void enableModemGPSPower(bool en = true)
{
power->setPowerOutPut(AXP202_LDO3, en);
}
#endif
#ifdef LILYGO_WATCH_HAS_SIM800L
#define SIM800_MODEM_BAUD 115200 //SIM800L BAUD
#define SIM800_MODEM_TX 33 //SIM800L TX PIN
#define SIM800_MODEM_RX 34 //SIM800L RX PIN
#define SIM800_MODEM_RST 14 //SIM800L RESET PIN,JUST ONLY SIM800L
#define SIM800_MODEM_PWRKEY 15 //SIM800L PWRKEY PIN,JUST ONLY SIM800L
void powerOnModem()
{
pinMode(SIM800_MODEM_RST, OUTPUT);
digitalWrite(SIM800_MODEM_RST, HIGH);
pinMode(SIM800_MODEM_PWRKEY, OUTPUT);
digitalWrite(SIM800_MODEM_PWRKEY, HIGH);
delay(200);
digitalWrite(SIM800_MODEM_PWRKEY, LOW);
delay(1000);
digitalWrite(SIM800_MODEM_PWRKEY, HIGH);
}
void powerOffModem()
{
// 1.5s<T 1 <33s
digitalWrite(SIM800_MODEM_PWRKEY, LOW);
delay(1500);
}
void restartModem()
{
// PWRKEY pull-down 15s < T1 < 2s
digitalWrite(SIM800_MODEM_PWRKEY, LOW);
delay(1800);
// T3 > 800ms
digitalWrite(SIM800_MODEM_PWRKEY, HIGH);
delay(900);
// pull-down > 1s power on
digitalWrite(SIM800_MODEM_PWRKEY, LOW);
delay(1000);
digitalWrite(SIM800_MODEM_PWRKEY, HIGH);
}
#endif /*LILYGO_WATCH_HAS_SIM800L*/
#endif /*LILYGO_WATCH_HAS_SIM800L || LILYGO_WATCH_HAS_SIM868*/
/******************************************
* DISPLAY
* ***************************************/
#ifdef LILYGO_WATCH_HAS_DISPLAY
void displayOff()
{
if (tft) {
tft->writecommand(0x10);
}
#ifdef LILYGO_WATCH_HAS_TOUCH
touchToSleep();
#endif /*LILYGO_WATCH_HAS_TOUCH*/
}
void displaySleep()
{
tft->writecommand(0x10);
#ifdef LILYGO_WATCH_HAS_TOUCH
touchToMonitor();
#endif /*LILYGO_WATCH_HAS_TOUCH*/
}
void displayWakeup()
{
tft->writecommand(0x11);
}
#endif /*LILYGO_WATCH_HAS_DISPLAY*/
/******************************************
* BACKLIGHT
* ***************************************/
#ifdef LILYGO_WATCH_HAS_BACKLIGHT
void openBL()
{
#if !defined(LILYGO_WATCH_2020_V1) && defined(LILYGO_WATCH_HAS_AXP202)
power->setPowerOutPut(AXP202_LDO2, AXP202_ON);
#endif /*LILYGO_WATCH_2020_V1*/
bl->on();
}
void closeBL()
{
#if !defined(LILYGO_WATCH_2020_V1) && defined(LILYGO_WATCH_HAS_AXP202)
power->setPowerOutPut(AXP202_LDO2, AXP202_OFF);
#endif /*LILYGO_WATCH_2020_V1*/
bl->off();
}
void setBrightness(uint8_t level)
{
bl->adjust(level);
}
#endif /*LILYGO_WATCH_HAS_BACKLIGHT*/
/******************************************
* LVGL
* ***************************************/
#if defined(LILYGO_WATCH_LVGL) && defined(LILYGO_WATCH_HAS_DISPLAY)
void lvgl_whirling(uint8_t rot)
{
tft->setRotation(rot);
disp_drv.hor_res = tft->width();
disp_drv.ver_res = tft->height();
lv_disp_drv_update(lv_disp_get_default(), &disp_drv);
}
private:
#ifdef TWATCH_USE_PSRAM_ALLOC_LVGL
lv_color_t *buf1 = nullptr;
#ifdef TWATCH_LVGL_DOUBLE_BUFFER
lv_color_t *buf2 = nullptr;
#endif
#endif
public:
bool lvgl_begin()
{
if (tft == nullptr) {
return false;
}
lv_init();
lv_indev_drv_t indev_drv;
lv_disp_drv_init(&disp_drv);
static lv_disp_buf_t disp_buf;
#ifdef TWATCH_USE_PSRAM_ALLOC_LVGL
if (psramFound()) {
buf1 = (lv_color_t *)ps_calloc(sizeof(lv_color_t), LVGL_BUFFER_SIZE);
} else {
buf1 = (lv_color_t *)calloc(sizeof(lv_color_t), LVGL_BUFFER_SIZE);
}
if (!buf1) {
log_e("buf1 alloc failed\n");
return false;
}
#ifdef TWATCH_LVGL_DOUBLE_BUFFER
if (psramFound()) {
buf2 = (lv_color_t *)ps_calloc(sizeof(lv_color_t), LVGL_BUFFER_SIZE);
} else {
buf2 = (lv_color_t *)calloc(sizeof(lv_color_t), LVGL_BUFFER_SIZE);
}
if (!buf2) {
log_e("buf2 alloc failed\n");
free(buf1);
return false;
}
#endif /*TWATCH_LVGL_DOUBLE_BUFFER*/
#else
static lv_color_t buf1[LVGL_BUFFER_SIZE];
#endif /*TWATCH_LVGL_DOUBLE_BUFFER*/
#ifdef TWATCH_LVGL_DOUBLE_BUFFER
lv_disp_buf_init(&disp_buf, buf1, buf2, LVGL_BUFFER_SIZE);
#else
lv_disp_buf_init(&disp_buf, buf1, NULL, LVGL_BUFFER_SIZE);
#endif
disp_drv.hor_res = tft->width();
disp_drv.ver_res = tft->height();
disp_drv.flush_cb = disp_flush;
/*Set a display buffer*/
disp_drv.buffer = &disp_buf;
lv_disp_drv_register(&disp_drv);
#if defined(LILYGO_WATCH_HAS_TOUCH)
/*Register a touchpad input device*/
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = touchpad_read;
lv_indev_drv_register(&indev_drv);
#endif /*LILYGO_WATCH_HAS_TOUCH*/
#if defined(LILYGO_WATCH_2020_V1) || defined(LILYGO_WATCH_2020_V2) || defined(LILYGO_WATCH_2020_V3)
#define LILYGO_WATCH_LVGL_FS_SPIFFS
#endif
#ifdef LILYGO_WATCH_LVGL_FS
#if defined(LILYGO_WATCH_LVGL_FS_SPIFFS)
SPIFFS.begin(true, "/fs");
#else
//TODO:
#endif /*LILYGO_WATCH_LVGL_FS_SPIFFS*/
lv_fs_if_init();
#ifdef LILYGO_WATCH_LVGL_DECODER
lv_png_init();
#endif
#endif /*LILYGO_WATCH_LVGL_FS*/
tickTicker = new Ticker();
startLvglTick();
return true;
}
void startLvglTick()
{
tickTicker->attach_ms(5, []() {
lv_tick_inc(5);
});
}
void stopLvglTick()
{
tickTicker->detach();
}
#endif /*LILYGO_WATCH_LVGL*/
/******************************************
* RTC
* ***************************************/
#ifdef LILYGO_WATCH_HAS_PCF8563
PCF8563_Class *rtc = nullptr;
void rtcAttachInterrupt(void (*rtc_cb)(void))
{
pinMode(RTC_INT_PIN, INPUT_PULLUP); //need change to rtc_pin
attachInterrupt(RTC_INT_PIN, rtc_cb, FALLING);
}
#endif /*LILYGO_WATCH_HAS_PCF8563*/
#ifdef LILYGO_WATCH_HAS_BACKLIGHT
BackLight *bl = nullptr;
#endif
#ifdef LILYGO_WATCH_HAS_AXP202
AXP20X_Class *power = nullptr;
#endif
#if defined(LILYGO_WATCH_HAS_DISPLAY)
TFT_eSPI *tft = nullptr;
#endif
#ifdef LILYGO_WATCH_HAS_BUZZER
Buzzer *buzzer = nullptr;
#endif
#ifdef LILYGO_WATCH_HAS_BMA423
BMA *bma = nullptr;
#endif
#ifdef LILYGO_WATCH_HAS_BUTTON
Button2 *button = nullptr;
#endif
#ifdef LILYGO_WATCH_HAS_MPU6050
MPU6050 *mpu = nullptr;
#endif
#if defined(LILYGO_EINK_GDEW0371W7)
ePaperDisplay *ePaper = nullptr;
#endif
#if defined(LILYGO_EINK_GDEH0154D67_TP) || defined(LILYGO_EINK_GDEH0154D67_BL) || defined(LILYGO_EINK_GDEP015OC1) || defined(LILYGO_EINK_DEPG0150BN)
GxIO_Class *io = nullptr;
GxEPD_Class *ePaper = nullptr;
#endif
#ifdef LILYGO_WATCH_DRV2605
Adafruit_DRV2605 *drv = nullptr;
#endif
#ifdef LILYGO_WATCH_HAS_MOTOR
Motor *motor = nullptr;
void motor_begin()
{
if (motor == nullptr) {
motor = new Motor(MOTOR_PIN);
}
motor->begin();
}
void shake()
{
if (motor) {
motor->onec();
}
}
#endif
#ifdef LILYGO_WATCH_HAS_NFC
Adafruit_PN532 *nfc = nullptr;
void nfc_begin()
{
if (nfc == nullptr) {
nfc = new Adafruit_PN532(TWATCH_PN532_IRQ, TWATCH_PN532_RESET);
}
nfc->begin();
nfc->setI2CWrite(nfcWriteBytes);
nfc->setI2CRead(nfcReadBytes);
}
#endif
#ifdef LILYGO_WATCH_HAS_SDCARD
SPIClass *sdhander = nullptr;
bool sdcard_begin()
{
if (!sdhander) {
sdhander = new SPIClass(HSPI);
pinMode(SD_MISO, INPUT_PULLUP);
sdhander->begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
}
if (!SD.begin(SD_CS, *sdhander)) {
log_e("SD Card Mount Failed");
return false;
}
return true;
}
#endif /*LILYGO_WATCH_HAS_SDCARD*/
#if defined(LILYGO_WATCH_HAS_GPS) || defined(LILYGO_WATCH_HAS_S76_S78G) || defined(LILYGO_WATCH_AIR530_GPS)
HardwareSerial *hwSerial = nullptr;
#endif
#if defined(LILYGO_WATCH_AIR530_GPS)
//retain Air530
Air530 *gps = nullptr;
Air530 *gps_begin()
{
if (hwSerial == nullptr) {
hwSerial = new HardwareSerial(1);
hwSerial->begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX, GPS_TX);
}
if (gps == nullptr) {
gps = new Air530(hwSerial, GPS_WAKE);
}
return gps;
}
#elif defined(LILYGO_WATCH_HAS_GPS)
TinyGPSPlus *gps = nullptr;
void gps_begin()
{
if (gps == nullptr) {
gps = new TinyGPSPlus();
}
if (hwSerial == nullptr) {
hwSerial = new HardwareSerial(1);
}
hwSerial->begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX, GPS_TX);
}
bool gpsHandler()
{
if (gps == nullptr) return false;
while (hwSerial->available()) {
if (gps->encode(hwSerial->read())) {
return true;
}
}
return false;
}