-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathFactoryTest.ino
250 lines (210 loc) · 7.17 KB
/
FactoryTest.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
#include <TFT_eSPI.h>
#include <SPI.h>
#include "WiFi.h"
#include <Wire.h>
#include "Button2.h"
#include <esp_adc_cal.h>
#include "bmp.h"
#include "driver/rtc_io.h"
// TFT Pins has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
// #define TFT_MOSI 19
// #define TFT_SCLK 18
// #define TFT_CS 5
// #define TFT_DC 16
// #define TFT_RST 23
// #define TFT_BL 4 // Display backlight control pin
#define ADC_EN 14 //ADC_EN is the ADC detection enable port
#define ADC_PIN 34
#define BUTTON_1 35
#define BUTTON_2 0
TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library
Button2 btn1(BUTTON_1);
Button2 btn2(BUTTON_2);
char buff[512];
int vref = 1100;
int btnCick = false;
#define ENABLE_SPI_SDCARD
//Uncomment will use SDCard, this is just a demonstration,
//how to use the second SPI
#ifdef ENABLE_SPI_SDCARD
#include "FS.h"
#include "SD.h"
SPIClass SDSPI(HSPI);
#define MY_CS 33
#define MY_SCLK 25
#define MY_MISO 27
#define MY_MOSI 26
void setupSDCard()
{
SDSPI.begin(MY_SCLK, MY_MISO, MY_MOSI, MY_CS);
//Assuming use of SPI SD card
if (!SD.begin(MY_CS, SDSPI)) {
Serial.println("Card Mount Failed");
tft.setTextColor(TFT_RED);
tft.drawString("SDCard Mount FAIL", tft.width() / 2, tft.height() / 2 - 32);
tft.setTextColor(TFT_GREEN);
} else {
tft.setTextColor(TFT_GREEN);
Serial.println("SDCard Mount PASS");
tft.drawString("SDCard Mount PASS", tft.width() / 2, tft.height() / 2 - 48);
String size = String((uint32_t)(SD.cardSize() / 1024 / 1024)) + "MB";
tft.drawString(size, tft.width() / 2, tft.height() / 2 - 32);
}
}
#else
#define setupSDCard()
#endif
void wifi_scan();
//! Long time delay, it is recommended to use shallow sleep, which can effectively reduce the current consumption
void espDelay(int ms)
{
esp_sleep_enable_timer_wakeup(ms * 1000);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
esp_light_sleep_start();
}
void showVoltage()
{
static uint64_t timeStamp = 0;
if (millis() - timeStamp > 1000) {
timeStamp = millis();
uint16_t v = analogRead(ADC_PIN);
float battery_voltage = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
String voltage = "Voltage :" + String(battery_voltage) + "V";
Serial.println(voltage);
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.drawString(voltage, tft.width() / 2, tft.height() / 2 );
}
}
void button_init()
{
btn1.setLongClickHandler([](Button2 & b) {
btnCick = false;
int r = digitalRead(TFT_BL);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.drawString("Press again to wake up", tft.width() / 2, tft.height() / 2 );
espDelay(6000);
digitalWrite(TFT_BL, !r);
tft.writecommand(TFT_DISPOFF);
tft.writecommand(TFT_SLPIN);
//After using light sleep, you need to disable timer wake, because here use external IO port to wake up
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
// esp_sleep_enable_ext1_wakeup(GPIO_SEL_35, ESP_EXT1_WAKEUP_ALL_LOW);
rtc_gpio_init(GPIO_NUM_14);
rtc_gpio_set_direction(GPIO_NUM_14, RTC_GPIO_MODE_OUTPUT_ONLY);
rtc_gpio_set_level(GPIO_NUM_14, 1);
delay(500);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_35, 0);
delay(200);
esp_deep_sleep_start();
});
btn1.setPressedHandler([](Button2 & b) {
Serial.println("Detect Voltage..");
btnCick = true;
});
btn2.setPressedHandler([](Button2 & b) {
btnCick = false;
Serial.println("btn press wifi scan");
wifi_scan();
});
}
void button_loop()
{
btn1.loop();
btn2.loop();
}
void wifi_scan()
{
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(1);
tft.drawString("Scan Network", tft.width() / 2, tft.height() / 2);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int16_t n = WiFi.scanNetworks();
tft.fillScreen(TFT_BLACK);
if (n == 0) {
tft.drawString("no networks found", tft.width() / 2, tft.height() / 2);
} else {
tft.setTextDatum(TL_DATUM);
tft.setCursor(0, 0);
Serial.printf("Found %d net\n", n);
for (int i = 0; i < n; ++i) {
sprintf(buff,
"[%d]:%s(%d)",
i + 1,
WiFi.SSID(i).c_str(),
WiFi.RSSI(i));
tft.println(buff);
}
}
// WiFi.mode(WIFI_OFF);
}
void setup()
{
Serial.begin(115200);
Serial.println("Start");
/*
ADC_EN is the ADC detection enable port
If the USB port is used for power supply, it is turned on by default.
If it is powered by battery, it needs to be set to high level
*/
pinMode(ADC_EN, OUTPUT);
digitalWrite(ADC_EN, HIGH);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setTextColor(TFT_GREEN);
tft.setCursor(0, 0);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(1);
/*
if (TFT_BL > 0) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
pinMode(TFT_BL, OUTPUT); // Set backlight pin to output mode
digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); // Turn backlight on. TFT_BACKLIGHT_ON has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
}
*/
tft.setSwapBytes(true);
tft.pushImage(0, 0, 240, 135, ttgo);
espDelay(5000);
tft.setRotation(0);
tft.fillScreen(TFT_RED);
espDelay(1000);
tft.fillScreen(TFT_BLUE);
espDelay(1000);
tft.fillScreen(TFT_GREEN);
espDelay(1000);
button_init();
esp_adc_cal_characteristics_t adc_chars;
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &adc_chars); //Check type of calibration value used to characterize ADC
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
Serial.printf("eFuse Vref:%u mV", adc_chars.vref);
vref = adc_chars.vref;
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
Serial.printf("Two Point --> coeff_a:%umV coeff_b:%umV\n", adc_chars.coeff_a, adc_chars.coeff_b);
} else {
Serial.println("Default Vref: 1100mV");
}
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
setupSDCard();
tft.drawString("LeftButton:", tft.width() / 2, tft.height() / 2 - 16);
tft.drawString("[WiFi Scan]", tft.width() / 2, tft.height() / 2 );
tft.drawString("RightButton:", tft.width() / 2, tft.height() / 2 + 16);
tft.drawString("[Voltage Monitor]", tft.width() / 2, tft.height() / 2 + 32 );
tft.drawString("RightButtonLongPress:", tft.width() / 2, tft.height() / 2 + 48);
tft.drawString("[Deep Sleep]", tft.width() / 2, tft.height() / 2 + 64 );
tft.setTextDatum(TL_DATUM);
}
void loop()
{
if (btnCick) {
showVoltage();
}
button_loop();
}