aboutsummaryrefslogtreecommitdiff
path: root/desktop/serial_utils.h
blob: 8c351e498dfc18939dcb560770d11baf11485868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#pragma once

#include "types.h"

#include <SerialStream.h>

template <typename T>
T read (LibSerial::SerialStream& serial)
{
    T x;
    serial.read((char*) &x, sizeof(T));
    return x;
}

template <>
U32 read<U32> (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 <typename T>
void write (LibSerial::SerialStream& serial, const T& val)
{
    serial.write((const char*) &val, sizeof(T));
}