Refactor state machine
This commit is contained in:
parent
72734c7faa
commit
f8fe64b1b3
@ -3,6 +3,7 @@
|
|||||||
#include <Adafruit_ST7735.h>
|
#include <Adafruit_ST7735.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266HTTPClient.h>
|
#include <ESP8266HTTPClient.h>
|
||||||
|
#include <memory>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <WiFiClientSecureBearSSL.h>
|
#include <WiFiClientSecureBearSSL.h>
|
||||||
|
|
||||||
@ -46,44 +47,144 @@
|
|||||||
#define STATUS_WIDTH (10 * CX)
|
#define STATUS_WIDTH (10 * CX)
|
||||||
#define STATUS_HEIGHT CY
|
#define STATUS_HEIGHT CY
|
||||||
|
|
||||||
|
// Function definitions
|
||||||
|
|
||||||
|
void drawLayout();
|
||||||
|
uint16_t rgb(uint16_t, uint16_t, uint16_t);
|
||||||
|
String fetchDepartures();
|
||||||
|
|
||||||
// Peripherals
|
// Peripherals
|
||||||
|
|
||||||
Adafruit_ST7735 display(TFT_CS, TFT_DC, TFT_RESET);
|
Adafruit_ST7735 display(TFT_CS, TFT_DC, TFT_RESET);
|
||||||
|
|
||||||
// App state
|
// App state interface
|
||||||
|
|
||||||
typedef enum {
|
class State {
|
||||||
CONNECTING,
|
public:
|
||||||
SHOWING_DEPARTURES
|
virtual void enter() = 0;
|
||||||
} State;
|
virtual void tick() = 0;
|
||||||
|
virtual ~State() = default;
|
||||||
|
};
|
||||||
|
|
||||||
State state = CONNECTING;
|
class StateConnecting : public State {
|
||||||
State previous_state = state;
|
public:
|
||||||
int frame = 0;
|
void enter() override;
|
||||||
// Use this variable to trigger state enter effects
|
void tick() override;
|
||||||
int state_changed = 1;
|
};
|
||||||
|
|
||||||
|
class StateFetching : public State {
|
||||||
|
public:
|
||||||
|
void enter() override;
|
||||||
|
void tick() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class StateShowingDepartures : public State {
|
||||||
|
private:
|
||||||
|
// Todo: discard JsonDocument asap, parse into better format
|
||||||
|
JsonDocument departures;
|
||||||
|
unsigned long entered;
|
||||||
|
public:
|
||||||
|
StateShowingDepartures(String&);
|
||||||
|
void enter() override;
|
||||||
|
void tick() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
// App state implementation
|
||||||
|
|
||||||
|
uint64_t currentTick = 0;
|
||||||
|
// Initially true so that enter() of the initial state is called.
|
||||||
|
bool stateChanged = true;
|
||||||
|
std::unique_ptr<State> state = std::make_unique<StateConnecting>();
|
||||||
|
|
||||||
|
template <typename StateType, typename... Args>
|
||||||
|
void setState(Args&&... args) {
|
||||||
|
state = std::make_unique<StateType>(std::forward<Args>(args)...);
|
||||||
|
stateChanged = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void StateConnecting::enter() {
|
||||||
|
Serial.println("Entering StateConnecting");
|
||||||
|
}
|
||||||
|
|
||||||
|
void StateConnecting::tick() {
|
||||||
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
|
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
||||||
|
display.setCursor(STATUS_X, STATUS_Y);
|
||||||
|
display.printf(" connect %c", "|/-\\"[currentTick % 4]);
|
||||||
|
delay(250);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setState<StateFetching>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetching is a bit of an ugly duckling; it performs all its logic in enter().
|
||||||
|
// This is because request it does is synchronous.
|
||||||
|
// Sadly this means no animation is possible right now.
|
||||||
|
// At least the fetch call still does yield() under the hood :)
|
||||||
|
void StateFetching::enter() {
|
||||||
|
Serial.println("Entering StateFetching");
|
||||||
|
|
||||||
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
|
setState<StateConnecting>();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
||||||
|
display.setCursor(STATUS_X, STATUS_Y);
|
||||||
|
display.print(" fetching");
|
||||||
|
|
||||||
|
String departuresRaw = fetchDepartures();
|
||||||
|
|
||||||
|
setState<StateShowingDepartures>(departuresRaw);
|
||||||
|
};
|
||||||
|
|
||||||
|
void StateFetching::tick() {
|
||||||
|
}
|
||||||
|
|
||||||
|
StateShowingDepartures::StateShowingDepartures(String &departuresRaw) {
|
||||||
|
deserializeJson(departures, departuresRaw);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StateShowingDepartures::enter() {
|
||||||
|
Serial.println("Entering StateShowingDepartures");
|
||||||
|
entered = millis();
|
||||||
|
|
||||||
|
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
||||||
|
display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG);
|
||||||
|
|
||||||
|
int line = 0;
|
||||||
|
for (JsonVariant departure : departures["departures"].as<JsonArray>()) {
|
||||||
|
display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line);
|
||||||
|
display.printf("%2s %-15.15s ",
|
||||||
|
departure["symbol"].as<const char*>(),
|
||||||
|
departure["direction"].as<const char *>()
|
||||||
|
);
|
||||||
|
if (departure["leaving"].as<String>().equals("sofort")) {
|
||||||
|
int16_t x = display.getCursorX();
|
||||||
|
int16_t y = display.getCursorY();
|
||||||
|
display.drawBitmap(x + 6 * CX - tiny_train_dims[0], y, tiny_train[0], tiny_train_dims[0], tiny_train_dims[1], COLOR_TEXT);
|
||||||
|
} else {
|
||||||
|
display.printf("%6s", departure["leaving"].as<const char*>());
|
||||||
|
}
|
||||||
|
|
||||||
|
line++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void StateShowingDepartures::tick() {
|
||||||
|
unsigned long elapsed = millis() - entered;
|
||||||
|
if (elapsed > 25000) {
|
||||||
|
setState<StateFetching>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int buttonPushed = false;
|
int buttonPushed = false;
|
||||||
JsonDocument departures;
|
|
||||||
|
|
||||||
void ICACHE_RAM_ATTR onButtonFalling() {
|
void ICACHE_RAM_ATTR onButtonFalling() {
|
||||||
buttonPushed = true;
|
buttonPushed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t rgb(uint16_t r, uint16_t g, uint16_t b) {
|
|
||||||
// Color layout: RRRRRGGGGGGBBBBB (5, 6, 5)
|
|
||||||
return ((b >> 3) & 0b11111) << 11 | ((g >> 2) & 0b111111) << 5 | ((r >> 3) & 0b11111);
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawLayout() {
|
|
||||||
display.fillScreen(COLOR_BG);
|
|
||||||
display.drawFastHLine(5, 4, 150, COLOR_DIVIDER);
|
|
||||||
display.drawFastHLine(5, 109, 150, COLOR_DIVIDER);
|
|
||||||
display.drawFastHLine(5, 123, 150, COLOR_DIVIDER);
|
|
||||||
display.setCursor(CLOCK_X, CLOCK_Y);
|
|
||||||
display.print("13:12");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
@ -102,10 +203,39 @@ void setup() {
|
|||||||
|
|
||||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||||
attachInterrupt(BUTTON_PIN, onButtonFalling, FALLING);
|
attachInterrupt(BUTTON_PIN, onButtonFalling, FALLING);
|
||||||
|
|
||||||
|
state = std::make_unique<StateConnecting>();
|
||||||
}
|
}
|
||||||
|
|
||||||
#define QUERY_STRING "stop_id=de:08221:1225&platform=A"
|
#define QUERY_STRING "stop_id=de:08221:1225&platform=A"
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (stateChanged) {
|
||||||
|
stateChanged = false;
|
||||||
|
// Note: enter() may call setState(). In that case we want to end up right back here.
|
||||||
|
state->enter();
|
||||||
|
} else {
|
||||||
|
state->tick();
|
||||||
|
currentTick++;
|
||||||
|
}
|
||||||
|
// Let background system do its thing
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t rgb(uint16_t r, uint16_t g, uint16_t b) {
|
||||||
|
// Color layout: RRRRRGGGGGGBBBBB (5, 6, 5)
|
||||||
|
return ((b >> 3) & 0b11111) << 11 | ((g >> 2) & 0b111111) << 5 | ((r >> 3) & 0b11111);
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawLayout() {
|
||||||
|
display.fillScreen(COLOR_BG);
|
||||||
|
display.drawFastHLine(5, 4, 150, COLOR_DIVIDER);
|
||||||
|
display.drawFastHLine(5, 109, 150, COLOR_DIVIDER);
|
||||||
|
display.drawFastHLine(5, 123, 150, COLOR_DIVIDER);
|
||||||
|
display.setCursor(CLOCK_X, CLOCK_Y);
|
||||||
|
display.print("13:12");
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Error handling
|
// TODO: Error handling
|
||||||
String fetchDepartures() {
|
String fetchDepartures() {
|
||||||
std::unique_ptr<BearSSL::WiFiClientSecure> https(new BearSSL::WiFiClientSecure);
|
std::unique_ptr<BearSSL::WiFiClientSecure> https(new BearSSL::WiFiClientSecure);
|
||||||
@ -131,69 +261,3 @@ String fetchDepartures() {
|
|||||||
|
|
||||||
return client.getString();
|
return client.getString();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
|
||||||
if (state != previous_state) {
|
|
||||||
state_changed = 1;
|
|
||||||
previous_state = state;
|
|
||||||
}
|
|
||||||
logic_loop();
|
|
||||||
frame++;
|
|
||||||
state_changed = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void logic_loop() {
|
|
||||||
switch (state) {
|
|
||||||
case CONNECTING: {
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
|
||||||
display.setCursor(STATUS_X, STATUS_Y);
|
|
||||||
display.printf(" connect %c", "|/-\\"[frame % 4]);
|
|
||||||
delay(FRAME_DELAY);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
|
||||||
display.setCursor(STATUS_X, STATUS_Y);
|
|
||||||
display.print(" fetching");
|
|
||||||
|
|
||||||
String departuresRaw = fetchDepartures();
|
|
||||||
deserializeJson(departures, departuresRaw);
|
|
||||||
|
|
||||||
state = SHOWING_DEPARTURES;
|
|
||||||
frame = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
case SHOWING_DEPARTURES:
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
state = CONNECTING;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
display.fillRect(STATUS_X, STATUS_Y, STATUS_WIDTH, STATUS_HEIGHT, COLOR_BG);
|
|
||||||
display.fillRect(MAIN_X, MAIN_Y, MAIN_WIDTH, MAIN_HEIGHT, COLOR_BG);
|
|
||||||
|
|
||||||
int line = 0;
|
|
||||||
for (JsonVariant departure : departures["departures"].as<JsonArray>()) {
|
|
||||||
display.setCursor(MAIN_X, MAIN_Y + (CY + 3) * line);
|
|
||||||
display.printf("%2s %-15.15s ",
|
|
||||||
departure["symbol"].as<const char*>(),
|
|
||||||
departure["direction"].as<const char *>()
|
|
||||||
);
|
|
||||||
if (departure["leaving"].as<String>().equals("sofort")) {
|
|
||||||
int16_t x = display.getCursorX();
|
|
||||||
int16_t y = display.getCursorY();
|
|
||||||
display.drawBitmap(x + 6 * CX - tiny_train_dims[0], y, tiny_train[0], tiny_train_dims[0], tiny_train_dims[1], COLOR_TEXT);
|
|
||||||
} else {
|
|
||||||
display.printf("%6s", departure["leaving"].as<const char*>());
|
|
||||||
}
|
|
||||||
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(25000);
|
|
||||||
state = CONNECTING;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user