Add build scripts and API calls for WebUI

This commit is contained in:
Djuri Baars 2024-03-17 02:15:59 +01:00
parent d46b07399f
commit 59c9bf4477
11 changed files with 141 additions and 35 deletions

View file

@ -20,6 +20,8 @@ monitor_filters = esp32_exception_decoder, colorize
board_build.filesystem = littlefs
board_build.partitions = partition.csv
build_flags =
!python scripts/git_rev.py
-DLAST_BUILD_TIME=$UNIX_TIME
lib_deps =
zinggjm/GxEPD2@^1.5.6
https://github.com/tzapu/WiFiManager.git#v2.0.17

8
scripts/git_rev.py Normal file
View file

@ -0,0 +1,8 @@
import subprocess
revision = (
subprocess.check_output(["git", "rev-parse", "HEAD"])
.strip()
.decode("utf-8")
)
print("'-DGIT_REV=\"%s\"'" % revision)

View file

@ -38,6 +38,11 @@ void setupPreferences()
{
preferences.putString(SETTING_CURRENCY, CURRENCY_USD);
}
if (!preferences.isKey(SETTING_HOSTNAME_PREFIX))
{
preferences.putString(SETTING_HOSTNAME_PREFIX, "oc");
}
}
void setupWifi()
@ -221,3 +226,4 @@ void OTAUpdateTask(void *pvParameters) {
vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay to avoid high CPU usage
}
}

View file

@ -9,6 +9,10 @@ String currentRow1 = "";
String currentRow2 = "";
String currentRow3 = "";
char currentIcon1;
char currentIcon2;
char currentIcon3;
void setupDisplay()
{
display.init(0, true);
@ -60,8 +64,11 @@ void setupDisplay()
// display.display(true);
}
void updateRow2(String c)
void updateRow2(String c, char icon)
{
if (c.equals(currentRow2) && icon == currentIcon2)
return;
display.setRotation(1);
display.setFont(&ROW2_FONT);
display.setTextColor(GxEPD_BLACK);
@ -82,17 +89,23 @@ void updateRow2(String c)
display.setFont(&ROW2_ICONFONT);
display.setCursor(x, y);
display.print(ICON_BLOCK);
display.print(icon);
display.setFont(&ROW2_FONT);
display.setCursor(x + ROW2_ICONWIDTH, y);
display.print(c);
} while (display.nextPage());
// display.display(true);
currentRow2 = c;
currentIcon2 = icon;
}
void updateRow3(String c)
void updateRow3(String c, char icon)
{
if (c.equals(currentRow3) && icon == currentIcon3)
return;
display.setRotation(1);
display.setFont(&LibreFranklin_SemiBold15pt7b);
display.setTextColor(GxEPD_WHITE);
@ -114,13 +127,16 @@ void updateRow3(String c)
display.setFont(&orangeclock_icons15pt7b);
display.setCursor(x, y);
display.print(ICON_SATS);
display.print(icon);
display.setFont(&LibreFranklin_SemiBold15pt7b);
display.setCursor(x + ROW3_ICONWIDTH, y);
display.print(c);
} while (display.nextPage());
currentRow3 = c;
currentIcon3 = icon;
}
void showSetupText(String t)
@ -133,7 +149,7 @@ void showSetupText(String t)
// center the bounding box by transposition of the origin:
uint16_t x = ((display.width() - (tbw)) / 2) - tbx;
uint16_t y = ((display.height() - tbh) / 2) - tby;
display.setFullWindow();
display.firstPage();
do
{
@ -147,8 +163,11 @@ void showSetupText(String t)
} while (display.nextPage());
}
void updateRow1(String c)
void updateRow1(String c, char icon)
{
if (c.equals(currentRow1) && icon == currentIcon1)
return;
// struct tm timeinfo;
// if (!getLocalTime(&timeinfo))
// {
@ -180,13 +199,16 @@ void updateRow1(String c)
display.setFont(&ROW1_ICONFONT);
display.setCursor(x, y);
display.print(ICON_PIE);
display.print(icon);
display.setFont(&ROW1_FONT);
display.setCursor(x + ROW1_ICONWIDTH, y);
display.print(c);
} while (display.nextPage());
currentRow1 = c;
currentIcon1 = icon;
}
void updateRows(String row1Content, String row2Content, String row3Content)

