diff --git a/src/display.cpp b/src/display.cpp index c89e5a89b283d92c03284ed16db5e5bcb9cbff1e..b444ae36b4fc09ce16dfdfb3f79cefe3ed61fa26 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -39,15 +39,15 @@ void displayLoop() if (runGame) { backgroundLayer.fillScreen({0, 0, 0}); - for (int row = 0; row < SCREEN_HEIGHT; row++) + for (uint8_t row = 0; row < SCREEN_HEIGHT; row++) { - for (int col = 0; col < SCREEN_WIDTH; col++) + for (uint8_t col = 0; col < SCREEN_WIDTH; col++) { - if (currentGame[row][col] > 0) + if (currentGame[row][col]) { if (colorMode > 0) { - int age = colorMap[row][col]; + uint8_t age = colorMap[row][col]; if (colorMode == 1) { pixelColor.red = (uint8_t)(age == 1 ? 255 : 0); @@ -71,7 +71,7 @@ void displayLoop() } } -void ageColor(int age, int decay) +void ageColor(uint8_t age, uint8_t decay) { if (age <= 2 * decay) { @@ -105,14 +105,20 @@ void ageColor(int age, int decay) } } -void showMessage(char *msg) +void showMessage(String strMsg) { + uint8_t fontWidth = 3; + uint8_t fontHeight = 5; indexedLayer.fillScreen(0); - indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg) * 4) / 2, (matrix.getScreenHeight() / 2) - 3, 1, msg); + uint8_t msgLen = strMsg.length(); + uint8_t msgWidth = (msgLen * fontWidth) + msgLen; + char charMsg[msgLen + 1]; + strMsg.toCharArray(charMsg, msgLen + 1); + indexedLayer.drawString((matrix.getScreenWidth() / 2) - (msgWidth / 2), (matrix.getScreenHeight() / 2) - ceil(fontHeight / 2), 1, charMsg); indexedLayer.swapBuffers(false); } -void showEndScreen(int ticks) +void showEndScreen(uint16_t ticks) { char msg_game[15]; sprintf(msg_game, "Game %d ended", gameEra); @@ -124,13 +130,13 @@ void showEndScreen(int ticks) indexedLayer.swapBuffers(false); } -void displayBrightness(int brightness) +void displayBrightness(uint8_t brightness) { // brightness = lightPowerMap8bit[brightness]; matrix.setBrightness(brightness); } -void gameBrightness(int brightness) +void gameBrightness(uint8_t brightness) { // brightness = lightPowerMap8bit[brightness]; backgroundLayer.setBrightness(brightness); diff --git a/src/display.h b/src/display.h index 727b240e4304bc9cde7b6da279ac950ec3e0166f..9b9f31f22df4fb89e519b75cc71124f759db6f1b 100644 --- a/src/display.h +++ b/src/display.h @@ -6,21 +6,21 @@ #include <SmartMatrix.h> extern bool runGame; -extern int defaultBrightness; -extern int gameEra; -extern int colorMode; -extern int colorDecay; +extern uint8_t defaultBrightness; +extern uint16_t gameEra; +extern uint8_t colorMode; +extern uint8_t colorDecay; extern uint8_t colorR; extern uint8_t colorG; extern uint8_t colorB; void setupDisplay(); void displayLoop(); -void ageColor(int age, int decay); -void showEndScreen(int ticks); -void showMessage(char* msg); -void displayBrightness(int brightness); -void gameBrightness(int brightness); +void ageColor(uint8_t age, uint8_t decay); +void showEndScreen(uint16_t ticks); +void showMessage(String msg); +void displayBrightness(uint8_t brightness); +void gameBrightness(uint8_t brightness); void setGameColor(uint8_t r, uint8_t g, uint8_t b); void clearDisplay(); diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp index 4f88ad4cbf274c05ea4ed9a852a1d8cbc6d086eb..cc0fe513c58ac2d067fb4adc4588dc391a79938e 100644 --- a/src/gameoflife.cpp +++ b/src/gameoflife.cpp @@ -9,23 +9,23 @@ bool currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; bool nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; -int colorMap[SCREEN_HEIGHT][SCREEN_WIDTH]; +uint8_t colorMap[SCREEN_HEIGHT][SCREEN_WIDTH]; bool runGame = true; bool gameOver = false; -int gameEra = 0; -int currentTick; -int finalTicks; -int cellsAliveNow; -int cellsAliveBefore; -int noEvolutionTicks; -int noEvolutionTicksLimit = 100; +uint16_t gameEra = 0; +uint16_t currentTick; +uint16_t finalTicks; +uint16_t cellsAliveNow; +uint16_t cellsAliveBefore; +uint8_t noEvolutionTicks; +uint8_t noEvolutionTicksLimit = 100; -const uint scoreScreenTimeout = 5000; +const uint16_t endScreenTimeout = 5000; unsigned long currentMillis; -void setupGameOfLife() +void setupGame() { gameOver = false; gameEra++; @@ -35,7 +35,7 @@ void setupGameOfLife() noEvolutionTicks = 0; randomSeed(analogRead(34)); createRandomMatrix(currentGame); - for (int i = 0; i < 10; i++) + for (uint8_t i = 0; i < 10; i++) { addGlider(); } @@ -44,28 +44,28 @@ void setupGameOfLife() void createRandomMatrix(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { - for (int row = 0; row < SCREEN_HEIGHT; row++) + for (uint8_t row = 0; row < SCREEN_HEIGHT; row++) { - for (int col = 0; col < SCREEN_WIDTH; col++) + for (uint8_t col = 0; col < SCREEN_WIDTH; col++) { - a[row][col] = random(100) < 25 ? 1 : 0; + a[row][col] = random(100) < 25 ? true : false; colorMap[row][col] = 0; } } } -void gameOfLife(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) +void gameTick(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { cellsAliveNow = 0; currentTick++; memcpy(nextGame, a, sizeof(a)); // copy currentGame to nextGame - for (int row = 0; row < SCREEN_HEIGHT; row++) + for (uint8_t row = 0; row < SCREEN_HEIGHT; row++) { - for (int col = 0; col < SCREEN_WIDTH; col++) + for (uint8_t col = 0; col < SCREEN_WIDTH; col++) { - int neighbors = (a[row][(col - 1) % SCREEN_WIDTH] + a[row][(col + 1) % SCREEN_WIDTH] + a[(row - 1) % SCREEN_HEIGHT][col] + + uint8_t neighbors = (a[row][(col - 1) % SCREEN_WIDTH] + a[row][(col + 1) % SCREEN_WIDTH] + a[(row - 1) % SCREEN_HEIGHT][col] + a[(row + 1) % SCREEN_HEIGHT][col] + a[(row - 1) % SCREEN_HEIGHT][(col - 1) % SCREEN_WIDTH] + a[(row - 1) % SCREEN_HEIGHT][(col + 1) % SCREEN_WIDTH] + a[(row + 1) % SCREEN_HEIGHT][(col - 1) % SCREEN_WIDTH] + @@ -77,19 +77,21 @@ void gameOfLife(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) // too few/many neighbors -> die if (neighbors < 2 || neighbors > 3) { - nextGame[row][col] = 0; + nextGame[row][col] = false; colorMap[row][col] = 0; } // else: right amount of neighbors -> stay alive else { - colorMap[row][col]++; + if (colorMap[row][col] < 255) { + colorMap[row][col]++; + } } } // cell was dead, but has right amount of neighbors now -> come alive else if (neighbors == 3) { - nextGame[row][col] = 1; + nextGame[row][col] = true; colorMap[row][col] = 1; cellsAliveNow++; } @@ -104,15 +106,15 @@ void gameOfLife(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) memcpy(a, nextGame, sizeof(nextGame)); // copy nextGame to currentGame } -void createGlider(int row, int col, bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) +void createGlider(uint8_t row, uint8_t col, bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { // 010 // 001 // 111 - int glider[3][3] = {{0, 0, 1}, {1, 0, 1}, {0, 1, 1}}; - for (int y = 0; y < 3; y++) + uint8_t glider[3][3] = {{0, 0, 1}, {1, 0, 1}, {0, 1, 1}}; + for (uint8_t y = 0; y < 3; y++) { - for (int x = 0; x < 3; x++) + for (uint8_t x = 0; x < 3; x++) { a[row + y][col + x] = glider[y][x]; } @@ -131,15 +133,15 @@ void gameLoop() return; } - gameOfLife(currentGame); + gameTick(currentGame); if (gameOver) { - int brightness = constrain(floor(255 * (1 - ((float)(millis() - currentMillis) / (float)scoreScreenTimeout * 2))), 0, 255); + uint8_t brightness = constrain(floor(255 * (1 - ((float)(millis() - currentMillis) / (float)endScreenTimeout * 2))), 0, 255); gameBrightness(brightness); - if (millis() - currentMillis > scoreScreenTimeout) + if (millis() - currentMillis > endScreenTimeout) { resetGame(); } @@ -179,6 +181,6 @@ void gameLoop() void resetGame() { - setupGameOfLife(); + setupGame(); clearDisplay(); } diff --git a/src/gameoflife.h b/src/gameoflife.h index 6697727c4fb994c18b9c9872624bf304310394f9..deff28477ffe1bb4b9f993c5b3b60540474f4ae9 100644 --- a/src/gameoflife.h +++ b/src/gameoflife.h @@ -8,16 +8,16 @@ extern bool currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; extern bool nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; -extern int colorMap[SCREEN_HEIGHT][SCREEN_WIDTH]; +extern uint8_t colorMap[SCREEN_HEIGHT][SCREEN_WIDTH]; extern bool runGame; -extern int noEvolutionTicksLimit; -extern int defaultBrightness; +extern uint8_t noEvolutionTicksLimit; +extern uint8_t defaultBrightness; -void setupGameOfLife(); +void setupGame(); void createRandomMatrix(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); -void gameOfLife(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); -void createGlider(int row, int col, bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); +void gameTick(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); +void createGlider(uint8_t row, uint8_t col, bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); void addGlider(); void gameLoop(); void endGame(); diff --git a/src/main.cpp b/src/main.cpp index a757a57e54fe35185bbb58e27207686de3e524a4..6bec0862bb977cef33a9fd12eff2c437830a49dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,33 +5,26 @@ #include "gameoflife.h" #include "settings.h" -int defaultBrightness; -int gameInterval; -int colorMode; -int colorDecay; +uint8_t defaultBrightness; +uint16_t gameInterval; +uint8_t colorMode; +uint8_t colorDecay; uint8_t colorR; uint8_t colorG; uint8_t colorB; unsigned long lastTick; -void showTitle() { - char msg[13]; - String titleMsg = "Game of Life"; - titleMsg.toCharArray(msg, 13); - showMessage(msg); -} - void setup() { Serial.begin(115200); logLine("", true); loadSettings(); setupDisplay(); - showTitle(); + showMessage("Game of Life"); setupNetwork(); clearDisplay(); - setupGameOfLife(); + setupGame(); } void loop() diff --git a/src/network.cpp b/src/network.cpp index af216d8f3562cb76c5fb88f2e371a25fd4aaa8be..fe1100d0004644ce990bbf1c98e35efe877f2853 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -13,7 +13,7 @@ AsyncWebSocket ws("/ws"); ESPTelnet telnet; IPAddress ip; -int otaProgress = 0; +uint8_t otaProgress = 0; void setupNetwork() { @@ -69,11 +69,11 @@ void setupOTA() clearDisplay(); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - int percent = ceil(progress/(total/100)); + uint8_t percent = ceil(progress/(total/100)); if (percent > otaProgress) { - char msg[10]; + char msg[9]; sprintf(msg, "OTA:%3d%%", percent); - showMessage(msg); + showMessage(String(msg)); logLine("OTA: ", false); logLine((String)percent, false); logLine("%"); @@ -316,7 +316,7 @@ void sendHighscores() ws.textAll(json); } -void sendGameStats(int game, int ticks, int cells) +void sendGameStats(uint16_t game, uint16_t ticks, uint16_t cells) { DynamicJsonDocument doc(200); JsonObject stats = doc.createNestedObject("stats"); diff --git a/src/network.h b/src/network.h index b57b5b7afd867e1bd0a8faf5020a4a9ca9b88c03..1dca27988737c2cbb50db34844f42a6e4804aad0 100644 --- a/src/network.h +++ b/src/network.h @@ -13,8 +13,8 @@ #include <ArduinoJson.h> extern bool runGame; -extern int defaultBrightness; -extern int gameInterval; +extern uint8_t defaultBrightness; +extern uint16_t gameInterval; extern Preferences preferences; void setupNetwork(); @@ -34,6 +34,6 @@ void handleJson(uint8_t* data); void updateSettings(StaticJsonDocument<200U> doc); void sendSettings(); void sendHighscores(); -void sendGameStats(int game, int ticks, int cells); +void sendGameStats(uint16_t game, uint16_t ticks, uint16_t cells); #endif diff --git a/src/settings.cpp b/src/settings.cpp index 8faba919c3fcd118a1c960637c118905cec60510..537a20a3f86bc6e7287ff8477850e751901bc6a6 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -40,7 +40,7 @@ void clearSettings() { ESP.restart(); } -bool updateHighscores(int games, int ticks, int cells) { +bool updateHighscores(uint16_t games, uint16_t ticks, uint16_t cells) { bool changed = false; Highscores hs = getHighscores(); preferences.begin("highscores", false); diff --git a/src/settings.h b/src/settings.h index 2190a76bba6fb4721d31edc8622db67b9f8783ff..911340df4d000b5e629c4b6a5a3566f8b6bdaed9 100644 --- a/src/settings.h +++ b/src/settings.h @@ -5,22 +5,22 @@ #include <Arduino.h> #include <Preferences.h> -extern int defaultBrightness; -extern int gameInterval; +extern uint8_t defaultBrightness; +extern uint16_t gameInterval; extern uint8_t colorR; extern uint8_t colorG; extern uint8_t colorB; typedef struct { - int games; - int ticks; - int cells; + uint16_t games; + uint16_t ticks; + uint16_t cells; } Highscores; void loadSettings(); void saveSettings(); void clearSettings(); -bool updateHighscores(int games, int ticks, int cells); +bool updateHighscores(uint16_t games, uint16_t ticks, uint16_t cells); Highscores getHighscores(); #endif