Use mutexes to guard the MCP

This commit is contained in:
Djuri Baars 2023-11-13 20:02:58 +01:00
parent a3783ceffa
commit 59f9b29df3
5 changed files with 164 additions and 77 deletions

View File

@ -9,6 +9,8 @@ void buttonTask(void *parameter)
while (1)
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
std::lock_guard<std::mutex> lock(mcpMutex);
TickType_t currentTime = xTaskGetTickCount();
if ((currentTime - lastDebounceTime) >= debounceDelay)
{
@ -35,13 +37,16 @@ void buttonTask(void *parameter)
}
}
mcp.clearInterrupts();
}
else
{
}
// Very ugly, but for some reason this is necessary
while (!digitalRead(MCP_INT_PIN))
{
mcp.clearInterrupts();
}
}
}
}
void IRAM_ATTR handleButtonInterrupt()

View File

@ -5,6 +5,7 @@
Preferences preferences;
Adafruit_MCP23X17 mcp;
std::vector<std::string> screenNameMap(SCREEN_COUNT);
std::mutex mcpMutex;
void setup()
{
@ -15,6 +16,8 @@ void setup()
{
queueLedEffect(LED_POWER_TEST);
}
{
std::lock_guard<std::mutex> lockMcp(mcpMutex);
if (mcp.digitalRead(3) == LOW)
{
preferences.putBool("wifiConfigured", false);
@ -22,6 +25,7 @@ void setup()
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<std::mutex> 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<String, NUM_SCREENS> epdContent = {"Welcome!", "", "To setup\r\nscan QR or\r\nconnect\r\nmanually", "", explainText, "", qrText};
setEpdContent(epdContent);
});
setEpdContent(epdContent); });
wm.setSaveConfigCallback([]()
{
@ -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();
}
@ -449,7 +459,8 @@ void WiFiEvent(WiFiEvent_t event)
{
Serial.printf("[WiFi-event] event: %d\n", event);
switch (event) {
switch (event)
{
case ARDUINO_EVENT_WIFI_READY:
Serial.println("WiFi interface ready");
break;
@ -504,5 +515,7 @@ void WiFiEvent(WiFiEvent_t event)
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
Serial.println("STA IPv6 is preferred");
break;
default: break;
}}
default:
break;
}
}

View File

@ -98,6 +98,8 @@ void refreshFromMemory()
void setupDisplays()
{
std::lock_guard<std::mutex> 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<std::mutex> lock(epdMutex[epdIndex]);
{
std::lock_guard<std::mutex> lockMcp(mcpMutex);
displays[epdIndex].init(0, false, 40);
}
uint count = 0;
while (EPD_BUSY[epdIndex].digitalRead() == HIGH || count < 10)
{

View File

@ -16,6 +16,7 @@ void setupOTA()
ArduinoOTA.setMdnsEnabled(false);
ArduinoOTA.setRebootOnSuccess(false);
ArduinoOTA.begin();
//downloadUpdate();
xTaskCreate(handleOTATask, "handleOTA", 4096, NULL, tskIDLE_PRIORITY, &taskOtaHandle);
}
@ -78,9 +79,67 @@ 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<JsonArray>())
{
if (asset["name"].as<String>().compareTo("firmware.bin") == 0) {
downloadUrl = asset["browser_download_url"].as<String>();
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();

View File

@ -5,10 +5,13 @@
#include <Adafruit_MCP23X17.h>
#include <ArduinoJson.h>
#include <Preferences.h>
#include <mutex>
#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;