From ce683d59013a098d0cc74b34b0584aba8a360efd Mon Sep 17 00:00:00 2001 From: Jan Grewe <jan@faked.org> Date: Sat, 3 Jun 2023 13:38:55 +0200 Subject: [PATCH] some tweaks and speedup --- src/display.cpp | 19 +++++++++++-------- src/gameoflife.cpp | 4 ++-- src/main.cpp | 2 +- src/network.cpp | 9 ++++----- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 8280b49..6aaf300 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -17,7 +17,6 @@ const uint8_t kIndexedLayerOptions = (SM_INDEXED_OPTIONS_NONE); const int defaultBrightness = (brightnessPercent * 255) / 100; rgb24 colorWhite = {0xff, 0xff, 0xff}; -rgb24 colorBlack = {0x00, 0x00, 0x00}; SMARTMATRIX_ALLOCATE_BUFFERS(matrix, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions); SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions); @@ -29,6 +28,7 @@ void setupDisplay() matrix.addLayer(&indexedLayer); matrix.begin(); matrix.setBrightness(defaultBrightness); + indexedLayer.setFont(font3x5); backgroundLayer.setBrightness(255); backgroundLayer.enableColorCorrection(true); } @@ -37,23 +37,26 @@ void displayLoop() { if (runGame) { + backgroundLayer.fillScreen({0, 0, 0}); for (int i = 0; i < SCREEN_HEIGHT; i++) { for (int j = 0; j < SCREEN_WIDTH; j++) { - backgroundLayer.drawPixel(j, i, g[i][j] ? colorWhite : colorBlack); + if (g[i][j]) + { + backgroundLayer.drawPixel(j, i, colorWhite); + } } } backgroundLayer.swapBuffers(false); } } -void showMessage(char* msg) +void showMessage(char *msg) { - indexedLayer.setFont(font3x5); indexedLayer.fillScreen(0); indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg) * 4) / 2, (matrix.getScreenHeight() / 2) - 3, 1, msg); - indexedLayer.swapBuffers(); + indexedLayer.swapBuffers(false); } void showEndScreen(int ticks) @@ -62,16 +65,16 @@ void showEndScreen(int ticks) sprintf(msg_game, "Game %d ended", gameEra); char msg_evo[12]; sprintf(msg_evo, "Ticks: %d", ticks); - indexedLayer.setFont(font3x5); + indexedLayer.fillScreen(0); indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg_game) * 4) / 2, (matrix.getScreenHeight() / 2) - 2 - 5, 1, msg_game); indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg_evo) * 4) / 2, (matrix.getScreenHeight() / 2) + 2, 1, msg_evo); - indexedLayer.swapBuffers(); + indexedLayer.swapBuffers(false); } void gameBrightness(int brightness) { logLine("Game Brightness: " + (String)brightness); - //brightness = lightPowerMap8bit[brightness]; + // brightness = lightPowerMap8bit[brightness]; backgroundLayer.setBrightness(brightness); } diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp index 4ba0309..3c45507 100644 --- a/src/gameoflife.cpp +++ b/src/gameoflife.cpp @@ -141,8 +141,8 @@ void gameLoop() cellsAliveBefore = cellsAliveNow; } - if (cellsAliveNow >= cellsAliveBefore - 3 && - cellsAliveNow <= cellsAliveBefore + 3) + if (cellsAliveNow >= cellsAliveBefore - 5 && + cellsAliveNow <= cellsAliveBefore + 5) { noEvolutionTicks++; } diff --git a/src/main.cpp b/src/main.cpp index eb08576..5b7b1aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,7 @@ int noEvolutionTicksLimit = 100; int brightnessPercent = 20; -int gameInterval = 50; +int gameInterval = 10; TickTwo gameTimer(gameLoop, gameInterval); TickTwo displayTimer(displayLoop, gameInterval); diff --git a/src/network.cpp b/src/network.cpp index f8717ef..df71d8f 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -63,12 +63,12 @@ void setupOTA() clearDisplay(); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - char p[32]; - sprintf(p, "Progress: %u%%\n", (progress/(total/100))); - logLine(p); char msg[10]; sprintf(msg, "OTA:%3d%%", (progress/(total/100))); - showMessage(msg); }); + showMessage(msg); + char p[32]; + sprintf(p, "Progress: %u%%\n", (progress/(total/100))); + logLine(p); }); ArduinoOTA.onError([](ota_error_t error) { if(error == OTA_AUTH_ERROR) logLine("OTA Auth Failed"); @@ -119,7 +119,6 @@ void networkLoop() } } -// (optional) callback functions for telnet events void onTelnetConnect(String ip) { Serial.print("- Telnet: "); -- GitLab