diff --git a/include/CommandHandler.h b/include/CommandHandler.h index 17be512d..2fe01e04 100644 --- a/include/CommandHandler.h +++ b/include/CommandHandler.h @@ -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 diff --git a/src/CommandHandler.cpp b/src/CommandHandler.cpp index cb261ea0..d32ae0f7 100644 --- a/src/CommandHandler.cpp +++ b/src/CommandHandler.cpp @@ -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" @@ -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(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?"); @@ -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; @@ -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) { diff --git a/src/EStopManager.cpp b/src/EStopManager.cpp index bf215467..cde4871a 100644 --- a/src/EStopManager.cpp +++ b/src/EStopManager.cpp @@ -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)); }