From 62e5882d1f02f206239c2b0676ea977e98fd0b93 Mon Sep 17 00:00:00 2001 From: Chad Date: Sat, 18 Apr 2026 16:14:29 -0500 Subject: [PATCH] Fix WiFi disconnect on scan and add countdown timer to Hard Money screen --- src/main.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 113 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 35eb422..4ad53b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -190,7 +190,10 @@ double silverHighDay = 0, silverLowDay = 0, silverHighMonth = 0, silverLowMonth int hmSparklinePeriod = 2; bool hmNeedsRefresh = false; unsigned long hmLastFetch = 0; -const unsigned long HM_FETCH_INTERVAL = 60000; +const unsigned long HM_FETCH_INTERVAL_DAILY = 180000; +const unsigned long HM_FETCH_INTERVAL_HISTORICAL = 86400000; +bool hmHighLowInitialized = false; +bool hmFirstLoad = true; enum ScreenState { SCREEN_DASHBOARD, @@ -582,6 +585,11 @@ void fetchHmHighLowForSymbol(const char* symbol, double* highDay, double* lowDay } } +bool checkPriceBreaksLimits(double price, double highMonth, double lowMonth) { + if (price <= 0 || highMonth <= 0 || lowMonth <= 0) return false; + return (price > highMonth || price < lowMonth); +} + void fetchAllHmData(int period) { btcHmCount = goldHmCount = silverHmCount = 0; btcHmLoaded = goldHmLoaded = silverHmLoaded = false; @@ -595,9 +603,27 @@ void fetchAllHmData(int period) { fetchHmHistoryForSymbol("SI=F", silverHmHistory, &silverHmCount, period); silverHmLoaded = silverHmCount > 0; - fetchHmHighLowForSymbol("BTC-USD", &btcHighDay, &btcLowDay, &btcHighMonth, &btcLowMonth, &btcHighYear, &btcLowYear); - fetchHmHighLowForSymbol("GC=F", &goldHighDay, &goldLowDay, &goldHighMonth, &goldLowMonth, &goldHighYear, &goldLowYear); - fetchHmHighLowForSymbol("SI=F", &silverHighDay, &silverLowDay, &silverHighMonth, &silverLowMonth, &silverHighYear, &silverLowYear); + if (!hmHighLowInitialized || hmFirstLoad) { + fetchHmHighLowForSymbol("BTC-USD", &btcHighDay, &btcLowDay, &btcHighMonth, &btcLowMonth, &btcHighYear, &btcLowYear); + fetchHmHighLowForSymbol("GC=F", &goldHighDay, &goldLowDay, &goldHighMonth, &goldLowMonth, &goldHighYear, &goldLowYear); + fetchHmHighLowForSymbol("SI=F", &silverHighDay, &silverLowDay, &silverHighMonth, &silverLowMonth, &silverHighYear, &silverLowYear); + hmHighLowInitialized = true; + hmFirstLoad = false; + } else { + bool btcBreaks = checkPriceBreaksLimits(btcPrice, btcHighMonth, btcLowMonth); + bool goldBreaks = checkPriceBreaksLimits(goldPrice, goldHighMonth, goldLowMonth); + bool silverBreaks = checkPriceBreaksLimits(silverPrice, silverHighMonth, silverLowMonth); + + if (btcBreaks) { + fetchHmHighLowForSymbol("BTC-USD", &btcHighDay, &btcLowDay, &btcHighMonth, &btcLowMonth, &btcHighYear, &btcLowYear); + } + if (goldBreaks) { + fetchHmHighLowForSymbol("GC=F", &goldHighDay, &goldLowDay, &goldHighMonth, &goldLowMonth, &goldHighYear, &goldLowYear); + } + if (silverBreaks) { + fetchHmHighLowForSymbol("SI=F", &silverHighDay, &silverLowDay, &silverHighMonth, &silverLowMonth, &silverHighYear, &silverLowYear); + } + } hmLastFetch = millis(); } @@ -939,8 +965,6 @@ void drawIndexCustomizeScreen() { void scanWifiNetworks() { WiFi.mode(WIFI_STA); - WiFi.disconnect(); - delay(100); int n = WiFi.scanNetworks(); scannedCount = 0; @@ -1656,9 +1680,13 @@ void drawHardMoneyDetailScreen() { lcd.fillSmoothRoundRect(0, 0, SCREEN_WIDTH, 45, 8, TFT_ORANGE); lcd.setTextColor(TFT_WHITE, TFT_ORANGE); + lcd.setFont(&fonts::DejaVu18); + lcd.setTextDatum(textdatum_t::middle_left); + lcd.drawString("< BACK", 15, 22); lcd.setFont(&fonts::DejaVu24); lcd.setTextDatum(textdatum_t::middle_center); lcd.drawString("HARD MONEY", SCREEN_WIDTH / 2, 22); + drawFlipIcon(SCREEN_WIDTH - 32, 22, 12, TFT_WHITE); int col1X = 50; int col2X = 310; @@ -1816,20 +1844,74 @@ void drawHardMoneyDetailScreen() { } } - lcd.fillSmoothRoundRect(20, SCREEN_HEIGHT - 50, 150, 40, 8, TFT_GRAY); - lcd.setTextColor(TFT_WHITE, TFT_GRAY); + lcd.fillSmoothRoundRect(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40, 8, TFT_ORANGE); + lcd.setTextColor(TFT_WHITE, TFT_ORANGE); lcd.setFont(&fonts::DejaVu18); + lcd.setTextDatum(textdatum_t::middle_left); + String wifiStatus; + if (wifiConnecting) { + wifiStatus = "Connecting..."; + } else if (WiFi.status() == WL_CONNECTED) { + wifiStatus = WiFi.SSID(); + if (wifiStatus.length() > 20) { + wifiStatus = wifiStatus.substring(0, 17) + "..."; + } + } else { + wifiStatus = "OFFLINE"; + } + lcd.drawString("WiFi: " + wifiStatus, 20, SCREEN_HEIGHT - 20); + lcd.setTextDatum(textdatum_t::middle_center); - lcd.drawString("BACK", 95, SCREEN_HEIGHT - 30); + int secondsLeft = (REFRESH_INTERVAL - (millis() - lastRefresh)) / 1000; + if (secondsLeft < 0) secondsLeft = 0; + int mins = secondsLeft / 60; + int secs = secondsLeft % 60; + char timeStr[8]; + sprintf(timeStr, "%d:%02d", mins, secs); + lcd.drawString(String(timeStr), SCREEN_WIDTH / 2, SCREEN_HEIGHT - 20); + + drawGearIcon(SCREEN_WIDTH - 32, SCREEN_HEIGHT - 20, 9, TFT_WHITE); } bool handleHardMoneyTouch(int32_t x, int32_t y) { - if (x >= 20 && x < 170 && y >= SCREEN_HEIGHT - 50 && y < SCREEN_HEIGHT - 10) { + if (x >= 10 && x < 100 && y >= 5 && y < 40) { currentScreen = SCREEN_DASHBOARD; needsRefresh = true; return true; } + if (x >= SCREEN_WIDTH - 55 && x < SCREEN_WIDTH - 10 && y >= 5 && y < 40) { + displayFlipped = !displayFlipped; + preferences.begin("dashboard", false); + preferences.putBool("flipped", displayFlipped); + preferences.end(); + lcd.setRotation(displayFlipped ? 2 : 0); + return true; + } + + if (x >= 20 && x < 200 && y >= SCREEN_HEIGHT - 40 && y < SCREEN_HEIGHT) { + currentScreen = SCREEN_WIFI_CONFIG; + wifiScanDone = false; + wifiPassword = ""; + selectedSSID = ""; + keyboardPos = 0; + keyboardBuffer[0] = '\0'; + keyboardCaps = false; + keyboardShift = false; + keyboardSymbols = false; + showPasswordPlain = false; + scanWifiNetworks(); + return true; + } + + if (x >= SCREEN_WIDTH - 50 && x < SCREEN_WIDTH - 14 && y >= SCREEN_HEIGHT - 32 && y < SCREEN_HEIGHT - 8) { + currentScreen = SCREEN_CUSTOMIZE_STOCKS; + selectedStockSlot = -1; + keyboardPos = 0; + keyboardBuffer[0] = '\0'; + return true; + } + int btnWidth = 50; int btnHeight = 30; int btnGap = 10; @@ -1882,6 +1964,7 @@ bool handleDashboardTouch(int32_t x, int32_t y) { currentScreen = SCREEN_HARD_MONEY_DETAIL; hmSparklinePeriod = 2; hmNeedsRefresh = true; + hmFirstLoad = true; return true; } @@ -2053,11 +2136,24 @@ void loop() { delay(300); } - static unsigned long lastCountdownUpdate = 0; - if (currentScreen == SCREEN_DASHBOARD && millis() - lastCountdownUpdate >= 1000) { - lastCountdownUpdate = millis(); - drawFooterCountdown(); - } +static unsigned long lastCountdownUpdate = 0; +if (currentScreen == SCREEN_DASHBOARD && millis() - lastCountdownUpdate >= 1000) { + lastCountdownUpdate = millis(); + drawFooterCountdown(); +} +if (currentScreen == SCREEN_HARD_MONEY_DETAIL && millis() - lastCountdownUpdate >= 1000) { + lastCountdownUpdate = millis(); + lcd.setFont(&fonts::DejaVu18); + lcd.setTextColor(TFT_WHITE, TFT_ORANGE); + lcd.setTextDatum(textdatum_t::middle_center); + int secondsLeft = (REFRESH_INTERVAL - (millis() - lastRefresh)) / 1000; + if (secondsLeft < 0) secondsLeft = 0; + int mins = secondsLeft / 60; + int secs = secondsLeft % 60; + char timeStr[8]; + sprintf(timeStr, "%d:%02d", mins, secs); + lcd.drawString(String(timeStr), SCREEN_WIDTH / 2, SCREEN_HEIGHT - 20); +} if (currentScreen == SCREEN_HARD_MONEY_DETAIL) { if (hmNeedsRefresh || !btcHmLoaded || !goldHmLoaded || !silverHmLoaded) { @@ -2066,7 +2162,8 @@ void loop() { fetchAllHmData(hmSparklinePeriod); drawHardMoneyDetailScreen(); } - if (millis() - hmLastFetch > HM_FETCH_INTERVAL) { + unsigned long fetchInterval = (hmSparklinePeriod == 0) ? HM_FETCH_INTERVAL_DAILY : HM_FETCH_INTERVAL_HISTORICAL; + if (millis() - hmLastFetch > fetchInterval) { fetchAllHmData(hmSparklinePeriod); drawHardMoneyDetailScreen(); }