-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.h
57 lines (50 loc) · 1.47 KB
/
Message.h
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
#if not defined (__AVR__) || (__avr__)
#include "Serial.h"
#endif
#include "SerialBuffer.h"
/**
* Message abstraction for Serial I/O
*/
template <unsigned int Size>
struct Message {
private:
SerialBuffer<Size> buffer;
public:
Message() {}
/**
* Get data from message and put directly into argument addresses
* - Returns success of read operation
* - If unsuccessful, the data is unmodified
*/
template <typename ...Data>
inline bool read(Data&... data) {
constexpr auto size = (0 + ... + sizeof(Data));
static_assert(Size == size, "Incorrect data size");
if (!buffer.receive()) return false;
(buffer >> ... >> data);
buffer.reset();
return true;
}
/** Write objects directly into message and Serial output */
template <typename ...Data>
inline void write(Data... data) {
constexpr auto size = (0 + ... + sizeof(Data));
static_assert(Size == size, "Incorrect data size");
(buffer << ... << data);
buffer.emit();
}
/** Write string directly into message and Serial output */
template<unsigned int N>
inline void write(const char (&str)[N]) {
static_assert(N == Size, "Incorrect string size");
for (auto c: str) buffer << c;
buffer.emit();
}
/** Write array directly into message and Serial output */
template<typename T, unsigned int N>
inline void write(const T (&obj)[N]) {
static_assert(N * sizeof(T) == Size, "Incorrect array size");
for (auto c: obj) buffer << c;
buffer.emit();
}
};