diff --git a/CMakeLists.txt b/CMakeLists.txt index 2af601f..8414fc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/platformio.ini b/platformio.ini index d2e6209..889b5f2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/main.cpp b/src/main.cpp index e660b1e..9edc899 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include "mode/Mode.h" #include "display/Display.h" +#include #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("NONE
"); + server.sendContent("BORDER
"); + server.sendContent("CLOCK
"); + server.sendContent("GAME_OF_LIFE_BLACK_WHITE
"); + server.sendContent("GAME_OF_LIFE_GRAYSCALE
"); + server.sendContent("GAME_OF_LIFE_COLOR_FADE
"); + server.sendContent("GAME_OF_LIFE_RANDOM_COLOR
"); + server.sendContent("PONG
"); + server.sendContent("SPACE_INVADERS
"); + server.sendContent("NEW_YEAR
"); + 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) { diff --git a/src/mode/NewYear/NewYear.h b/src/mode/NewYear/NewYear.h index a9753dd..49fcf48 100644 --- a/src/mode/NewYear/NewYear.h +++ b/src/mode/NewYear/NewYear.h @@ -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); } }