-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.cpp
155 lines (132 loc) · 3.21 KB
/
stream.cpp
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
#include "stream.h"
#include <cstring>
#include <cstdlib>
using serial_ns::ring_t;
static ring_t *USART1_DATA = nullptr;
static ring_t *USART2_DATA = nullptr;
static ring_t *USART6_DATA = nullptr;
Stream::Stream(UART_HandleTypeDef *huart):
_huart(huart), _ring(nullptr)
{
if (this->_huart->Instance == USART1) {
USART1_DATA = new ring_t();
this->_ring = USART1_DATA;
} else if (this->_huart->Instance == USART2) {
USART2_DATA = new ring_t();
this->_ring = USART2_DATA;
} else if (this->_huart->Instance == USART6) {
USART6_DATA = new ring_t();
this->_ring = USART6_DATA;
}
HAL_UART_Receive_IT(huart, this->_ring->buf, 1);
}
void Stream::write_IT(const char *data, size_t len)
{
char* data_casted = const_cast<char*>(data);
uint8_t *data_p = reinterpret_cast<uint8_t*>(data_casted);
HAL_UART_Transmit_IT(this->_huart, data_p, len);
HAL_Delay(len);
}
void Stream::write_IT(const char *data)
{
this->write_IT(data, strlen(data));
}
void Stream::write(const char *data)
{
char* data_casted = const_cast<char*>(data);
uint8_t *data_p = reinterpret_cast<uint8_t*>(data_casted);
HAL_UART_Transmit((this->_huart), data_p, ::strlen(data), HAL_MAX_DELAY);
}
void Stream::write(const char data)
{
uint8_t data_casted = static_cast<uint8_t>(data);
HAL_UART_Transmit((this->_huart), &data_casted, 1, HAL_MAX_DELAY);
}
void Stream::writeln(const char *data)
{
this->write(data);
this->write(etl::endl);
}
void Stream::write(int val, int radix)
{
char itoa_buf[16];
::itoa(val, itoa_buf, radix);
this->write(itoa_buf);
}
void Stream::writeln(int val, int radix)
{
this->write(val, radix);
this->writeln("");
}
int Stream::read()
{
return this->_ring->read();
}
int Stream::readln(char* buf)
{
uint16_t i = 0;
while (true) {
const int readbyte = this->read();
if (readbyte == '\n')
break;
if (readbyte == '\r')
continue;
if (readbyte > 0)
buf[i++] = static_cast<char>(readbyte);
}
buf[i] = '\0';
return i;
}
String Stream::readString()
{
String res{};
char buf[2]{0};
if (!this->available()) // nothing to read
return res;
while (true) {
const int readbyte = this->read();
if (readbyte == '\n')
break;
if (readbyte == '\r')
continue;
if (readbyte > 0) {
buf[0] = static_cast<char>(readbyte);
res += buf;
}
}
return res;
}
int ring_t::read()
{
int res = 0;
if (this->read_counter == this->write_counter)
return -86; // ENODATA
res = static_cast<int>(this->buf[this->read_counter++]);
if (this->read_counter >= ring_t::USART_BUF_SIZE) {
this->read_counter = 0;
}
return res;
}
void ring_t::incoming_byte_handler(UART_HandleTypeDef *huart)
{
++this->write_counter;
if (this->write_counter == this->read_counter)
++this->read_counter;
if (this->read_counter >= ring_t::USART_BUF_SIZE) {
this->read_counter = 0;
}
if (this->write_counter >= ring_t::USART_BUF_SIZE) {
this->write_counter = 0;
}
HAL_UART_Receive_IT(huart, &(this->buf[this->write_counter]), 1);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1) {
USART1_DATA->incoming_byte_handler(huart);
} else if (huart->Instance == USART2) {
USART2_DATA->incoming_byte_handler(huart);
} else if (huart->Instance == USART6) {
USART6_DATA->incoming_byte_handler(huart);
}
}