Skip to content

Commit

Permalink
Convert from FreeRTOS Semaphore to std::mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Sep 2, 2023
1 parent 986d67a commit 55afa81
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
15 changes: 2 additions & 13 deletions lib/Hoymiles/src/Hoymiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@
#include "inverters/HM_4CH.h"
#include <Arduino.h>

#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)

HoymilesClass Hoymiles;

void HoymilesClass::init()
{
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use

_pollInterval = 0;
_radioNrf.reset(new HoymilesRadio_NRF());
_radioCmt.reset(new HoymilesRadio_CMT());
Expand All @@ -41,7 +33,7 @@ void HoymilesClass::initCMT(int8_t pin_sdio, int8_t pin_clk, int8_t pin_cs, int8

void HoymilesClass::loop()
{
HOY_SEMAPHORE_TAKE();
std::lock_guard<std::mutex> lock(_mutex);
_radioNrf->loop();
_radioCmt->loop();

Expand Down Expand Up @@ -116,8 +108,6 @@ void HoymilesClass::loop()
}
}
}

HOY_SEMAPHORE_GIVE();
}

std::shared_ptr<InverterAbstract> HoymilesClass::addInverter(const char* name, uint64_t serial)
Expand Down Expand Up @@ -195,9 +185,8 @@ void HoymilesClass::removeInverterBySerial(uint64_t serial)
{
for (uint8_t i = 0; i < _inverters.size(); i++) {
if (_inverters[i]->serial() == serial) {
HOY_SEMAPHORE_TAKE();
std::lock_guard<std::mutex> lock(_mutex);
_inverters.erase(_inverters.begin() + i);
HOY_SEMAPHORE_GIVE();
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/Hoymiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class HoymilesClass {
std::unique_ptr<HoymilesRadio_NRF> _radioNrf;
std::unique_ptr<HoymilesRadio_CMT> _radioCmt;

SemaphoreHandle_t _xSemaphore;
std::mutex _mutex;

uint32_t _pollInterval = 0;
uint32_t _lastPoll = 0;
Expand Down

0 comments on commit 55afa81

Please sign in to comment.