From e62be3b12d3c94d709a77d89f52c31f7c4ac475d Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Dec 2021 22:22:47 -0800 Subject: Initial commit. --- desktop/serial_utils.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 desktop/serial_utils.h (limited to 'desktop/serial_utils.h') diff --git a/desktop/serial_utils.h b/desktop/serial_utils.h new file mode 100755 index 0000000..8c351e4 --- /dev/null +++ b/desktop/serial_utils.h @@ -0,0 +1,33 @@ +#pragma once + +#include "types.h" + +#include + +template +T read (LibSerial::SerialStream& serial) +{ + T x; + serial.read((char*) &x, sizeof(T)); + return x; +} + +template <> +U32 read (LibSerial::SerialStream& serial) +{ + U32 x = 0; + for (int i = 0; i < 4; ++i) + { + U8 b; + serial.read((char*)&b, 1); + x = x >> 8; + x |= (b << 24); + } + return x; +} + +template +void write (LibSerial::SerialStream& serial, const T& val) +{ + serial.write((const char*) &val, sizeof(T)); +} -- cgit v1.2.3