Skip to content

Commit

Permalink
Add missing E-Stop enable/disable command
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Oct 21, 2024
1 parent 6711e9d commit a5bcdfa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 52 deletions.
7 changes: 4 additions & 3 deletions include/serial/command_handlers/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OpenShock::Serial::CommandHandlers {
OpenShock::Serial::CommandGroup EchoHandler();
OpenShock::Serial::CommandGroup ValidGpiosHandler();
OpenShock::Serial::CommandGroup RfTxPinHandler();
OpenShock::Serial::CommandGroup ESStopPinHandler();
OpenShock::Serial::CommandGroup EStopHandler();
OpenShock::Serial::CommandGroup DomainHandler();
OpenShock::Serial::CommandGroup AuthTokenHandler();
OpenShock::Serial::CommandGroup LcgOverrideHandler();
Expand All @@ -23,15 +23,16 @@ namespace OpenShock::Serial::CommandHandlers {
OpenShock::Serial::CommandGroup RfTransmitHandler();
OpenShock::Serial::CommandGroup FactoryResetHandler();

inline std::vector<OpenShock::Serial::CommandGroup> AllCommandHandlers() {
inline std::vector<OpenShock::Serial::CommandGroup> AllCommandHandlers()
{
return {
VersionHandler(),
RestartHandler(),
SysInfoHandler(),
EchoHandler(),
ValidGpiosHandler(),
RfTxPinHandler(),
ESStopPinHandler(),
EStopHandler(),
DomainHandler(),
AuthTokenHandler(),
LcgOverrideHandler(),
Expand Down
84 changes: 84 additions & 0 deletions src/serial/command_handlers/estop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "serial/command_handlers/common.h"

#include "config/Config.h"
#include "Convert.h"
#include "EStopManager.h"

void _handleEStopEnabledCommand(std::string_view arg, bool isAutomated)
{
bool enabled;
if (arg.empty()) {
if (!OpenShock::Config::GetEStopEnabled(enabled)) {
SERPR_ERROR("Failed to get EStop enabled from config");
return;
}

// Get EStop enabled
SERPR_RESPONSE("EStopEnabled|%s", enabled ? "true" : "false");
return;
}

if (!OpenShock::Convert::ToBool(arg, enabled)) {
SERPR_ERROR("Invalid argument (must be a boolean)");
return;
}

if (!OpenShock::EStopManager::SetEStopEnabled(enabled)) {
SERPR_ERROR("Failed to set EStop enabled");
return;
}

if (!OpenShock::Config::SetEStopEnabled(enabled)) {
SERPR_ERROR("Failed to save config");
return;
}

SERPR_SUCCESS("Saved config");
}

void _handleEStopPinCommand(std::string_view arg, bool isAutomated)
{
gpio_num_t estopPin;
if (arg.empty()) {
if (!OpenShock::Config::GetEStopGpioPin(estopPin)) {
SERPR_ERROR("Failed to get EStop pin from config");
return;
}

// Get EStop pin
SERPR_RESPONSE("EStopPin|%hhi", static_cast<int8_t>(estopPin));
return;
}

if (!OpenShock::Convert::ToGpioNum(arg, estopPin)) {
SERPR_ERROR("Invalid argument (number invalid or out of range)");
return;
}

if (!OpenShock::EStopManager::SetEStopPin(estopPin)) {
SERPR_ERROR("Failed to set EStop pin");
return;
}

if (!OpenShock::Config::SetEStopGpioPin(estopPin)) {
SERPR_ERROR("Failed to save config");
return;
}

SERPR_SUCCESS("Saved config");
}

OpenShock::Serial::CommandGroup OpenShock::Serial::CommandHandlers::EStopHandler()
{
auto group = OpenShock::Serial::CommandGroup("estop"sv);

auto& getEnabledCommand = group.addCommand("enabled"sv, "Get the E-Stop enabled state."sv, _handleEStopEnabledCommand);
auto& setEnabledCommand = group.addCommand("enabled"sv, "Set the E-Stop enabled state."sv, _handleEStopEnabledCommand);
setEnabledCommand.addArgument("enabled"sv, "must be a boolean"sv, "true"sv);

auto& getPinCommand = group.addCommand("pin"sv, "Get the GPIO pin used for the E-Stop."sv, _handleEStopPinCommand);
auto& setPinCommand = group.addCommand("pin"sv, "Set the GPIO pin used for the E-Stop."sv, _handleEStopPinCommand);
setPinCommand.addArgument("pin"sv, "must be a number"sv, "4"sv);

return group;
}
49 changes: 0 additions & 49 deletions src/serial/command_handlers/estoppin.cpp

This file was deleted.

0 comments on commit a5bcdfa

Please sign in to comment.