avr_tester/
pins.rs

1mod analog_pin;
2mod digital_pin;
3
4use crate::*;
5use avr_simulator::ffi;
6use std::marker::PhantomData;
7
8pub use self::analog_pin::*;
9pub use self::digital_pin::*;
10
11/// Provides access to analog and digital pins.
12pub struct Pins<'a> {
13    avr: &'a mut AvrTester,
14}
15
16impl<'a> Pins<'a> {
17    pub(crate) fn new(avr: &'a mut AvrTester) -> Self {
18        Self { avr }
19    }
20}
21
22/// Asynchronous equivalent of [`Pins`].
23///
24/// See [`avr_rt()`] for more details.
25pub struct PinsAsync {
26    _pd: PhantomData<()>,
27}
28
29impl PinsAsync {
30    pub(crate) fn new() -> Self {
31        Self {
32            _pd: Default::default(),
33        }
34    }
35}
36
37macro_rules! analog_pins {
38    ( $( $fn:ident($irq:expr) ),* $(,)? ) => {
39        impl<'a> Pins<'a> {
40            $(
41                pub fn $fn(&mut self) -> AnalogPin<'_> {
42                    AnalogPin::new(self.avr, $irq)
43                }
44            )*
45        }
46
47        impl PinsAsync {
48            $(
49                pub fn $fn(&self) -> AnalogPinAsync {
50                    AnalogPinAsync::new($irq)
51                }
52            )*
53        }
54    }
55}
56
57analog_pins! {
58    adc0(ffi::ADC_IRQ_ADC0),
59    adc1(ffi::ADC_IRQ_ADC1),
60    adc2(ffi::ADC_IRQ_ADC2),
61    adc3(ffi::ADC_IRQ_ADC3),
62    adc4(ffi::ADC_IRQ_ADC4),
63    adc5(ffi::ADC_IRQ_ADC5),
64    adc6(ffi::ADC_IRQ_ADC6),
65    adc7(ffi::ADC_IRQ_ADC7),
66    adc8(ffi::ADC_IRQ_ADC8),
67    adc9(ffi::ADC_IRQ_ADC9),
68    adc10(ffi::ADC_IRQ_ADC10),
69    adc11(ffi::ADC_IRQ_ADC11),
70    adc12(ffi::ADC_IRQ_ADC12),
71    adc13(ffi::ADC_IRQ_ADC13),
72    adc14(ffi::ADC_IRQ_ADC14),
73    adc15(ffi::ADC_IRQ_ADC15),
74    temp(ffi::ADC_IRQ_TEMP),
75}
76
77macro_rules! digital_pins {
78    ( $( $fn:ident($port:expr, $pin:expr) ),* $(,)? ) => {
79        impl<'a> Pins<'a> {
80            $(
81                pub fn $fn(&mut self) -> DigitalPin<'_> {
82                    DigitalPin::new(self.avr, $port, $pin)
83                }
84            )*
85        }
86
87        impl PinsAsync {
88            $(
89                pub fn $fn(&self) -> DigitalPinAsync {
90                    DigitalPinAsync::new($port, $pin)
91                }
92            )*
93        }
94    }
95}
96
97digital_pins! {
98    pa0('A', 0),
99    pa1('A', 1),
100    pa2('A', 2),
101    pa3('A', 3),
102    pa4('A', 4),
103    pa5('A', 5),
104    pa6('A', 6),
105    pa7('A', 7),
106    //
107    pb0('B', 0),
108    pb1('B', 1),
109    pb2('B', 2),
110    pb3('B', 3),
111    pb4('B', 4),
112    pb5('B', 5),
113    pb6('B', 6),
114    pb7('B', 7),
115    //
116    pc0('C', 0),
117    pc1('C', 1),
118    pc2('C', 2),
119    pc3('C', 3),
120    pc4('C', 4),
121    pc5('C', 5),
122    pc6('C', 6),
123    pc7('C', 7),
124    //
125    pd0('D', 0),
126    pd1('D', 1),
127    pd2('D', 2),
128    pd3('D', 3),
129    pd4('D', 4),
130    pd5('D', 5),
131    pd6('D', 6),
132    pd7('D', 7),
133    //
134    pe0('E', 0),
135    pe1('E', 1),
136    pe2('E', 2),
137    pe3('E', 3),
138    pe4('E', 4),
139    pe5('E', 5),
140    pe6('E', 6),
141    pe7('E', 7),
142    //
143    pf0('F', 0),
144    pf1('F', 1),
145    pf2('F', 2),
146    pf3('F', 3),
147    pf4('F', 4),
148    pf5('F', 5),
149    pf6('F', 6),
150    pf7('F', 7),
151    //
152    pg0('G', 0),
153    pg1('G', 1),
154    pg2('G', 2),
155    pg3('G', 3),
156    pg4('G', 4),
157    pg5('G', 5),
158    pg6('G', 6),
159    pg7('G', 7),
160    //
161    ph0('H', 0),
162    ph1('H', 1),
163    ph2('H', 2),
164    ph3('H', 3),
165    ph4('H', 4),
166    ph5('H', 5),
167    ph6('H', 6),
168    ph7('H', 7),
169}