-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventProcessing_TTGOWATCH.ino
277 lines (230 loc) · 6.59 KB
/
EventProcessing_TTGOWATCH.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
#include "config.h"
//Variables
bool patternMatched;
// Connection Variables
const char* ssid = "iPhone von Martin (3)";
const char* password = "987654321";
int ctr = 0;
//Acting as a server
WiFiServer server(1080);
IPAddress ip(172, 20, 10, 14);
//IPAddress ip(172, 20, 10, 13);
//IPAddress ip(172, 20, 10, 12);
IPAddress gateway(172, 20, 10, 1);
IPAddress subnet(255, 255, 255, 240);
//------
//Acting as a client (targets - change to not include server number)
WiFiClient master;
IPAddress server1(172, 20, 10, 12);
IPAddress server2(172, 20, 10, 13);
//UI Elements
TTGOClass *ttgo;
lv_obj_t *queueLabel;
lv_obj_t *detectorLabel;
static void init_wifi_connection()
{
//Init ESP32 Wifi
WiFi.config(ip, gateway, subnet); // force to use fixed IP
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
server.begin();
Serial.print(" connected to Wifi! IP address : ");
Serial.println(WiFi.localIP());
}
// SERVER METHODS
static void handle_connection()
{
WiFiClient client = server.available();
client.setTimeout(50);
if(client)
{
//Print client IP address
Serial.print("Client :");
Serial.println(client.localIP());
while (client.connected() && client.available())
{
int res = receiveAndStoreEvent(client);
if(res < 0)
break;
}
client.stop();
Serial.println("Client disconnected!");
patternMatched = CEP();
}
}
// CLIENT METHODS
static void connect_and_send_event(IPAddress server, Event e)
{
Serial.print("Trying to connect to server");
while (master.connect(server, 1080) != 1) // Connection to the server
{
Serial.print(".");
ctr++;
if (ctr > 3)
{
ctr = 0;
Serial.println();
Serial.println("Server not available");
return;
}
}
Serial.println();
Serial.println("Connected to Server");
sendEvent(master, e);
Serial.println("Sending Event " + events[e]);
master.stop();
}
static void event_a_handler(lv_obj_t *obj, lv_event_t event)
{
if(event == LV_EVENT_CLICKED)
{
connect_and_send_event(server1, A);
connect_and_send_event(server2, A);
}
}
static void event_b_handler(lv_obj_t *obj, lv_event_t event)
{
if(event == LV_EVENT_CLICKED)
{
connect_and_send_event(server1, B);
connect_and_send_event(server2, B);
}
}
static void event_c_handler(lv_obj_t *obj, lv_event_t event)
{
if(event == LV_EVENT_CLICKED)
{
connect_and_send_event(server1, C);
connect_and_send_event(server2, C);
}
}
static void event_d_handler(lv_obj_t *obj, lv_event_t event)
{
if(event == LV_EVENT_CLICKED)
{
connect_and_send_event(server1, D);
connect_and_send_event(server2, D);
}
}
static void event_e_handler(lv_obj_t *obj, lv_event_t event)
{
if(event == LV_EVENT_CLICKED)
{
connect_and_send_event(server1, E);
connect_and_send_event(server2, E);
}
}
static void event_f_handler(lv_obj_t *obj, lv_event_t event)
{
if(event == LV_EVENT_CLICKED)
{
connect_and_send_event(server1, F);
connect_and_send_event(server2, F);
}
}
static void restart_handler(lv_obj_t *obj, lv_event_t event)
{
Serial.println("Restarting...");
delay(100);
ESP.restart();
}
static void build_ui()
{
//Build UI
ttgo = TTGOClass::getWatch();
ttgo->begin();
ttgo->openBL();
ttgo->lvgl_begin();
lv_obj_t *label;
lv_obj_t *btn1 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn1, restart_handler);
lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, 90);
label = lv_label_create(btn1, NULL);
lv_label_set_text(label, "Restart");
lv_obj_t *btn2 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn2, event_a_handler);
lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, -80, -90);
lv_btn_set_fit2(btn2, LV_FIT_TIGHT, LV_FIT_TIGHT);
label = lv_label_create(btn2, NULL);
lv_label_set_text(label, "A");
lv_obj_t *btn3 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn3, event_b_handler);
lv_obj_align(btn3, NULL, LV_ALIGN_CENTER, 0, -90);
lv_btn_set_fit2(btn3, LV_FIT_TIGHT, LV_FIT_TIGHT);
label = lv_label_create(btn3, NULL);
lv_label_set_text(label, "B");
lv_obj_t *btn4 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn4, event_c_handler);
lv_obj_align(btn4, NULL, LV_ALIGN_CENTER, 80, -90);
lv_btn_set_fit2(btn4, LV_FIT_TIGHT, LV_FIT_TIGHT);
label = lv_label_create(btn4, NULL);
lv_label_set_text(label, "C");
lv_obj_t *btn5 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn5, event_d_handler);
lv_obj_align(btn5, NULL, LV_ALIGN_CENTER, -80, -40);
lv_btn_set_fit2(btn5, LV_FIT_TIGHT, LV_FIT_TIGHT);
label = lv_label_create(btn5, NULL);
lv_label_set_text(label, "D");
lv_obj_t *btn6 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn6, event_e_handler);
lv_obj_align(btn6, NULL, LV_ALIGN_CENTER, 0, -40);
lv_btn_set_fit2(btn6, LV_FIT_TIGHT, LV_FIT_TIGHT);
label = lv_label_create(btn6, NULL);
lv_label_set_text(label, "E");
lv_obj_t *btn7 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn7, event_f_handler);
lv_obj_align(btn7, NULL, LV_ALIGN_CENTER, 80, -40);
lv_btn_set_fit2(btn7, LV_FIT_TIGHT, LV_FIT_TIGHT);
label = lv_label_create(btn7, NULL);
lv_label_set_text(label, "F");
queueLabel = lv_label_create(lv_scr_act() , NULL);
lv_label_set_text(queueLabel, "");
lv_obj_align(queueLabel, NULL, LV_ALIGN_CENTER, -115, 25);
detectorLabel = lv_label_create(lv_scr_act() , NULL);
lv_label_set_text(detectorLabel, "Detected: -");
lv_obj_align(detectorLabel, NULL, LV_ALIGN_CENTER, 70, 25);
}
void refreshQueueLabel()
{
String content = getQueue();
if(lv_obj_get_screen(queueLabel) == lv_scr_act())
{
int len = content.length() + 1;
char buf[len];
sprintf(buf, "Queue: %s", content);
lv_label_set_text(queueLabel, buf);
}
}
void refreshDetectorLabel()
{
if(lv_obj_get_screen(detectorLabel) == lv_scr_act())
{
if(patternMatched)
{
lv_label_set_text(detectorLabel, "Detected: +");
}
else
{
lv_label_set_text(detectorLabel, "Detected: -");
}
}
}
void setup()
{
Serial.begin(115200);
initCEP();
build_ui();
init_wifi_connection();
}
void loop()
{
lv_task_handler();
handle_connection();
refreshQueueLabel();
refreshDetectorLabel();
delay(500);
}