-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathlib.rs
More file actions
72 lines (65 loc) · 2.16 KB
/
lib.rs
File metadata and controls
72 lines (65 loc) · 2.16 KB
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
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
// disable warning for already-stabilized features.
// Needed to pass CI, because we deny warnings.
// We don't immediately remove them to not immediately break older nightlies.
// When all features are stable, we'll remove them.
#![cfg_attr(all(feature = "async", nightly), allow(stable_features))]
#![cfg_attr(
all(feature = "async", nightly),
feature(async_fn_in_trait, impl_trait_projections)
)]
// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`.
#[cfg(feature = "defmt-03")]
use defmt_03 as defmt;
pub mod i2c;
pub mod spi;
/// This adapter will [panic] if the inner device encounters an error.
///
/// It currently supports [embedded_hal::digital::OutputPin], but other traits may be added in the future.
///
/// # Example
///
/// ```
/// use core::convert::Infallible;
/// use embedded_hal::digital::OutputPin;
/// use embedded_hal_bus::UnwrappingAdapter;
///
/// /// This could be any function or struct that requires an infallible output pin
/// fn requires_infallible(output: impl OutputPin<Error = Infallible>) { /* ... */ }
///
/// fn accepts_fallible(output: impl OutputPin) {
/// // this wouldn't work:
/// // requires_infallible(output);
///
/// let unwrapping_output = UnwrappingAdapter(output);
/// requires_infallible(unwrapping_output);
/// }
/// ```
#[repr(transparent)]
pub struct UnwrappingAdapter<T>(pub T);
impl<T> embedded_hal::digital::ErrorType for UnwrappingAdapter<T> {
type Error = core::convert::Infallible;
}
impl<T> embedded_hal::digital::OutputPin for UnwrappingAdapter<T>
where
T: embedded_hal::digital::OutputPin,
{
#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
self.0.set_low().unwrap();
Ok(())
}
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
self.0.set_high().unwrap();
Ok(())
}
#[inline]
fn set_state(&mut self, state: embedded_hal::digital::PinState) -> Result<(), Self::Error> {
self.0.set_state(state).unwrap();
Ok(())
}
}