-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSloppySoftwareSerialStream.hpp
222 lines (175 loc) · 4.66 KB
/
SloppySoftwareSerialStream.hpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-FileCopyrightText: 2025 smdn <[email protected]>
// SPDX-License-Identifier: MIT
#ifndef SloppySoftwareSerialStream_hpp
#define SloppySoftwareSerialStream_hpp
#include <Arduino.h>
// The *sloppy* software serial implementation using with digitalRead(), digitalWrite() and delayMicroseconds().
template <
int8_t PIN_RX,
int8_t PIN_TX,
unsigned TX_DELAY_MICROSECS,
unsigned RX_DATA_DELAY_MICROSECS,
unsigned RX_START_DELAY_MICROSECS,
unsigned RX_STOP_DELAY_MICROSECS
>
class SloppySoftwareSerialStream {
private:
using self_t = SloppySoftwareSerialStream<
PIN_RX,
PIN_TX,
TX_DELAY_MICROSECS,
RX_DATA_DELAY_MICROSECS,
RX_START_DELAY_MICROSECS,
RX_STOP_DELAY_MICROSECS
>;
using index_t = uint8_t;
public:
SloppySoftwareSerialStream()
{
}
void begin(
const uint16_t& baud,
const uint16_t& configurations
) /* const */
{
pinMode(PIN_TX, OUTPUT);
digitalWrite(PIN_TX, HIGH);
pinMode(PIN_RX, INPUT);
attachInterrupt(PIN_RX, self_t::handleReceive, CHANGE);
attachedInstance = this;
receiveBufferIndexForRead = 0;
receiveBufferIndexForWrite = 0;
}
bool writeBytes(
const uint8_t* /*const*/ data,
const size_t& length
) /* const */
{
for (auto index = 0; index < length; index++) {
// start bit
digitalWrite(PIN_TX, LOW);
delayMicroseconds(TX_DELAY_MICROSECS);
auto d = data[index];
// data bits
for (auto i = 0; i < dataSizeInBits; i++) {
digitalWrite(PIN_TX, d & 0b1 ? HIGH : LOW);
delayMicroseconds(TX_DELAY_MICROSECS);
d >>= 1;
}
// stop bit
digitalWrite(PIN_TX, HIGH);
delayMicroseconds(TX_DELAY_MICROSECS);
}
return true;
}
size_t readBytes(
uint8_t* /*const*/ buffer,
const size_t& length
) /* const */
{
size_t count = 0;
uint8_t* buf = buffer;
for (;;) {
if (length <= count)
break;
auto b = readWithTimeout();
if (b < 0)
break;
*buf++ = (uint8_t)b;
count++;
}
return count;
}
private:
static constexpr size_t dataSizeInBits = 8; // size of data bits
static constexpr size_t receiveBufferSize = 9 * 2; // = return value length * 2
static self_t* attachedInstance;
uint8_t receiveBuffer[receiveBufferSize];
volatile index_t receiveBufferIndexForRead;
volatile index_t receiveBufferIndexForWrite;
static inline void handleReceive()
{
if (!attachedInstance)
return;
// disable interrupt while receiving
noInterrupts();
attachedInstance->receive();
// re-enable interrupt
interrupts();
}
void receive()
{
// expect the start bit
if (digitalRead(PIN_RX))
return; // not a start bit
delayMicroseconds(RX_START_DELAY_MICROSECS);
// read data bits
uint8_t data = 0x00;
for (size_t i = 0; i < dataSizeInBits; i++) {
delayMicroseconds(RX_DATA_DELAY_MICROSECS);
data >>= 1;
if (digitalRead(PIN_RX))
data |= 0b10000000;
}
// store to the buffer
auto nextIndex = (index_t)((receiveBufferIndexForWrite + 1) % receiveBufferSize);
if (nextIndex == receiveBufferIndexForRead) {
// buffer overflow, discard the received data
}
else {
receiveBuffer[receiveBufferIndexForWrite] = data;
receiveBufferIndexForWrite = nextIndex;
}
// expect the stop bit
delayMicroseconds(RX_STOP_DELAY_MICROSECS);
}
int read()
{
if (receiveBufferIndexForRead == receiveBufferIndexForWrite)
return -1; // buffer empty or overflow
index_t index = receiveBufferIndexForRead;
receiveBufferIndexForRead = (index_t)((receiveBufferIndexForRead + 1) % receiveBufferSize);
return receiveBuffer[index];
}
int readWithTimeout()
{
constexpr unsigned int timeoutMilliseconds = 500;
unsigned int startAt = millis();
unsigned int timeoutAt = startAt + timeoutMilliseconds;
for (;;) {
auto b = read();
if (0 <= b)
return b;
unsigned int now = millis();
if (now < startAt)
return -1; // counter rewound
if (timeoutAt <= now)
return -1; // timed out
}
}
};
template <
int8_t PIN_RX,
int8_t PIN_TX,
unsigned TX_DELAY_MICROSECS,
unsigned RX_DATA_DELAY_MICROSECS,
unsigned RX_START_DELAY_MICROSECS,
unsigned RX_STOP_DELAY_MICROSECS
>
SloppySoftwareSerialStream<
PIN_RX,
PIN_TX,
TX_DELAY_MICROSECS,
RX_DATA_DELAY_MICROSECS,
RX_START_DELAY_MICROSECS,
RX_STOP_DELAY_MICROSECS
>*
SloppySoftwareSerialStream<
PIN_RX,
PIN_TX,
TX_DELAY_MICROSECS,
RX_DATA_DELAY_MICROSECS,
RX_START_DELAY_MICROSECS,
RX_STOP_DELAY_MICROSECS
>::attachedInstance = nullptr;
#endif // SloppySoftwareSerialStream_hpp