-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchScreen.ino
212 lines (177 loc) · 5.34 KB
/
TouchScreen.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
#include <Preferences.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <PubSubClient.h>
#include <FS.h>
#include "Free_Fonts.h"
#include <TFT_eSPI.h>
#include <TFT_eWidget.h>
TFT_eSPI tft = TFT_eSPI();
#define CALIBRATION_FILE "/TouchCalData1"
#define REPEAT_CAL false
ButtonWidget btnR = ButtonWidget(&tft);
#define BUTTON_W 100
#define BUTTON_H 50
ButtonWidget *btn[] = { &btnR };
uint8_t buttonCount = sizeof(btn) / sizeof(btn[0]);
// MQTT Broker
const char *mqtt_broker = "mqtt.local";
const char *topic = "fairylights/toggle";
const char *state_topic = "homeassistant/switch/sonoff_1001ffea20_1/state";
byte on_state[] = {'o','n'};
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char *topic, byte *payload, unsigned int length) {
if (memcmp(payload, on_state, sizeof(on_state)) == 0) {
Serial.println("New state is on");
btnR.drawSmoothButton(true, 3, TFT_BLACK, "ON");
} else {
Serial.println("New state is off");
btnR.drawSmoothButton(false, 3, TFT_BLACK, "OFF");
}
}
void btnR_pressAction(void) {
if (btnR.justPressed()) {
Serial.println("Button toggled");
client.publish(topic, "Light toggle.");
btnR.setPressTime(millis());
}
}
void btnR_releaseAction(void) {
// Not action
}
void initButtons() {
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(5, 30);
tft.print("Fairy Lights");
// uint16_t x = (tft.width() - BUTTON_W) / 2;
// uint16_t y = tft.height() / 2 - BUTTON_H - 10;
uint16_t x = 15;
uint16_t y = (BUTTON_H / 2) + 20;
btnR.initButtonUL(x, y, BUTTON_W, BUTTON_H, TFT_WHITE, TFT_BLACK, TFT_GREEN, "OFF", 1);
btnR.setPressAction(btnR_pressAction);
//btnR.setReleaseAction(btnR_releaseAction);
btnR.drawSmoothButton(false, 3, TFT_BLACK); // 3 is outline width, TFT_BLACK is the surrounding background colour for anti-aliasing
}
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF18);
// Calibrate the touch screen and retrieve the scaling factors
touch_calibrate();
initButtons();
// Get WiFi creds from preferences storage
Preferences wifiCreds;
wifiCreds.begin("wifiCreds", true);
String ssid = wifiCreds.getString("ssid");
String pwd = wifiCreds.getString("password");
wifiCreds.end();
WiFi.begin(ssid.c_str(), pwd.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (!MDNS.begin("mpi3501")) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
Serial.println("\nWiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
if (client.connect(client_id.c_str())) {
Serial.println("EMQX MQTT broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// Publish and subscribe
// client.publish(topic, "Hi, I'm ESP32 ^^");
client.subscribe(state_topic);
}
void loop() {
static uint32_t scanTime = millis();
uint16_t t_x = 9999, t_y = 9999; // To store the touch coordinates
// Scan keys every 50ms at most
if (millis() - scanTime >= 50) {
// Pressed will be set true if there is a valid touch on the screen
bool pressed = tft.getTouch(&t_x, &t_y);
scanTime = millis();
for (uint8_t b = 0; b < buttonCount; b++) {
if (pressed) {
if (btn[b]->contains(t_x, t_y)) {
btn[b]->press(true);
btn[b]->pressAction();
}
} else {
btn[b]->press(false);
btn[b]->releaseAction();
}
}
}
client.loop();
}
void touch_calibrate() {
uint16_t calData[5];
uint8_t calDataOK = 0;
// check file system exists
if (!LittleFS.begin()) {
Serial.println("Formating file system");
LittleFS.format();
LittleFS.begin();
}
// check if calibration file exists and size is correct
if (LittleFS.exists(CALIBRATION_FILE)) {
if (REPEAT_CAL) {
// Delete if we want to re-calibrate
LittleFS.remove(CALIBRATION_FILE);
} else {
File f = LittleFS.open(CALIBRATION_FILE, "r");
if (f) {
if (f.readBytes((char *)calData, 14) == 14)
calDataOK = 1;
f.close();
}
}
}
if (calDataOK && !REPEAT_CAL) {
// calibration data valid
tft.setTouch(calData);
} else {
// data not valid so recalibrate
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 0);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println("Touch corners as indicated");
tft.setTextFont(1);
tft.println();
if (REPEAT_CAL) {
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.println("Set REPEAT_CAL to false to stop this running again!");
}
tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Calibration complete!");
// store data
File f = LittleFS.open(CALIBRATION_FILE, "w");
if (f) {
f.write((const unsigned char *)calData, 14);
f.close();
}
}
}