Handle button presses

This commit is contained in:
Paul Brinkmeier 2025-04-27 18:45:47 +02:00
parent a563cc45b5
commit b753d9f8ea
2 changed files with 31 additions and 9 deletions

View File

@ -2,13 +2,19 @@
## TODO ## TODO
### Server
- [x] Use timeout for fetching departures - [x] Use timeout for fetching departures
- [x] Add basic auth - [x] Add basic auth
- [x] Create Nix package - [x] Create Nix package
- [x] Create container - [x] Create container
- [x] Write ESP8266 client
- [ ] Make port configurable
- [x] Transfer using JSON - [x] Transfer using JSON
- [x] Correctly implement basic auth - [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 - [ ] Add query parameter for selecting EFAClient
### Client
- [x] Write ESP8266 client
- [x] Add code for handling button presses

View File

@ -61,8 +61,12 @@ Adafruit_ST7735 display(TFT_CS, TFT_DC, TFT_RESET);
class State { class State {
public: public:
virtual void enter() = 0; // When the state is entered
virtual void tick() = 0; virtual void enter();
// Called in the business loop
virtual void tick();
// Called when the button is pushed
virtual void button();
virtual ~State() = default; virtual ~State() = default;
}; };
@ -75,7 +79,6 @@ public:
class StateFetching : public State { class StateFetching : public State {
public: public:
void enter() override; void enter() override;
void tick() override;
}; };
class StateShowingDepartures : public State { class StateShowingDepartures : public State {
@ -87,8 +90,15 @@ public:
StateShowingDepartures(String&); StateShowingDepartures(String&);
void enter() override; void enter() override;
void tick() override; void tick() override;
void button() override;
}; };
// Empty default implementations
void State::enter() {}
void State::tick() {}
void State::button() {}
// App state implementation // App state implementation
uint64_t currentTick = 0; uint64_t currentTick = 0;
@ -143,9 +153,6 @@ void StateFetching::enter() {
setState<StateShowingDepartures>(departuresRaw); setState<StateShowingDepartures>(departuresRaw);
}; };
void StateFetching::tick() {
}
StateShowingDepartures::StateShowingDepartures(String &departuresRaw) { StateShowingDepartures::StateShowingDepartures(String &departuresRaw) {
deserializeJson(departures, departuresRaw); deserializeJson(departures, departuresRaw);
} }
@ -154,9 +161,11 @@ void StateShowingDepartures::enter() {
Serial.println("Entering StateShowingDepartures"); Serial.println("Entering StateShowingDepartures");
entered = millis(); entered = millis();
// clear status and main areas
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG); display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG); display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG);
// draw timetable
int line = 0; int line = 0;
for (JsonVariant departure : departures["departures"].as<JsonArray>()) { for (JsonVariant departure : departures["departures"].as<JsonArray>()) {
display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line); display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line);
@ -183,6 +192,10 @@ void StateShowingDepartures::tick() {
} }
} }
void StateShowingDepartures::button() {
setState<StateFetching>();
}
int buttonPushed = false; int buttonPushed = false;
void ICACHE_RAM_ATTR onButtonFalling() { void ICACHE_RAM_ATTR onButtonFalling() {
@ -218,6 +231,9 @@ void loop() {
stateChanged = false; stateChanged = false;
// Note: enter() may call setState(). In that case we want to end up right back here. // Note: enter() may call setState(). In that case we want to end up right back here.
state->enter(); state->enter();
} else if (buttonPushed) {
buttonPushed = false;
state->button();
} else { } else {
state->tick(); state->tick();
currentTick++; currentTick++;