From 02c371b0f52e6216b7cc78143149840ac022622d Mon Sep 17 00:00:00 2001 From: h2zero <32826625+h2zero@users.noreply.github.com> Date: Mon, 28 Jun 2021 14:20:58 -0600 Subject: [PATCH] Use native FreeRTOS semaphore functions. (#998) --- main/ZgatewayBT.ino | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 88917c1..cded749 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -34,7 +34,7 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht # ifdef ESP32 # include "FreeRTOS.h" -FreeRTOS::Semaphore semaphoreCreateOrUpdateDevice = FreeRTOS::Semaphore("createOrUpdateDevice"); +SemaphoreHandle_t semaphoreCreateOrUpdateDevice; // Headers used for deep sleep functions # include # include @@ -215,8 +215,10 @@ bool updateWorB(JsonObject& BTdata, bool isWhite) { void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model) { # ifdef ESP32 - if (!semaphoreCreateOrUpdateDevice.take(30000, "createOrUpdateDevice")) + if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(30000)) == pdFALSE) { + Log.error(F("Semaphore NOT taken" CR)); return; + } # endif BLEdevice* device = getDeviceByMac(mac); @@ -251,7 +253,7 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model oneWhite = oneWhite || device->isWhtL; # ifdef ESP32 - semaphoreCreateOrUpdateDevice.give(); + xSemaphoreGive(semaphoreCreateOrUpdateDevice); # endif } @@ -878,6 +880,9 @@ void setupBT() { atomic_init(&jsonBTBufferQueueNext, 0); // in theory, we don't need this atomic_init(&jsonBTBufferQueueLast, 0); // in theory, we don't need this + semaphoreCreateOrUpdateDevice = xSemaphoreCreateBinary(); + xSemaphoreGive(semaphoreCreateOrUpdateDevice); + BLEDevice::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize); BLEDevice::init(""); @@ -1035,11 +1040,13 @@ void launchBTDiscovery() { if (newDevices == 0) return; # ifdef ESP32 - if (!semaphoreCreateOrUpdateDevice.take(1000, "launchBTDiscovery")) + if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(1000) == pdFALSE)) { + Log.error(F("Semaphore NOT taken" CR)); return; + } newDevices = 0; vector localDevices = devices; - semaphoreCreateOrUpdateDevice.give(); + xSemaphoreGive(semaphoreCreateOrUpdateDevice); for (vector::iterator it = localDevices.begin(); it != localDevices.end(); ++it) { # else newDevices = 0; @@ -1747,9 +1754,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding if (WorBupdated) { # ifdef ESP32 - if (!semaphoreCreateOrUpdateDevice.take(1000, "dumpDevices")) { + if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(1000) == pdTRUE)) { dumpDevices(); - semaphoreCreateOrUpdateDevice.give(); + xSemaphoreGive(semaphoreCreateOrUpdateDevice); } # else dumpDevices();