gusimplewhiteboard
wb_input3d.c
Go to the documentation of this file.
1/*
2 * file wb_input3d.c
3 *
4 * This file was generated by classgenerator from Input3D.txt.
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
64#include "wb_input3d.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_input3d_description(const struct wb_input3d* 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, "yaw=%d", self->yaw);
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, "pitch=%d", self->pitch);
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, "roll=%d", self->roll);
150 if (len >= bufferSize) {
151 return descString;
152 }
153 len = gu_strlcat(descString, ", ", bufferSize);
154 if (len >= bufferSize) {
155 return descString;
156 }
157 len += snprintf(descString + len, bufferSize - len, "power=%d", self->power);
158 return descString;
159#pragma clang diagnostic pop
160}
161
165const char* wb_input3d_to_string(const struct wb_input3d* self, char* toString, size_t bufferSize)
166{
167#pragma clang diagnostic push
168#pragma clang diagnostic ignored "-Wunused-variable"
169 size_t len = 0;
170 if (len >= bufferSize) {
171 return toString;
172 }
173 len += snprintf(toString + len, bufferSize - len, "%d", self->yaw);
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, "%d", self->pitch);
182 if (len >= bufferSize) {
183 return toString;
184 }
185 len = gu_strlcat(toString, ", ", bufferSize);
186 if (len >= bufferSize) {
187 return toString;
188 }
189 len += snprintf(toString + len, bufferSize - len, "%d", self->roll);
190 if (len >= bufferSize) {
191 return toString;
192 }
193 len = gu_strlcat(toString, ", ", bufferSize);
194 if (len >= bufferSize) {
195 return toString;
196 }
197 len += snprintf(toString + len, bufferSize - len, "%d", self->power);
198 return toString;
199#pragma clang diagnostic pop
200}
201
205struct wb_input3d* wb_input3d_from_string(struct wb_input3d* self, const char* str)
206{
207 size_t temp_length = strlen(str);
208 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
209 if (length < 1 || length > INPUT3D_DESC_BUFFER_SIZE) {
210 return self;
211 }
212 char var_str_buffer[INPUT3D_DESC_BUFFER_SIZE + 1];
213 char* var_str = &var_str_buffer[0];
214 char key_buffer[6];
215 char* key = &key_buffer[0];
216 int bracecount = 0;
217 int startVar = 0;
218 int index = 0;
219 int startKey = 0;
220 int endKey = -1;
221 int varIndex = 0;
222 if (index == 0 && str[0] == '{') {
223 index = 1;
224 }
225 startVar = index;
226 startKey = startVar;
227 do {
228 for (int i = index; i < length; i++) {
229 index = i + 1;
230 if (bracecount == 0 && str[i] == '=') {
231 endKey = i - 1;
232 startVar = index;
233 continue;
234 }
235 if (bracecount == 0 && isspace(str[i])) {
236 startVar = index;
237 if (endKey == -1) {
238 startKey = index;
239 }
240 continue;
241 }
242 if (bracecount == 0 && str[i] == ',') {
243 index = i - 1;
244 break;
245 }
246 if (str[i] == '{') {
247 bracecount++;
248 continue;
249 }
250 if (str[i] == '}') {
251 bracecount--;
252 if (bracecount < 0) {
253 index = i - 1;
254 break;
255 }
256 }
257 if (i == length - 1) {
258 index = i;
259 }
260 }
261 if (endKey >= startKey && endKey - startKey < length) {
262 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
263 key[(endKey - startKey) + 1] = 0;
264 } else {
265 key[0] = 0;
266 }
267 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
268 var_str[(index - startVar) + 1] = 0;
269 bracecount = 0;
270 index += 2;
271 startVar = index;
272 startKey = startVar;
273 endKey = -1;
274 if (strlen(key) > 0) {
275 if (0 == strcmp("yaw", key)) {
276 varIndex = 0;
277 } else if (0 == strcmp("pitch", key)) {
278 varIndex = 1;
279 } else if (0 == strcmp("roll", key)) {
280 varIndex = 2;
281 } else if (0 == strcmp("power", key)) {
282 varIndex = 3;
283 } else {
284 varIndex = -1;
285 }
286 }
287 switch (varIndex) {
288 case -1: { break; }
289 case 0:
290 {
291 self->yaw = ((int16_t)atoi(var_str));
292 break;
293 }
294 case 1:
295 {
296 self->pitch = ((int16_t)atoi(var_str));
297 break;
298 }
299 case 2:
300 {
301 self->roll = ((int16_t)atoi(var_str));
302 break;
303 }
304 case 3:
305 {
306 self->power = ((int16_t)atoi(var_str));
307 break;
308 }
309 }
310 if (varIndex >= 0) {
311 varIndex++;
312 }
313 } while(index < length);
314 return self;
315}
316
317/*#ifdef WHITEBOARD_SERIALISATION*/
318
322size_t wb_input3d_to_network_serialised(const struct wb_input3d *self, char *dst)
323{
324 uint16_t bit_offset = 0;
325 int16_t yaw_nbo = htons(self->yaw);
326 do {
327 int8_t b;
328 for (b = (16 - 1); b >= 0; b--) {
329 do {
330 uint16_t byte = bit_offset / 8;
331 uint16_t bit = 7 - (bit_offset % 8);
332 unsigned long newbit = !!((yaw_nbo >> b) & 1U);
333 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
334 bit_offset = bit_offset + 1;
335 } while(false);
336 }
337 } while(false);
338
339 int16_t pitch_nbo = htons(self->pitch);
340 do {
341 int8_t b;
342 for (b = (16 - 1); b >= 0; b--) {
343 do {
344 uint16_t byte = bit_offset / 8;
345 uint16_t bit = 7 - (bit_offset % 8);
346 unsigned long newbit = !!((pitch_nbo >> b) & 1U);
347 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
348 bit_offset = bit_offset + 1;
349 } while(false);
350 }
351 } while(false);
352
353 int16_t roll_nbo = htons(self->roll);
354 do {
355 int8_t b;
356 for (b = (16 - 1); b >= 0; b--) {
357 do {
358 uint16_t byte = bit_offset / 8;
359 uint16_t bit = 7 - (bit_offset % 8);
360 unsigned long newbit = !!((roll_nbo >> b) & 1U);
361 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
362 bit_offset = bit_offset + 1;
363 } while(false);
364 }
365 } while(false);
366
367 int16_t power_nbo = htons(self->power);
368 do {
369 int8_t b;
370 for (b = (16 - 1); b >= 0; b--) {
371 do {
372 uint16_t byte = bit_offset / 8;
373 uint16_t bit = 7 - (bit_offset % 8);
374 unsigned long newbit = !!((power_nbo >> b) & 1U);
375 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
376 bit_offset = bit_offset + 1;
377 } while(false);
378 }
379 } while(false);
380 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
381 (void)self;
382 (void)dst;
383 return bit_offset;
384}
385
389size_t wb_input3d_from_network_serialised(const char *src, struct wb_input3d *dst)
390{
391 uint16_t bit_offset = 0;
392 do {
393 int8_t b;
394 for (b = (16 - 1); b >= 0; b--) {
395 do {
396 uint16_t byte = bit_offset / 8;
397 uint16_t bit = 7 - (bit_offset % 8);
398 char dataByte = src[byte];
399 unsigned char bitValue = (dataByte >> bit) & 1U;
400 dst->yaw ^= (-bitValue ^ dst->yaw) & (1UL << b);
401 bit_offset = bit_offset + 1;
402 } while(false);
403 }
404 } while(false);
405 dst->yaw = ntohs(dst->yaw);
406
407 do {
408 int8_t b;
409 for (b = (16 - 1); b >= 0; b--) {
410 do {
411 uint16_t byte = bit_offset / 8;
412 uint16_t bit = 7 - (bit_offset % 8);
413 char dataByte = src[byte];
414 unsigned char bitValue = (dataByte >> bit) & 1U;
415 dst->pitch ^= (-bitValue ^ dst->pitch) & (1UL << b);
416 bit_offset = bit_offset + 1;
417 } while(false);
418 }
419 } while(false);
420 dst->pitch = ntohs(dst->pitch);
421
422 do {
423 int8_t b;
424 for (b = (16 - 1); b >= 0; b--) {
425 do {
426 uint16_t byte = bit_offset / 8;
427 uint16_t bit = 7 - (bit_offset % 8);
428 char dataByte = src[byte];
429 unsigned char bitValue = (dataByte >> bit) & 1U;
430 dst->roll ^= (-bitValue ^ dst->roll) & (1UL << b);
431 bit_offset = bit_offset + 1;
432 } while(false);
433 }
434 } while(false);
435 dst->roll = ntohs(dst->roll);
436
437 do {
438 int8_t b;
439 for (b = (16 - 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->power ^= (-bitValue ^ dst->power) & (1UL << b);
446 bit_offset = bit_offset + 1;
447 } while(false);
448 }
449 } while(false);
450 dst->power = ntohs(dst->power);
451 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
452 (void)src;
453 (void)dst;
454 return bit_offset;
455}
456
457/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
Definition: wb_input3d.h:105
int16_t yaw
yaw
Definition: wb_input3d.h:110
int16_t power
power value, ie.
Definition: wb_input3d.h:125
int16_t pitch
pitch
Definition: wb_input3d.h:115
int16_t roll
roll
Definition: wb_input3d.h:120
const char * wb_input3d_description(const struct wb_input3d *self, char *descString, size_t bufferSize)
Convert to a description string.
Definition: wb_input3d.c:125
size_t wb_input3d_to_network_serialised(const struct wb_input3d *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
Definition: wb_input3d.c:322
const char * wb_input3d_to_string(const struct wb_input3d *self, char *toString, size_t bufferSize)
Convert to a string.
Definition: wb_input3d.c:165
#define htons(x)
Definition: wb_input3d.c:101
struct wb_input3d * wb_input3d_from_string(struct wb_input3d *self, const char *str)
Convert from a string.
Definition: wb_input3d.c:205
#define ntohs(x)
Definition: wb_input3d.c:102
size_t wb_input3d_from_network_serialised(const char *src, struct wb_input3d *dst)
Convert from a compressed, serialised, network byte order byte stream.
Definition: wb_input3d.c:389
#define INPUT3D_DESC_BUFFER_SIZE
Definition: wb_input3d.h:93