commit f9ba6f7f83a7bc90b293ed9479287fcb3c39c56b Author: Peter Hajas Date: Tue Jul 19 21:59:19 2022 -0600 initial commit diff --git a/api.ino b/api.ino new file mode 100644 index 0000000..ee9e45a --- /dev/null +++ b/api.ino @@ -0,0 +1,49 @@ +#include +#include + +#define PLAYER_UPDATE_INTERVAL_MS (500) + +static unsigned long playerLastUpdateTime = 0; + +String httpURL(String path) { + return String(OWNTONE_SERVER_ADDRESS) + "/api/" + path; +} + +String httpGET(String path) { + HTTPClient http; + + Serial.println(httpURL(path)); + + http.begin(httpURL(path)); + http.GET(); + String out = http.getString(); + http.end(); + + return out; +} + +void apiDebug(AppState *state) { + +} + +void apiLoop(AppState *state) { + bool playerNeedsUpdate = (state->sinceBoot - playerLastUpdateTime) > PLAYER_UPDATE_INTERVAL_MS; + playerNeedsUpdate = playerNeedsUpdate || (playerLastUpdateTime > state->sinceBoot); + if (playerNeedsUpdate) { + String playerText = httpGET(String("player")); + FirebaseJson json; + json.setJsonData(playerText); + FirebaseJsonData result; + json.get(result, "volume"); + Serial.println(result.type); + Serial.println(result.to()); + + state->volume = result.to(); + + playerLastUpdateTime = state->sinceBoot; + } +} + +void apiSetup(AppState *state) { + +} diff --git a/ui.ino b/ui.ino new file mode 100644 index 0000000..06cd758 --- /dev/null +++ b/ui.ino @@ -0,0 +1,47 @@ +#define LGFX_AUTODETECT; +#define LGFX_USE_V1 +#include + +#define UI_WIDTH (320) +#define UI_HEIGHT (240) + +#define STATUS_BAR_HEIGHT (20) + +static LGFX lcd; +static AppState lastAppState; + +void uiDebug(AppState *state) { + +} + +void uiLoop(AppState *state) { + lcd.startWrite(); + + lcd.setTextColor(0x000000U, 0x00FF00); + + bool statusBarDirty = false; + statusBarDirty = state->wifiOnline != lastAppState.wifiOnline; + statusBarDirty = statusBarDirty || (state->volume != lastAppState.volume); + if (statusBarDirty) { + lcd.fillRect(0, 0, UI_WIDTH, STATUS_BAR_HEIGHT, 0x00FF00); + if (state->wifiOnline) { + lcd.drawString(state->wifiIP.toString(), 10, 10); + lcd.drawString(String(state->volume), UI_WIDTH - 20, 10); + } + else { + lcd.drawString("connecting...", 10, 10); + } + } + + lastAppState = *state; + + lcd.endWrite(); +} + +void uiSetup(AppState *state) { + lcd.init(); + lcd.fillScreen(0xFF0000); + lcd.setBrightness(128); + + lastAppState = AppStateMake(); +} diff --git a/wifi.ino b/wifi.ino new file mode 100644 index 0000000..dcfe2eb --- /dev/null +++ b/wifi.ino @@ -0,0 +1,28 @@ +#include "rpcWiFi.h" + +void wifiDebug(AppState *appState) { + Serial.printf("online: %d\n", appState->wifiOnline); + if (appState->wifiOnline) { + Serial.printf("IP: %s\n", WiFi.localIP().toString()); + } +} + +void wifiJoin(AppState *appState) { + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + WiFi.begin(WIFI_SSID, WIFI_PASS); + + while (WiFi.status() != WL_CONNECTED) { + Serial.println("connecting..."); + delay(5000); + } + + appState->wifiOnline = true; + appState->wifiIP = WiFi.localIP(); +} + +void wifiLoop(AppState *appState) { + if (appState->wifiOnline) { + WiFi.localIP(); + } +} diff --git a/wiowntone.ino b/wiowntone.ino new file mode 100644 index 0000000..19ff0b9 --- /dev/null +++ b/wiowntone.ino @@ -0,0 +1,67 @@ +#include "rpcWiFi.h" + +#define DEBUG (false) + +const char *WIFI_SSID = "TortugaNoT"; +const char *WIFI_PASS = "april302021"; +const char *OWNTONE_SERVER_ADDRESS = "http://192.168.1.2:3689"; + +typedef struct AppState { + unsigned long sinceBoot; + + bool wifiOnline; + IPAddress wifiIP; + + int volume; // out of 100; +} AppState; + +AppState AppStateMake(void) { + AppState out; + out.sinceBoot = 0; + out.wifiOnline = false; + out.wifiIP = IPAddress(); + out.volume = 0; + return out; +} + +AppState state; + +void debug() { + wifiDebug(&state); + uiDebug(&state); + apiDebug(&state); +} + +void handleInput() { + +} + +void setup() { + state = AppStateMake(); + + uiSetup(&state); + wifiJoin(&state); + apiSetup(&state); + + // prepare buttons + pinMode(WIO_KEY_A, INPUT_PULLUP); + pinMode(WIO_KEY_B, INPUT_PULLUP); + pinMode(WIO_KEY_C, INPUT_PULLUP); + pinMode(WIO_5S_UP, INPUT_PULLUP); + pinMode(WIO_5S_DOWN, INPUT_PULLUP); + pinMode(WIO_5S_LEFT, INPUT_PULLUP); + pinMode(WIO_5S_RIGHT, INPUT_PULLUP); + pinMode(WIO_5S_PRESS, INPUT_PULLUP); +} + +void loop() { + if (DEBUG) { + debug(); + } + + state.sinceBoot = millis(); + + uiLoop(&state); + wifiLoop(&state); + apiLoop(&state); +}