-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMassagePacker.h
190 lines (155 loc) · 4.74 KB
/
MassagePacker.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
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
/* * Copyright (C) 2017 Thomas O. Fredericks, Sofian Audry
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef MassagePacker_h
#define MassagePacker_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#ifndef MASSAGE_PACKER_BUFFERSIZE
/**
* Max. size of message buffer. Can be predefined before including MassagePacker.h
* to increase/decrease size.
*/
#define MASSAGE_PACKER_BUFFERSIZE 256
#endif
/// Main MassagePacker abstract class.
class MassagePacker
{
public:
/// Constructor.
MassagePacker() {
flush();
}
// Virtual destructor.
virtual ~MassagePacker() {}
/// Flushes current message in buffer (if any).
void flush() {
_messageSize = 0;
}
/// Returns size of buffer.
size_t size() const {
return _messageSize;
}
// Returns a pointer to the buffer.
const uint8_t* buffer() const {
return _buffer;
}
/// Begins the sending of a message.
virtual void beginPacket(const char* address) = 0;
/// Adds a byte.
virtual void addByte(uint8_t value) = 0;
/// Adds an int.
virtual void addInt(int16_t value) = 0;
/// Adds a long.
virtual void addLong(int32_t value) = 0;
/// Adds a float.
virtual void addFloat(float value) = 0;
/// Ends the sending of a message.
virtual void endPacket() = 0;
/// Ends the sending of a message and sends it through a Stream
virtual void streamPacket(Stream* stream) {
endPacket();
stream->write( buffer(), size() );
}
/// Create a packet with no arguments.
virtual void packEmpty(const char *address)
{
beginPacket(address);
endPacket();
}
/// Create a packet with a single byte value.
virtual void packOneByte(const char *address, uint8_t value)
{
beginPacket(address);
addByte(value);
endPacket();
}
/// Create a packet with a single int value.
virtual void packOneInt(const char *address, int16_t value)
{
beginPacket(address);
addInt(value);
endPacket();
}
/// Create a packet with a single long value.
virtual void packOneLong(const char *address, int32_t value)
{
beginPacket(address);
addLong(value);
endPacket();
}
/// Create a packet with a single float value.
virtual void packOneFloat(const char *address, float value)
{
beginPacket(address);
addFloat(value);
endPacket();
}
/// Create a packet with no arguments.
virtual void streamEmpty(Stream* stream, const char *address)
{
beginPacket(address);
streamPacket(stream);
}
/// Create a packet with a single byte value.
virtual void streamOneByte(Stream* stream, const char *address, uint8_t value)
{
beginPacket(address);
addByte(value);
streamPacket(stream);
}
/// Create a packet with a single int value.
virtual void streamOneInt(Stream* stream, const char *address, int16_t value)
{
beginPacket(address);
addInt(value);
streamPacket(stream);
}
/// Create a packet with a single long value.
virtual void streamOneLong(Stream* stream, const char *address, int32_t value)
{
beginPacket(address);
addLong(value);
streamPacket(stream);
}
/// Create a packet with a single float value.
virtual void streamOneFloat(Stream* stream, const char *address, float value)
{
beginPacket(address);
addFloat(value);
streamPacket(stream);
}
protected:
// Writes single byte to buffer (returns false if buffer is full and cannot be written to).
bool _store(uint8_t value)
{
if (_messageSize >= MASSAGE_PACKER_BUFFERSIZE)
return false;
_buffer[_messageSize++] = value;
return true;
}
// Current size of message in buffer.
size_t _messageSize;
// Buffer that holds the data for current message to be sent.
uint8_t _buffer[MASSAGE_PACKER_BUFFERSIZE];
};
#endif