FS index.html + App.actions + restructure

This commit is contained in:
Patrick Haßel 2025-01-08 20:32:38 +01:00
parent 65a7f170f9
commit 5b9e0d3f63
23 changed files with 267 additions and 159 deletions

72
data/index.html Normal file
View File

@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>Sporttafel</title>
<style>
body {
font-family: sans-serif;
font-size: 8vw;
margin: 0;
}
button {
width: 33vmin;
height: 33vmin;
font-size: 9vw;
}
button.cancel {
width: 15vmin;
height: 15vmin;
}
table {
border-collapse: collapse;
}
td {
text-align: center;
}
</style>
<script>
function get(path) {
var r = new XMLHttpRequest();
r.open("GET", path, true);
r.send();
}
</script>
</head>
<body>
<table>
<tr>
<td>
<button onclick="get('/action/cancel')" class="cancel">&cross;</button>
</td>
<td>
<button onclick="get('/action/up')">&uarr;</button>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<button onclick="get('/action/left')">&larr;</button>
</td>
<td>
<button onclick="get('/action/confirm')">&check;</button>
</td>
<td>
<button onclick="get('/action/right')">&rarr;</button>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<button onclick="get('/action/down')">&darr;</button>
</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>

View File

@ -2,6 +2,7 @@
platform = espressif32
board = esp32dev
framework = arduino
build_type = debug
board_build.filesystem = littlefs
lib_deps = bblanchon/ArduinoJson
; https://github.com/adafruit/Adafruit_NeoPixel
@ -9,10 +10,10 @@ lib_deps = bblanchon/ArduinoJson
build_flags = -DWIFI_SSID=\"HappyNet\"
-DWIFI_PKEY=\"1Grausame!Sackratte7\"
-DWIFI_HOST=\"Sporttafel\"
;upload_port = 10.0.0.119
;upload_protocol = espota
upload_port = /dev/ttyUSB0
upload_speed = 921600
upload_port = 10.0.0.119
upload_protocol = espota
;upload_port = /dev/ttyUSB0
;upload_speed = 921600
monitor_port = /dev/ttyUSB0
monitor_speed = 115200
monitor_filters = esp32_exception_decoder

View File

@ -1,13 +0,0 @@
#include "INDEX_HTML.h"
const char *INDEX_HTML = R"(<!DOCTYPE html>
<html lang="de">
<head>
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>Sporttafel</title>
</head>
<body>
<h1>Sporttafel</h1>
</body>
</html>
)";

View File

@ -1,6 +0,0 @@
#ifndef INDEX_HTML_H
#define INDEX_HTML_H
extern const char *INDEX_HTML;
#endif

View File

