Skip to content

Commit 03efe46

Browse files
committed
cleanup
1 parent ec697ca commit 03efe46

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

ESP32/src/serial.hpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "driver/uart.h"
2+
#include <string>
3+
4+
QueueHandle_t cpuQueue, gpuQueue;
5+
6+
const uart_port_t uart = UART_NUM_0;
7+
8+
void uartRX(void* sock) {
9+
const int bufferSize = 1024;
10+
uint8_t data[bufferSize];
11+
12+
uart_config_t uart_config;
13+
uart_config.baud_rate = 115200;
14+
uart_config.data_bits = UART_DATA_8_BITS;
15+
uart_config.parity = UART_PARITY_DISABLE;
16+
uart_config.stop_bits = UART_STOP_BITS_1;
17+
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
18+
uart_config.source_clk = UART_SCLK_APB;
19+
20+
uart_driver_install(uart, 1024 * 2, 0, 0, NULL, 0);
21+
uart_param_config(uart, &uart_config);
22+
23+
int length;
24+
static std::string bufferString;
25+
int separator;
26+
27+
28+
while (true)
29+
{
30+
uart_get_buffered_data_len(uart, (size_t*)&length);
31+
length = uart_read_bytes(uart, data, length, 20 / portTICK_RATE_MS);
32+
33+
if(length > 0) {
34+
std::string dataString((char*)data, length);
35+
bufferString.append(dataString);
36+
}
37+
38+
separator = bufferString.find(';');
39+
while(separator != std::string::npos) {
40+
std::string parse = bufferString.substr(0, separator);
41+
bufferString.erase(0, separator + 1);
42+
separator = bufferString.find(';');
43+
44+
switch (parse.at(0))
45+
{
46+
case 'P': { // set pwm1 percentage
47+
parse.erase(0, 1);
48+
int speed = std::atoi(parse.c_str());
49+
xQueueSendToBack(cpuQueue, &speed, 0);
50+
break;
51+
}
52+
53+
case 'W': { // set pwm2 percentage
54+
parse.erase(0, 1);
55+
int speed = std::atoi(parse.c_str());
56+
xQueueSendToBack(gpuQueue, &speed, 0);
57+
break;
58+
}
59+
}
60+
}
61+
62+
vTaskDelay(50 / portTICK_PERIOD_MS);
63+
}
64+
65+
}

0 commit comments

Comments
 (0)