writer.c
7.64 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (C) 2017 Inria
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup drivers_feetech
* @{
*
* @file
* @brief Feetech messages writer
*
* @author Loïc Dauphin <loic.dauphin@inria.fr>
*
* @}
*/
#include "feetech_writer.h"
void feetech_writer_init(feetech_writer_t *writer, uint8_t *buffer, size_t buffer_max_size)
{
writer->buffer = buffer;
writer->size = 0;
writer->buffer_max_size = buffer_max_size;
}
const uint8_t *feetech_writer_get_data(const feetech_writer_t *writer)
{
return (const uint8_t*)writer->buffer;
}
size_t feetech_writer_get_size(const feetech_writer_t *writer)
{
return writer->size;
}
void feetech_writer_response_make(feetech_writer_t *writer, uint8_t id, const uint8_t *buffer, size_t size)
{
const size_t len = 2 + size;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = id;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = 0;
for (size_t i = 0 ; i < size ; i++) {
sum += writer->buffer[5 + i] = buffer[i];
}
writer->buffer[size - 1] = ~sum;
}
else {
writer->size = 0;
}
}
void feetech_writer_ack_make(feetech_writer_t *writer, uint8_t id)
{
const size_t len = 2;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = id;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = 0;
writer->buffer[5] = ~sum;
}
else {
writer->size = 0;
}
}
void feetech_writer_ping_make(feetech_writer_t *writer, uint8_t id)
{
const size_t len = 2;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = id;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = INST_PING;
writer->buffer[5] = ~sum;
}
else {
writer->size = 0;
}
}
void feetech_writer_write8_make(feetech_writer_t *writer, uint8_t id, uint8_t reg, uint8_t value)
{
const size_t len = 4;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = id;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = INST_WRITE;
sum += writer->buffer[5] = reg;
sum += writer->buffer[6] = value;
writer->buffer[7] = ~sum;
}
else {
writer->size = 0;
}
}
void feetech_writer_write16_make(feetech_writer_t *writer, uint8_t id, uint8_t reg, uint16_t value)
{
const size_t len = 5;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = id;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = INST_WRITE;
sum += writer->buffer[5] = reg;
sum += writer->buffer[6] = (value >> 8) & 0xFF;
sum += writer->buffer[7] = value & 0xFF;
writer->buffer[8] = ~sum;
}
else {
writer->size = 0;
}
}
void feetech_writer_write_make(feetech_writer_t *writer, uint8_t id, uint8_t reg, const uint8_t *buffer, size_t size)
{
const size_t len = 3 + size;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = id;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = INST_WRITE;
sum += writer->buffer[5] = reg;
for (size_t i = 0 ; i < size ; i++) {
sum += writer->buffer[6 + i] = buffer[i];
}
writer->buffer[writer->size - 1] = ~sum;
}
else {
writer->size = 0;
}
}
void feetech_writer_read_make(feetech_writer_t *writer, uint8_t id, uint8_t reg, size_t size)
{
const size_t len = 4;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = id;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = INST_READ;
sum += writer->buffer[5] = reg;
sum += writer->buffer[6] = (uint8_t)size;
writer->buffer[7] = ~sum;
}
else {
writer->size = 0;
}
}
size_t feetech_writer_sync_write_required(feetech_writer_t *writer)
{
if (8 <= writer->size && writer->buffer[4] == INST_SYNC_WRITE) {
return writer->buffer[6];
}
return 0;
}
void feetech_writer_sync_write_end(feetech_writer_t *writer)
{
if (writer->size <= 8) {
writer->size = 0;
}
}
void feetech_writer_sync_write_add(feetech_writer_t *writer, uint8_t id, const uint8_t *buffer, size_t size)
{
if (feetech_writer_sync_write_required(writer) == size &&
size != 0 && writer->size + size + 1 <= writer->buffer_max_size) {
uint8_t sum = ~writer->buffer[writer->size - 1];
sum += writer->buffer[writer->size - 1] = id;
for (size_t i = 0 ; i < size ; i++) {
sum += writer->buffer[writer->size + i] = buffer[i];
}
writer->buffer[3] += size + 1;
writer->buffer[writer->size + size] = ~(sum + size + 1);
writer->size += size + 1;
}
else {
writer->size = 0;
}
}
void feetech_writer_sync_write_add_8bits(feetech_writer_t *writer, uint8_t id, uint8_t value)
{
if (feetech_writer_sync_write_required(writer) == 1 &&
writer->size + 2 <= writer->buffer_max_size) {
uint8_t sum = ~writer->buffer[writer->size - 1];
sum += writer->buffer[writer->size - 1] = id;
sum += writer->buffer[writer->size] = value;
writer->buffer[3] += 2;
writer->buffer[writer->size + 1] = ~(sum + 2);
writer->size += 2;
}
else {
writer->size = 0;
}
}
void feetech_writer_sync_write_add_16bits(feetech_writer_t *writer, uint8_t id, uint16_t value)
{
if (feetech_writer_sync_write_required(writer) == 2 &&
writer->size + 3 <= writer->buffer_max_size) {
uint8_t sum = ~writer->buffer[writer->size - 1];
sum += writer->buffer[writer->size - 1] = id;
sum += writer->buffer[writer->size + 0] = (value >> 8) & 0xFF;
sum += writer->buffer[writer->size + 1] = value & 0xFF;
writer->buffer[3] += 3;
writer->buffer[writer->size + 2] = ~(sum + 3);
writer->size += 3;
}
else {
writer->size = 0;
}
}
void feetech_writer_sync_write_begin(feetech_writer_t *writer, uint8_t reg, size_t size)
{
const size_t len = 4;
if (len + 4 <= writer->buffer_max_size) {
writer->size = len + 4;
uint8_t sum = 0;
writer->buffer[0] = FEETECH_START;
writer->buffer[1] = FEETECH_START;
sum += writer->buffer[2] = 0xFF;
sum += writer->buffer[3] = len;
sum += writer->buffer[4] = INST_SYNC_WRITE;
sum += writer->buffer[5] = reg;
sum += writer->buffer[6] = (uint8_t)size;
writer->buffer[7] = ~sum;
}
}