View file

@ -6,7 +6,7 @@
void setupDisplay();
void showSetupText(String t);
void updateRow1(String c);
void updateRow2(String c);
void updateRow3(String c);
void updateRow1(String c, char icon);
void updateRow2(String c, char icon);
void updateRow3(String c, char icon);
void updateRows(String row1Content, String row2Content, String row3Content);

View file

@ -21,7 +21,7 @@ GxEPD2_BW<EPD_CLASS, EPD_CLASS::HEIGHT> display = EPD_CLASS(4, 2, 3, 1);
GxEPD2_BW<EPD_CLASS, EPD_CLASS::HEIGHT> display = EPD_CLASS(5, 3, 2, 1);
#endif
typedef void (*MethodPtr)(String);
typedef void (*MethodPtr)(String, char);
MethodPtr methods[] = {nullptr, updateRow1, updateRow2, updateRow3};
@ -96,35 +96,40 @@ void loop()
for (uint i = 1; i <= 3; i++)
{
String rowContent = "";
char icon;
char keyName[5];
snprintf(keyName, sizeof(keyName), "row%d", i);
Serial.print(keyName);
Serial.print(" ");
Serial.println(preferences.getUInt(keyName));
switch (preferences.getUInt(keyName))
{
case LINE_BLOCKHEIGHT:
icon = ICON_BLOCK;
rowContent = getBlock();
break;
case LINE_MEMPOOL_FEES:
icon = ICON_PIE;
rowContent = getMempoolFees();
break;
case LINE_MEMPOOL_FEES_MEDIAN:
icon = ICON_PIE;
rowContent = "NOT IMPL";
break;
case LINE_HALVING_COUNTDOWN:
icon = ICON_HOURGLASS;
rowContent = "NOT IMPL";
break;
case LINE_SATSPERUNIT:
{
icon = ICON_SATS;
uint satsPerDollar = int(round(1 / float(getPrice()) * 10e7));
rowContent = satsPerDollar;
break;
}
case LINE_FIATPRICE:
icon = ICON_DOLLAR;
rowContent = getPrice();
break;
case LINE_MARKETCAP:
icon = ICON_GLOBE;
rowContent = "NOT IMPL";
break;
case LINE_TIME:
@ -137,7 +142,7 @@ void loop()
rowContent = "DEFAULT";
}
methods[i](rowContent);
methods[i](rowContent, icon);
}
// String block = String(getBlock());

13
src/shared.cpp Normal file
View file

@ -0,0 +1,13 @@
#include "shared.hpp"
String getMyHostname()
{
uint8_t mac[6];
// WiFi.macAddress(mac);
esp_efuse_mac_get_default(mac);
char hostname[15];
String hostnamePrefix = preferences.getString(SETTING_HOSTNAME_PREFIX);
snprintf(hostname, sizeof(hostname), "%s-%02x%02x%02x", hostnamePrefix,
mac[3], mac[4], mac[5]);
return hostname;
}

View file

