Repository navigation
Expand file tree
/
Copy pathESP32SerialCommands.cpp
More file actions
161 lines (144 loc) · 3.94 KB
/
Copy pathESP32SerialCommands.cpp
File metadata and controls
161 lines (144 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "ESP32SerialCommands.h"
#include <deki/LogSystem.h>
#include "SDCardComponent.h"
#include "IDekiSDCard.h" // from deki-sdcard
#ifdef ESP32
#include "driver/uart.h"
#include <cstring>
#include <string>
#endif
namespace DekiEsp32
{
#ifdef ESP32
#define SERIAL_UART_NUM UART_NUM_0
#define SERIAL_BUF_SIZE 256
#endif
bool ESP32SerialCommands::s_Initialized = false;
bool ESP32SerialCommands::s_InStorageMode = false;
#ifdef ESP32
static void serial_send(const char* str)
{
uart_write_bytes(SERIAL_UART_NUM, str, strlen(str));
uart_write_bytes(SERIAL_UART_NUM, "\r\n", 2);
}
#endif
void ESP32SerialCommands::Initialize(unsigned long baudRate)
{
#ifdef ESP32
if (s_Initialized)
{
return;
}
uart_config_t uart_config = {};
uart_config.baud_rate = static_cast<int>(baudRate);
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
uart_config.source_clk = UART_SCLK_DEFAULT;
uart_param_config(SERIAL_UART_NUM, &uart_config);
uart_driver_install(SERIAL_UART_NUM, SERIAL_BUF_SIZE * 2, 0, 0, nullptr, 0);
s_Initialized = true;
DEKI_LOG_INTERNAL("ESP32SerialCommands: Initialized at %lu baud", baudRate);
#else
(void)baudRate;
#endif
}
void ESP32SerialCommands::ProcessCommands()
{
#ifdef ESP32
size_t available = 0;
uart_get_buffered_data_len(SERIAL_UART_NUM, &available);
if (available == 0)
{
return;
}
// Read up to the newline.
char buf[SERIAL_BUF_SIZE];
int len = 0;
while (len < (int)sizeof(buf) - 1)
{
uint8_t c;
int read = uart_read_bytes(SERIAL_UART_NUM, &c, 1, pdMS_TO_TICKS(10));
if (read <= 0 || c == '\n')
{
break;
}
if (c != '\r')
{
buf[len++] = static_cast<char>(c);
}
}
buf[len] = '\0';
std::string cmd(buf);
if (cmd.empty())
{
return;
}
DEKI_LOG_INTERNAL("ESP32SerialCommands: Received command: %s", cmd.c_str());
if (cmd == "STORAGE_MODE")
{
auto* sdCard = DekiSdCard::SDCardComponent::GetSDCardPackage();
if (sdCard && sdCard->SupportsStorageMode())
{
if (sdCard->SetStorageMode(true))
{
s_InStorageMode = true;
serial_send("OK:STORAGE_MODE");
DEKI_LOG_INTERNAL("ESP32SerialCommands: Entered storage mode");
}
else
{
serial_send("ERROR:STORAGE_MODE_FAILED");
DEKI_LOG_ERROR("ESP32SerialCommands: Failed to enter storage mode");
}
}
else
{
serial_send("ERROR:STORAGE_MODE_NOT_SUPPORTED");
DEKI_LOG_WARNING("ESP32SerialCommands: Storage mode not supported (no SD card package)");
}
}
else if (cmd == "EXIT_STORAGE")
{
auto* sdCard = DekiSdCard::SDCardComponent::GetSDCardPackage();
if (sdCard && sdCard->SetStorageMode(false))
{
s_InStorageMode = false;
serial_send("OK:EXIT_STORAGE");
DEKI_LOG_INTERNAL("ESP32SerialCommands: Exited storage mode");
}
else
{
serial_send("ERROR:EXIT_STORAGE_FAILED");
DEKI_LOG_ERROR("ESP32SerialCommands: Failed to exit storage mode");
}
}
else if (cmd == "STATUS")
{
if (s_InStorageMode)
{
serial_send("STATUS:STORAGE_MODE");
}
else
{
serial_send("STATUS:RUNNING");
}
}
else if (cmd == "PING")
{
serial_send("PONG");
}
else
{
std::string err = "ERROR:UNKNOWN_COMMAND:" + cmd;
serial_send(err.c_str());
DEKI_LOG_WARNING("ESP32SerialCommands: Unknown command: %s", cmd.c_str());
}
#endif
}
bool ESP32SerialCommands::IsInStorageMode()
{
return s_InStorageMode;
}
} // namespace DekiEsp32