aboutsummaryrefslogtreecommitdiff
path: root/robot/bin.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-05-18 11:18:04 +0200
committerlonkaars <loek@pipeframe.xyz>2022-05-18 11:18:04 +0200
commit3f4b1c7284304d8c4ae2e4dd17359a2b4c1c573c (patch)
tree41410bbbdd7fc33ce63ac9873356846eb525dbe7 /robot/bin.c
parent837acf351ae96e2392efde175a61fd33e0774961 (diff)
[WIP] serial communication (broken w/ segfault)
Diffstat (limited to 'robot/bin.c')
-rw-r--r--robot/bin.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/robot/bin.c b/robot/bin.c
index 4242edc..a2c91a4 100644
--- a/robot/bin.c
+++ b/robot/bin.c
@@ -1,3 +1,5 @@
+#include <stdlib.h>
+
#include "bin.h"
#define W2_ENDIAN_LITTLE (1)
@@ -12,8 +14,40 @@
#define _BYTE_2 ((uint32_t)(0xff << (_SHIFT_2B)))
#define _BYTE_3 ((uint32_t)(0xff << (_SHIFT_3B)))
+const uint8_t W2_STRUCT_T_SIZES[] = {sizeof(uint8_t), sizeof(uint16_t), sizeof(uint32_t)};
+
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshift-count-overflow"
+w2_s_bin *w2_bin_from_uint8_t(uint8_t data) {
+ size_t size = 1;
+ w2_s_bin *ret = malloc(sizeof(w2_s_bin) + sizeof(uint8_t) * size);
+ ret->bytes = size;
+ ret->data[0] = data;
+ return ret;
+}
+
+w2_s_bin *w2_bin_from_uint16_t(uint16_t data) {
+ size_t size = 2;
+ w2_s_bin *ret = malloc(sizeof(w2_s_bin) + sizeof(uint8_t) * size);
+ data = w2_bin_hton16(data);
+ ret->bytes = size;
+ ret->data[0] = (data & _BYTE_1) >> _SHIFT_1B;
+ ret->data[1] = (data & _BYTE_0) >> _SHIFT_0B;
+ return ret;
+}
+
+w2_s_bin *w2_bin_from_uint32_t(uint32_t data) {
+ size_t size = 4;
+ w2_s_bin *ret = malloc(sizeof(w2_s_bin) + sizeof(uint8_t) * size);
+ data = w2_bin_hton32(data);
+ ret->bytes = size;
+ ret->data[0] = (data & _BYTE_3) >> _SHIFT_3B;
+ ret->data[1] = (data & _BYTE_2) >> _SHIFT_2B;
+ ret->data[2] = (data & _BYTE_1) >> _SHIFT_1B;
+ ret->data[3] = (data & _BYTE_0) >> _SHIFT_0B;
+ return ret;
+}
+
uint32_t w2_bin_hton32(uint32_t h32) {
if (g_w2_endianness == W2_ENDIAN_BIG) return h32;
return ((h32 & _BYTE_0) << _SHIFT_3B) | ((h32 & _BYTE_1) << _SHIFT_1B) |