|
@@ -21,11 +21,32 @@
|
21
|
21
|
*/
|
22
|
22
|
#pragma once
|
23
|
23
|
|
24
|
|
-#include "macros.h"
|
25
|
24
|
#include "serial_base.h"
|
26
|
25
|
|
27
|
|
-// Used in multiple places
|
28
|
|
-typedef int8_t serial_index_t;
|
|
26
|
+// A mask containing a bitmap of the serial port to act upon
|
|
27
|
+// This is written to ensure a serial index is never used as a serial mask
|
|
28
|
+class SerialMask {
|
|
29
|
+ uint8_t mask;
|
|
30
|
+
|
|
31
|
+ // This constructor is private to ensure you can't convert an index to a mask
|
|
32
|
+ // The compiler will stop here if you are mixing index and mask in your code.
|
|
33
|
+ // If you need to, you'll have to use the explicit static "from" method here
|
|
34
|
+ SerialMask(const serial_index_t);
|
|
35
|
+
|
|
36
|
+public:
|
|
37
|
+ inline constexpr bool enabled(const SerialMask PortMask) const { return mask & PortMask.mask; }
|
|
38
|
+ inline constexpr SerialMask combine(const SerialMask other) const { return SerialMask(mask | other.mask); }
|
|
39
|
+ inline constexpr SerialMask operator<< (const int offset) const { return SerialMask(mask << offset); }
|
|
40
|
+ static inline SerialMask from(const serial_index_t index) {
|
|
41
|
+ if (index.valid()) return SerialMask(_BV(index.index));
|
|
42
|
+ return SerialMask(0); // A invalid index mean no output
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ constexpr SerialMask(const uint8_t mask) : mask(mask) {}
|
|
46
|
+ constexpr SerialMask(const SerialMask & other) : mask(other.mask) {} // Can't use = default here since not all framework support this
|
|
47
|
+
|
|
48
|
+ static constexpr uint8_t All = 0xFF;
|
|
49
|
+};
|
29
|
50
|
|
30
|
51
|
// The most basic serial class: it dispatch to the base serial class with no hook whatsoever. This will compile to nothing but the base serial class
|
31
|
52
|
template <class SerialT>
|
|
@@ -39,10 +60,10 @@ struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
|
39
|
60
|
void msgDone() {}
|
40
|
61
|
|
41
|
62
|
// We don't care about indices here, since if one can call us, it's the right index anyway
|
42
|
|
- int available(uint8_t) { return (int)SerialT::available(); }
|
43
|
|
- int read(uint8_t) { return (int)SerialT::read(); }
|
44
|
|
- bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
|
45
|
|
- void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
|
|
63
|
+ int available(serial_index_t) { return (int)SerialT::available(); }
|
|
64
|
+ int read(serial_index_t) { return (int)SerialT::read(); }
|
|
65
|
+ bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
|
|
66
|
+ void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
|
46
|
67
|
|
47
|
68
|
// We have 2 implementation of the same method in both base class, let's say which one we want
|
48
|
69
|
using SerialT::available;
|
|
@@ -77,11 +98,10 @@ struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {
|
77
|
98
|
bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
|
78
|
99
|
void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
|
79
|
100
|
|
80
|
|
- int available(uint8_t ) { return (int)out.available(); }
|
81
|
|
- int read(uint8_t ) { return (int)out.read(); }
|
82
|
|
- int available() { return (int)out.available(); }
|
83
|
|
- int read() { return (int)out.read(); }
|
84
|
|
-
|
|
101
|
+ int available(serial_index_t ) { return (int)out.available(); }
|
|
102
|
+ int read(serial_index_t ) { return (int)out.read(); }
|
|
103
|
+ int available() { return (int)out.available(); }
|
|
104
|
+ int read() { return (int)out.read(); }
|
85
|
105
|
|
86
|
106
|
ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
|
87
|
107
|
};
|
|
@@ -102,8 +122,8 @@ struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
|
102
|
122
|
bool connected() { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
|
103
|
123
|
void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
|
104
|
124
|
|
105
|
|
- int available(uint8_t) { return (int)out.available(); }
|
106
|
|
- int read(uint8_t) { return (int)out.read(); }
|
|
125
|
+ int available(serial_index_t) { return (int)out.available(); }
|
|
126
|
+ int read(serial_index_t) { return (int)out.read(); }
|
107
|
127
|
int available() { return (int)out.available(); }
|
108
|
128
|
int read() { return (int)out.read(); }
|
109
|
129
|
|
|
@@ -130,8 +150,8 @@ struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public Seria
|
130
|
150
|
if (eofHook) eofHook(userPointer);
|
131
|
151
|
}
|
132
|
152
|
|
133
|
|
- int available(uint8_t) { return (int)SerialT::available(); }
|
134
|
|
- int read(uint8_t) { return (int)SerialT::read(); }
|
|
153
|
+ int available(serial_index_t) { return (int)SerialT::available(); }
|
|
154
|
+ int read(serial_index_t) { return (int)SerialT::read(); }
|
135
|
155
|
using SerialT::available;
|
136
|
156
|
using SerialT::read;
|
137
|
157
|
using SerialT::flush;
|
|
@@ -170,53 +190,51 @@ template <class Serial0T, class Serial1T, const uint8_t offset = 0, const uint8_
|
170
|
190
|
struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > {
|
171
|
191
|
typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > BaseClassT;
|
172
|
192
|
|
173
|
|
- uint8_t portMask;
|
|
193
|
+ SerialMask portMask;
|
174
|
194
|
Serial0T & serial0;
|
175
|
195
|
Serial1T & serial1;
|
176
|
196
|
|
177
|
|
- enum Masks {
|
178
|
|
- UsageMask = ((1 << step) - 1), // A bit mask containing as many bits as step
|
179
|
|
- FirstOutputMask = (UsageMask << offset),
|
180
|
|
- SecondOutputMask = (UsageMask << (offset + step)),
|
181
|
|
- AllMask = FirstOutputMask | SecondOutputMask,
|
182
|
|
- };
|
|
197
|
+ static constexpr uint8_t Usage = ((1 << step) - 1); // A bit mask containing as many bits as step
|
|
198
|
+ static constexpr uint8_t FirstOutput = (Usage << offset);
|
|
199
|
+ static constexpr uint8_t SecondOutput = (Usage << (offset + step));
|
|
200
|
+ static constexpr uint8_t Both = FirstOutput | SecondOutput;
|
183
|
201
|
|
184
|
202
|
NO_INLINE size_t write(uint8_t c) {
|
185
|
203
|
size_t ret = 0;
|
186
|
|
- if (portMask & FirstOutputMask) ret = serial0.write(c);
|
187
|
|
- if (portMask & SecondOutputMask) ret = serial1.write(c) | ret;
|
|
204
|
+ if (portMask.enabled(FirstOutput)) ret = serial0.write(c);
|
|
205
|
+ if (portMask.enabled(SecondOutput)) ret = serial1.write(c) | ret;
|
188
|
206
|
return ret;
|
189
|
207
|
}
|
190
|
208
|
NO_INLINE void msgDone() {
|
191
|
|
- if (portMask & FirstOutputMask) serial0.msgDone();
|
192
|
|
- if (portMask & SecondOutputMask) serial1.msgDone();
|
|
209
|
+ if (portMask.enabled(FirstOutput)) serial0.msgDone();
|
|
210
|
+ if (portMask.enabled(SecondOutput)) serial1.msgDone();
|
193
|
211
|
}
|
194
|
|
- int available(uint8_t index) {
|
195
|
|
- if (index >= 0 + offset && index < step + offset)
|
|
212
|
+ int available(serial_index_t index) {
|
|
213
|
+ if (index.within(0 + offset, step + offset - 1))
|
196
|
214
|
return serial0.available(index);
|
197
|
|
- else if (index >= step + offset && index < 2 * step + offset)
|
|
215
|
+ else if (index.within(step + offset, 2 * step + offset - 1))
|
198
|
216
|
return serial1.available(index);
|
199
|
217
|
return false;
|
200
|
218
|
}
|
201
|
|
- int read(uint8_t index) {
|
202
|
|
- if (index >= 0 + offset && index < step + offset)
|
|
219
|
+ int read(serial_index_t index) {
|
|
220
|
+ if (index.within(0 + offset, step + offset - 1))
|
203
|
221
|
return serial0.read(index);
|
204
|
|
- else if (index >= step + offset && index < 2 * step + offset)
|
|
222
|
+ else if (index.within(step + offset, 2 * step + offset - 1))
|
205
|
223
|
return serial1.read(index);
|
206
|
224
|
return -1;
|
207
|
225
|
}
|
208
|
226
|
void begin(const long br) {
|
209
|
|
- if (portMask & FirstOutputMask) serial0.begin(br);
|
210
|
|
- if (portMask & SecondOutputMask) serial1.begin(br);
|
|
227
|
+ if (portMask.enabled(FirstOutput)) serial0.begin(br);
|
|
228
|
+ if (portMask.enabled(SecondOutput)) serial1.begin(br);
|
211
|
229
|
}
|
212
|
230
|
void end() {
|
213
|
|
- if (portMask & FirstOutputMask) serial0.end();
|
214
|
|
- if (portMask & SecondOutputMask) serial1.end();
|
|
231
|
+ if (portMask.enabled(FirstOutput)) serial0.end();
|
|
232
|
+ if (portMask.enabled(SecondOutput)) serial1.end();
|
215
|
233
|
}
|
216
|
234
|
bool connected() {
|
217
|
235
|
bool ret = true;
|
218
|
|
- if (portMask & FirstOutputMask) ret = CALL_IF_EXISTS(bool, &serial0, connected);
|
219
|
|
- if (portMask & SecondOutputMask) ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
|
|
236
|
+ if (portMask.enabled(FirstOutput)) ret = CALL_IF_EXISTS(bool, &serial0, connected);
|
|
237
|
+ if (portMask.enabled(SecondOutput)) ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
|
220
|
238
|
return ret;
|
221
|
239
|
}
|
222
|
240
|
|
|
@@ -225,15 +243,15 @@ struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset,
|
225
|
243
|
|
226
|
244
|
// Redirect flush
|
227
|
245
|
NO_INLINE void flush() {
|
228
|
|
- if (portMask & FirstOutputMask) serial0.flush();
|
229
|
|
- if (portMask & SecondOutputMask) serial1.flush();
|
|
246
|
+ if (portMask.enabled(FirstOutput)) serial0.flush();
|
|
247
|
+ if (portMask.enabled(SecondOutput)) serial1.flush();
|
230
|
248
|
}
|
231
|
249
|
NO_INLINE void flushTX() {
|
232
|
|
- if (portMask & FirstOutputMask) CALL_IF_EXISTS(void, &serial0, flushTX);
|
233
|
|
- if (portMask & SecondOutputMask) CALL_IF_EXISTS(void, &serial1, flushTX);
|
|
250
|
+ if (portMask.enabled(FirstOutput)) CALL_IF_EXISTS(void, &serial0, flushTX);
|
|
251
|
+ if (portMask.enabled(SecondOutput)) CALL_IF_EXISTS(void, &serial1, flushTX);
|
234
|
252
|
}
|
235
|
253
|
|
236
|
|
- MultiSerial(Serial0T & serial0, Serial1T & serial1, int8_t mask = AllMask, const bool e = false) :
|
|
254
|
+ MultiSerial(Serial0T & serial0, Serial1T & serial1, const SerialMask mask = Both, const bool e = false) :
|
237
|
255
|
BaseClassT(e),
|
238
|
256
|
portMask(mask), serial0(serial0), serial1(serial1) {}
|
239
|
257
|
};
|