Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit e10f5ef
Author: hhvrc <[email protected]>
Date:   Wed Oct 23 10:42:44 2024 +0200

    Start using SimpleMutex
  • Loading branch information
hhvrc committed Oct 23, 2024
1 parent f85a991 commit 652de8e
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 168 deletions.
4 changes: 2 additions & 2 deletions include/PinPatternManager.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "Common.h"
#include "SimpleMutex.h"

#include <hal/gpio_types.h>

#include <freertos/semphr.h>
#include <freertos/task.h>

#include <cstdint>
Expand Down Expand Up @@ -41,6 +41,6 @@ namespace OpenShock {
gpio_num_t m_gpioPin;
std::vector<State> m_pattern;
TaskHandle_t m_taskHandle;
SemaphoreHandle_t m_taskMutex;
SimpleMutex m_taskMutex;
};
} // namespace OpenShock
7 changes: 4 additions & 3 deletions include/RGBPatternManager.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "Common.h"
#include "SimpleMutex.h"

#include <hal/gpio_types.h>

#include <freertos/semphr.h>
#include <freertos/task.h>

#include <esp32-hal-rmt.h>
Expand Down Expand Up @@ -32,7 +32,8 @@ namespace OpenShock {

void SetPattern(const RGBState* pattern, std::size_t patternLength);
template<std::size_t N>
inline void SetPattern(const RGBState (&pattern)[N]) {
inline void SetPattern(const RGBState (&pattern)[N])
{
SetPattern(pattern, N);
}
void SetBrightness(uint8_t brightness);
Expand All @@ -47,6 +48,6 @@ namespace OpenShock {
std::vector<RGBState> m_pattern;
rmt_obj_t* m_rmtHandle;
TaskHandle_t m_taskHandle;
SemaphoreHandle_t m_taskMutex;
SimpleMutex m_taskMutex;
};
} // namespace OpenShock
37 changes: 16 additions & 21 deletions src/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ const char* const TAG = "CommandHandler";
#include "Logging.h"
#include "radio/RFTransmitter.h"
#include "ReadWriteMutex.h"
#include "SimpleMutex.h"
#include "Time.h"
#include "util/TaskUtils.h"

#include <freertos/queue.h>
#include <freertos/semphr.h>

#include <memory>
#include <unordered_map>

const int64_t KEEP_ALIVE_INTERVAL = 60'000;
const uint16_t KEEP_ALIVE_DURATION = 300;

using namespace OpenShock;

uint32_t calculateEepyTime(int64_t timeToKeepAlive)
{
int64_t now = OpenShock::millis();
Expand All @@ -33,19 +31,21 @@ uint32_t calculateEepyTime(int64_t timeToKeepAlive)

struct KnownShocker {
bool killTask;
ShockerModelType model;
OpenShock::ShockerModelType model;
uint16_t shockerId;
int64_t lastActivityTimestamp;
};

static ReadWriteMutex s_rfTransmitterMutex = {};
static std::unique_ptr<RFTransmitter> s_rfTransmitter = nullptr;
static OpenShock::ReadWriteMutex s_rfTransmitterMutex = {};
static std::unique_ptr<OpenShock::RFTransmitter> s_rfTransmitter = nullptr;

static SemaphoreHandle_t s_estopManagerMutex = nullptr;
static OpenShock::SimpleMutex s_estopManagerMutex = {};

static ReadWriteMutex s_keepAliveMutex = {};
static QueueHandle_t s_keepAliveQueue = nullptr;
static TaskHandle_t s_keepAliveTaskHandle = nullptr;
static OpenShock::ReadWriteMutex s_keepAliveMutex = {};
static QueueHandle_t s_keepAliveQueue = nullptr;
static TaskHandle_t s_keepAliveTaskHandle = nullptr;

using namespace OpenShock;

void _keepAliveTask(void* arg)
{
Expand Down Expand Up @@ -111,7 +111,7 @@ bool _internalSetKeepAliveEnabled(bool enabled)
return true;
}

ScopedWriteLock keepAliveLock(&s_keepAliveMutex);
ScopedWriteLock lock__(&s_keepAliveMutex);

if (enabled) {
OS_LOGV(TAG, "Enabling keep-alive task");
Expand Down Expand Up @@ -160,8 +160,6 @@ bool CommandHandler::Init()
}
initialized = true;

s_estopManagerMutex = xSemaphoreCreateMutex();

Config::RFConfig rfConfig;
if (!Config::GetRFConfig(rfConfig)) {
OS_LOGE(TAG, "Failed to get RF config");
Expand Down Expand Up @@ -218,7 +216,7 @@ SetGPIOResultCode CommandHandler::SetRfTxPin(gpio_num_t txPin)
return SetGPIOResultCode::InvalidPin;
}

ScopedWriteLock rftxLock(&s_rfTransmitterMutex);
ScopedWriteLock lock__(&s_rfTransmitterMutex);

if (s_rfTransmitter != nullptr) {
OS_LOGV(TAG, "Destroying existing RF transmitter");
Expand All @@ -245,23 +243,20 @@ SetGPIOResultCode CommandHandler::SetRfTxPin(gpio_num_t txPin)
SetGPIOResultCode CommandHandler::SetEStopPin(gpio_num_t estopPin)
{
if (OpenShock::IsValidInputPin(static_cast<int8_t>(estopPin))) {
xSemaphoreTake(s_estopManagerMutex, portMAX_DELAY);
ScopedLock lock__(&s_estopManagerMutex);

if (!EStopManager::SetEStopPin(estopPin)) {
OS_LOGE(TAG, "Failed to set EStop pin");

xSemaphoreGive(s_estopManagerMutex);
return SetGPIOResultCode::InternalError;
}

if (!Config::SetEStopGpioPin(estopPin)) {
OS_LOGE(TAG, "Failed to set EStop pin in config");

xSemaphoreGive(s_estopManagerMutex);
return SetGPIOResultCode::InternalError;
}

xSemaphoreGive(s_estopManagerMutex);
return SetGPIOResultCode::Success;
} else {
return SetGPIOResultCode::InvalidPin;
Expand Down Expand Up @@ -318,7 +313,7 @@ gpio_num_t CommandHandler::GetRfTxPin()

bool CommandHandler::HandleCommand(ShockerModelType model, uint16_t shockerId, ShockerCommandType type, uint8_t intensity, uint16_t durationMs)
{
ScopedReadLock rftxLock(&s_rfTransmitterMutex);
ScopedReadLock lock__rf(&s_rfTransmitterMutex);

if (s_rfTransmitter == nullptr) {
OS_LOGW(TAG, "RF Transmitter is not initialized, ignoring command");
Expand All @@ -340,8 +335,8 @@ bool CommandHandler::HandleCommand(ShockerModelType model, uint16_t shockerId, S

bool ok = s_rfTransmitter->SendCommand(model, shockerId, type, intensity, durationMs);

rftxLock.unlock();
ScopedReadLock keepAliveLock(&s_keepAliveMutex);
lock__rf.unlock();
ScopedReadLock lock__ka(&s_keepAliveMutex);

if (ok && s_keepAliveQueue != nullptr) {
KnownShocker cmd {.model = model, .shockerId = shockerId, .lastActivityTimestamp = OpenShock::millis() + durationMs};
Expand Down
6 changes: 3 additions & 3 deletions src/EStopManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ bool EStopManager::Init()
return false;
}

OpenShock::ScopedLock lock(&s_estopMutex);
OpenShock::ScopedLock lock__(&s_estopMutex);

if (!_setEStopPinImpl(cfg.gpioPin)) {
OS_LOGE(TAG, "Failed to set EStop pin");
Expand All @@ -230,7 +230,7 @@ bool EStopManager::Init()

bool EStopManager::SetEStopEnabled(bool enabled)
{
OpenShock::ScopedLock lock(&s_estopMutex);
OpenShock::ScopedLock lock__(&s_estopMutex);

if (s_estopPin == GPIO_NUM_NC) {
gpio_num_t pin;
Expand All @@ -251,7 +251,7 @@ bool EStopManager::SetEStopEnabled(bool enabled)

bool EStopManager::SetEStopPin(gpio_num_t pin)
{
OpenShock::ScopedLock lock(&s_estopMutex);
OpenShock::ScopedLock lock__(&s_estopMutex);

return _setEStopPinImpl(pin);
}
Expand Down
Loading

0 comments on commit 652de8e

Please sign in to comment.