diff --git a/data/index.htm b/data/index.htm index 25b3efd946bd04db7113946b397832ff3eb74de8..1a8f1482dd7a819c37485b377186858a431ee729 100644 --- a/data/index.htm +++ b/data/index.htm @@ -44,7 +44,13 @@ addMessage("Event: " + e.data); }; es.addEventListener('ota', function(e) { - addMessage("Event[ota]: " + e.data); + addMessage("[OTA] " + e.data); + }, false); + es.addEventListener('game', function(e) { + addMessage("[GAME] " + e.data); + }, false); + es.addEventListener('display', function(e) { + addMessage("[DISPLAY] " + e.data); }, false); } function onBodyLoad(){ diff --git a/src/display.cpp b/src/display.cpp index 856ff8905e93428fff614cdfc24ffc70eeeca73a..18ef2dda2fb2bcee5e140f9b8c51f73c64615c76 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -1,6 +1,7 @@ #include <Arduino.h> #include "gameoflife.h" #include "display.h" +#include "network.h" #define COLOR_DEPTH 24 // known working: 24, 48 - If the sketch uses type `rgb24` directly, COLOR_DEPTH must be 24 const uint8_t kMatrixWidth = 64; // known working: 32, 64, 96, 128 @@ -46,14 +47,19 @@ void displayLoop() } } -void showEndScreen(int ticks) { +void showEndScreen(int ticks) +{ showEvolutions(ticks); } -void fadeOutGame(int brightness) { +void fadeOutGame(int brightness) +{ + char msg[37]; + sprintf(msg, "Setting game layer brightness to: %d", brightness); + sendEvent("display", msg); brightness = lightPowerMap8bit[brightness]; backgroundLayer.setBrightness(brightness); - //backgroundLayer.swapBuffers(false); + // backgroundLayer.swapBuffers(false); } void showEvolutions(int ticks) @@ -62,7 +68,7 @@ void showEvolutions(int ticks) char evolutions[17]; sprintf(evolutions, "Evolutions: %d", ticks); indexedLayer.setFont(font3x5); - indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg) * 4) / 2, (matrix.getScreenHeight() / 2) - 2 - 5 , 1, msg); + indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg) * 4) / 2, (matrix.getScreenHeight() / 2) - 2 - 5, 1, msg); indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(evolutions) * 4) / 2, (matrix.getScreenHeight() / 2) + 2, 1, evolutions); indexedLayer.swapBuffers(); indexedLayer.fillScreen(0); diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp index c508f3de2b0cde996ef006047b06a2998d8a29c3..f90f80a67daafff10d3572bf5dc07caecd09f15d 100644 --- a/src/gameoflife.cpp +++ b/src/gameoflife.cpp @@ -1,6 +1,7 @@ #include <Arduino.h> #include "display.h" #include "gameoflife.h" +#include "network.h" // https://github.com/Stavrosfil/game-of-life-esp32 @@ -31,6 +32,7 @@ void setupGameOfLife() { addGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), g); } + sendEvent("game", "Game of Life has been initialized"); } void createRandomMatrix(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) @@ -72,7 +74,6 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) if (total < 2 || total > 3) { arrayCopy[i][j] = 0; - cellsAliveNow--; } } else if (total == 3) @@ -94,6 +95,9 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) if (currentTick % 2) { cellsAliveBefore = cellsAliveNow; + char msg[50]; + sprintf(msg, "Cells alive now: %d, Cells alive before: %d", cellsAliveNow, cellsAliveBefore); + sendEvent("game", msg); } if (cellsAliveNow >= cellsAliveBefore - 3 && @@ -114,7 +118,10 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) currentMillis = millis(); showEndScreen(finalTicks); gameOver = true; - } else { + sendEvent("game", "No Evolution detected anymore, ending Game of Life"); + } + else + { int brightness = 255 * (1 - ((millis() - currentMillis) / scoreScreenTimeout)); fadeOutGame(brightness); } diff --git a/src/network.cpp b/src/network.cpp index 5053e9599175b140e446bc0879481c17adaca88e..f1f7847ceabe4176c5df1c12729f162eb2b8f63c 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -80,3 +80,7 @@ void networkLoop() { ArduinoOTA.handle(); } + +void sendEvent(const char *tag, const char *msg) { + events.send(msg, tag); +} diff --git a/src/network.h b/src/network.h index 23ff9b0e0b089ec63732ac05411b0e3d41c3b67e..e77c7c7bc6cccf4718b22dc248169f5fb3e583c3 100644 --- a/src/network.h +++ b/src/network.h @@ -19,5 +19,6 @@ void setupWifi(); void setupOTA(); void setupWebserver(); void networkLoop(); +void sendEvent(const char *tag, const char *msg); #endif