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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::*;
use std::array;

pub struct Uart<'a> {
    sim: &'a mut AvrSimulator,
    id: usize,
}

impl<'a> Uart<'a> {
    pub(crate) fn new(sim: &'a mut AvrSimulator, id: usize) -> Self {
        Self { sim, id }
    }

    /// Transmits an object to AVR.
    ///
    /// See: [`UartSend`].
    ///
    /// See also: [`Self::send_byte()`].
    pub fn send<T>(&mut self, value: T)
    where
        T: UartSend,
    {
        value.send(self);
    }

    /// Transmits a byte to AVR.
    ///
    /// Note that you don't have to worry about AVR's UART's buffer limits - if
    /// transmitting this byte would overflow that buffer, AvrTester will
    /// automatically re-schedule it to be sent at the nearest opportunity.
    ///
    /// See also: [`Self::send()`].
    pub fn send_byte(&mut self, value: u8) {
        self.sim.uart_send(self.id, value);
    }

    /// Retrieves an object from AVR.
    ///
    /// See: [`UartRecv`].
    ///
    /// See also: [`Self::recv_byte()`].
    pub fn recv<T>(&mut self) -> T
    where
        T: UartRecv,
    {
        T::recv(self)
    }

    /// Retrieves a byte from AVR.
    ///
    /// Returns `None` if there are no more bytes in the buffer, in which case
    /// no more bytes will appear at least until the next [`AvrTester::run()`].
    ///
    /// See also: [`Self::recv()`].
    pub fn recv_byte(&mut self) -> Option<u8> {
        self.sim.uart_recv(self.id)
    }
}

/// A type that can be transmitted through [`Uart::send()`].
///
/// You can implement it for your types to make tests more readable.
pub trait UartSend {
    fn send<'a>(&self, uart: &mut Uart<'a>);
}

impl<T> UartSend for &T
where
    T: UartSend + ?Sized,
{
    fn send<'a>(&self, uart: &mut Uart<'a>) {
        T::send(self, uart)
    }
}

impl UartSend for u8 {
    fn send<'a>(&self, uart: &mut Uart<'a>) {
        uart.send_byte(*self);
    }
}

impl UartSend for [u8] {
    fn send<'a>(&self, uart: &mut Uart<'a>) {
        for value in self {
            value.send(uart);
        }
    }
}

impl UartSend for str {
    fn send<'a>(&self, uart: &mut Uart<'a>) {
        self.as_bytes().send(uart);
    }
}

impl<const N: usize> UartSend for [u8; N] {
    fn send<'a>(&self, uart: &mut Uart<'a>) {
        (self as &[u8]).send(uart);
    }
}

/// A type that can be retrieved through [`Uart::recv()`].
///
/// You can implement it for your types to make tests more readable.
pub trait UartRecv {
    fn recv(uart: &mut Uart<'_>) -> Self;
}

impl UartRecv for u8 {
    fn recv(uart: &mut Uart<'_>) -> Self {
        uart.recv_byte()
            .expect("UART's buffer is empty (i.e. AVR did not send more bytes)")
    }
}

impl<const N: usize> UartRecv for [u8; N] {
    fn recv(uart: &mut Uart<'_>) -> Self {
        array::from_fn(|_| uart.recv())
    }
}

impl UartRecv for Vec<u8> {
    fn recv(uart: &mut Uart<'_>) -> Self {
        let mut bytes = Vec::new();

        while let Some(byte) = uart.recv_byte() {
            bytes.push(byte);
        }

        bytes
    }
}

impl UartRecv for String {
    fn recv(uart: &mut Uart<'_>) -> Self {
        String::from_utf8_lossy(&Vec::<u8>::recv(uart)).to_string()
    }
}