-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP32_AdafruitGFX_2DGameEngine.ino
422 lines (354 loc) · 9.34 KB
/
ESP32_AdafruitGFX_2DGameEngine.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
#include <Adafruit_GFX.h> // Core graphics library
#include <XTronical_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include "Graphics.h"
// set up pins we are going to use to talk to the screen
#define TFT_DC 2 // register select (stands for Data Control perhaps!)
#define TFT_RST 4 // Display reset pin, you can also connect this to the ESP32 reset
// in which case, set this #define pin to -1!
#define TFT_CS 5 // Display enable (Chip select), if not enabled will not talk on SPI bus
#define SD_CS 22
// initialise the routine to talk to this display with these pin connections (as we've missed off
// TFT_SCLK and TFT_MOSI the routine presumes we are using hardware SPI and internally uses 13 and 11
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Graphics gfx = Graphics(&tft);
// WLAN sektion
char* ssid = "WLAN-C6QFYT";
const char* password = "4258803659978466";
// Webserver
WebServer server(80);
int arrowRotation = 0;
SPIClass SpiSettings;
uint16_t color = 0;
void setup(void) {
Serial.begin(115200);
tft.init(); // LCD init
tft.setRotation(0);
Serial.println("LCD init finished");
DrawLoadingScreen(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // WIFI connect
DrawLoadingScreen(20);
delay(500);
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
DrawLoadingScreen(35);
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Signalstärke: ");
Serial.print(GetWifiStrength(10));
Serial.println("");
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
DrawLoadingScreen(50);
server.on("/", handleRoot);
server.on("/test", handleTest);
server.on("/map", handleMap);
server.begin();
DrawLoadingScreen(75);
btStop();
DrawLoadingScreen(80);
Serial.println("Init SD Card");
SpiSettings = *new SPIClass(HSPI);
SpiSettings.begin(25, 27, 26, 22);
if(!SD.begin(22, SpiSettings))
{
DrawErrorAndHalt("SD Card could not be initialized.");
}
DrawLoadingScreen(95);
Serial.println("Start GUI task");
xTaskCreate(
taskOne, /* Task function. */
"GUI task", /* String with name of task. */
10000, /* Stack size in words. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
// Serial.println("Start webserver task");
// xTaskCreate(
// taskTwo, /* Task function. */
// "Webserver task", /* String with name of task. */
// 10000, /* Stack size in words. */
// NULL, /* Parameter passed as input of the task */
// 1, /* Priority of the task. */
// NULL); /* Task handle. */
}
///
///
/// Task One (Core 0)
///
///
void taskOne( void * parameter )
{
while(1)
{
DrawTestImage(color);
color++;
//DrawImageWithFilter();
//DrawFilesOnSD();
//DrawTopThreeNetworks();
//DrawRotatingArrow();
delay(50); // delay wird hier benötigt um dem Watchdog zeit zu geben
}
vTaskDelete( NULL );
}
///
///
/// Task Two (Core 1)
///
///
void taskTwo( void * parameter)
{
while(1)
{
server.handleClient();
}
vTaskDelete( NULL );
}
///
///
/// loop function
///
///
void loop() {
delay(1000);
}
///
///
/// WebServer handling
///
///
void handleRoot()
{
char temp[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf(temp, 400,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP32 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from ESP32!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send(200, "text/html", temp);
}
void handleTest()
{
String message = "Number of args received:";
message += server.args();
message += "\n";
for (int i = 0; i < server.args(); i++) {
message += "Arg nr " + String(i) + " -> ";
message += server.argName(i) + ": ";
message += server.arg(i) + "\n";
}
server.send(200, "text/plain", message);
Serial.println(message);
}
void handleMap()
{
String password = "";
for (int i = 0; i < server.args(); i++) {
if (server.argName(i) == "password")
{
password = server.arg(i);
}
}
if (password == "test")
{
server.send(200, "text/plain", "yay");
}
else
{
server.send(402, "text/plain");
}
}
///
///
/// Graphics calculation
///
///
void DrawRotatingArrow()
{
uint16_t time = millis();
tft.fillScreen(ST7735_BLACK);
time = millis() - time;
drawArrow(ST7735_WHITE, 48, 32, .5, arrowRotation);
tft.displayBuffer();
arrowRotation += 30;
if (arrowRotation >= 360)
{
arrowRotation = 0;
}
}
void drawArrow(uint16_t color, int x, int y, float factor, int rotation)
{
int width = 32 * factor;
int height = 64 * factor;
int arrowHeight = 32 * factor;
int constant = 10 * factor;
//Spitze
int ax = x + (width / 2);
int ay = y + height + arrowHeight;
int bx = x - constant;
int by = y + height;
int cx = x + width + constant - 1;
int cy = y + height - 1;
//Transformierung des Origin zu (0,0) für die rotation
int transformedax = ax - x - width / 2;
int transformeday = ay - y - height / 2;
int transformedbx = bx - x - width / 2;
int transformedby = by - y - height / 2;
int transformedcx = cx - x - width / 2;
int transformedcy = cy - y - height / 2;
float rad = rotation * PI / 180;
// Transformierung und rotation
ax = transformedax * cos(rad) - transformeday * sin(rad) + x + width / 2;
ay = transformeday * cos(rad) + transformedax * sin(rad) + y + height / 2;
bx = transformedbx * cos(rad) - transformedby * sin(rad) + x + width / 2;
by = transformedby * cos(rad) + transformedbx * sin(rad) + y + height / 2;
cx = transformedcx * cos(rad) - transformedcy * sin(rad) + x + width / 2;
cy = transformedcy * cos(rad) + transformedcx * sin(rad) + y + height / 2;
gfx.drawRect(x, y, width, height, rotation, color);
gfx.drawTriangle( ax,
ay,
bx,
by,
cx,
cy,
0,
color);
}
void DrawLoadingScreen(int percentage)
{
tft.fillScreen(ST7735_BLACK);
int margin = 5;
int step = (128 - (2 * margin)) / 100;
gfx.drawText(36, 54, "Loading", ST7735_GREEN);
gfx.drawHLine(margin, margin + step * percentage, 64, ST7735_GREEN);
gfx.drawText(52, 70, String(percentage), ST7735_GREEN);
tft.displayBuffer();
}
void DrawErrorAndHalt(String error)
{
Serial.println(error);
gfx.drawText(error, ST7735_RED);
tft.displayBuffer();
while(1);
}
void DrawImageWithFilter()
{
File img = SD.open("/pepe.bmp");
char* pepe = (char*)malloc(sizeof(char)*img.size());
img.readBytes(pepe, img.size());
tft.displayBuffer();
}
void DrawTestImage(uint16_t color)
{
tft.fillScreen(color);
gfx.drawText(String(color), ST7735_CYAN);
tft.displayBuffer();
}
///
///
/// Funcionality
///
///
void DrawFilesOnSD()
{
tft.fillScreen(ST7735_BLACK);
File root = SD.open("/");
gfx.drawText(GetSDContent(root), ST7735_BLUE);
tft.displayBuffer();
}
void DrawTopThreeNetworks()
{
tft.fillScreen(ST7735_BLACK);
int numberOfNetworks = WiFi.scanNetworks();
String ssidList[8];
int ssidStrenghts[8];
if (numberOfNetworks > 8)
{
numberOfNetworks = 8;
}
for(int i =0; i<numberOfNetworks; i++)
{
ssidList[i] = WiFi.SSID(i);
ssidStrenghts[i] = GetWifiStrengthForSSID(5, i);
}
int best = 0;
int secondBest = 1;
int thirdBest = 2;
// Sortiere Array
String msg = ssidList[best] + " " + ssidStrenghts[best] + "\n\n" +
ssidList[secondBest] + " " + ssidStrenghts[secondBest] + "\n\n" +
ssidList[thirdBest] + " " + ssidStrenghts[thirdBest];
gfx.drawText(msg, ST7735_BLUE);
tft.displayBuffer();
//free(cMsg);
}
///
///
/// Extension Methoden
///
///
int GetWifiStrength(int points){
long rssi = 0;
long averageRSSI=0;
for (int i=0;i < points;i++){
rssi += WiFi.RSSI();
delay(20);
}
averageRSSI=rssi/points;
return averageRSSI;
}
int GetWifiStrengthForSSID(int points, int number){
long rssi = 0;
long averageRSSI=0;
for (int i=0;i < points;i++){
rssi += WiFi.RSSI(number);
delay(20);
}
averageRSSI=rssi/points;
return averageRSSI;
}
String GetSDContent(File dir) {
String result = "";
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
result += entry.name();
if (entry.isDirectory()) {
result += " (D)";
}
entry.close();
result += "\n";
}
return result;
}