From 59f9b29df3548c20499ef98b5edf5e50afb65b0a Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Mon, 13 Nov 2023 20:02:58 +0100 Subject: [PATCH] Use mutexes to guard the MCP --- src/lib/button_handler.cpp | 15 ++-- src/lib/config.cpp | 151 ++++++++++++++++++++----------------- src/lib/epd.cpp | 9 ++- src/lib/ota.cpp | 63 +++++++++++++++- src/lib/shared.hpp | 3 + 5 files changed, 164 insertions(+), 77 deletions(-) diff --git a/src/lib/button_handler.cpp b/src/lib/button_handler.cpp index 12a0ef4..d4d3f9e 100644 --- a/src/lib/button_handler.cpp +++ b/src/lib/button_handler.cpp @@ -9,6 +9,8 @@ void buttonTask(void *parameter) while (1) { ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + std::lock_guard lock(mcpMutex); + TickType_t currentTime = xTaskGetTickCount(); if ((currentTime - lastDebounceTime) >= debounceDelay) { @@ -35,11 +37,14 @@ void buttonTask(void *parameter) } } mcp.clearInterrupts(); - // Very ugly, but for some reason this is necessary - while (!digitalRead(MCP_INT_PIN)) - { - mcp.clearInterrupts(); - } + } + else + { + } + // Very ugly, but for some reason this is necessary + while (!digitalRead(MCP_INT_PIN)) + { + mcp.clearInterrupts(); } } } diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 9384eb6..2c06590 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -5,6 +5,7 @@ Preferences preferences; Adafruit_MCP23X17 mcp; std::vector screenNameMap(SCREEN_COUNT); +std::mutex mcpMutex; void setup() { @@ -15,12 +16,15 @@ void setup() { queueLedEffect(LED_POWER_TEST); } - if (mcp.digitalRead(3) == LOW) { - preferences.putBool("wifiConfigured", false); + std::lock_guard lockMcp(mcpMutex); + if (mcp.digitalRead(3) == LOW) + { + preferences.putBool("wifiConfigured", false); - WiFi.eraseAP(); - queueLedEffect(LED_EFFECT_WIFI_ERASE_SETTINGS); + WiFi.eraseAP(); + queueLedEffect(LED_EFFECT_WIFI_ERASE_SETTINGS); + } } tryImprovSetup(); @@ -56,8 +60,13 @@ void tryImprovSetup() uint8_t x_buffer[16]; uint8_t x_position = 0; + bool buttonPress = false; + { + std::lock_guard lockMcp(mcpMutex); + buttonPress = (mcp.digitalRead(2) == LOW); + } // Hold second button to start QR code wifi config - if (mcp.digitalRead(2) == LOW) + if (buttonPress) { WiFiManager wm; @@ -81,8 +90,7 @@ void tryImprovSetup() const String qrText = "qrWIFI:S:" + wifiManager->getConfigPortalSSID() + ";T:WPA;P:" + softAP_password.c_str() + ";;"; const String explainText = "*SSID: *\r\n" + wifiManager->getConfigPortalSSID() + "\r\n\r\n*Password:*\r\n" + softAP_password; std::array epdContent = {"Welcome!", "", "To setup\r\nscan QR or\r\nconnect\r\nmanually", "", explainText, "", qrText}; - setEpdContent(epdContent); - }); + setEpdContent(epdContent); }); wm.setSaveConfigCallback([]() { @@ -133,7 +141,7 @@ void tryImprovSetup() vTaskDelay(pdMS_TO_TICKS(400)); } } - // queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); + // queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); } void setupTime() @@ -153,7 +161,6 @@ void setupPreferences() { preferences.begin("btclock", false); - setFgColor(preferences.getUInt("fgColor", DEFAULT_FG_COLOR)); setBgColor(preferences.getUInt("bgColor", DEFAULT_BG_COLOR)); setBlockHeight(preferences.getUInt("blockHeight", 816000)); @@ -171,9 +178,12 @@ void setupWebsocketClients(void *pvParameters) { setupBlockNotify(); - if (preferences.getBool("fetchEurPrice", false)) { + if (preferences.getBool("fetchEurPrice", false)) + { setupPriceFetchTask(); - } else { + } + else + { setupPriceNotify(); } @@ -211,7 +221,7 @@ void setupHardware() WiFi.setHostname(getMyHostname().c_str()); ; if (!psramInit()) - { + { Serial.println(F("PSRAM not available")); } @@ -449,60 +459,63 @@ void WiFiEvent(WiFiEvent_t event) { Serial.printf("[WiFi-event] event: %d\n", event); - switch (event) { - case ARDUINO_EVENT_WIFI_READY: - Serial.println("WiFi interface ready"); - break; - case ARDUINO_EVENT_WIFI_SCAN_DONE: - Serial.println("Completed scan for access points"); - break; - case ARDUINO_EVENT_WIFI_STA_START: - Serial.println("WiFi client started"); - break; - case ARDUINO_EVENT_WIFI_STA_STOP: - Serial.println("WiFi clients stopped"); - break; - case ARDUINO_EVENT_WIFI_STA_CONNECTED: - Serial.println("Connected to access point"); - break; - case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - Serial.println("Disconnected from WiFi access point"); - queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); - break; - case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: - Serial.println("Authentication mode of access point has changed"); - break; - case ARDUINO_EVENT_WIFI_STA_GOT_IP: - Serial.print("Obtained IP address: "); - Serial.println(WiFi.localIP()); - break; - case ARDUINO_EVENT_WIFI_STA_LOST_IP: - Serial.println("Lost IP address and IP address is reset to 0"); - queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); - break; - case ARDUINO_EVENT_WIFI_AP_START: - Serial.println("WiFi access point started"); - break; - case ARDUINO_EVENT_WIFI_AP_STOP: - Serial.println("WiFi access point stopped"); - break; - case ARDUINO_EVENT_WIFI_AP_STACONNECTED: - Serial.println("Client connected"); - break; - case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: - Serial.println("Client disconnected"); - break; - case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: - Serial.println("Assigned IP address to client"); - break; - case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: - Serial.println("Received probe request"); - break; - case ARDUINO_EVENT_WIFI_AP_GOT_IP6: - Serial.println("AP IPv6 is preferred"); - break; - case ARDUINO_EVENT_WIFI_STA_GOT_IP6: - Serial.println("STA IPv6 is preferred"); - break; - default: break; - }} \ No newline at end of file + switch (event) + { + case ARDUINO_EVENT_WIFI_READY: + Serial.println("WiFi interface ready"); + break; + case ARDUINO_EVENT_WIFI_SCAN_DONE: + Serial.println("Completed scan for access points"); + break; + case ARDUINO_EVENT_WIFI_STA_START: + Serial.println("WiFi client started"); + break; + case ARDUINO_EVENT_WIFI_STA_STOP: + Serial.println("WiFi clients stopped"); + break; + case ARDUINO_EVENT_WIFI_STA_CONNECTED: + Serial.println("Connected to access point"); + break; + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.println("Disconnected from WiFi access point"); + queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); + break; + case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: + Serial.println("Authentication mode of access point has changed"); + break; + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.print("Obtained IP address: "); + Serial.println(WiFi.localIP()); + break; + case ARDUINO_EVENT_WIFI_STA_LOST_IP: + Serial.println("Lost IP address and IP address is reset to 0"); + queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); + break; + case ARDUINO_EVENT_WIFI_AP_START: + Serial.println("WiFi access point started"); + break; + case ARDUINO_EVENT_WIFI_AP_STOP: + Serial.println("WiFi access point stopped"); + break; + case ARDUINO_EVENT_WIFI_AP_STACONNECTED: + Serial.println("Client connected"); + break; + case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: + Serial.println("Client disconnected"); + break; + case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: + Serial.println("Assigned IP address to client"); + break; + case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: + Serial.println("Received probe request"); + break; + case ARDUINO_EVENT_WIFI_AP_GOT_IP6: + Serial.println("AP IPv6 is preferred"); + break; + case ARDUINO_EVENT_WIFI_STA_GOT_IP6: + Serial.println("STA IPv6 is preferred"); + break; + default: + break; + } +} \ No newline at end of file diff --git a/src/lib/epd.cpp b/src/lib/epd.cpp index ee3d93d..7671400 100644 --- a/src/lib/epd.cpp +++ b/src/lib/epd.cpp @@ -98,6 +98,8 @@ void refreshFromMemory() void setupDisplays() { + std::lock_guard lockMcp(mcpMutex); + for (uint i = 0; i < NUM_SCREENS; i++) { displays[i].init(0, true, 30); @@ -209,7 +211,12 @@ extern "C" void updateDisplay(void *pvParameters) noexcept ulTaskNotifyTake(pdTRUE, portMAX_DELAY); std::lock_guard lock(epdMutex[epdIndex]); - displays[epdIndex].init(0, false, 40); + + { + std::lock_guard lockMcp(mcpMutex); + + displays[epdIndex].init(0, false, 40); + } uint count = 0; while (EPD_BUSY[epdIndex].digitalRead() == HIGH || count < 10) { diff --git a/src/lib/ota.cpp b/src/lib/ota.cpp index c9d04bc..bf3b37f 100644 --- a/src/lib/ota.cpp +++ b/src/lib/ota.cpp @@ -16,6 +16,7 @@ void setupOTA() ArduinoOTA.setMdnsEnabled(false); ArduinoOTA.setRebootOnSuccess(false); ArduinoOTA.begin(); + //downloadUpdate(); xTaskCreate(handleOTATask, "handleOTA", 4096, NULL, tskIDLE_PRIORITY, &taskOtaHandle); } @@ -78,14 +79,72 @@ void handleOTATask(void *parameter) void downloadUpdate() { + WiFiClientSecure client; + client.setInsecure(); + HTTPClient http; + http.setUserAgent(USER_AGENT); + + // Send HTTP request to CoinGecko API + http.useHTTP10(true); + + http.begin(client, "https://api.github.com/repos/btclock/btclock_v3/releases/latest"); + int httpCode = http.GET(); + + if (httpCode == 200) + { +// WiFiClient * stream = http->getStreamPtr(); + + StaticJsonDocument<64> filter; + + JsonObject filter_assets_0 = filter["assets"].createNestedObject(); + filter_assets_0["name"] = true; + filter_assets_0["browser_download_url"] = true; + + SpiRamJsonDocument doc(1536); + + DeserializationError error = deserializeJson(doc, http.getStream(), DeserializationOption::Filter(filter)); + + if (error) + { + Serial.print("deserializeJson() failed: "); + Serial.println(error.c_str()); + return; + } + + String downloadUrl; + for (JsonObject asset : doc["assets"].as()) + { + if (asset["name"].as().compareTo("firmware.bin") == 0) { + downloadUrl = asset["browser_download_url"].as(); + break; + } + } + + Serial.printf("Download update from %s", downloadUrl); + + + + // esp_http_client_config_t config = { + // .url = CONFIG_FIRMWARE_UPGRADE_URL, + // }; + // esp_https_ota_config_t ota_config = { + // .http_config = &config, + // }; + // esp_err_t ret = esp_https_ota(&ota_config); + // if (ret == ESP_OK) + // { + // esp_restart(); + // } + } } -void onOTAError(ota_error_t error) { +void onOTAError(ota_error_t error) +{ Serial.println("\nOTA update error, restarting"); Wire.end(); SPI.end(); delay(1000); - ESP.restart(); + ESP.restart(); } void onOTAComplete() diff --git a/src/lib/shared.hpp b/src/lib/shared.hpp index 73868a2..85cd95b 100644 --- a/src/lib/shared.hpp +++ b/src/lib/shared.hpp @@ -5,10 +5,13 @@ #include #include #include +#include + #include "utils.hpp" extern Adafruit_MCP23X17 mcp; extern Preferences preferences; +extern std::mutex mcpMutex; const PROGMEM int SCREEN_BLOCK_HEIGHT = 0; const PROGMEM int SCREEN_MSCW_TIME = 1;