From 42ced6346b840d39c490bb282b46e0500fc86177 Mon Sep 17 00:00:00 2001 From: melody Date: Tue, 19 Aug 2025 14:21:36 +0200 Subject: [PATCH] fix screen logic --- main.ino | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/main.ino b/main.ino index 0c2627d..3b9c395 100644 --- a/main.ino +++ b/main.ino @@ -215,22 +215,34 @@ class DisplayConfig { tft.setRotation(1); tft.fillScreen(BACKGROUND_COLOUR); }; - Screen* applyScreen(int index){ - this->screenIndex = index; - tft.fillScreen(BACKGROUND_COLOUR); - return this->screens[index]; - }; - Screen* cycleScreen() { - this->screenIndex = (this->screenIndex + 1) % this->screenCount; - return this->applyScreen(this->screenIndex); + + void setScreen(int i) { + this->screenIndex = i; } - Screen* getCurrentScreen() {return this->screens[this->screenIndex];} - Screen* getCurrentScreenIndex() {return this->screenIndex;} + void applyScreen(int i){ + this->setScreen(i); + this->drawScreen(i); + }; + void cycleScreen() {this->screenIndex = (this->screenIndex + 1) % this->screenCount;} + + // Screen* getCurrentScreen() {return this->screens[this->screenIndex];} + int getCurrentScreenIndex() {return this->screenIndex;} + + void drawScreen(int i) { + tft.fillScreen(BACKGROUND_COLOUR); + this->screens[i]->draw(); + } + + void drawCurrentScreen() { + this->drawScreen(this->screenIndex); + } }; + + const byte DEBOUNCE_DELAY = 50; unsigned long previousMillis = -1000000000; unsigned long lastDebounceTime = 0; @@ -254,8 +266,6 @@ Screen* screenBigHum; DisplayConfig* config; -Screen* currentScreen; - void setup() { // Datapoint storage. Arguments: Unit, amount of datapoints to be saved. @@ -293,8 +303,6 @@ void setup() { Serial.begin(115200); Serial.println(F("Serial started.")); - currentScreen = config->getCurrentScreen(); - pinMode(buttonPin, INPUT); Wire.begin(); @@ -310,7 +318,7 @@ void setup() { void loop() { unsigned long currentMillis = millis(); if (sht3xErrorLastCycle) { - config->applyScreen(config->getCurrentScreenIndex()); + config->drawCurrentScreen(); sht3xErrorLastCycle = false; } if (currentMillis - previousMillis >= WAIT_TIME * 1000) { @@ -323,7 +331,7 @@ void loop() { humData->addData(hum); - currentScreen->draw(); + config->drawCurrentScreen(); Serial.print(F("Temperature: ")); Serial.print(temp); @@ -345,7 +353,8 @@ void loop() { } if (digitalRead(buttonPin)){ if (!buttonPressedLastCycle && millis() - lastDebounceTime > DEBOUNCE_DELAY){ - config->cycleScreen()->draw(); + config->cycleScreen(); + config->drawCurrentScreen(); buttonPressedLastCycle = true; lastDebounceTime = millis(); } @@ -353,4 +362,3 @@ void loop() { buttonPressedLastCycle = false; } } -