|
@@ -24,6 +24,9 @@
|
24
|
24
|
#include "macros.h"
|
25
|
25
|
#include "serial_base.h"
|
26
|
26
|
|
|
27
|
+// Used in multiple places
|
|
28
|
+typedef int8_t serial_index_t;
|
|
29
|
+
|
27
|
30
|
// 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
|
28
|
31
|
template <class SerialT>
|
29
|
32
|
struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
|
|
@@ -35,10 +38,11 @@ struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
|
35
|
38
|
|
36
|
39
|
void msgDone() {}
|
37
|
40
|
|
38
|
|
- bool available(uint8_t index) { return index == 0 && SerialT::available(); }
|
39
|
|
- int read(uint8_t index) { return index == 0 ? SerialT::read() : -1; }
|
40
|
|
- bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
|
41
|
|
- void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
|
|
41
|
+ // 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); }
|
42
|
46
|
|
43
|
47
|
// We have 2 implementation of the same method in both base class, let's say which one we want
|
44
|
48
|
using SerialT::available;
|
|
@@ -65,18 +69,19 @@ struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {
|
65
|
69
|
bool & condition;
|
66
|
70
|
SerialT & out;
|
67
|
71
|
NO_INLINE size_t write(uint8_t c) { if (condition) return out.write(c); return 0; }
|
68
|
|
- void flush() { if (condition) out.flush(); }
|
69
|
|
- void begin(long br) { out.begin(br); }
|
70
|
|
- void end() { out.end(); }
|
|
72
|
+ void flush() { if (condition) out.flush(); }
|
|
73
|
+ void begin(long br) { out.begin(br); }
|
|
74
|
+ void end() { out.end(); }
|
71
|
75
|
|
72
|
76
|
void msgDone() {}
|
73
|
|
- bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
|
74
|
|
- void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
|
|
77
|
+ bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
|
|
78
|
+ void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
|
|
79
|
+
|
|
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(); }
|
75
|
84
|
|
76
|
|
- bool available(uint8_t index) { return index == 0 && out.available(); }
|
77
|
|
- int read(uint8_t index) { return index == 0 ? out.read() : -1; }
|
78
|
|
- using BaseClassT::available;
|
79
|
|
- using BaseClassT::read;
|
80
|
85
|
|
81
|
86
|
ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
|
82
|
87
|
};
|
|
@@ -97,10 +102,10 @@ struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
|
97
|
102
|
bool connected() { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
|
98
|
103
|
void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
|
99
|
104
|
|
100
|
|
- bool available(uint8_t index) { return index == 0 && out.available(); }
|
101
|
|
- int read(uint8_t index) { return index == 0 ? out.read() : -1; }
|
102
|
|
- bool available() { return out.available(); }
|
103
|
|
- int read() { return out.read(); }
|
|
105
|
+ int available(uint8_t) { return (int)out.available(); }
|
|
106
|
+ int read(uint8_t) { return (int)out.read(); }
|
|
107
|
+ int available() { return (int)out.available(); }
|
|
108
|
+ int read() { return (int)out.read(); }
|
104
|
109
|
|
105
|
110
|
ForwardSerial(const bool e, SerialT & out) : BaseClassT(e), out(out) {}
|
106
|
111
|
};
|
|
@@ -125,8 +130,8 @@ struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public Seria
|
125
|
130
|
if (eofHook) eofHook(userPointer);
|
126
|
131
|
}
|
127
|
132
|
|
128
|
|
- bool available(uint8_t index) { return index == 0 && SerialT::available(); }
|
129
|
|
- int read(uint8_t index) { return index == 0 ? SerialT::read() : -1; }
|
|
133
|
+ int available(uint8_t) { return (int)SerialT::available(); }
|
|
134
|
+ int read(uint8_t) { return (int)SerialT::read(); }
|
130
|
135
|
using SerialT::available;
|
131
|
136
|
using SerialT::read;
|
132
|
137
|
using SerialT::flush;
|
|
@@ -157,21 +162,22 @@ struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public Seria
|
157
|
162
|
|
158
|
163
|
// Forward constructor
|
159
|
164
|
template <typename... Args>
|
160
|
|
- RuntimeSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...) {}
|
|
165
|
+ RuntimeSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...), writeHook(0), eofHook(0), userPointer(0) {}
|
161
|
166
|
};
|
162
|
167
|
|
163
|
168
|
// A class that's duplicating its output conditionally to 2 serial interface
|
164
|
|
-template <class Serial0T, class Serial1T, const uint8_t offset = 0>
|
165
|
|
-struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset> > {
|
166
|
|
- typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset> > BaseClassT;
|
|
169
|
+template <class Serial0T, class Serial1T, const uint8_t offset = 0, const uint8_t step = 1>
|
|
170
|
+struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > {
|
|
171
|
+ typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > BaseClassT;
|
167
|
172
|
|
168
|
173
|
uint8_t portMask;
|
169
|
174
|
Serial0T & serial0;
|
170
|
175
|
Serial1T & serial1;
|
171
|
176
|
|
172
|
177
|
enum Masks {
|
173
|
|
- FirstOutputMask = (1 << offset),
|
174
|
|
- SecondOutputMask = (1 << (offset + 1)),
|
|
178
|
+ UsageMask = ((1 << step) - 1), // A bit mask containing as many bits as step
|
|
179
|
+ FirstOutputMask = (UsageMask << offset),
|
|
180
|
+ SecondOutputMask = (UsageMask << (offset + step)),
|
175
|
181
|
AllMask = FirstOutputMask | SecondOutputMask,
|
176
|
182
|
};
|
177
|
183
|
|
|
@@ -185,19 +191,19 @@ struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset>
|
185
|
191
|
if (portMask & FirstOutputMask) serial0.msgDone();
|
186
|
192
|
if (portMask & SecondOutputMask) serial1.msgDone();
|
187
|
193
|
}
|
188
|
|
- bool available(uint8_t index) {
|
189
|
|
- switch(index) {
|
190
|
|
- case 0 + offset: return serial0.available();
|
191
|
|
- case 1 + offset: return serial1.available();
|
192
|
|
- default: return false;
|
193
|
|
- }
|
|
194
|
+ int available(uint8_t index) {
|
|
195
|
+ if (index >= 0 + offset && index < step + offset)
|
|
196
|
+ return serial0.available(index);
|
|
197
|
+ else if (index >= step + offset && index < 2 * step + offset)
|
|
198
|
+ return serial1.available(index);
|
|
199
|
+ return false;
|
194
|
200
|
}
|
195
|
|
- NO_INLINE int read(uint8_t index) {
|
196
|
|
- switch(index) {
|
197
|
|
- case 0 + offset: return serial0.read();
|
198
|
|
- case 1 + offset: return serial1.read();
|
199
|
|
- default: return -1;
|
200
|
|
- }
|
|
201
|
+ int read(uint8_t index) {
|
|
202
|
+ if (index >= 0 + offset && index < step + offset)
|
|
203
|
+ return serial0.read(index);
|
|
204
|
+ else if (index >= step + offset && index < 2 * step + offset)
|
|
205
|
+ return serial1.read(index);
|
|
206
|
+ return -1;
|
201
|
207
|
}
|
202
|
208
|
void begin(const long br) {
|
203
|
209
|
if (portMask & FirstOutputMask) serial0.begin(br);
|