gusimplewhiteboard
wb_vision_field_feature.c
Go to the documentation of this file.
1/*
2 * file wb_vision_field_feature.c
3 *
4 * This file was generated by classgenerator from vision_field_feature.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Eugene Gilmore. 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 Eugene Gilmore.
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_field_feature_description(const struct wb_vision_field_feature* 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, "coordinate={", bufferSize);
134 if (len >= bufferSize) {
135 return descString;
136 }
137 char coordinate_buffer[PIXEL_COORDINATE_DESC_BUFFER_SIZE];
138 char* coordinate_p = coordinate_buffer;
139 const char* coordinate_description = wb_pixel_coordinate_description(&self->coordinate, coordinate_p, PIXEL_COORDINATE_DESC_BUFFER_SIZE);
140 len = gu_strlcat(descString, coordinate_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, "camera=%u", self->camera);
153 return descString;
154#pragma clang diagnostic pop
155}
156
160const char* wb_vision_field_feature_to_string(const struct wb_vision_field_feature* self, char* toString, size_t bufferSize)
161{
162#pragma clang diagnostic push
163#pragma clang diagnostic ignored "-Wunused-variable"
164 size_t len = 0;
165 if (len >= bufferSize) {
166 return toString;
167 }
168 len = gu_strlcat(toString, "{", bufferSize);
169 if (len >= bufferSize) {
170 return toString;
171 }
172 char coordinate_buffer[PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE];
173 char* coordinate_p = coordinate_buffer;
174 const char* coordinate_to_string = wb_pixel_coordinate_to_string(&self->coordinate, coordinate_p, PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE);
175 len = gu_strlcat(toString, coordinate_p, bufferSize);
176 if (len >= bufferSize) {
177 return toString;
178 }
179 len = gu_strlcat(toString, "}", bufferSize);
180 if (len >= bufferSize) {
181 return toString;
182 }
183 len = gu_strlcat(toString, ", ", bufferSize);
184 if (len >= bufferSize) {
185 return toString;
186 }
187 len += snprintf(toString + len, bufferSize - len, "%u", self->camera);
188 return toString;
189#pragma clang diagnostic pop
190}
191
196{
197 size_t temp_length = strlen(str);
198 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
199 if (length < 1 || length > VISION_FIELD_FEATURE_DESC_BUFFER_SIZE) {
200 return self;
201 }
202 char var_str_buffer[VISION_FIELD_FEATURE_DESC_BUFFER_SIZE + 1];
203 char* var_str = &var_str_buffer[0];
204 char key_buffer[11];
205 char* key = &key_buffer[0];
206 int bracecount = 0;
207 int startVar = 0;
208 int index = 0;
209 int startKey = 0;
210 int endKey = -1;
211 int varIndex = 0;
212 if (index == 0 && str[0] == '{') {
213 index = 1;
214 }
215 startVar = index;
216 startKey = startVar;
217 do {
218 for (int i = index; i < length; i++) {
219 index = i + 1;
220 if (bracecount == 0 && str[i] == '=') {
221 endKey = i - 1;
222 startVar = index;
223 continue;
224 }
225 if (bracecount == 0 && isspace(str[i])) {
226 startVar = index;
227 if (endKey == -1) {
228 startKey = index;
229 }
230 continue;
231 }
232 if (bracecount == 0 && str[i] == ',') {
233 index = i - 1;
234 break;
235 }
236 if (str[i] == '{') {
237 bracecount++;
238 continue;
239 }
240 if (str[i] == '}') {
241 bracecount--;
242 if (bracecount < 0) {
243 index = i - 1;
244 break;
245 }
246 }
247 if (i == length - 1) {
248 index = i;
249 }
250 }
251 if (endKey >= startKey && endKey - startKey < length) {
252 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
253 key[(endKey - startKey) + 1] = 0;
254 } else {
255 key[0] = 0;
256 }
257 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
258 var_str[(index - startVar) + 1] = 0;
259 bracecount = 0;
260 index += 2;
261 startVar = index;
262 startKey = startVar;
263 endKey = -1;
264 if (strlen(key) > 0) {
265 if (0 == strcmp("coordinate", key)) {
266 varIndex = 0;
267 } else if (0 == strcmp("camera", key)) {
268 varIndex = 1;
269 } else {
270 varIndex = -1;
271 }
272 }
273 switch (varIndex) {
274 case -1: { break; }
275 case 0:
276 {
277 wb_pixel_coordinate_from_string(&self->coordinate, var_str);
278 break;
279 }
280 case 1:
281 {
282 self->camera = ((uint8_t)atoi(var_str));
283 break;
284 }
285 }
286 if (varIndex >= 0) {
287 varIndex++;
288 }
289 } while(index < length);
290 return self;
291}
292
293/*#ifdef WHITEBOARD_SERIALISATION*/
294
299{
300 uint16_t bit_offset = 0;
301 uint8_t camera_nbo = (self->camera);
302 do {
303 int8_t b;
304 for (b = (8 - 1); b >= 0; b--) {
305 do {
306 uint16_t byte = bit_offset / 8;
307 uint16_t bit = 7 - (bit_offset % 8);
308 unsigned long newbit = !!((camera_nbo >> b) & 1U);
309 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
310 bit_offset = bit_offset + 1;
311 } while(false);
312 }
313 } while(false);
314 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
315 (void)self;
316 (void)dst;
317 return bit_offset;
318}
319
324{
325 uint16_t bit_offset = 0;
326 do {
327 int8_t b;
328 for (b = (8 - 1); b >= 0; b--) {
329 do {
330 uint16_t byte = bit_offset / 8;
331 uint16_t bit = 7 - (bit_offset % 8);
332 char dataByte = src[byte];
333 unsigned char bitValue = (dataByte >> bit) & 1U;
334 dst->camera ^= (-bitValue ^ dst->camera) & (1UL << b);
335 bit_offset = bit_offset + 1;
336 } while(false);
337 }
338 } while(false);
339 dst->camera = (dst->camera);
340 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
341 (void)src;
342 (void)dst;
343 return bit_offset;
344}
345
346/*#endif // WHITEBOARD_SERIALISATION*/
347
348gu_pixel_coordinate wb_vision_field_feature_coordinate_px_coord(const struct wb_vision_field_feature self, const uint16_t resWidth, const uint16_t resHeight)
349{
350 return wb_px_coord_to_px_coord(self.coordinate, resWidth, resHeight);
351}
WHITEBOARD_POSTER_STRING_CONVERSION.
struct wb_pixel_coordinate coordinate
The pixel coordinate.
uint8_t camera
camera it was seen on
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
const char * wb_vision_field_feature_to_string(const struct wb_vision_field_feature *self, char *toString, size_t bufferSize)
Convert to a string.
gu_pixel_coordinate wb_vision_field_feature_coordinate_px_coord(const struct wb_vision_field_feature self, const uint16_t resWidth, const uint16_t resHeight)
WHITEBOARD_POSTER_STRING_CONVERSION.
const char * wb_vision_field_feature_description(const struct wb_vision_field_feature *self, char *descString, size_t bufferSize)
Convert to a description string.
size_t wb_vision_field_feature_to_network_serialised(const struct wb_vision_field_feature *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
struct wb_vision_field_feature * wb_vision_field_feature_from_string(struct wb_vision_field_feature *self, const char *str)
Convert from a string.
size_t wb_vision_field_feature_from_network_serialised(const char *src, struct wb_vision_field_feature *dst)
Convert from a compressed, serialised, network byte order byte stream.
#define VISION_FIELD_FEATURE_DESC_BUFFER_SIZE