summaryrefslogtreecommitdiff
path: root/robot/bin.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-05-17 21:21:00 +0200
committerlonkaars <loek@pipeframe.xyz>2022-05-17 21:21:00 +0200
commit365dcc18fbd98645585cdbe009f537ecdaa90c1a (patch)
tree078c14da20867e177d658ae4cc517b81ab9ad1aa /robot/bin.c
parentf00fca5f6f9751b16d868f52bda908c7b4704457 (diff)
WIP sercomm implementation
- moved some module-specific constants to their respective header files - changed .clang-tidy to ignore global private global constants (starting with `_`) - suppressed some GCC warnings in bin.c and all pololu library warnings - added function signatures for sercomm protocol data generators - added endianness check in setup.c
Diffstat (limited to 'robot/bin.c')
-rw-r--r--robot/bin.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/robot/bin.c b/robot/bin.c
new file mode 100644
index 0000000..4261b50
--- /dev/null
+++ b/robot/bin.c
@@ -0,0 +1,30 @@
+#include "bin.h"
+
+#define W2_ENDIAN_LITTLE (1)
+#define W2_ENDIAN_BIG (0)
+
+#define _SHIFT_0B (8 * 0)
+#define _SHIFT_1B (8 * 1)
+#define _SHIFT_2B (8 * 2)
+#define _SHIFT_3B (8 * 3)
+#define _BYTE_0 ((uint32_t)(0xff << (_SHIFT_0B)))
+#define _BYTE_1 ((uint32_t)(0xff << (_SHIFT_1B)))
+#define _BYTE_2 ((uint32_t)(0xff << (_SHIFT_2B)))
+#define _BYTE_3 ((uint32_t)(0xff << (_SHIFT_3B)))
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wshift-count-overflow"
+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)
+ | ((h32 & _BYTE_2) >> _SHIFT_1B) | ((h32 & _BYTE_3) >> _SHIFT_3B);
+}
+#pragma GCC diagnostic pop
+
+uint16_t w2_bin_hton16(uint16_t h16) {
+ if(g_w2_endianness == W2_ENDIAN_BIG) return h16;
+ return ((h16 & _BYTE_0) << _SHIFT_1B) | ((h16 & _BYTE_1) >> _SHIFT_1B);
+}
+
+uint32_t w2_bin_ntoh32(uint32_t n32) { return w2_bin_hton32(n32); }
+uint16_t w2_bin_ntoh16(uint16_t n16) { return w2_bin_hton16(n16); }