embedded_platform/
gpio.rs1use core::pin;
10use core::task;
11
12pub mod get;
13pub mod set;
14
15pub trait Pin {
17 type Error;
21}
22
23pub trait InputPin: Pin {
25 fn poll_get(
27 self: pin::Pin<&mut Self>,
28 cx: &mut task::Context<'_>,
29 ) -> task::Poll<Result<bool, Self::Error>>;
30}
31
32pub trait InputPinExt: InputPin {
34 fn get(&mut self) -> get::Get<Self>
36 where
37 Self: Unpin,
38 {
39 get::get(self)
40 }
41}
42
43impl<A> InputPinExt for A where A: InputPin {}
44
45pub trait OutputPin: Pin {
47 fn poll_set(
49 self: pin::Pin<&mut Self>,
50 cx: &mut task::Context<'_>,
51 high: bool,
52 ) -> task::Poll<Result<(), Self::Error>>;
53}
54
55pub trait OutputPinExt: OutputPin {
57 fn set(&mut self, high: bool) -> set::Set<Self>
59 where
60 Self: Unpin,
61 {
62 set::set(self, high)
63 }
64}
65
66impl<A> OutputPinExt for A where A: OutputPin {}
67
68pub trait IntoFloatingInputPin: Pin {
71 type FloatingInputPin: InputPin<Error = Self::Error> + Unpin;
73
74 fn into_floating_input_pin(self) -> Result<Self::FloatingInputPin, Self::Error>;
76}
77
78pub trait IntoPullUpInputPin: Pin {
80 type PullUpInputPin: InputPin<Error = Self::Error> + Unpin;
82
83 fn into_pull_up_input_pin(self) -> Result<Self::PullUpInputPin, Self::Error>;
85}
86
87pub trait IntoPullDownInputPin: Pin {
89 type PullDownInputPin: InputPin<Error = Self::Error> + Unpin;
91
92 fn into_pull_down_input_pin(self) -> Result<Self::PullDownInputPin, Self::Error>;
94}
95
96pub trait IntoOpenDrainOutputPin: Pin {
98 type OpenDrainOutputPin: OutputPin<Error = Self::Error> + Unpin;
100
101 fn into_open_drain_output_pin(
103 self,
104 initial_high: bool,
105 ) -> Result<Self::OpenDrainOutputPin, Self::Error>;
106}
107
108pub trait IntoPushPullOutputPin: Pin {
110 type PushPullOutputPin: OutputPin<Error = Self::Error> + Unpin;
112
113 fn into_push_pull_output_pin(
115 self,
116 initial_high: bool,
117 ) -> Result<Self::PushPullOutputPin, Self::Error>;
118}
119
120#[derive(Clone, Copy, Debug)]
125pub struct NoConnect(bool);
126
127impl NoConnect {
128 pub fn new(value: bool) -> Self {
130 NoConnect(value)
131 }
132}
133
134impl Pin for NoConnect {
135 type Error = futures::never::Never;
136}
137
138impl InputPin for NoConnect {
139 fn poll_get(
140 self: pin::Pin<&mut Self>,
141 _cx: &mut task::Context<'_>,
142 ) -> task::Poll<Result<bool, Self::Error>> {
143 task::Poll::Ready(Ok(false))
144 }
145}
146
147impl OutputPin for NoConnect {
148 fn poll_set(
149 self: pin::Pin<&mut Self>,
150 _cx: &mut task::Context<'_>,
151 _high: bool,
152 ) -> task::Poll<Result<(), Self::Error>> {
153 task::Poll::Ready(Ok(()))
154 }
155}
156
157impl IntoFloatingInputPin for NoConnect {
158 type FloatingInputPin = Self;
159
160 fn into_floating_input_pin(self) -> Result<Self::FloatingInputPin, Self::Error> {
161 Ok(self)
162 }
163}
164
165impl IntoPullUpInputPin for NoConnect {
166 type PullUpInputPin = Self;
167
168 fn into_pull_up_input_pin(self) -> Result<Self::PullUpInputPin, Self::Error> {
169 Ok(self)
170 }
171}
172
173impl IntoPullDownInputPin for NoConnect {
174 type PullDownInputPin = Self;
175
176 fn into_pull_down_input_pin(self) -> Result<Self::PullDownInputPin, Self::Error> {
177 Ok(self)
178 }
179}
180
181impl IntoOpenDrainOutputPin for NoConnect {
182 type OpenDrainOutputPin = Self;
183
184 fn into_open_drain_output_pin(
185 self,
186 _initial_high: bool,
187 ) -> Result<Self::OpenDrainOutputPin, Self::Error> {
188 Ok(self)
189 }
190}
191
192impl IntoPushPullOutputPin for NoConnect {
193 type PushPullOutputPin = Self;
194
195 fn into_push_pull_output_pin(
196 self,
197 _initial_high: bool,
198 ) -> Result<Self::PushPullOutputPin, Self::Error> {
199 Ok(self)
200 }
201}