Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle automated serial commands #318

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions include/serial/command_handlers/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

#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__)
namespace OpenShock::Serial::Util {
void respError(bool isAutomated, const char* format, ...);
}

#define CLEAR_LINE "\r\x1B[K"

using namespace std::string_view_literals;
2 changes: 0 additions & 2 deletions src/serial/SerialInputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@ void RegisterCommandHandler(const OpenShock::Serial::CommandGroup& handler)
s_commandHandlers[handler.name()] = handler;
}

#define CLEAR_LINE "\r\x1B[K"

class SerialBuffer {
DISABLE_COPY(SerialBuffer);
DISABLE_MOVE(SerialBuffer);
Expand Down
30 changes: 30 additions & 0 deletions src/serial/command_handlers/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "serial/command_handlers/common.h"

#include <cJSON.h>

using namespace OpenShock;

void Serial::Util::respError(bool isAutomated, const char* format, ...)
{
if (isAutomated) {
cJSON* root = cJSON_CreateObject();
if (root == nullptr) {
::Serial.printf(CLEAR_LINE "Failed to create JSON object\n");
return;
}

cJSON_AddStringToObject(root, "error", msg);

char* out = cJSON_PrintUnformatted(root);

if (out != nullptr) {
::Serial.printf("$SYS$%s\n", out);
cJSON_free(out);
}

cJSON_Delete(root);
return;
}

::Serial.printf(CLEAR_LINE "Error: %s\n", msg);
}
Loading