Peter Hajas
2 years ago
commit
f9ba6f7f83
4 changed files with 191 additions and 0 deletions
@ -0,0 +1,49 @@
|
||||
#include <HTTPClient.h> |
||||
#include <FirebaseJson.h> |
||||
|
||||
#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<int>()); |
||||
|
||||
state->volume = result.to<int>(); |
||||
|
||||
playerLastUpdateTime = state->sinceBoot; |
||||
} |
||||
} |
||||
|
||||
void apiSetup(AppState *state) { |
||||
|
||||
} |
@ -0,0 +1,47 @@
|
||||
#define LGFX_AUTODETECT; |
||||
#define LGFX_USE_V1 |
||||
#include <LovyanGFX.hpp> |
||||
|
||||
#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(); |
||||
} |
@ -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(); |
||||
} |
||||
} |
@ -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); |
||||
} |
Loading…
Reference in new issue