Skip to content

fix: Serial now outputs CRLF rather than just LF for better compatibility #353

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

Merged
merged 3 commits into from
Apr 13, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/precious-eagle-cactus-fruit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'firmware': patch
---

fix: Serial now uses CRLF rather than just LF for better compatibility
2 changes: 1 addition & 1 deletion include/serial/command_handlers/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <Arduino.h>

#define SERPR_SYS(format, ...) ::Serial.printf("$SYS$|" format "\n", ##__VA_ARGS__)
#define SERPR_SYS(format, ...) ::Serial.printf("$SYS$|" format "\r\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__)
Expand Down
59 changes: 34 additions & 25 deletions src/serial/SerialInputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void _printCompleteHelp()
}
}

std::size_t paddedLength = longestCommand + 1 + longestArgument + 1; // +1 for space, +1 for newline
std::size_t paddedLength = longestCommand + 1 + longestArgument + 2; // +1 for space, +2 for newline

std::string buffer;
buffer.reserve((paddedLength * commandCount) + descriptionSize); // Approximate size
Expand Down Expand Up @@ -117,6 +117,7 @@ void _printCompleteHelp()

buffer.append(command.description());

buffer.push_back('\r');
buffer.push_back('\n');
}
}
Expand All @@ -129,7 +130,7 @@ void _printCommandHelp(Serial::CommandGroup& group)
{
std::size_t size = 0;
for (const auto& command : group.commands()) {
size++; // +1 for newline
size += 2; // +2 for newline
size += group.name().size();
size++; // +1 for space

Expand All @@ -141,29 +142,29 @@ void _printCommandHelp(Serial::CommandGroup& group)
size += arg.name.size() + 3; // +1 for space, +2 for <>
}

size++; // +1 for newline
size += 2; // +2 for newline

if (command.description().size() > 0) {
size = command.description().size() + 3; // +2 for indent, +1 for newline
size = command.description().size() + 4; // +2 for indent, +2 for newline
}

if (command.arguments().size() > 0) {
size += 13; // +13 for " Arguments:\n"
size += 14; // +14 for " Arguments:\r\n"
for (const auto& arg : command.arguments()) {
size += arg.name.size() + 7; // +4 for indent, +2 for <>, +1 for space
size += arg.constraint.size();
if (arg.constraintExtensions.size() > 0) {
size += 2; // +1 for ':', +1 for newline
size += 3; // +1 for ':', +2 for newline
for (const auto& ext : arg.constraintExtensions) {
size += ext.size() + 7; // +1 for newline, +6 for indent
size += ext.size() + 8; // +2 for newline, +6 for indent
}
} else {
size++; // +1 for newline
size += 2; // +2 for newline
}
}
}

size += 15; // +15 for " Example: \n"
size += 16; // +16 for " Example: \r\n"
size += group.name().size() + 1; // +1 for space

if (command.name().size() > 0) {
Expand All @@ -174,15 +175,16 @@ void _printCommandHelp(Serial::CommandGroup& group)
size += arg.exampleValue.size() + 1; // +1 for space
}

size++; // +1 for newline
size += 2; // +2 for newline
}

size++; // +1 for newline
size += 2; // +2 for newline

std::string buffer;
buffer.reserve(size); // TODO: Should be exact size, is 20 bytes off, figure out why

for (const auto& command : group.commands()) {
buffer.push_back('\r');
buffer.push_back('\n');
buffer.append(group.name());
buffer.push_back(' ');
Expand All @@ -199,16 +201,18 @@ void _printCommandHelp(Serial::CommandGroup& group)
buffer.push_back(' ');
}

buffer.push_back('\r');
buffer.push_back('\n');

if (command.description().size() > 0) {
buffer.append(2, ' ');
buffer.append(command.description());
buffer.push_back('\r');
buffer.push_back('\n');
}

if (command.arguments().size() > 0) {
buffer.append(" Arguments:\n"sv);
buffer.append(" Arguments:\r\n"sv);
for (const auto& arg : command.arguments()) {
buffer.append(4, ' ');
buffer.push_back('<');
Expand All @@ -217,19 +221,22 @@ void _printCommandHelp(Serial::CommandGroup& group)
buffer.push_back(' ');
buffer.append(arg.constraint);
if (arg.constraintExtensions.size() > 0) {
buffer.push_back('\r');
buffer.push_back('\n');
for (const auto& ext : arg.constraintExtensions) {
buffer.append(6, ' ');
buffer.append(ext);
buffer.push_back('\r');
buffer.push_back('\n');
}
} else {
buffer.push_back('\r');
buffer.push_back('\n');
}
}
}

buffer.append(" Example:\n "sv);
buffer.append(" Example:\r\n "sv);
buffer.append(group.name());
buffer.push_back(' ');

Expand All @@ -243,8 +250,10 @@ void _printCommandHelp(Serial::CommandGroup& group)
buffer.push_back(' ');
}

buffer.push_back('\r');
buffer.push_back('\n');
}
buffer.push_back('\r');
buffer.push_back('\n');

::Serial.print(buffer.data());
Expand Down Expand Up @@ -616,22 +625,22 @@ void SerialInputHandler::SetSerialEchoEnabled(bool enabled)

void SerialInputHandler::PrintWelcomeHeader()
{
::Serial.print(R"(
============== OPENSHOCK ==============
Contribute @ github.com/OpenShock
Discuss @ discord.gg/OpenShock
Type 'help' for available commands
=======================================
)");
::Serial.println("\
============== OPENSHOCK ==============\r\n\
Contribute @ github.com/OpenShock\r\n\
Discuss @ discord.gg/OpenShock\r\n\
Type 'help' for available commands\r\n\
=======================================\r\n\
");
}

void SerialInputHandler::PrintVersionInfo()
{
::Serial.print("\
Version: " OPENSHOCK_FW_VERSION "\n\
Build: " OPENSHOCK_FW_MODE "\n\
Commit: " OPENSHOCK_FW_GIT_COMMIT "\n\
Board: " OPENSHOCK_FW_BOARD "\n\
Chip: " OPENSHOCK_FW_CHIP "\n\
Version: " OPENSHOCK_FW_VERSION "\r\n\
Build: " OPENSHOCK_FW_MODE "\r\n\
Commit: " OPENSHOCK_FW_GIT_COMMIT "\r\n\
Board: " OPENSHOCK_FW_BOARD "\r\n\
Chip: " OPENSHOCK_FW_CHIP "\r\n\
");
}
2 changes: 1 addition & 1 deletion src/serial/command_handlers/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void _handleVersionCommand(std::string_view arg, bool isAutomated) {
(void)arg;

::Serial.print("\n");
::Serial.println();
OpenShock::SerialInputHandler::PrintVersionInfo();
}

Expand Down
Loading