avr_tester/uart.rs
1use crate::*;
2
3/// Provides access to the UART.
4///
5/// See: [`Uart::read()`] and [`Uart::write()`].
6pub struct Uart<'a> {
7 sim: &'a mut AvrSimulator,
8 id: char,
9}
10
11impl<'a> Uart<'a> {
12 pub(crate) fn new(sim: &'a mut AvrSimulator, id: char) -> Self {
13 Self { sim, id }
14 }
15
16 /// Retrieves a value from AVR.
17 ///
18 /// See: [`Readable`].
19 /// See also: [`Self::try_read_byte()`].
20 ///
21 /// # Examples
22 ///
23 /// ```no_run
24 /// # use avr_tester::*;
25 /// #
26 /// let mut avr = AvrTester::test();
27 ///
28 /// // Retrieves a single byte:
29 /// // (when the input buffer is empty, panics.)
30 /// assert_eq!(72, avr.uart0().read::<u8>());
31 ///
32 /// // Retrieves the entire buffer:
33 /// // (when it's empty, returns an empty vector.)
34 /// assert_eq!(vec![72, 101, 108, 108, 111], avr.uart0().read::<Vec<u8>>());
35 ///
36 /// // Retrieves `n` bytes from the buffer:
37 /// // (when there's not enough bytes, panics.)
38 /// assert_eq!([72, 101, 108, 108, 111], avr.uart0().read::<[u8; 5]>());
39 ///
40 /// // Retrieves the entire input buffer and converts it into string:
41 /// // (when it's empty, returns an empty string.)
42 /// assert_eq!("Hello", avr.uart0().read::<String>());
43 /// ```
44 pub fn read<T>(&mut self) -> T
45 where
46 T: Readable,
47 {
48 T::read(self)
49 }
50
51 /// Transmits a value to AVR.
52 ///
53 /// See: [`Writable`].
54 ///
55 /// # Examples
56 ///
57 /// ```no_run
58 /// # use avr_tester::*;
59 /// #
60 /// let mut avr = AvrTester::test();
61 ///
62 /// // Transmits a single byte:
63 /// avr.uart0().write(123);
64 ///
65 /// // Transmits many bytes:
66 /// avr.uart0().write([10, 20, 30]);
67 ///
68 /// // Transmits a string:
69 /// avr.uart0().write("Hello!");
70 ///
71 /// // Strings are transmitted as a series of their bytes, so the above is
72 /// // equivalent to:
73 /// avr.uart0().write([72, 101, 108, 108, 111, 33]);
74 /// // H e l l o !
75 /// ```
76 pub fn write<T>(&mut self, value: T)
77 where
78 T: Writable,
79 {
80 value.write(self);
81 }
82}
83
84impl Reader for Uart<'_> {
85 fn read_byte(&mut self) -> u8 {
86 self.try_read_byte().expect(
87 "UART's buffer is empty - got no more bytes to read; if you're \
88 receiving a large buffer, try running the simulator for a bit \
89 longer so that the simulated AVR has more time to respond",
90 )
91 }
92
93 fn try_read_byte(&mut self) -> Option<u8> {
94 self.sim.read_uart(self.id)
95 }
96}
97
98impl Writer for Uart<'_> {
99 fn write_byte(&mut self, value: u8) {
100 self.sim.write_uart(self.id, value);
101 }
102}
103
104/// Asynchronous equivalent of [`Uart`].
105///
106/// See [`avr_rt()`] for more details.
107pub struct UartAsync {
108 id: char,
109}
110
111impl UartAsync {
112 pub(crate) fn new(id: char) -> Self {
113 Self { id }
114 }
115
116 /// Asynchronous equivalent of [`Uart::read()`].
117 pub fn read<T>(&self) -> T
118 where
119 T: Readable,
120 {
121 self.with(|uart| uart.read())
122 }
123
124 /// Asynchronous equivalent of [`Uart::write()`].
125 pub fn write<T>(&mut self, value: T)
126 where
127 T: Writable,
128 {
129 self.with(|uart| uart.write(value))
130 }
131
132 fn with<T>(&self, f: impl FnOnce(&mut Uart) -> T) -> T {
133 ComponentRuntime::with(|rt| {
134 let mut uart = Uart::new(rt.sim(), self.id);
135
136 f(&mut uart)
137 })
138 }
139}