Skip to content

Commit

Permalink
Clean up SerilaiInputHandler (#281)
Browse files Browse the repository at this point in the history
* Clean up SerialInputHandler

* Pass isAutomated to command handlers

* Ignore non-printable characters in command

* Implement custom SerialBuffer instead of std::string

* Update formatting
  • Loading branch information
hhvrc authored Oct 7, 2024
1 parent 2ad3e32 commit a79387b
Show file tree
Hide file tree
Showing 26 changed files with 1,272 additions and 833 deletions.
6 changes: 3 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: true
Expand All @@ -97,7 +97,7 @@ BraceWrapping:
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterAttributes: Never
Expand Down Expand Up @@ -137,7 +137,7 @@ LambdaBodyIndentation: OuterScope
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PPIndentWidth: -1
PackConstructorInitializers: NextLine
PackConstructorInitializers: Never

PointerAlignment: Left
ReferenceAlignment: Left
Expand Down
3 changes: 3 additions & 0 deletions include/serial/SerialInputHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace OpenShock::SerialInputHandler {
[[nodiscard]] bool Init();
void Update();

bool SerialEchoEnabled();
void SetSerialEchoEnabled(bool enabled);

void PrintWelcomeHeader();
void PrintVersionInfo();
} // namespace OpenShock::SerialInputHandler
55 changes: 55 additions & 0 deletions include/serial/command_handlers/CommandEntry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <string_view>
#include <vector>

namespace OpenShock::Serial {
typedef void (*CommandHandler)(std::string_view arg, bool isAutomated);

class CommandArgument {
public:
std::string_view name;
std::string_view constraint;
std::string_view exampleValue;
std::vector<std::string_view> constraintExtensions;
};

class CommandEntry {
public:
CommandEntry(std::string_view description, CommandHandler commandHandler);
CommandEntry(std::string_view name, std::string_view description, CommandHandler commandHandler);

inline std::string_view name() const { return m_name; }
inline std::string_view description() const { return m_description; }
inline const std::vector<CommandArgument>& arguments() const { return m_arguments; }
inline const CommandHandler commandHandler() const { return m_commandHandler; }

CommandArgument& addArgument(std::string_view name, std::string_view constraint, std::string_view exampleValue, std::vector<std::string_view> constraintExtensions = {});

private:
std::string_view m_name;
std::string_view m_description;
std::vector<CommandArgument> m_arguments;
CommandHandler m_commandHandler;
};

class CommandGroup {
public:
CommandGroup() = default;
CommandGroup(std::string_view name);
CommandGroup(CommandGroup&& other) = default;
CommandGroup(const CommandGroup& other) = default;
CommandGroup& operator=(CommandGroup&& other) = default;
CommandGroup& operator=(const CommandGroup& other) = default;

inline std::string_view name() const { return m_name; }
inline const std::vector<CommandEntry>& commands() const { return m_commands; }

CommandEntry& addCommand(std::string_view description, CommandHandler commandHandler);
CommandEntry& addCommand(std::string_view name, std::string_view description, CommandHandler commandHandler);

private:
std::string_view m_name;
std::vector<CommandEntry> m_commands;
};
} // namespace OpenShock::Serial
14 changes: 14 additions & 0 deletions include/serial/command_handlers/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "serial/command_handlers/index.h"

#include "Logging.h"

#include <Arduino.h>

#define SERPR_SYS(format, ...) ::Serial.printf("$SYS$|" format "\n", ##__VA_ARGS__)
#define SERPR_RESPONSE(format, ...) SERPR_SYS("Response|" format, ##__VA_ARGS__)
#define SERPR_SUCCESS(format, ...) SERPR_SYS("Success|" format, ##__VA_ARGS__)
#define SERPR_ERROR(format, ...) SERPR_SYS("Error|" format, ##__VA_ARGS__)

using namespace std::string_view_literals;
47 changes: 47 additions & 0 deletions include/serial/command_handlers/index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "serial/command_handlers/CommandEntry.h"

#include <vector>

namespace OpenShock::Serial::CommandHandlers {
OpenShock::Serial::CommandGroup VersionHandler();
OpenShock::Serial::CommandGroup RestartHandler();
OpenShock::Serial::CommandGroup SysInfoHandler();
OpenShock::Serial::CommandGroup EchoHandler();
OpenShock::Serial::CommandGroup ValidGpiosHandler();
OpenShock::Serial::CommandGroup RfTxPinHandler();
OpenShock::Serial::CommandGroup ESStopPinHandler();
OpenShock::Serial::CommandGroup DomainHandler();
OpenShock::Serial::CommandGroup AuthTokenHandler();
OpenShock::Serial::CommandGroup LcgOverrideHandler();
OpenShock::Serial::CommandGroup HostnameHandler();
OpenShock::Serial::CommandGroup NetworksHandler();
OpenShock::Serial::CommandGroup KeepAliveHandler();
OpenShock::Serial::CommandGroup JsonConfigHandler();
OpenShock::Serial::CommandGroup RawConfigHandler();
OpenShock::Serial::CommandGroup RfTransmitHandler();
OpenShock::Serial::CommandGroup FactoryResetHandler();

inline std::vector<OpenShock::Serial::CommandGroup> AllCommandHandlers() {
return {
VersionHandler(),
RestartHandler(),
SysInfoHandler(),
EchoHandler(),
ValidGpiosHandler(),
RfTxPinHandler(),
ESStopPinHandler(),
DomainHandler(),
AuthTokenHandler(),
LcgOverrideHandler(),
HostnameHandler(),
NetworksHandler(),
KeepAliveHandler(),
JsonConfigHandler(),
RawConfigHandler(),
RfTransmitHandler(),
FactoryResetHandler(),
};
}
} // namespace OpenShock::Serial::CommandHandlers
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void appSetup() {

// Arduino setup function
void setup() {
Serial.begin(115'200);
::Serial.begin(115'200);

OpenShock::Config::Init();
OpenShock::OtaUpdateManager::Init();
Expand Down
Loading

0 comments on commit a79387b

Please sign in to comment.