From b753d9f8eaf816b3581f6ffaffff09ca5577709e Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Sun, 27 Apr 2025 18:45:47 +0200 Subject: [PATCH] Handle button presses --- README.md | 12 +++++++++--- client/client.ino | 28 ++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ac179db..e9fc523 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,19 @@ ## TODO +### Server + - [x] Use timeout for fetching departures - [x] Add basic auth - [x] Create Nix package - [x] Create container -- [x] Write ESP8266 client -- [ ] Make port configurable - [x] Transfer using JSON - [x] Correctly implement basic auth -- [ ] Use unidecode to replace non-ascii stuff in the backend +- [ ] Make port configurable +- [ ] Use unidecode to replace non-ascii stuff - [ ] Add query parameter for selecting EFAClient + +### Client + +- [x] Write ESP8266 client +- [x] Add code for handling button presses diff --git a/client/client.ino b/client/client.ino index fe3547e..9bdf47e 100644 --- a/client/client.ino +++ b/client/client.ino @@ -61,8 +61,12 @@ Adafruit_ST7735 display(TFT_CS, TFT_DC, TFT_RESET); class State { public: - virtual void enter() = 0; - virtual void tick() = 0; + // When the state is entered + virtual void enter(); + // Called in the business loop + virtual void tick(); + // Called when the button is pushed + virtual void button(); virtual ~State() = default; }; @@ -75,7 +79,6 @@ public: class StateFetching : public State { public: void enter() override; - void tick() override; }; class StateShowingDepartures : public State { @@ -87,8 +90,15 @@ public: StateShowingDepartures(String&); void enter() override; void tick() override; + void button() override; }; +// Empty default implementations + +void State::enter() {} +void State::tick() {} +void State::button() {} + // App state implementation uint64_t currentTick = 0; @@ -143,9 +153,6 @@ void StateFetching::enter() { setState(departuresRaw); }; -void StateFetching::tick() { -} - StateShowingDepartures::StateShowingDepartures(String &departuresRaw) { deserializeJson(departures, departuresRaw); } @@ -154,9 +161,11 @@ void StateShowingDepartures::enter() { Serial.println("Entering StateShowingDepartures"); entered = millis(); + // clear status and main areas display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG); display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG); + // draw timetable int line = 0; for (JsonVariant departure : departures["departures"].as()) { display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line); @@ -183,6 +192,10 @@ void StateShowingDepartures::tick() { } } +void StateShowingDepartures::button() { + setState(); +} + int buttonPushed = false; void ICACHE_RAM_ATTR onButtonFalling() { @@ -218,6 +231,9 @@ void loop() { stateChanged = false; // Note: enter() may call setState(). In that case we want to end up right back here. state->enter(); + } else if (buttonPushed) { + buttonPushed = false; + state->button(); } else { state->tick(); currentTick++;