Webserver
This commit is contained in:
parent
2351c42db2
commit
8f8a63cfb4
@ -30,4 +30,4 @@ add_custom_target(
|
||||
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})
|
||||
|
||||
@ -13,6 +13,7 @@ platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
lib_deps = https://github.com/adafruit/Adafruit_NeoPixel
|
||||
build_flags = -D PROG_ALL=true
|
||||
;upload_port = 10.0.0.120
|
||||
;upload_protocol = espota
|
||||
upload_port = /dev/ttyUSB0
|
||||
|
||||
41
src/main.cpp
41
src/main.cpp
@ -2,6 +2,7 @@
|
||||
#include <ArduinoOTA.h>
|
||||
#include "mode/Mode.h"
|
||||
#include "display/Display.h"
|
||||
#include <WebServer.h>
|
||||
|
||||
#if PROG_ALL
|
||||
#include "mode/GameOfLife/GameOfLife.h"
|
||||
@ -28,6 +29,8 @@ enum ModeId {
|
||||
NEW_YEAR,
|
||||
};
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
Display display(32, 8);
|
||||
|
||||
ModeId newModeId = NEW_YEAR;
|
||||
@ -52,6 +55,37 @@ void loadNewMode();
|
||||
|
||||
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() {
|
||||
delay(500);
|
||||
Serial.begin(115200);
|
||||
@ -79,6 +113,12 @@ void setup() {
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
|
||||
server.on("", web_index);
|
||||
server.on("/", web_index);
|
||||
server.on("/mode", web_setMode);
|
||||
server.on("/mode/", web_setMode);
|
||||
server.begin();
|
||||
|
||||
display.setup();
|
||||
}
|
||||
|
||||
@ -86,6 +126,7 @@ void setSpeed(double value);
|
||||
|
||||
void loop() {
|
||||
ArduinoOTA.handle();
|
||||
server.handleClient();
|
||||
bool hasIp = (uint32_t) WiFi.localIP() != 0;
|
||||
if (!connected) {
|
||||
if (hasIp) {
|
||||
|
||||
@ -80,20 +80,28 @@ class NewYear : public Mode {
|
||||
x++;
|
||||
display->print(&x, 1, s % 10, COLOR_WHITE);
|
||||
} else {
|
||||
for (Firework *firework = fireworksBegin; firework < fireworksEnd; firework++) {
|
||||
if (firework->isAlive()) {
|
||||
firework->step(timer->interval);
|
||||
firework->draw();
|
||||
}
|
||||
drawYear(counter, year);
|
||||
drawFirework(timer);
|
||||
}
|
||||
}
|
||||
|
||||
void drawYear(uint32_t counter, int year) {
|
||||
uint8_t x = 8;
|
||||
display->print(&x, 1, year / 1000 % 10, counter % 64 != 0 ? COLOR_WHITE : COLOR_BLACK);
|
||||
x++;
|
||||
display->print(&x, 1, year / 100 % 10, counter % 64 != 1 ? COLOR_WHITE : COLOR_BLACK);
|
||||
x++;
|
||||
display->print(&x, 1, year / 10 % 10, counter % 64 != 2 ? COLOR_WHITE : COLOR_BLACK);
|
||||
x++;
|
||||
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();
|
||||
}
|
||||
uint8_t x = 8;
|
||||
display->print(&x, 1, year / 1000 % 10, counter % 64 != 0 ? COLOR_WHITE : COLOR_BLACK);
|
||||
x++;
|
||||
display->print(&x, 1, year / 100 % 10, counter % 64 != 1 ? COLOR_WHITE : COLOR_BLACK);
|
||||
x++;
|
||||
display->print(&x, 1, year / 10 % 10, counter % 64 != 2 ? COLOR_WHITE : COLOR_BLACK);
|
||||
x++;
|
||||
display->print(&x, 1, year / 1 % 10, counter % 64 != 3 ? COLOR_WHITE : COLOR_BLACK);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user