@ -37,29 +37,30 @@
#define SETUPFONT LibreFranklin_SemiBold12pt7b
#endif
#define ICON_BLOCK "A"
#define ICON_EURO "B"
#define ICON_POUND "C"
#define ICON_YEN "D"
#define ICON_DOLLAR "E"
#define ICON_PIE "F"
#define ICON_GLOBE "G"
#define ICON_HOURGLASS "H"
#define ICON_LIGHTNING "I"
#define ICON_REFRESH "J"
#define ICON_NUCLEAR "K"
#define ICON_SATS "L"
#define ICON_SATUSD "M"
#define ICON_SETTINGS "N"
#define ICON_WIFI "O"
#define ICON_CROSS "P"
#define ICON_CHECK "Q"
#define ICON_WARNING "R"
#define ICON_BLOCK 'A'
#define ICON_EURO 'B'
#define ICON_POUND 'C'
#define ICON_YEN 'D'
#define ICON_DOLLAR 'E'
#define ICON_PIE 'F'
#define ICON_GLOBE 'G'
#define ICON_HOURGLASS 'H'
#define ICON_LIGHTNING 'I'
#define ICON_REFRESH 'J'
#define ICON_NUCLEAR 'K'
#define ICON_SATS 'L'
#define ICON_SATUSD 'M'
#define ICON_SETTINGS 'N'
#define ICON_WIFI 'O'
#define ICON_CROSS 'P'
#define ICON_CHECK 'Q'
#define ICON_WARNING 'R'
#define SETTING_ROW1_CONTENT "row1"
#define SETTING_ROW2_CONTENT "row2"
#define SETTING_ROW3_CONTENT "row3"
#define SETTING_CURRENCY "currency"
#define SETTING_HOSTNAME_PREFIX "hostnamePrefix"
const int LINE_BLOCKHEIGHT = 0;
const int LINE_MEMPOOL_FEES = 1;
@ -83,3 +84,13 @@ extern WiFiClientSecure client;
extern GxEPD2_BW<EPD_CLASS, EPD_CLASS::HEIGHT> display;
extern Preferences preferences;
extern bool isUpdating;
extern String currentRow1;
extern String currentRow2;
extern String currentRow3;
extern char currentIcon1;
extern char currentIcon2;
extern char currentIcon3;
String getMyHostname();

View file

@ -13,6 +13,7 @@ void setupWebserver()
Serial.println(F("An Error has occurred while mounting LittleFS"));
}
server.on("/api/status", HTTP_GET, onApiStatus);
server.on("/api/settings", HTTP_GET, onApiSettingsGet);
AsyncCallbackJsonWebHandler *settingsPatchHandler =
new AsyncCallbackJsonWebHandler("/api/json/settings", onApiSettingsPatch);
@ -31,6 +32,31 @@ void setupWebserver()
server.begin();
}
void onApiStatus(AsyncWebServerRequest *request)
{
JsonDocument root;
root["row1"] = currentRow1;
root["row2"] = currentRow2;
root["row3"] = currentRow3;
root["icon1"] = String(currentIcon1);
root["icon2"] = String(currentIcon2);
root["icon3"] = String(currentIcon3);
root["espUptime"] = esp_timer_get_time() / 1000000;
root["espFreeHeap"] = ESP.getFreeHeap();
root["espHeapSize"] = ESP.getHeapSize();
root["rssi"] = WiFi.RSSI();
AsyncResponseStream *response =
request->beginResponseStream("application/json");
serializeJson(root, *response);
request->send(response);
}
void onApiSettingsGet(AsyncWebServerRequest *request)
{
JsonDocument root;
@ -49,6 +75,17 @@ void onApiSettingsGet(AsyncWebServerRequest *request)
root[setting] = preferences.getBool(setting.c_str());
}
root["hostname"] = getMyHostname();
root["ip"] = WiFi.localIP();
root["txPower"] = WiFi.getTxPower();
#ifdef GIT_REV
root["gitRev"] = String(GIT_REV);
#endif
#ifdef LAST_BUILD_TIME
root["lastBuildTime"] = String(LAST_BUILD_TIME);
#endif
AsyncResponseStream *response =
request->beginResponseStream("application/json");
serializeJson(root, *response);

View file

@ -9,6 +9,8 @@
void setupWebserver();
void onApiStatus(AsyncWebServerRequest *request);
void onApiSettingsGet(AsyncWebServerRequest *request);
void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json);