summaryrefslogtreecommitdiff
path: root/src/driver.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/driver.cpp')
-rw-r--r--src/driver.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/driver.cpp b/src/driver.cpp
new file mode 100644
index 0000000..2beff5b
--- /dev/null
+++ b/src/driver.cpp
@@ -0,0 +1,89 @@
+#include <SPI.h>
+#include <TFT_eSPI.h>
+#include <WiFi.h>
+#include <WebServer.h>
+
+#define WIFI_HOTSPOT_IP "192.168.4.1"
+
+static TFT_eSPI tft;
+
+void display_setup(void) {
+ tft.init();
+ tft.setRotation(1);
+ tft.writecommand(TFT_DISPON);
+ tft.fillScreen(TFT_BLACK);
+}
+
+void display_on(void) {
+ tft.writecommand(TFT_DISPON);
+}
+
+void display_off(void) {
+ tft.writecommand(TFT_DISPOFF);
+}
+
+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([=]() {
+ WifiRequest req = {
+ .uri = server.uri().c_str(),
+ .method = server.method() == HTTP_GET ? "GET" : "POST",
+ .body = server.hasArg("plain") ? server.arg("plain").c_str() : nullptr,
+ };
+ 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();
+}