gusimplewhiteboard
wb_odometry.c
Go to the documentation of this file.
1/*
2 * file wb_odometry.c
3 *
4 * This file was generated by classgenerator from odometry.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Dimitri Joukoff. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 *
24 * This product includes software developed by Dimitri Joukoff.
25 *
26 * 4. Neither the name of the author nor the names of contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * -----------------------------------------------------------------------
43 * This program is free software; you can redistribute it and/or
44 * modify it under the above terms or under the terms of the GNU
45 * General Public License as published by the Free Software Foundation;
46 * either version 2 of the License, or (at your option) any later version.
47 *
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, see http://www.gnu.org/licenses/
55 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
56 * Fifth Floor, Boston, MA 02110-1301, USA.
57 *
58 */
59
60#ifndef WHITEBOARD_POSTER_STRING_CONVERSION
61#define WHITEBOARD_POSTER_STRING_CONVERSION
62#endif // WHITEBOARD_POSTER_STRING_CONVERSION
63
64#include "wb_odometry.h"
65#include <stdio.h>
66#include <string.h>
67#include <stdlib.h>
68#include <ctype.h>
69#include <limits.h>
70
71/* Network byte order functions */
72#pragma clang diagnostic push
73#pragma clang diagnostic ignored "-Wunused-macros"
74#if defined(__linux)
75# include <endian.h>
76# include <byteswap.h>
77#elif defined(__APPLE__)
78# include <machine/endian.h> //Needed for __BYTE_ORDER
79# include <architecture/byte_order.h> //Needed for byte swap functions
80# define bswap_16(x) NXSwapShort(x)
81# define bswap_32(x) NXSwapInt(x)
82# define bswap_64(x) NXSwapLongLong(x)
83#elif defined(ESP8266)
84# define bswap_16(x) __builtin_bswap16(x)
85# define bswap_32(x) __builtin_bswap32(x)
86# define bswap_64(x) __builtin_bswap64(x)
87#else
88 //Manually define swap macros?
89#endif
90
91#if (!defined(__BYTE_ORDER) && !defined(__LITTLE_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN)
92# if !defined(htonll) && !defined(ntohll)
93# define htonll(x) bswap_64(x)
94# define ntohll(x) bswap_64(x)
95# endif
96# if !defined(htonl) && !defined(ntohl)
97# define htonl(x) bswap_32(x)
98# define ntohl(x) bswap_32(x)
99# endif
100# if !defined(htons) && !defined(ntohs)
101# define htons(x) bswap_16(x)
102# define ntohs(x) bswap_16(x)
103# endif
104#else
105# if !defined(htonll) && !defined(ntohll)
106# define htonll(x) (x)
107# define ntohll(x) (x)
108# endif
109# if !defined(htonl) && !defined(ntohl)
110# define htonl(x) (x)
111# define ntohl(x) (x)
112# endif
113# if !defined(htons) && !defined(ntohs)
114# define htons(x) (x)
115# define ntohs(x) (x)
116# endif
117#endif
118#pragma clang diagnostic pop
119
120
121
125const char* wb_odometry_description(const struct wb_odometry* self, char* descString, size_t bufferSize)
126{
127#pragma clang diagnostic push
128#pragma clang diagnostic ignored "-Wunused-variable"
129 size_t len = 0;
130 if (len >= bufferSize) {
131 return descString;
132 }
133 len += snprintf(descString + len, bufferSize - len, "forward=%lf", (double) self->forward);
134 if (len >= bufferSize) {
135 return descString;
136 }
137 len = gu_strlcat(descString, ", ", bufferSize);
138 if (len >= bufferSize) {
139 return descString;
140 }
141 len += snprintf(descString + len, bufferSize - len, "left=%lf", (double) self->left);
142 if (len >= bufferSize) {
143 return descString;
144 }
145 len = gu_strlcat(descString, ", ", bufferSize);
146 if (len >= bufferSize) {
147 return descString;
148 }
149 len += snprintf(descString + len, bufferSize - len, "turn=%lf", (double) self->turn);
150 return descString;
151#pragma clang diagnostic pop
152}
153
157const char* wb_odometry_to_string(const struct wb_odometry* self, char* toString, size_t bufferSize)
158{
159#pragma clang diagnostic push
160#pragma clang diagnostic ignored "-Wunused-variable"
161 size_t len = 0;
162 if (len >= bufferSize) {
163 return toString;
164 }
165 len += snprintf(toString + len, bufferSize - len, "%lf", (double) self->forward);
166 if (len >= bufferSize) {
167 return toString;
168 }
169 len = gu_strlcat(toString, ", ", bufferSize);
170 if (len >= bufferSize) {
171 return toString;
172 }
173 len += snprintf(toString + len, bufferSize - len, "%lf", (double) self->left);
174 if (len >= bufferSize) {
175 return toString;
176 }
177 len = gu_strlcat(toString, ", ", bufferSize);
178 if (len >= bufferSize) {
179 return toString;
180 }
181 len += snprintf(toString + len, bufferSize - len, "%lf", (double) self->turn);
182 return toString;
183#pragma clang diagnostic pop
184}
185
189struct wb_odometry* wb_odometry_from_string(struct wb_odometry* self, const char* str)
190{
191 size_t temp_length = strlen(str);
192 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
193 if (length < 1 || length > ODOMETRY_DESC_BUFFER_SIZE) {
194 return self;
195 }
196 char var_str_buffer[ODOMETRY_DESC_BUFFER_SIZE + 1];
197 char* var_str = &var_str_buffer[0];
198 char key_buffer[8];
199 char* key = &key_buffer[0];
200 int bracecount = 0;
201 int startVar = 0;
202 int index = 0;
203 int startKey = 0;
204 int endKey = -1;
205 int varIndex = 0;
206 if (index == 0 && str[0] == '{') {
207 index = 1;
208 }
209 startVar = index;
210 startKey = startVar;
211 do {
212 for (int i = index; i < length; i++) {
213 index = i + 1;
214 if (bracecount == 0 && str[i] == '=') {
215 endKey = i - 1;
216 startVar = index;
217 continue;
218 }
219 if (bracecount == 0 && isspace(str[i])) {
220 startVar = index;
221 if (endKey == -1) {
222 startKey = index;
223 }
224 continue;
225 }
226 if (bracecount == 0 && str[i] == ',') {
227 index = i - 1;
228 break;
229 }
230 if (str[i] == '{') {
231 bracecount++;
232 continue;
233 }
234 if (str[i] == '}') {
235 bracecount--;
236 if (bracecount < 0) {
237 index = i - 1;
238 break;
239 }
240 }
241 if (i == length - 1) {
242 index = i;
243 }
244 }
245 if (endKey >= startKey && endKey - startKey < length) {
246 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
247 key[(endKey - startKey) + 1] = 0;
248 } else {
249 key[0] = 0;
250 }
251 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
252 var_str[(index - startVar) + 1] = 0;
253 bracecount = 0;
254 index += 2;
255 startVar = index;
256 startKey = startVar;
257 endKey = -1;
258 if (strlen(key) > 0) {
259 if (0 == strcmp("forward", key)) {
260 varIndex = 0;
261 } else if (0 == strcmp("left", key)) {
262 varIndex = 1;
263 } else if (0 == strcmp("turn", key)) {
264 varIndex = 2;
265 } else {
266 varIndex = -1;
267 }
268 }
269 switch (varIndex) {
270 case -1: { break; }
271 case 0:
272 {
273 self->forward = ((float)atof(var_str));
274 break;
275 }
276 case 1:
277 {
278 self->left = ((float)atof(var_str));
279 break;
280 }
281 case 2:
282 {
283 self->turn = ((float)atof(var_str));
284 break;
285 }
286 }
287 if (varIndex >= 0) {
288 varIndex++;
289 }
290 } while(index < length);
291 return self;
292}
293
294/*#ifdef WHITEBOARD_SERIALISATION*/
295
299size_t wb_odometry_to_network_serialised(const struct wb_odometry *self, char *dst)
300{
301 uint16_t bit_offset = 0;
302 //The class generator does not support float types for network conversion.
303
304 //The class generator does not support float types for network conversion.
305
306 //The class generator does not support float types for network conversion.
307 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
308 (void)self;
309 (void)dst;
310 return bit_offset;
311}
312
316size_t wb_odometry_from_network_serialised(const char *src, struct wb_odometry *dst)
317{
318 uint16_t bit_offset = 0;
319 //The class generator does not support float types for network conversion.
320
321 //The class generator does not support float types for network conversion.
322
323 //The class generator does not support float types for network conversion.
324 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
325 (void)src;
326 (void)dst;
327 return bit_offset;
328}
329
330/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
Definition: wb_odometry.h:109
const char * wb_odometry_to_string(const struct wb_odometry *self, char *toString, size_t bufferSize)
Convert to a string.
Definition: wb_odometry.c:157
const char * wb_odometry_description(const struct wb_odometry *self, char *descString, size_t bufferSize)
Convert to a description string.
Definition: wb_odometry.c:125
struct wb_odometry * wb_odometry_from_string(struct wb_odometry *self, const char *str)
Convert from a string.
Definition: wb_odometry.c:189
size_t wb_odometry_to_network_serialised(const struct wb_odometry *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
Definition: wb_odometry.c:299
size_t wb_odometry_from_network_serialised(const char *src, struct wb_odometry *dst)
Convert from a compressed, serialised, network byte order byte stream.
Definition: wb_odometry.c:316
#define ODOMETRY_DESC_BUFFER_SIZE
Definition: wb_odometry.h:97