gusimplewhiteboard
wb_vision_detection_horizons.c
Go to the documentation of this file.
1/*
2 * file wb_vision_detection_horizons.c
3 *
4 * This file was generated by classgenerator from vision_detection_horizons.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_horizons_description(const struct wb_vision_detection_horizons* 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#ifdef __APPLE__
134 len += snprintf(descString + len, bufferSize - len, "frameNumber=%llu", self->frameNumber);
135#else
136 len += snprintf(descString + len, bufferSize - len, "frameNumber=%lu", self->frameNumber);
137#endif
138 if (len >= bufferSize) {
139 return descString;
140 }
141 len = gu_strlcat(descString, ", ", bufferSize);
142 if (len >= bufferSize) {
143 return descString;
144 }
145 len = gu_strlcat(descString, "horizons={", bufferSize);
146 for (int horizons_index = 0; horizons_index < VISION_DETECTION_HORIZONS_HORIZONS_ARRAY_SIZE; horizons_index++) {
147 if (len >= bufferSize) {
148 return descString;
149 }
150 if (horizons_index > 0) {
151 len = gu_strlcat(descString, ", ", bufferSize);
152 }
153 len = gu_strlcat(descString, "{", bufferSize);
154 if (len >= bufferSize) {
155 return descString;
156 }
157 char horizons_1_buffer[VISION_DETECTION_HORIZON_DESC_BUFFER_SIZE];
158 char* horizons_1_p = horizons_1_buffer;
159 const char* horizons_1_description = wb_vision_detection_horizon_description(&self->horizons[horizons_index], horizons_1_p, VISION_DETECTION_HORIZON_DESC_BUFFER_SIZE);
160 len = gu_strlcat(descString, horizons_1_p, bufferSize);
161 if (len >= bufferSize) {
162 return descString;
163 }
164 len = gu_strlcat(descString, "}", bufferSize);
165 }
166 if (len >= bufferSize) {
167 return descString;
168 }
169 len = gu_strlcat(descString, "}", bufferSize);
170 if (len >= bufferSize) {
171 return descString;
172 }
173 len = gu_strlcat(descString, ", ", bufferSize);
174 if (len >= bufferSize) {
175 return descString;
176 }
177 len += snprintf(descString + len, bufferSize - len, "res_width=%u", self->res_width);
178 if (len >= bufferSize) {
179 return descString;
180 }
181 len = gu_strlcat(descString, ", ", bufferSize);
182 if (len >= bufferSize) {
183 return descString;
184 }
185 len += snprintf(descString + len, bufferSize - len, "res_height=%u", self->res_height);
186 return descString;
187#pragma clang diagnostic pop
188}
189
193const char* wb_vision_detection_horizons_to_string(const struct wb_vision_detection_horizons* self, char* toString, size_t bufferSize)
194{
195#pragma clang diagnostic push
196#pragma clang diagnostic ignored "-Wunused-variable"
197 size_t len = 0;
198 if (len >= bufferSize) {
199 return toString;
200 }
201#ifdef __APPLE__
202 len += snprintf(toString + len, bufferSize - len, "%llu", self->frameNumber);
203#else
204 len += snprintf(toString + len, bufferSize - len, "%lu", self->frameNumber);
205#endif
206 if (len >= bufferSize) {
207 return toString;
208 }
209 len = gu_strlcat(toString, ", ", bufferSize);
210 if (len >= bufferSize) {
211 return toString;
212 }
213 len = gu_strlcat(toString, "{", bufferSize);
214 for (int horizons_index = 0; horizons_index < VISION_DETECTION_HORIZONS_HORIZONS_ARRAY_SIZE; horizons_index++) {
215 if (len >= bufferSize) {
216 return toString;
217 }
218 if (horizons_index > 0) {
219 len = gu_strlcat(toString, ", ", bufferSize);
220 }
221 len = gu_strlcat(toString, "{", bufferSize);
222 if (len >= bufferSize) {
223 return toString;
224 }
226 char* horizons_1_p = horizons_1_buffer;
227 const char* horizons_1_to_string = wb_vision_detection_horizon_to_string(&self->horizons[horizons_index], horizons_1_p, VISION_DETECTION_HORIZON_TO_STRING_BUFFER_SIZE);
228 len = gu_strlcat(toString, horizons_1_p, bufferSize);
229 if (len >= bufferSize) {
230 return toString;
231 }
232 len = gu_strlcat(toString, "}", bufferSize);
233 }
234 if (len >= bufferSize) {
235 return toString;
236 }
237 len = gu_strlcat(toString, "}", bufferSize);
238 if (len >= bufferSize) {
239 return toString;
240 }
241 len = gu_strlcat(toString, ", ", bufferSize);
242 if (len >= bufferSize) {
243 return toString;
244 }
245 len += snprintf(toString + len, bufferSize - len, "%u", self->res_width);
246 if (len >= bufferSize) {
247 return toString;
248 }
249 len = gu_strlcat(toString, ", ", bufferSize);
250 if (len >= bufferSize) {
251 return toString;
252 }
253 len += snprintf(toString + len, bufferSize - len, "%u", self->res_height);
254 return toString;
255#pragma clang diagnostic pop
256}
257
262{
263 size_t temp_length = strlen(str);
264 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
265 if (length < 1 || length > VISION_DETECTION_HORIZONS_DESC_BUFFER_SIZE) {
266 return self;
267 }
268 char var_str_buffer[VISION_DETECTION_HORIZONS_DESC_BUFFER_SIZE + 1];
269 char* var_str = &var_str_buffer[0];
270 char key_buffer[12];
271 char* key = &key_buffer[0];
272 int bracecount = 0;
273 int lastBrace = -1;
274 int startVar = 0;
275 int index = 0;
276 int startKey = 0;
277 int endKey = -1;
278 int varIndex = 0;
279 if (index == 0 && str[0] == '{') {
280 index = 1;
281 }
282 startVar = index;
283 startKey = startVar;
284 do {
285 for (int i = index; i < length; i++) {
286 index = i + 1;
287 if (bracecount == 0 && str[i] == '=') {
288 endKey = i - 1;
289 startVar = index;
290 continue;
291 }
292 if (bracecount == 0 && isspace(str[i])) {
293 startVar = index;
294 if (endKey == -1) {
295 startKey = index;
296 }
297 continue;
298 }
299 if (bracecount == 0 && str[i] == ',') {
300 index = i - 1;
301 break;
302 }
303 if (str[i] == '{') {
304 bracecount++;
305 if (bracecount == 1) {
306 lastBrace = i;
307 }
308 continue;
309 }
310 if (str[i] == '}') {
311 bracecount--;
312 if (bracecount < 0) {
313 index = i - 1;
314 break;
315 }
316 }
317 if (i == length - 1) {
318 index = i;
319 }
320 }
321 if (endKey >= startKey && endKey - startKey < length) {
322 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
323 key[(endKey - startKey) + 1] = 0;
324 } else {
325 key[0] = 0;
326 }
327 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
328 var_str[(index - startVar) + 1] = 0;
329 bracecount = 0;
330 index += 2;
331 startVar = index;
332 startKey = startVar;
333 endKey = -1;
334 if (strlen(key) > 0) {
335 if (0 == strcmp("frameNumber", key)) {
336 varIndex = 0;
337 } else if (0 == strcmp("horizons", key)) {
338 varIndex = 1;
339 } else if (0 == strcmp("res_width", key)) {
340 varIndex = 2;
341 } else if (0 == strcmp("res_height", key)) {
342 varIndex = 3;
343 } else {
344 varIndex = -1;
345 }
346 }
347 switch (varIndex) {
348 case -1: { break; }
349 case 0:
350 {
351#ifdef __APPLE__
352 self->frameNumber = ((uint64_t)atoll(var_str));
353#else
354 self->frameNumber = ((uint64_t)atol(var_str));
355#endif
356 break;
357 }
358 case 1:
359 {
360 int restartIndex = index;
361 index = lastBrace + 1;
362 startVar = index;
363 startKey = startVar;
364 endKey = -1;
365 bracecount = 0;
366 for (int horizons_0_index = 0; horizons_0_index < VISION_DETECTION_HORIZONS_HORIZONS_ARRAY_SIZE; horizons_0_index++) {
367 for (int i = index; i < length; i++) {
368 index = i + 1;
369 if (bracecount == 0 && str[i] == '=') {
370 endKey = i - 1;
371 startVar = index;
372 continue;
373 }
374 if (bracecount == 0 && isspace(str[i])) {
375 startVar = index;
376 if (endKey == -1) {
377 startKey = index;
378 }
379 continue;
380 }
381 if (bracecount == 0 && str[i] == ',') {
382 index = i - 1;
383 break;
384 }
385 if (str[i] == '{') {
386 bracecount++;
387 continue;
388 }
389 if (str[i] == '}') {
390 bracecount--;
391 if (bracecount < 0) {
392 index = i - 1;
393 break;
394 }
395 }
396 if (i == length - 1) {
397 index = i;
398 }
399 }
400 if (endKey >= startKey && endKey - startKey < length) {
401 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
402 key[(endKey - startKey) + 1] = 0;
403 } else {
404 key[0] = 0;
405 }
406 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
407 var_str[(index - startVar) + 1] = 0;
408 bracecount = 0;
409 index += 2;
410 startVar = index;
411 startKey = startVar;
412 endKey = -1;
413 struct wb_vision_detection_horizon horizons_0 = {};
414 wb_vision_detection_horizon_from_string(&horizons_0, var_str);
415 self->horizons[horizons_0_index] = horizons_0;
416 }
417 index = restartIndex;
418 break;
419 }
420 case 2:
421 {
422 self->res_width = ((uint16_t)atoi(var_str));
423 break;
424 }
425 case 3:
426 {
427 self->res_height = ((uint16_t)atoi(var_str));
428 break;
429 }
430 }
431 if (varIndex >= 0) {
432 varIndex++;
433 }
434 } while(index < length);
435 return self;
436}
437
438/*#ifdef WHITEBOARD_SERIALISATION*/
439
444{
445 uint16_t bit_offset = 0;
446 uint64_t frameNumber_nbo = htonll(self->frameNumber);
447 do {
448 int8_t b;
449 for (b = (64 - 1); b >= 0; b--) {
450 do {
451 uint16_t byte = bit_offset / 8;
452 uint16_t bit = 7 - (bit_offset % 8);
453 unsigned long newbit = !!((frameNumber_nbo >> b) & 1U);
454 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
455 bit_offset = bit_offset + 1;
456 } while(false);
457 }
458 } while(false);
459
460 //Class generator does not support array network compression.
461 //Copying into the buffer, uncompressed
462 do { //limit declaration scope
463 uint32_t len = 2;
464 uint32_t bytes = len * sizeof(struct wb_vision_detection_horizon);
465 const char *buf = (const char *)&self->horizons[0];
466 uint32_t c;
467 int8_t b;
468 for (c = 0; c < bytes; c++) {
469 for (b = 7; b >= 0; b--) {
470 do {
471 uint16_t byte = bit_offset / 8;
472 uint16_t bit = 7 - (bit_offset % 8);
473 unsigned long newbit = !!((buf[c] >> b) & 1U);
474 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
475 bit_offset = bit_offset + 1;
476 } while(false);
477 }
478 }
479 } while(false);
480
481 uint16_t res_width_nbo = htons(self->res_width);
482 do {
483 int8_t b;
484 for (b = (16 - 1); b >= 0; b--) {
485 do {
486 uint16_t byte = bit_offset / 8;
487 uint16_t bit = 7 - (bit_offset % 8);
488 unsigned long newbit = !!((res_width_nbo >> b) & 1U);
489 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
490 bit_offset = bit_offset + 1;
491 } while(false);
492 }
493 } while(false);
494
495 uint16_t res_height_nbo = htons(self->res_height);
496 do {
497 int8_t b;
498 for (b = (16 - 1); b >= 0; b--) {
499 do {
500 uint16_t byte = bit_offset / 8;
501 uint16_t bit = 7 - (bit_offset % 8);
502 unsigned long newbit = !!((res_height_nbo >> b) & 1U);
503 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
504 bit_offset = bit_offset + 1;
505 } while(false);
506 }
507 } while(false);
508 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
509 (void)self;
510 (void)dst;
511 return bit_offset;
512}
513
518{
519 uint16_t bit_offset = 0;
520 do {
521 int8_t b;
522 for (b = (64 - 1); b >= 0; b--) {
523 do {
524 uint16_t byte = bit_offset / 8;
525 uint16_t bit = 7 - (bit_offset % 8);
526 char dataByte = src[byte];
527 unsigned char bitValue = (dataByte >> bit) & 1U;
528 dst->frameNumber ^= (-bitValue ^ dst->frameNumber) & (1UL << b);
529 bit_offset = bit_offset + 1;
530 } while(false);
531 }
532 } while(false);
533 dst->frameNumber = ntohll(dst->frameNumber);
534
535 //Class generator does not support array network compression.
536 //Copying into the buffer, uncompressed
537 do { //limit declaration scope
538 uint32_t len = 2;
539 uint32_t bytes = len * sizeof(struct wb_vision_detection_horizon);
540 char *buf = (char *)malloc(bytes);
541 uint32_t c;
542 int8_t b;
543 for (c = 0; c < bytes; c++) {
544 for (b = 7; b >= 0; b--) {
545 do {
546 uint16_t byte = bit_offset / 8;
547 uint16_t bit = 7 - (bit_offset % 8);
548 char dataByte = src[byte];
549 unsigned char bitValue = (dataByte >> bit) & 1U;
550 buf[c] ^= (-bitValue ^ buf[c]) & (1UL << b);
551 bit_offset = bit_offset + 1;
552 } while(false);
553 }
554 }
555 memcpy(&dst->horizons[0], &buf[0], bytes);
556 free(buf);
557 } while(false);
558
559 do {
560 int8_t b;
561 for (b = (16 - 1); b >= 0; b--) {
562 do {
563 uint16_t byte = bit_offset / 8;
564 uint16_t bit = 7 - (bit_offset % 8);
565 char dataByte = src[byte];
566 unsigned char bitValue = (dataByte >> bit) & 1U;
567 dst->res_width ^= (-bitValue ^ dst->res_width) & (1UL << b);
568 bit_offset = bit_offset + 1;
569 } while(false);
570 }
571 } while(false);
572 dst->res_width = ntohs(dst->res_width);
573
574 do {
575 int8_t b;
576 for (b = (16 - 1); b >= 0; b--) {
577 do {
578 uint16_t byte = bit_offset / 8;
579 uint16_t bit = 7 - (bit_offset % 8);
580 char dataByte = src[byte];
581 unsigned char bitValue = (dataByte >> bit) & 1U;
582 dst->res_height ^= (-bitValue ^ dst->res_height) & (1UL << b);
583 bit_offset = bit_offset + 1;
584 } while(false);
585 }
586 } while(false);
587 dst->res_height = ntohs(dst->res_height);
588 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
589 (void)src;
590 (void)dst;
591 return bit_offset;
592}
593
594/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
Array of fieldHorizons, one instance reported by each camera.
uint16_t res_width
The resolution width.
uint16_t res_height
The resolution height.
struct wb_vision_detection_horizon horizons[2]
horizon objects
const char * wb_vision_detection_horizon_to_string(const struct wb_vision_detection_horizon *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_vision_detection_horizon * wb_vision_detection_horizon_from_string(struct wb_vision_detection_horizon *self, const char *str)
Convert from a string.
const char * wb_vision_detection_horizon_description(const struct wb_vision_detection_horizon *self, char *descString, size_t bufferSize)
Convert to a description string.
#define VISION_DETECTION_HORIZON_TO_STRING_BUFFER_SIZE
#define VISION_DETECTION_HORIZON_DESC_BUFFER_SIZE
size_t wb_vision_detection_horizons_from_network_serialised(const char *src, struct wb_vision_detection_horizons *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_vision_detection_horizons_description(const struct wb_vision_detection_horizons *self, char *descString, size_t bufferSize)
Convert to a description string.
const char * wb_vision_detection_horizons_to_string(const struct wb_vision_detection_horizons *self, char *toString, size_t bufferSize)
Convert to a string.
#define ntohll(x)
#define htons(x)
#define htonll(x)
struct wb_vision_detection_horizons * wb_vision_detection_horizons_from_string(struct wb_vision_detection_horizons *self, const char *str)
Convert from a string.
#define ntohs(x)
size_t wb_vision_detection_horizons_to_network_serialised(const struct wb_vision_detection_horizons *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define VISION_DETECTION_HORIZONS_HORIZONS_ARRAY_SIZE
WHITEBOARD_POSTER_STRING_CONVERSION.
#define VISION_DETECTION_HORIZONS_DESC_BUFFER_SIZE