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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use embedded_hal_nb::serial::{self, ErrorType, Read, Write};

use super::{Error, Queue, Uart};

impl ErrorType for Uart {
    type Error = Error;
}

impl serial::Error for Error {
    fn kind(&self) -> serial::ErrorKind {
        serial::ErrorKind::Other
    }
}

/// `Read<u8>` trait implementation for `embedded-hal` v1.0.0.
impl Read<u8> for Uart {
    fn read(&mut self) -> nb::Result<u8, Self::Error> {
        let mut buffer = [0u8; 1];
        if Uart::read(self, &mut buffer)? == 0 {
            Err(nb::Error::WouldBlock)
        } else {
            Ok(buffer[0])
        }
    }
}

/// `Read<u8>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::serial::Read<u8> for Uart {
    type Error = Error;

    fn read(&mut self) -> nb::Result<u8, Self::Error> {
        Read::read(self)
    }
}

/// `Write<u8>` trait implementation for `embedded-hal` v1.0.0.
impl Write<u8> for Uart {
    fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
        if Uart::write(self, &[word])? == 0 {
            Err(nb::Error::WouldBlock)
        } else {
            Ok(())
        }
    }

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        Uart::flush(self, Queue::Output)?;

        Ok(())
    }
}

/// `Write<u8>` trait implementation for `embedded-hal` v0.2.7.
impl embedded_hal_0::serial::Write<u8> for Uart {
    type Error = Error;

    fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
        Write::write(self, word)
    }

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        Write::flush(self)
    }
}