summaryrefslogtreecommitdiff
path: root/src/driver.cpp
blob: 2beff5b76c3ddca950581aeece55a780b2c9433e (plain)
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
#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();
}