Use native FreeRTOS semaphore functions. (#998)

This commit is contained in:
h2zero authored and GitHub committed 2021-06-28 15:20:58 -05:00
1 parent 8cbd1984af
commit 02c371b0f5
1 file changed
+14 -7
+14 -7
View File
@@ -34,7 +34,7 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
# ifdef ESP32 # ifdef ESP32
# include "FreeRTOS.h" # include "FreeRTOS.h"
FreeRTOS::Semaphore semaphoreCreateOrUpdateDevice = FreeRTOS::Semaphore("createOrUpdateDevice"); SemaphoreHandle_t semaphoreCreateOrUpdateDevice;
// Headers used for deep sleep functions // Headers used for deep sleep functions
# include <NimBLEAdvertisedDevice.h> # include <NimBLEAdvertisedDevice.h>
# include <NimBLEDevice.h> # include <NimBLEDevice.h>
@@ -215,8 +215,10 @@ bool updateWorB(JsonObject& BTdata, bool isWhite) {
void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model) { void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model) {
# ifdef ESP32 # ifdef ESP32
if (!semaphoreCreateOrUpdateDevice.take(30000, "createOrUpdateDevice")) if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(30000)) == pdFALSE) {
Log.error(F("Semaphore NOT taken" CR));
return; return;
}
# endif # endif
BLEdevice* device = getDeviceByMac(mac); BLEdevice* device = getDeviceByMac(mac);
@@ -251,7 +253,7 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model
oneWhite = oneWhite || device->isWhtL; oneWhite = oneWhite || device->isWhtL;
# ifdef ESP32 # ifdef ESP32
semaphoreCreateOrUpdateDevice.give(); xSemaphoreGive(semaphoreCreateOrUpdateDevice);
# endif # endif
} }
@@ -878,6 +880,9 @@ void setupBT() {
atomic_init(&jsonBTBufferQueueNext, 0); // in theory, we don't need this atomic_init(&jsonBTBufferQueueNext, 0); // in theory, we don't need this
atomic_init(&jsonBTBufferQueueLast, 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::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize);
BLEDevice::init(""); BLEDevice::init("");
@@ -1035,11 +1040,13 @@ void launchBTDiscovery() {
if (newDevices == 0) if (newDevices == 0)
return; return;
# ifdef ESP32 # ifdef ESP32
if (!semaphoreCreateOrUpdateDevice.take(1000, "launchBTDiscovery")) if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(1000) == pdFALSE)) {
Log.error(F("Semaphore NOT taken" CR));
return; return;
}
newDevices = 0; newDevices = 0;
vector<BLEdevice*> localDevices = devices; vector<BLEdevice*> localDevices = devices;
semaphoreCreateOrUpdateDevice.give(); xSemaphoreGive(semaphoreCreateOrUpdateDevice);
for (vector<BLEdevice*>::iterator it = localDevices.begin(); it != localDevices.end(); ++it) { for (vector<BLEdevice*>::iterator it = localDevices.begin(); it != localDevices.end(); ++it) {
# else # else
newDevices = 0; newDevices = 0;
@@ -1747,9 +1754,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
if (WorBupdated) { if (WorBupdated) {
# ifdef ESP32 # ifdef ESP32
if (!semaphoreCreateOrUpdateDevice.take(1000, "dumpDevices")) { if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(1000) == pdTRUE)) {
dumpDevices(); dumpDevices();
semaphoreCreateOrUpdateDevice.give(); xSemaphoreGive(semaphoreCreateOrUpdateDevice);
} }
# else # else
dumpDevices(); dumpDevices();