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

Ditch Arduino Serial #317

Draft
wants to merge 8 commits 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
14 changes: 6 additions & 8 deletions include/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <esp_ota_ops.h>
#include <esp_system.h>

extern "C" int log_printf(const char* fmt, ...);

template<std::size_t N>
constexpr const char* openshockPathToFileName(const char (&path)[N]) {
std::size_t pos = 0;
Expand All @@ -32,37 +30,37 @@ constexpr const char* openshockPathToFileName(const char (&path)[N]) {
#define OPENSHOCK_LOG_FORMAT(letter, format) "[%lli][" #letter "][%s:%u] %s(): " format "\r\n", OpenShock::millis(), openshockPathToFileName(__FILE__), __LINE__, __FUNCTION__

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_VERBOSE
#define OS_LOGV(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(V, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGV(TAG, format, ...) esp_log_write(ESP_LOG_VERBOSE, TAG, OPENSHOCK_LOG_FORMAT(V, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGV(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_DEBUG
#define OS_LOGD(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(D, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGD(TAG, format, ...) esp_log_write(ESP_LOG_DEBUG, TAG, OPENSHOCK_LOG_FORMAT(D, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGD(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_INFO
#define OS_LOGI(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(I, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGI(TAG, format, ...) esp_log_write(ESP_LOG_INFO, TAG, OPENSHOCK_LOG_FORMAT(I, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGI(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_WARN
#define OS_LOGW(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(W, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGW(TAG, format, ...) esp_log_write(ESP_LOG_WARN, TAG, OPENSHOCK_LOG_FORMAT(W, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGW(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_ERROR
#define OS_LOGE(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGE(TAG, format, ...) esp_log_write(ESP_LOG_ERROR, TAG, OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGE(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_NONE
#define OS_LOGN(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGN(TAG, format, ...) esp_log_write(ESP_LOG_NONE, TAG, OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGN(TAG, format, ...) do {} while(0)
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/serial/command_handlers/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "Logging.h"

#include <Arduino.h>
#include <cstdio>

#define SERPR_SYS(format, ...) ::Serial.printf("$SYS$|" format "\n", ##__VA_ARGS__)
#define SERPR_SYS(format, ...) 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__)
Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const char* const TAG = "main";
#include "wifi/WiFiManager.h"
#include "wifi/WiFiScanManager.h"

#include <Arduino.h>

#include <memory>

// Internal setup function, returns true if setup succeeded, false otherwise.
Expand Down Expand Up @@ -92,7 +90,7 @@ void appSetup()
// Arduino setup function
void setup()
{
::Serial.begin(115'200);
// esp_log_level_set(ESP_LOG_VERBOSE);

OpenShock::Config::Init();

Expand Down
9 changes: 5 additions & 4 deletions src/serial/SerialInputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const char* const TAG = "SerialInputHandler";
#include <cJSON.h>
#include <Esp.h>

#include <cstdio>
#include <cstring>
#include <string_view>
#include <unordered_map>
Expand Down Expand Up @@ -420,7 +421,7 @@ void _skipSerialWhitespaces(SerialBuffer& buffer)

void _echoBuffer(std::string_view buffer)
{
::Serial.printf(CLEAR_LINE "> %.*s", buffer.size(), buffer.data());
printf(CLEAR_LINE "> %.*s", buffer.size(), buffer.data());
}

void _echoHandleSerialInput(std::string_view buffer, bool hasData)
Expand Down Expand Up @@ -467,7 +468,7 @@ void _processSerialLine(std::string_view line)
line = line.substr(1);
} else if (s_echoEnabled) {
_echoBuffer(line);
::Serial.println();
putchar('\n');
}

auto parts = OpenShock::StringSplit(line, ' ', 1);
Expand Down Expand Up @@ -558,7 +559,7 @@ void _serialRxTask(void*)
_skipSerialWhitespaces(buffer);
break;
case SerialReadResult::AutoCompleteRequest:
::Serial.printf(CLEAR_LINE "> %.*s [AutoComplete is not implemented]", buffer.size(), buffer.data());
printf(CLEAR_LINE "> %.*s [AutoComplete is not implemented]", buffer.size(), buffer.data());
break;
case SerialReadResult::Data:
_echoHandleSerialInput(buffer, true);
Expand Down Expand Up @@ -590,7 +591,7 @@ bool SerialInputHandler::Init()

SerialInputHandler::PrintWelcomeHeader();
SerialInputHandler::PrintVersionInfo();
::Serial.println();
putchar('\n');

if (!Config::GetSerialInputConfigEchoEnabled(s_echoEnabled)) {
OS_LOGE(TAG, "Failed to get serial echo status from config");
Expand Down
6 changes: 4 additions & 2 deletions src/serial/command_handlers/factoryreset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

#include <esp_system.h>

#include <cstdio>

void _handleFactoryResetCommand(std::string_view arg, bool isAutomated)
{
(void)arg;

::Serial.println("Resetting to factory defaults...");
printf("Resetting to factory defaults...\n");
OpenShock::Config::FactoryReset();
::Serial.println("Restarting...");
printf("Restarting...\n");
esp_restart();
}

Expand Down
2 changes: 1 addition & 1 deletion src/serial/command_handlers/restart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
void _handleRestartCommand(std::string_view arg, bool isAutomated) {
(void)arg;

::Serial.println("Restarting ESP...");
printf("Restarting ESP...\n");
esp_restart();
}

Expand Down
4 changes: 3 additions & 1 deletion src/serial/command_handlers/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include "serial/SerialInputHandler.h"

#include <cstdio>
#include <vector>

void _handleVersionCommand(std::string_view arg, bool isAutomated) {
(void)arg;

::Serial.print("\n");
putchar('\n');

OpenShock::SerialInputHandler::PrintVersionInfo();
}

Expand Down