Skip to content

Commit 3a58c8d

Browse files
committed
AI Slop fix
1 parent a936399 commit 3a58c8d

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

include/serial/SerialCompat.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <driver/uart.h>
4+
5+
// Use the same UART that your console / printf uses
6+
// On most boards this is UART_NUM_0
7+
#define OPENSHOCK_UART = UART_NUM_0;
8+
9+
namespace OpenShock {
10+
// Call this once during startup (instead of Serial.begin)
11+
inline void SerialInit()
12+
{
13+
// If you already use esp_console, you might already have
14+
// the driver installed. Then you can skip the install part.
15+
uart_config_t uart_config = {
16+
.baud_rate = 115200,
17+
.data_bits = UART_DATA_8_BITS,
18+
.parity = UART_PARITY_DISABLE,
19+
.stop_bits = UART_STOP_BITS_1,
20+
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
21+
.source_clk = UART_SCLK_DEFAULT,
22+
};
23+
ESP_ERROR_CHECK(uart_param_config(OPENSHOCK_UART, &uart_config));
24+
25+
// RX buffer only; TX is handled by vfs/printf
26+
ESP_ERROR_CHECK(uart_driver_install(OPENSHOCK_UART, 2048, 0, 0, nullptr, 0));
27+
28+
// Route stdin/stdout to this UART so printf/scanf work
29+
esp_vfs_dev_uart_use_driver(OPENSHOCK_UART);
30+
}
31+
32+
// Arduino-like helpers
33+
inline int SerialAvailable()
34+
{
35+
size_t len = 0;
36+
uart_get_buffered_data_len(OPENSHOCK_UART, &len);
37+
return static_cast<int>(len);
38+
}
39+
40+
inline int SerialRead()
41+
{
42+
std::uint8_t c;
43+
// timeout = 0 ticks → non-blocking
44+
int n = uart_read_bytes(OPENSHOCK_UART, &c, 1, 0);
45+
if (n == 1) {
46+
return static_cast<int>(c);
47+
}
48+
return -1; // no data
49+
}
50+
} // namespace OpenShock

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const char* const TAG = "main";
1111
#include "GatewayConnectionManager.h"
1212
#include "Logging.h"
1313
#include "OtaUpdateManager.h"
14+
#include "serial/SerialCompat.h"
1415
#include "serial/SerialInputHandler.h"
1516
#include "util/TaskUtils.h"
1617
#include "VisualStateManager.h"
@@ -90,7 +91,8 @@ void appSetup()
9091
// Arduino setup function
9192
void setup()
9293
{
93-
// esp_log_level_set(ESP_LOG_VERBOSE);
94+
OpenShock::SerialInit();
95+
esp_log_level_set(ESP_LOG_VERBOSE);
9496

9597
OpenShock::Config::Init();
9698

src/serial/SerialInputHandler.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const char* const TAG = "SerialInputHandler";
1212
#include "FormatHelpers.h"
1313
#include "http/HTTPRequestManager.h"
1414
#include "Logging.h"
15+
#include "serial/SerialCompat.h"
1516
#include "serial/command_handlers/CommandEntry.h"
1617
#include "serial/command_handlers/common.h"
1718
#include "serial/command_handlers/index.h"
@@ -367,7 +368,7 @@ enum class SerialReadResult {
367368
SerialReadResult _tryReadSerialLine(SerialBuffer& buffer)
368369
{
369370
// Check if there's any data available
370-
int available = ::Serial.available();
371+
int available = OpenShockSerialAvailable();
371372
if (available <= 0) {
372373
return SerialReadResult::NoData;
373374
}
@@ -377,11 +378,18 @@ SerialReadResult _tryReadSerialLine(SerialBuffer& buffer)
377378

378379
// Read the data into the buffer
379380
while (available-- > 0) {
380-
char c = ::Serial.read();
381+
int r = OpenShockSerialRead();
382+
if (r < 0) {
383+
break; // no more data even though the previous length said otherwise
384+
}
385+
386+
char c = static_cast<char>(r);
381387

382388
// Handle backspace
383389
if (c == '\b') {
384-
buffer.pop_back(); // Remove the last character from the buffer if it exists
390+
if (!buffer.empty()) {
391+
buffer.pop_back(); // Remove the last character from the buffer if it exists
392+
}
385393
continue;
386394
}
387395

@@ -413,10 +421,15 @@ SerialReadResult _tryReadSerialLine(SerialBuffer& buffer)
413421

414422
void _skipSerialWhitespaces(SerialBuffer& buffer)
415423
{
416-
int available = ::Serial.available();
424+
int available = OpenShockSerialAvailable();
417425

418426
while (available-- > 0) {
419-
char c = ::Serial.read();
427+
int r = OpenShockSerialRead();
428+
if (r < 0) {
429+
break;
430+
}
431+
432+
char c = static_cast<char>(r);
420433

421434
if (c != ' ' && c != '\r' && c != '\n') {
422435
buffer.push_back(c);

0 commit comments

Comments
 (0)