Webserver

This commit is contained in:
Patrick Haßel 2021-12-31 15:48:27 +01:00
parent 2351c42db2
commit 8f8a63cfb4
4 changed files with 64 additions and 14 deletions

View File

@ -30,4 +30,4 @@ add_custom_target(
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
) )
add_executable(Z_DUMMY_TARGET ${SRC_LIST} src/mode/Test/Border.h src/mode/Clock/Clock.h src/mode/SpaceInvaders/SpaceInvaders.h src/mode/Timer.h src/mode/Pong/Pong.cpp src/BASICS.cpp src/display/Display.cpp src/mode/NewYear/NewYear.h src/mode/NewYear/Firework.h src/mode/NewYear/NewYear.cpp) add_executable(Z_DUMMY_TARGET ${SRC_LIST})

View File

@ -13,6 +13,7 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
lib_deps = https://github.com/adafruit/Adafruit_NeoPixel lib_deps = https://github.com/adafruit/Adafruit_NeoPixel
build_flags = -D PROG_ALL=true
;upload_port = 10.0.0.120 ;upload_port = 10.0.0.120
;upload_protocol = espota ;upload_protocol = espota
upload_port = /dev/ttyUSB0 upload_port = /dev/ttyUSB0

View File

@ -2,6 +2,7 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "mode/Mode.h" #include "mode/Mode.h"
#include "display/Display.h" #include "display/Display.h"
#include <WebServer.h>
#if PROG_ALL #if PROG_ALL
#include "mode/GameOfLife/GameOfLife.h" #include "mode/GameOfLife/GameOfLife.h"
@ -28,6 +29,8 @@ enum ModeId {
NEW_YEAR, NEW_YEAR,
}; };
WebServer server(80);
Display display(32, 8); Display display(32, 8);
ModeId newModeId = NEW_YEAR; ModeId newModeId = NEW_YEAR;
@ -52,6 +55,37 @@ void loadNewMode();
void setBrightness(int value); void setBrightness(int value);
void web_index() {
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", "");
server.sendContent("<a href='/mode?mode=0'>NONE</a><br>");
server.sendContent("<a href='/mode?mode=1'>BORDER</a><br>");
server.sendContent("<a href='/mode?mode=2'>CLOCK</a><br>");
server.sendContent("<a href='/mode?mode=3'>GAME_OF_LIFE_BLACK_WHITE</a><br>");
server.sendContent("<a href='/mode?mode=4'>GAME_OF_LIFE_GRAYSCALE</a><br>");
server.sendContent("<a href='/mode?mode=5'>GAME_OF_LIFE_COLOR_FADE</a><br>");
server.sendContent("<a href='/mode?mode=6'>GAME_OF_LIFE_RANDOM_COLOR</a><br>");
server.sendContent("<a href='/mode?mode=7'>PONG</a><br>");
server.sendContent("<a href='/mode?mode=8'>SPACE_INVADERS</a><br>");
server.sendContent("<a href='/mode?mode=9'>NEW_YEAR</a><br>");
server.client().flush();
}
void web_setMode() {
if (!server.hasArg("mode")) {
server.send(400, "text/plain", "Missing 'mode'");
return;
}
double value = strtod(server.arg("mode").c_str(), nullptr);
if (isnan(value)) {
server.send(400, "text/plain", "'mode' not a number");
return;
}
newModeId = (ModeId) value;
server.sendHeader("location", "/");
server.send(301, "text/plain", "ok");
}
void setup() { void setup() {
delay(500); delay(500);
Serial.begin(115200); Serial.begin(115200);
@ -79,6 +113,12 @@ void setup() {
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
server.on("", web_index);
server.on("/", web_index);
server.on("/mode", web_setMode);
server.on("/mode/", web_setMode);
server.begin();
display.setup(); display.setup();
} }
@ -86,6 +126,7 @@ void setSpeed(double value);
void loop() { void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
server.handleClient();
bool hasIp = (uint32_t) WiFi.localIP() != 0; bool hasIp = (uint32_t) WiFi.localIP() != 0;
if (!connected) { if (!connected) {
if (hasIp) { if (hasIp) {

View File

@ -80,12 +80,12 @@ class NewYear : public Mode {
x++; x++;
display->print(&x, 1, s % 10, COLOR_WHITE); display->print(&x, 1, s % 10, COLOR_WHITE);
} else { } else {
for (Firework *firework = fireworksBegin; firework < fireworksEnd; firework++) { drawYear(counter, year);
if (firework->isAlive()) { drawFirework(timer);
firework->step(timer->interval);
firework->draw();
} }
} }
void drawYear(uint32_t counter, int year) {
uint8_t x = 8; uint8_t x = 8;
display->print(&x, 1, year / 1000 % 10, counter % 64 != 0 ? COLOR_WHITE : COLOR_BLACK); display->print(&x, 1, year / 1000 % 10, counter % 64 != 0 ? COLOR_WHITE : COLOR_BLACK);
x++; x++;
@ -95,6 +95,14 @@ class NewYear : public Mode {
x++; x++;
display->print(&x, 1, year / 1 % 10, counter % 64 != 3 ? COLOR_WHITE : COLOR_BLACK); display->print(&x, 1, year / 1 % 10, counter % 64 != 3 ? COLOR_WHITE : COLOR_BLACK);
} }
void drawFirework(const Timer *timer) const {
for (auto *firework = (Firework *) fireworksBegin; firework < fireworksEnd; firework++) {
if (firework->isAlive()) {
firework->step(timer->interval);
firework->draw();
}
}
} }
public: public: