diff --git a/platformio.ini b/platformio.ini index 22f1f4a..643b015 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/scripts/git_rev.py b/scripts/git_rev.py new file mode 100644 index 0000000..9594475 --- /dev/null +++ b/scripts/git_rev.py @@ -0,0 +1,8 @@ +import subprocess + +revision = ( + subprocess.check_output(["git", "rev-parse", "HEAD"]) + .strip() + .decode("utf-8") +) +print("'-DGIT_REV=\"%s\"'" % revision) \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index 9363094..65ad555 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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 } } + diff --git a/src/config.hpp b/src/config.hpp index 2def29c..1312aa6 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -20,4 +20,4 @@ void OTAUpdateTask(void *pvParameters); void wakeModemSleep(); void setModemSleep(); -bool inPowerSaveMode(); \ No newline at end of file +bool inPowerSaveMode(); diff --git a/src/epd.cpp b/src/epd.cpp index 9552f3c..10ad8d0 100644 --- a/src/epd.cpp +++ b/src/epd.cpp @@ -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) diff --git a/src/epd.hpp b/src/epd.hpp index a19d202..ab02ff1 100644 --- a/src/epd.hpp +++ b/src/epd.hpp @@ -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); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 049c592..2b42fb5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,7 +21,7 @@ GxEPD2_BW display = EPD_CLASS(4, 2, 3, 1); GxEPD2_BW 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()); diff --git a/src/shared.cpp b/src/shared.cpp new file mode 100644 index 0000000..3258a71 --- /dev/null +++ b/src/shared.cpp @@ -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; +} \ No newline at end of file diff --git a/src/shared.hpp b/src/shared.hpp index 52ef439..b4a5b81 100644 --- a/src/shared.hpp +++ b/src/shared.hpp @@ -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; @@ -82,4 +83,14 @@ const int LINE_DATE = 100; extern WiFiClientSecure client; extern GxEPD2_BW display; extern Preferences preferences; -extern bool isUpdating; \ No newline at end of file +extern bool isUpdating; + +extern String currentRow1; +extern String currentRow2; +extern String currentRow3; + +extern char currentIcon1; +extern char currentIcon2; +extern char currentIcon3; + +String getMyHostname(); diff --git a/src/webserver.cpp b/src/webserver.cpp index 785de57..18f6720 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -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); diff --git a/src/webserver.hpp b/src/webserver.hpp index 7ca57cc..61a154d 100644 --- a/src/webserver.hpp +++ b/src/webserver.hpp @@ -9,6 +9,8 @@ void setupWebserver(); +void onApiStatus(AsyncWebServerRequest *request); + void onApiSettingsGet(AsyncWebServerRequest *request); void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json);