Skip to content

Commit

Permalink
Isolate EStopManager
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Oct 25, 2024
1 parent d7d9a6a commit 0257b2e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
1 change: 0 additions & 1 deletion include/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace OpenShock::CommandHandler {
gpio_num_t GetEstopPin();

bool SetKeepAliveEnabled(bool enabled);
bool SetKeepAlivePaused(bool paused);

bool HandleCommand(ShockerModelType shockerModel, uint16_t shockerId, ShockerCommandType type, uint8_t intensity, uint16_t durationMs);
} // namespace OpenShock::CommandHandler
40 changes: 21 additions & 19 deletions src/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const char* const TAG = "CommandHandler";
#include "Common.h"
#include "config/Config.h"
#include "EStopManager.h"
#include "EStopState.h"
#include "events/Events.h"
#include "Logging.h"
#include "radio/RFTransmitter.h"
#include "ReadWriteMutex.h"
Expand Down Expand Up @@ -151,8 +153,21 @@ bool _internalSetKeepAliveEnabled(bool enabled)
return true;
}

void _handleOpenShockEStopStateChangeEvent(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
(void)event_handler_arg;
(void)event_base;
(void)event_id;

EStopState state = *reinterpret_cast<EStopState*>(event_data);

_internalSetKeepAliveEnabled(state == EStopState::Idle);
}

bool CommandHandler::Init()
{
esp_err_t err;

static bool initialized = false;
if (initialized) {
OS_LOGW(TAG, "RF Transmitter and EStopManager are already initialized?");
Expand Down Expand Up @@ -200,6 +215,12 @@ bool CommandHandler::Init()
return false;
}

err = esp_event_handler_register(OPENSHOCK_EVENTS, OPENSHOCK_EVENT_ESTOP_STATE_CHANGED, _handleOpenShockEStopStateChangeEvent, nullptr);
if (err != ESP_OK) {
OS_LOGE(TAG, "Failed to register event handler for OPENSHOCK_EVENTS: %s", esp_err_to_name(err));
return false;
}

// TODO: Implement EStopManager pin change logic

return true;
Expand Down Expand Up @@ -277,25 +298,6 @@ bool CommandHandler::SetKeepAliveEnabled(bool enabled)
return true;
}

bool CommandHandler::SetKeepAlivePaused(bool paused)
{
bool keepAliveEnabled = false;
if (!Config::GetRFConfigKeepAliveEnabled(keepAliveEnabled)) {
OS_LOGE(TAG, "Failed to get keep-alive enabled from config");
return false;
}

if (keepAliveEnabled == false && paused == false) {
OS_LOGW(TAG, "Keep-alive is disabled in config, ignoring unpause command");
return false;
}
if (!_internalSetKeepAliveEnabled(!paused)) {
return false;
}

return true;
}

gpio_num_t CommandHandler::GetRfTxPin()
{
if (s_rfTransmitter != nullptr) {
Expand Down
3 changes: 0 additions & 3 deletions src/EStopManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ static int64_t s_estopActivatedAt = 0;

void _estopUpdateExternals(bool isActive, bool isAwaitingRelease)
{
// Set KeepAlive state
OpenShock::CommandHandler::SetKeepAlivePaused(isActive);

// Post an event
ESP_ERROR_CHECK(esp_event_post(OPENSHOCK_EVENTS, OPENSHOCK_EVENT_ESTOP_STATE_CHANGED, &s_estopState, sizeof(s_estopState), portMAX_DELAY));
}
Expand Down

0 comments on commit 0257b2e

Please sign in to comment.