gusimplewhiteboard
wb_particle_position.c
Go to the documentation of this file.
1/*
2 * file wb_particle_position.c
3 *
4 * This file was generated by classgenerator from Particle_Position.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Vlad Estivill-Castro. 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 Vlad Estivill-Castro.
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
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_particle_position_description(const struct wb_particle_position* 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 = gu_strlcat(descString, "position={", bufferSize);
134 if (len >= bufferSize) {
135 return descString;
136 }
137 char position_buffer[POINT2D_DESC_BUFFER_SIZE];
138 char* position_p = position_buffer;
139 const char* position_description = wb_point2d_description(&self->position, position_p, POINT2D_DESC_BUFFER_SIZE);
140 len = gu_strlcat(descString, position_p, bufferSize);
141 if (len >= bufferSize) {
142 return descString;
143 }
144 len = gu_strlcat(descString, "}", bufferSize);
145 if (len >= bufferSize) {
146 return descString;
147 }
148 len = gu_strlcat(descString, ", ", bufferSize);
149 if (len >= bufferSize) {
150 return descString;
151 }
152 len += snprintf(descString + len, bufferSize - len, "headingInDegrees=%d", self->headingInDegrees);
153 if (len >= bufferSize) {
154 return descString;
155 }
156 len = gu_strlcat(descString, ", ", bufferSize);
157 if (len >= bufferSize) {
158 return descString;
159 }
160 len += snprintf(descString + len, bufferSize - len, "confidence=%lf", (double) self->confidence);
161 return descString;
162#pragma clang diagnostic pop
163}
164
168const char* wb_particle_position_to_string(const struct wb_particle_position* self, char* toString, size_t bufferSize)
169{
170#pragma clang diagnostic push
171#pragma clang diagnostic ignored "-Wunused-variable"
172 size_t len = 0;
173 if (len >= bufferSize) {
174 return toString;
175 }
176 len = gu_strlcat(toString, "{", bufferSize);
177 if (len >= bufferSize) {
178 return toString;
179 }
180 char position_buffer[POINT2D_TO_STRING_BUFFER_SIZE];
181 char* position_p = position_buffer;
182 const char* position_to_string = wb_point2d_to_string(&self->position, position_p, POINT2D_TO_STRING_BUFFER_SIZE);
183 len = gu_strlcat(toString, position_p, bufferSize);
184 if (len >= bufferSize) {
185 return toString;
186 }
187 len = gu_strlcat(toString, "}", bufferSize);
188 if (len >= bufferSize) {
189 return toString;
190 }
191 len = gu_strlcat(toString, ", ", bufferSize);
192 if (len >= bufferSize) {
193 return toString;
194 }
195 len += snprintf(toString + len, bufferSize - len, "%d", self->headingInDegrees);
196 if (len >= bufferSize) {
197 return toString;
198 }
199 len = gu_strlcat(toString, ", ", bufferSize);
200 if (len >= bufferSize) {
201 return toString;
202 }
203 len += snprintf(toString + len, bufferSize - len, "%lf", (double) self->confidence);
204 return toString;
205#pragma clang diagnostic pop
206}
207
212{
213 size_t temp_length = strlen(str);
214 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
215 if (length < 1 || length > PARTICLE_POSITION_DESC_BUFFER_SIZE) {
216 return self;
217 }
218 char var_str_buffer[PARTICLE_POSITION_DESC_BUFFER_SIZE + 1];
219 char* var_str = &var_str_buffer[0];
220 char key_buffer[17];
221 char* key = &key_buffer[0];
222 int bracecount = 0;
223 int startVar = 0;
224 int index = 0;
225 int startKey = 0;
226 int endKey = -1;
227 int varIndex = 0;
228 if (index == 0 && str[0] == '{') {
229 index = 1;
230 }
231 startVar = index;
232 startKey = startVar;
233 do {
234 for (int i = index; i < length; i++) {
235 index = i + 1;
236 if (bracecount == 0 && str[i] == '=') {
237 endKey = i - 1;
238 startVar = index;
239 continue;
240 }
241 if (bracecount == 0 && isspace(str[i])) {
242 startVar = index;
243 if (endKey == -1) {
244 startKey = index;
245 }
246 continue;
247 }
248 if (bracecount == 0 && str[i] == ',') {
249 index = i - 1;
250 break;
251 }
252 if (str[i] == '{') {
253 bracecount++;
254 continue;
255 }
256 if (str[i] == '}') {
257 bracecount--;
258 if (bracecount < 0) {
259 index = i - 1;
260 break;
261 }
262 }
263 if (i == length - 1) {
264 index = i;
265 }
266 }
267 if (endKey >= startKey && endKey - startKey < length) {
268 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
269 key[(endKey - startKey) + 1] = 0;
270 } else {
271 key[0] = 0;
272 }
273 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
274 var_str[(index - startVar) + 1] = 0;
275 bracecount = 0;
276 index += 2;
277 startVar = index;
278 startKey = startVar;
279 endKey = -1;
280 if (strlen(key) > 0) {
281 if (0 == strcmp("position", key)) {
282 varIndex = 0;
283 } else if (0 == strcmp("headingInDegrees", key)) {
284 varIndex = 1;
285 } else if (0 == strcmp("confidence", key)) {
286 varIndex = 2;
287 } else {
288 varIndex = -1;
289 }
290 }
291 switch (varIndex) {
292 case -1: { break; }
293 case 0:
294 {
295 wb_point2d_from_string(&self->position, var_str);
296 break;
297 }
298 case 1:
299 {
300 self->headingInDegrees = ((int16_t)atoi(var_str));
301 break;
302 }
303 case 2:
304 {
305 self->confidence = ((float)atof(var_str));
306 break;
307 }
308 }
309 if (varIndex >= 0) {
310 varIndex++;
311 }
312 } while(index < length);
313 return self;
314}
315
316/*#ifdef WHITEBOARD_SERIALISATION*/
317
322{
323 uint16_t bit_offset = 0;
324 int16_t headingInDegrees_nbo = htons(self->headingInDegrees);
325 do {
326 int8_t b;
327 for (b = (16 - 1); b >= 0; b--) {
328 do {
329 uint16_t byte = bit_offset / 8;
330 uint16_t bit = 7 - (bit_offset % 8);
331 unsigned long newbit = !!((headingInDegrees_nbo >> b) & 1U);
332 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
333 bit_offset = bit_offset + 1;
334 } while(false);
335 }
336 } while(false);
337
338 //The class generator does not support float types for network conversion.
339 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
340 (void)self;
341 (void)dst;
342 return bit_offset;
343}
344
349{
350 uint16_t bit_offset = 0;
351 do {
352 int8_t b;
353 for (b = (16 - 1); b >= 0; b--) {
354 do {
355 uint16_t byte = bit_offset / 8;
356 uint16_t bit = 7 - (bit_offset % 8);
357 char dataByte = src[byte];
358 unsigned char bitValue = (dataByte >> bit) & 1U;
359 dst->headingInDegrees ^= (-bitValue ^ dst->headingInDegrees) & (1UL << b);
360 bit_offset = bit_offset + 1;
361 } while(false);
362 }
363 } while(false);
365
366 //The class generator does not support float types for network conversion.
367 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
368 (void)src;
369 (void)dst;
370 return bit_offset;
371}
372
373/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
int16_t headingInDegrees
90 degrees is looking ahead into the oponetns goal.
#define htons(x)
const char * wb_particle_position_description(const struct wb_particle_position *self, char *descString, size_t bufferSize)
Convert to a description string.
size_t wb_particle_position_from_network_serialised(const char *src, struct wb_particle_position *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_particle_position_to_string(const struct wb_particle_position *self, char *toString, size_t bufferSize)
Convert to a string.
size_t wb_particle_position_to_network_serialised(const struct wb_particle_position *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
struct wb_particle_position * wb_particle_position_from_string(struct wb_particle_position *self, const char *str)
Convert from a string.
#define ntohs(x)
#define PARTICLE_POSITION_DESC_BUFFER_SIZE
struct wb_point2d * wb_point2d_from_string(struct wb_point2d *self, const char *str)
Convert from a string.
Definition: wb_point2d.c:173
const char * wb_point2d_description(const struct wb_point2d *self, char *descString, size_t bufferSize)
Convert to a description string.
Definition: wb_point2d.c:125
const char * wb_point2d_to_string(const struct wb_point2d *self, char *toString, size_t bufferSize)
Convert to a string.
Definition: wb_point2d.c:149
#define POINT2D_DESC_BUFFER_SIZE
Definition: wb_point2d.h:93
#define POINT2D_TO_STRING_BUFFER_SIZE
Definition: wb_point2d.h:94