-
Notifications
You must be signed in to change notification settings - Fork 1
/
WirePacker.h
210 lines (195 loc) · 5.01 KB
/
WirePacker.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
@file WirePacker.h
@author Gutierrez PS <https://github.com/gutierrezps>
@brief Class to pack the data before sending to other I2C device
@date 2020-06-16
WirePacker is used to pack the data before sending it to
another I2C device, be it master->slave or slave->master.
After creating the packer object, add data with write()
or with Print methods such as printf(). When finished,
call end() to close the packet.
After that, use available() and read() methods to
read each packet byte and send to the other device.
Packet format:
[0]: start byte (0x02)
[1]: packet length
[2]: data[0]
[3]: data[1]
...
[n+1]: data[n-1]
[n+2]: CRC8 of packet length and data
[n+3]: end byte (0x04)
*/
#ifndef WirePacker_h
#define WirePacker_h
#include <Arduino.h>
#include <Print.h>
#include "WireCrc.h"
#define PACKER_BUFFER_LENGTH 128
class WirePacker : public Print {
public:
WirePacker();
/**
Add a byte to the packet, only if end() was not called yet.
* * @param data byte to be added
@return size_t 1 if the byte was added
*/
size_t write(uint8_t data);
/**
Add a number of bytes to the packet. The number of bytes added
may be different from quantity if the buffer becomes full.
* * @param data byte array to be added
@param quantity number of bytes to add
@return size_t number of bytes added
*/
size_t write(const uint8_t *data, size_t quantity);
inline size_t write(const char * s) {
return write((uint8_t*) s, strlen(s));
}
inline size_t write(unsigned long n) {
return write((uint8_t)n);
}
inline size_t write(long n) {
return write((uint8_t)n);
}
inline size_t write(unsigned int n) {
return write((uint8_t)n);
}
inline size_t write(int n) {
return write((uint8_t)n);
}
/**
Returns packet length so far
* * @return size_t
*/
size_t packetLength() const {
if (isPacketOpen_) {
return totalLength_ + 2;
}
return totalLength_;
}
/**
Closes the packet. After that, use avaiable() and read()
to get the packet bytes.
* */
void end();
/**
Returns how many packet bytes are available to be read.
* * @return size_t
*/
size_t available();
/**
Read the next available packet byte. At each call,
the value returned by available() will be decremented.
* * @return int -1 if there are no bytes to be read
*/
int read();
/**
Resets the packing process.
* */
void reset();
/**
Debug. Prints packet data to Serial.
* */
void printToSerial();
private:
const uint8_t frameStart_ = 0x02;
const uint8_t frameEnd_ = 0x04;
uint8_t buffer_[PACKER_BUFFER_LENGTH];
uint8_t index_;
uint8_t totalLength_;
bool isPacketOpen_;
};
#endif
/**
@file WirePacker.cpp
@author Gutierrez PS <https://github.com/gutierrezps>
@brief Class to pack the data before sending to other I2C device
@date 2020-06-16
*/
WirePacker::WirePacker() {
reset();
}
size_t WirePacker::write(uint8_t data) {
if (!isPacketOpen_) {
return 0;
}
// leave room for crc and end bytes
if (totalLength_ >= PACKER_BUFFER_LENGTH - 2) {
return 0;
}
buffer_[index_] = data;
++index_;
totalLength_ = index_;
return 1;
}
size_t WirePacker::write(const uint8_t *data, size_t quantity) {
for (size_t i = 0; i < quantity; ++i) {
if (!write(data[i])) {
return i;
}
}
return quantity;
}
void WirePacker::end() {
isPacketOpen_ = false;
// make room for CRC byte
++index_;
buffer_[index_] = frameEnd_;
++index_;
totalLength_ = index_;
buffer_[1] = totalLength_;
// ignore start, length, crc and end bytes
uint8_t payloadLength = totalLength_ - 4;
WireCrc crc8;
crc8.calc(&totalLength_, 1); // include length in CRC
uint8_t crc = crc8.update(buffer_ + 2, payloadLength);
buffer_[index_ - 2] = crc;
// prepare for reading
index_ = 0;
}
size_t WirePacker::available() {
if (isPacketOpen_) {
return 0;
}
return totalLength_ - index_;
}
int WirePacker::read() {
int value = -1;
if (!isPacketOpen_ && index_ < totalLength_) {
value = buffer_[index_];
++index_;
}
return value;
}
void WirePacker::reset() {
buffer_[0] = frameStart_;
index_ = 2;
totalLength_ = 2;
isPacketOpen_ = true;
}
void WirePacker::printToSerial() {
printf("length: %d, ", totalLength_);
if (isPacketOpen_) {
Serial.print("open, ");
} else {
Serial.print("closed, ");
}
if (totalLength_ > 2) {
Serial.print("buffer str: '");
for (size_t i = 0; i < totalLength_; ++i) {
if (buffer_[i] < 32 || buffer_[i] >= 127) {
Serial.print(".");
} else {
Serial.print((char) buffer_[i]);
}
}
Serial.print("', buffer hex: ");
for (size_t i = 0; i < totalLength_; ++i) {
printf("%02X ", buffer_[i]);
}
} else {
Serial.print("empty");
}
Serial.println();
}