@ -1,27 +1,29 @@
#include "App.h"
#include "../core/log.h"
#include "AppMatch.h"
App *app = nullptr;
uint8_t getHeapUsagePercent() {
return (ESP.getHeapSize() - ESP.getFreeHeap()) / ESP.getHeapSize();
return static_cast<uint8_t>(round((100.0 * ESP.getHeapSize() - ESP.getFreeHeap()) / ESP.getHeapSize()));
}
void appStart(const String& name) {
appStop();
Serial.printf("Loading app: \"%s\" (heap usage: %d%%)\n", name.c_str(), getHeapUsagePercent());
info("Loading app: \"%s\" (heap usage: %d%%)", name.c_str(), getHeapUsagePercent());
if (name.equals(APP_MATCH_NAME)) {
app = new AppMatch();
} else {
Serial.printf("No such app: \"%s\"\n", name.c_str());
error("No such app: \"%s\"\n", name.c_str());
return;
}
Serial.printf("App instantiated: \"%s\" (heap usage: %d%%)\n", app->getName(), getHeapUsagePercent());
info("App instantiated: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent());
app->start();
Serial.printf("App started: \"%s\" (heap usage: %d%%)\n", app->getName(), getHeapUsagePercent());
info("App started: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent());
}
void appStop() {
@ -29,14 +31,14 @@ void appStop() {
return;
}
const auto name = app->getName();
Serial.printf("Stopping app: \"%s\" (heap usage: %d%%)\n", name, getHeapUsagePercent());
info("Stopping app: \"%s\" (heap usage: %d%%)", name, getHeapUsagePercent());
app->stop();
Serial.printf("App stopped: \"%s\" (heap usage: %d%%)\n", name, getHeapUsagePercent());
info("App stopped: \"%s\" (heap usage: %d%%)", name, getHeapUsagePercent());
delete app;
app = nullptr;
Serial.printf("App unloaded: \"%s\" (heap usage: %d%%)\n", name, getHeapUsagePercent());
info("App unloaded: \"%s\" (heap usage: %d%%)", name, getHeapUsagePercent());
display.clear();
display.flush();

View File

@ -3,6 +3,7 @@
#include <ArduinoJson.h>
#include <LittleFS.h>
#include <core/log.h>
class App {
@ -34,12 +35,14 @@ public:
}
void loop() {
static auto lastMillis = millis();
const auto dtMillis = millis() - lastMillis;
const auto now = millis();
static auto lastMillis = now;
const auto dtMillis = now - lastMillis;
lastMillis = now;
_loop(dtMillis);
if (dirty) {
dirty = false;
_draw();
draw();
}
}
@ -47,6 +50,30 @@ public:
_stop();
}
virtual void confirm() {
//
}
virtual void cancel() {
//
}
virtual void up() {
//
}
virtual void down() {
//
}
virtual void left() {
//
}
virtual void right() {
//
}
protected:
template<typename T>
@ -65,7 +92,7 @@ protected:
//
}
virtual void _draw() {
virtual void draw() {
//
}
@ -80,7 +107,9 @@ protected:
private:
void configLoad() {
auto file = LittleFS.open(String("/apps/") + name + ".json", "r");
const auto path = String("/apps/") + name + ".json";
configJson.clear();
auto file = LittleFS.open(path, "r");
deserializeJson(configJson, file);
config = configJson.to<JsonObject>();
}

View File

@ -83,7 +83,7 @@ protected:
if (blinkIntervalMillis > 0) {
const auto now = millis();
if (blinkMillis - now > 500) {
if (now - blinkMillis > 500) {
blinkMillis = now;
blinkState = !blinkState;
markDirty();
@ -102,27 +102,41 @@ protected:
}
}
void _draw() override {
void draw() override {
display.clear();
if (blinkIntervalMillis == 0 || blinkState) {
if (totalMinutes > 0) {
display.setColor(totalMillis < configMillis / 2 ? YELLOW : GREEN);
display.printf("%2d:%02d", totalMinutes, partSeconds);
Serial.printf("%2d:%02d", totalMinutes, partSeconds);
info("%2d:%02d", totalMinutes, partSeconds);
} else if (totalMillis > 0) {
display.setColor(RED);
display.printf("%2d.%02d", partSeconds, partCentis);
} else {
display.printf("00:00");
}
} else if (state == PAUSE) {
info("PAUS", totalMinutes, partSeconds);
}
display.flush();
}
void confirm() override {
if (state == PAUSE) {
setState(MINUTES);
} else {
setState(PAUSE);
}
}
void cancel() override {
_start();
}
private:
void blinkEnable(const unsigned long intervalMillis) {
blinkState = true;
blinkState = false;
blinkIntervalMillis = intervalMillis;
blinkMillis = millis();
}
@ -146,7 +160,7 @@ private:
blinkEnable(100);
break;
}
Serial.printf("state changed to %s", getStateName());
info("state changed to %s", getStateName());
markDirty();
}

View File

@ -1,11 +1,12 @@
#include "filesystem.h"
#include <LittleFS.h>
#include "log.h"
void filesystemMount() {
if (LittleFS.begin(true)) {
Serial.println("filesystem mounted");
info("filesystem mounted");
} else {
Serial.println("failed to mount filesystem");
error("failed to mount filesystem");
}
}

65
src/core/http.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "http.h"
#include <ESPAsyncWebServer.h>
#include <app/App.h>
#include "log.h"
AsyncWebServer server(80);
void httpIndex(AsyncWebServerRequest *request) {
info(request->url().c_str());
request->send(LittleFS, "/index.html", "text/html");
}
void httpActionLeft(AsyncWebServerRequest *request) {
request->send(200);
if (app != nullptr) {
app->left();
}
}
void httpActionUp(AsyncWebServerRequest *request) {
request->send(200);
if (app != nullptr) {
app->up();
}
}
void httpActionDown(AsyncWebServerRequest *request) {
request->send(200);
if (app != nullptr) {
app->down();
}
}
void httpActionRight(AsyncWebServerRequest *request) {
request->send(200);
if (app != nullptr) {
app->right();
}
}
void httpActionCancel(AsyncWebServerRequest *request) {
request->send(200);
if (app != nullptr) {
app->cancel();
}
}
void httpActionConfirm(AsyncWebServerRequest *request) {
request->send(200);
if (app != nullptr) {
app->confirm();
}
}
void httpSetup() {
server.on("/", HTTP_GET, httpIndex);
server.on("/action/left", HTTP_GET, httpActionLeft);
server.on("/action/up", HTTP_GET, httpActionUp);
server.on("/action/down", HTTP_GET, httpActionDown);
server.on("/action/right", HTTP_GET, httpActionRight);
server.on("/action/cancel", HTTP_GET, httpActionCancel);
server.on("/action/confirm", HTTP_GET, httpActionConfirm);
server.begin();
}

View File

@ -3,8 +3,4 @@
void httpSetup();
void httpLoop();
void httpPublish(char *payload);
#endif

View File

@ -1,13 +1,56 @@
#include "log.h"
#include <cstdio>
#include <cstdarg>
#include <cstdio>
#include "clock.h"
void doLog(LogLevel level, const char *format, va_list args);
auto logLevel = DEBUG;
void log(const LogLevel level, const char *format, const va_list args) {
void logSetup() {
delay(500);
Serial.begin(115200);
info("Startup");
}
void log(const LogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);
doLog(level, format, args);
va_end(args);
}
void error(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(ERROR, format, args);
va_end(args);
}
void warn(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(WARN, format, args);
va_end(args);
}
void info(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(INFO, format, args);
va_end(args);
}
void debug(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(DEBUG, format, args);
va_end(args);
}
void doLog(const LogLevel level, const char *format, const va_list args) {
if (level > logLevel) {
return;
}
@ -34,38 +77,3 @@ void log(const LogLevel level, const char *format, const va_list args) {
yield();
}
void log(const LogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);
log(level, format, args);
va_end(args);
}
void error(const char *format, ...) {
va_list args;
va_start(args, format);
log(ERROR, format, args);
va_end(args);
}
void warn(const char *format, ...) {
va_list args;
va_start(args, format);
log(WARN, format, args);
va_end(args);
}
void info(const char *format, ...) {
va_list args;
va_start(args, format);
log(INFO, format, args);
va_end(args);
}
void debug(const char *format, ...) {
va_list args;
va_start(args, format);
log(DEBUG, format, args);
va_end(args);
}

View File

@ -8,7 +8,7 @@ enum LogLevel {
DEBUG = 3
};
void log(LogLevel level, const char *format, ...);
void logSetup();
void error(const char *format, ...);
@ -18,4 +18,6 @@ void info(const char *format, ...);
void debug(const char *format, ...);
void log(LogLevel level, const char *format, ...);
#endif

View File

@ -3,6 +3,8 @@
// #include <Adafruit_NeoPixel.h>
#include "../core/log.h"
#include "Color.h"
#include "font.h"
@ -29,16 +31,6 @@ class Display {
public:
Display() /* : leds(PIXEL_COUNT, GPIO_NUM_13) */ {
Serial.printf("%20s = %d\n", "PIXELS_PER_SEGMENT", PIXELS_PER_SEGMENT);
Serial.printf("%20s = %d\n", "PIXELS_PER_DOT", PIXELS_PER_DOT);
Serial.printf("%20s = %d\n", "SEGMENTS_PER_DIGIT", SEGMENTS_PER_DIGIT);
Serial.printf("%20s = %d\n", "DOTS_PER_DIGIT", DOTS_PER_DIGIT);
Serial.printf("%20s = %d\n", "DIGITS", DIGITS);
Serial.printf("%20s = %d\n", "PIXELS_PER_DIGIT_SEGMENTS", PIXELS_PER_DIGIT_SEGMENTS);
Serial.printf("%20s = %d\n", "PIXELS_PER_DIGIT_DOTS", PIXELS_PER_DIGIT_DOTS);
Serial.printf("%20s = %d\n", "PIXELS_PER_DIGIT_AND_DOTS", PIXELS_PER_DIGIT_AND_DOTS);
Serial.printf("%20s = %d\n", "PIXEL_COUNT", PIXEL_COUNT);
Serial.printf("%20s = %d\n", "PIXEL_BYTE_COUNT", PIXEL_BYTE_COUNT);
// leds.begin();
setBrightness(6);
clear();

View File

@ -1,6 +1,7 @@
#include "font.h"
#include <Arduino.h>
#include "../core/log.h"
SYMBOL SYMBOLS[][SYMBOL_SIZE] = {
{X,X,X,X,X,X,_}, // 0
@ -33,7 +34,7 @@ SYMBOL SYMBOLS[][SYMBOL_SIZE] = {
{_,_,_,_,_,X,X}, // R
{X,X,_,X,X,_,X}, // S
{X,_,_,_,X,X,X}, // T
{_,_,_,X,X,X,_}, // U
{X,_,X,X,X,X,_}, // U
{X,_,X,_,X,_,_}, // V
{X,_,X,_,X,_,X}, // W
{_,_,_,X,_,X,_}, // X
@ -61,7 +62,7 @@ SYMBOL *getSymbol(const char character) {
case '^': return SYMBOLS[38];
case ' ': return SYMBOLS[39];
default: {
Serial.printf("[ERROR] NO SYMBOL MAPPING FOR CHARACTER \"%c\" = #%d\n", character, character);
error("[ERROR] NO SYMBOL MAPPING FOR CHARACTER \"%c\" = #%d\n", character, character);
return SYMBOLS[SYMBOL_SIZE - 1];
}
}

View File

@ -1,53 +0,0 @@
#include "http.h"
#include <ESPAsyncWebServer.h>
#include "INDEX_HTML.h"
#include "log.h"
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
void httpIndex(AsyncWebServerRequest *request) {
request->send(200, "text/html", INDEX_HTML);
}
void httpSetup() {
ws.onEvent([](AsyncWebSocket *socket, AsyncWebSocketClient *client, AwsEventType type, void *arg, unsigned char *message, unsigned length) {
const char *t;
switch (type) {
case WS_EVT_CONNECT:
t = "CONNECT";
break;
case WS_EVT_DISCONNECT:
t = "DISCONNECT";
break;
case WS_EVT_PONG:
t = "PONG";
break;
case WS_EVT_ERROR:
t = "ERROR";
break;
case WS_EVT_DATA:
t = "DATA";
break;
default:
t = "[???]";
break;
}
debug("%s: %s (%d bytes)", client->remoteIP().toString().c_str(), t, length);
});
server.addHandler(&ws);
server.on("/", HTTP_GET, httpIndex);
server.begin();
}
void httpLoop() {
ws.cleanupClients();
}
void httpPublish(char *payload) {
ws.textAll(payload);
}

View File

@ -1,15 +1,13 @@
#include <Arduino.h>
#include <core/log.h>
#include "core/boot.h"
#include "core/filesystem.h"
#include "core/http.h"
#include "core/wifi.h"
#include "boot.h"
#include "filesystem.h"
#include "http.h"
#include "wifi.h"
#include "app/AppMatch.h"
void setup() {
delay(500);
Serial.begin(115200);
Serial.print("Startup\n");
logSetup();
bootDelay();
filesystemMount();
httpSetup();
@ -18,6 +16,5 @@ void setup() {
void loop() {
wifiLoop();
httpLoop();
appLoop();
}