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
|
#include <SPI.h>
#include <TFT_eSPI.h>
#include <WiFi.h>
#include <WebServer.h>
#define WIFI_HOTSPOT_IP "192.168.4.1"
#define BACKLIGHT_PIN 22
static TFT_eSPI tft;
void display_setup(void) {
pinMode(BACKLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, HIGH);
tft.init();
tft.setRotation(1);
tft.writecommand(TFT_DISPON);
tft.fillScreen(TFT_BLACK);
}
void display_on(void) {
tft.writecommand(TFT_DISPON);
digitalWrite(BACKLIGHT_PIN, HIGH);
}
void display_off(void) {
tft.writecommand(TFT_DISPOFF);
digitalWrite(BACKLIGHT_PIN, LOW);
}
void display_begin(void) {
tft.startWrite();
}
void display_end(void) {
tft.endWrite();
}
void display_clear(uint16_t color) {
tft.fillScreen(color);
}
void display_set_pixel(int32_t x, int32_t y, uint16_t color) {
if (x < 0 || x >= DISPLAY_WIDTH || y < 0 || y >= DISPLAY_HEIGHT)
return;
tft.drawPixel(x, y, color);
}
#define BUTTON_PIN 4
void button_setup(void) {
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
bool button_get(void) {
return digitalRead(BUTTON_PIN) == LOW;
}
static WebServer server(80);
static bool hotspot_active = false;
void wifi_setup(void) {
WiFi.mode(WIFI_OFF);
}
void wifi_hotspot_start(const char *ssid, const char *password, WifiHandler handler) {
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
server.onNotFound([=]() {
String uri = server.uri();
String body;
// build body string from parsed args if plain isn't available
if (server.hasArg("plain")) {
body = server.arg("plain");
} else {
for (int i = 0; i < server.args(); i++) {
if (!body.isEmpty()) body += "&";
body += server.argName(i).c_str();
body += "=";
body += server.arg(i).c_str();
}
}
WifiRequest req = {
.uri = uri.c_str(),
.method = server.method() == HTTP_GET ? "GET" : "POST",
.body = body.isEmpty() ? nullptr : body.c_str(),
};
WifiResponse res = handler(&req);
server.send(res.status, res.content_type, res.body);
});
server.begin();
hotspot_active = true;
}
void wifi_hotspot_stop(void) {
server.stop();
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_OFF);
hotspot_active = false;
}
void wifi_tick(void) {
if (hotspot_active)
server.handleClient();
}
|