gusimplewhiteboard
wb_vision_detection_ball.c
Go to the documentation of this file.
1/*
2 * file wb_vision_detection_ball.c
3 *
4 * This file was generated by classgenerator from vision_detection_ball.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Carl Lusty. 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 Carl Lusty.
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_vision_detection_ball_description(const struct wb_vision_detection_ball* 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 switch (self->sightingType) {
134 case BallDetected:
135 {
136 len += snprintf(descString + len, bufferSize - len, "sightingType=BallDetected");
137 break;
138 }
139 case NoBallDetected:
140 {
141 len += snprintf(descString + len, bufferSize - len, "sightingType=NoBallDetected");
142 break;
143 }
144 }
145 if (len >= bufferSize) {
146 return descString;
147 }
148 len = gu_strlcat(descString, ", ", bufferSize);
149 if (len >= bufferSize) {
150 return descString;
151 }
152 len = gu_strlcat(descString, "coordinate={", bufferSize);
153 if (len >= bufferSize) {
154 return descString;
155 }
156 char coordinate_buffer[PIXEL_COORDINATE_DESC_BUFFER_SIZE];
157 char* coordinate_p = coordinate_buffer;
158 const char* coordinate_description = wb_pixel_coordinate_description(&self->coordinate, coordinate_p, PIXEL_COORDINATE_DESC_BUFFER_SIZE);
159 len = gu_strlcat(descString, coordinate_p, bufferSize);
160 if (len >= bufferSize) {
161 return descString;
162 }
163 len = gu_strlcat(descString, "}", bufferSize);
164 if (len >= bufferSize) {
165 return descString;
166 }
167 len = gu_strlcat(descString, ", ", bufferSize);
168 if (len >= bufferSize) {
169 return descString;
170 }
171 len += snprintf(descString + len, bufferSize - len, "verticalRadius=%u", self->verticalRadius);
172 if (len >= bufferSize) {
173 return descString;
174 }
175 len = gu_strlcat(descString, ", ", bufferSize);
176 if (len >= bufferSize) {
177 return descString;
178 }
179 len += snprintf(descString + len, bufferSize - len, "horizontalRadius=%u", self->horizontalRadius);
180 return descString;
181#pragma clang diagnostic pop
182}
183
187const char* wb_vision_detection_ball_to_string(const struct wb_vision_detection_ball* self, char* toString, size_t bufferSize)
188{
189#pragma clang diagnostic push
190#pragma clang diagnostic ignored "-Wunused-variable"
191 size_t len = 0;
192 if (len >= bufferSize) {
193 return toString;
194 }
195 switch (self->sightingType) {
196 case BallDetected:
197 {
198 len += snprintf(toString + len, bufferSize - len, "BallDetected");
199 break;
200 }
201 case NoBallDetected:
202 {
203 len += snprintf(toString + len, bufferSize - len, "NoBallDetected");
204 break;
205 }
206 }
207 if (len >= bufferSize) {
208 return toString;
209 }
210 len = gu_strlcat(toString, ", ", bufferSize);
211 if (len >= bufferSize) {
212 return toString;
213 }
214 len = gu_strlcat(toString, "{", bufferSize);
215 if (len >= bufferSize) {
216 return toString;
217 }
218 char coordinate_buffer[PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE];
219 char* coordinate_p = coordinate_buffer;
220 const char* coordinate_to_string = wb_pixel_coordinate_to_string(&self->coordinate, coordinate_p, PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE);
221 len = gu_strlcat(toString, coordinate_p, bufferSize);
222 if (len >= bufferSize) {
223 return toString;
224 }
225 len = gu_strlcat(toString, "}", bufferSize);
226 if (len >= bufferSize) {
227 return toString;
228 }
229 len = gu_strlcat(toString, ", ", bufferSize);
230 if (len >= bufferSize) {
231 return toString;
232 }
233 len += snprintf(toString + len, bufferSize - len, "%u", self->verticalRadius);
234 if (len >= bufferSize) {
235 return toString;
236 }
237 len = gu_strlcat(toString, ", ", bufferSize);
238 if (len >= bufferSize) {
239 return toString;
240 }
241 len += snprintf(toString + len, bufferSize - len, "%u", self->horizontalRadius);
242 return toString;
243#pragma clang diagnostic pop
244}
245
250{
251 size_t temp_length = strlen(str);
252 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
253 if (length < 1 || length > VISION_DETECTION_BALL_DESC_BUFFER_SIZE) {
254 return self;
255 }
256 char var_str_buffer[VISION_DETECTION_BALL_DESC_BUFFER_SIZE + 1];
257 char* var_str = &var_str_buffer[0];
258 char key_buffer[17];
259 char* key = &key_buffer[0];
260 int bracecount = 0;
261 int startVar = 0;
262 int index = 0;
263 int startKey = 0;
264 int endKey = -1;
265 int varIndex = 0;
266 if (index == 0 && str[0] == '{') {
267 index = 1;
268 }
269 startVar = index;
270 startKey = startVar;
271 do {
272 for (int i = index; i < length; i++) {
273 index = i + 1;
274 if (bracecount == 0 && str[i] == '=') {
275 endKey = i - 1;
276 startVar = index;
277 continue;
278 }
279 if (bracecount == 0 && isspace(str[i])) {
280 startVar = index;
281 if (endKey == -1) {
282 startKey = index;
283 }
284 continue;
285 }
286 if (bracecount == 0 && str[i] == ',') {
287 index = i - 1;
288 break;
289 }
290 if (str[i] == '{') {
291 bracecount++;
292 continue;
293 }
294 if (str[i] == '}') {
295 bracecount--;
296 if (bracecount < 0) {
297 index = i - 1;
298 break;
299 }
300 }
301 if (i == length - 1) {
302 index = i;
303 }
304 }
305 if (endKey >= startKey && endKey - startKey < length) {
306 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
307 key[(endKey - startKey) + 1] = 0;
308 } else {
309 key[0] = 0;
310 }
311 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
312 var_str[(index - startVar) + 1] = 0;
313 bracecount = 0;
314 index += 2;
315 startVar = index;
316 startKey = startVar;
317 endKey = -1;
318 if (strlen(key) > 0) {
319 if (0 == strcmp("sightingType", key)) {
320 varIndex = 0;
321 } else if (0 == strcmp("coordinate", key)) {
322 varIndex = 1;
323 } else if (0 == strcmp("verticalRadius", key)) {
324 varIndex = 2;
325 } else if (0 == strcmp("horizontalRadius", key)) {
326 varIndex = 3;
327 } else {
328 varIndex = -1;
329 }
330 }
331 switch (varIndex) {
332 case -1: { break; }
333 case 0:
334 {
335 if (strcmp("BallDetected", var_str) == 0) {
336#pragma clang diagnostic push
337#pragma clang diagnostic ignored "-Wbad-function-cast"
338 self->sightingType = BallDetected;
339#pragma clang diagnostic pop
340 } else if (strcmp("NoBallDetected", var_str) == 0) {
341#pragma clang diagnostic push
342#pragma clang diagnostic ignored "-Wbad-function-cast"
343 self->sightingType = NoBallDetected;
344#pragma clang diagnostic pop
345 } else {
346#pragma clang diagnostic push
347#pragma clang diagnostic ignored "-Wbad-function-cast"
348 self->sightingType = ((enum BallOptions)atoi(var_str));
349#pragma clang diagnostic pop
350 }
351 break;
352 }
353 case 1:
354 {
355 wb_pixel_coordinate_from_string(&self->coordinate, var_str);
356 break;
357 }
358 case 2:
359 {
360 self->verticalRadius = ((uint16_t)atoi(var_str));
361 break;
362 }
363 case 3:
364 {
365 self->horizontalRadius = ((uint16_t)atoi(var_str));
366 break;
367 }
368 }
369 if (varIndex >= 0) {
370 varIndex++;
371 }
372 } while(index < length);
373 return self;
374}
375
376/*#ifdef WHITEBOARD_SERIALISATION*/
377
382{
383 uint16_t bit_offset = 0;
384 enum BallOptions sightingType_nbo = htonl(self->sightingType);
385 do {
386 int8_t b;
387 for (b = (32 - 1); b >= 0; b--) {
388 do {
389 uint16_t byte = bit_offset / 8;
390 uint16_t bit = 7 - (bit_offset % 8);
391 unsigned long newbit = !!((sightingType_nbo >> b) & 1U);
392 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
393 bit_offset = bit_offset + 1;
394 } while(false);
395 }
396 } while(false);
397
398 uint16_t verticalRadius_nbo = htons(self->verticalRadius);
399 do {
400 int8_t b;
401 for (b = (16 - 1); b >= 0; b--) {
402 do {
403 uint16_t byte = bit_offset / 8;
404 uint16_t bit = 7 - (bit_offset % 8);
405 unsigned long newbit = !!((verticalRadius_nbo >> b) & 1U);
406 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
407 bit_offset = bit_offset + 1;
408 } while(false);
409 }
410 } while(false);
411
412 uint16_t horizontalRadius_nbo = htons(self->horizontalRadius);
413 do {
414 int8_t b;
415 for (b = (16 - 1); b >= 0; b--) {
416 do {
417 uint16_t byte = bit_offset / 8;
418 uint16_t bit = 7 - (bit_offset % 8);
419 unsigned long newbit = !!((horizontalRadius_nbo >> b) & 1U);
420 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
421 bit_offset = bit_offset + 1;
422 } while(false);
423 }
424 } while(false);
425 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
426 (void)self;
427 (void)dst;
428 return bit_offset;
429}
430
435{
436 uint16_t bit_offset = 0;
437 do {
438 int8_t b;
439 for (b = (32 - 1); b >= 0; b--) {
440 do {
441 uint16_t byte = bit_offset / 8;
442 uint16_t bit = 7 - (bit_offset % 8);
443 char dataByte = src[byte];
444 unsigned char bitValue = (dataByte >> bit) & 1U;
445 dst->sightingType ^= (-bitValue ^ dst->sightingType) & (1UL << b);
446 bit_offset = bit_offset + 1;
447 } while(false);
448 }
449 } while(false);
450 dst->sightingType = ntohl(dst->sightingType);
451
452 do {
453 int8_t b;
454 for (b = (16 - 1); b >= 0; b--) {
455 do {
456 uint16_t byte = bit_offset / 8;
457 uint16_t bit = 7 - (bit_offset % 8);
458 char dataByte = src[byte];
459 unsigned char bitValue = (dataByte >> bit) & 1U;
460 dst->verticalRadius ^= (-bitValue ^ dst->verticalRadius) & (1UL << b);
461 bit_offset = bit_offset + 1;
462 } while(false);
463 }
464 } while(false);
466
467 do {
468 int8_t b;
469 for (b = (16 - 1); b >= 0; b--) {
470 do {
471 uint16_t byte = bit_offset / 8;
472 uint16_t bit = 7 - (bit_offset % 8);
473 char dataByte = src[byte];
474 unsigned char bitValue = (dataByte >> bit) & 1U;
475 dst->horizontalRadius ^= (-bitValue ^ dst->horizontalRadius) & (1UL << b);
476 bit_offset = bit_offset + 1;
477 } while(false);
478 }
479 } while(false);
481 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
482 (void)src;
483 (void)dst;
484 return bit_offset;
485}
486
487/*#endif // WHITEBOARD_SERIALISATION*/
488
489gu_pixel_coordinate wb_vision_detection_ball_coordinate_px_coord(const struct wb_vision_detection_ball self, const uint16_t resWidth, const uint16_t resHeight)
490{
491 return wb_px_coord_to_px_coord(self.coordinate, resWidth, resHeight);
492}
WHITEBOARD_POSTER_STRING_CONVERSION.
struct wb_pixel_coordinate coordinate
The pixel coordinate.
uint16_t verticalRadius
The vertical radius in pixels.
enum BallOptions sightingType
Ball detection details.
uint16_t horizontalRadius
The horizontal radius in pixels.
const char * wb_pixel_coordinate_description(const struct wb_pixel_coordinate *self, char *descString, size_t bufferSize)
Convert to a description string.
struct wb_pixel_coordinate * wb_pixel_coordinate_from_string(struct wb_pixel_coordinate *self, const char *str)
Convert from a string.
const char * wb_pixel_coordinate_to_string(const struct wb_pixel_coordinate *self, char *toString, size_t bufferSize)
Convert to a string.
gu_pixel_coordinate wb_px_coord_to_px_coord(const struct wb_pixel_coordinate coord, const uint16_t resWidth, const uint16_t resHeight)
#define PIXEL_COORDINATE_DESC_BUFFER_SIZE
#define PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE
size_t wb_vision_detection_ball_to_network_serialised(const struct wb_vision_detection_ball *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define htons(x)
size_t wb_vision_detection_ball_from_network_serialised(const char *src, struct wb_vision_detection_ball *dst)
Convert from a compressed, serialised, network byte order byte stream.
struct wb_vision_detection_ball * wb_vision_detection_ball_from_string(struct wb_vision_detection_ball *self, const char *str)
Convert from a string.
const char * wb_vision_detection_ball_to_string(const struct wb_vision_detection_ball *self, char *toString, size_t bufferSize)
Convert to a string.
#define ntohl(x)
gu_pixel_coordinate wb_vision_detection_ball_coordinate_px_coord(const struct wb_vision_detection_ball self, const uint16_t resWidth, const uint16_t resHeight)
WHITEBOARD_POSTER_STRING_CONVERSION.
#define ntohs(x)
#define htonl(x)
const char * wb_vision_detection_ball_description(const struct wb_vision_detection_ball *self, char *descString, size_t bufferSize)
Convert to a description string.
@ BallDetected
Ball Found.
@ NoBallDetected
No Ball Found.
#define VISION_DETECTION_BALL_DESC_BUFFER_SIZE