Skip to content

Commit b8e3ec5

Browse files
bors[bot]eldruin
andauthored
Merge #298
298: Turn traits and execution model modules inside out r=therealprof a=eldruin A proposal for discussion. ## Advantadges - Place for execution-model-independent type definitions - For example useful for SPI modes - Fits better with users who might think of peripheral first, then execution model. - `@Sh3Rm4n` - Helps discoverability, since the modules are not "hidden" behind their execution model. - `@Rahix` ## Disadvantadges - Breaks every last `embedded-hal`-dependent code - Users may want to import several `embedded_hal::xxx::blocking` modules, for example, although there is no much reason to do so. - There might be confusion between `embedded_hal::xxx::nb` and `embedded_hal::nb`. ## Open questions - Reexport execution-model-independent types in `blocking`/`nb` submodules? ## Alternatives Keep the structure as-is Co-authored-by: Diego Barrios Romero <[email protected]>
2 parents 3dcb672 + 25ba012 commit b8e3ec5

29 files changed

+1120
-1079
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
### Changed
1616
- Swap PWM channel arguments to references
1717
- All trait methods have been renamed to remove the `try_` prefix (i.e. `try_send` -> `send`) for consistency.
18-
- Moved all traits into two modules depending on the execution model: `blocking` and `nb` (non-blocking).
18+
- Moved all traits into two sub modules for each feature depending on the execution model: `blocking` and `nb` (non-blocking). For example, the spi traits can now be found under `embedded_hal::spi::blocking` or `embedded_hal::spi::nb`.
19+
- Execution-model-independent definitions have been moved into the feature module. For example, SPI `Phase` is now defined in `embedded_hal::spi::Phase`. For convenience, these definitions are reexported in both of its blocking and non-blocking submodules.
1920
- Re-export `nb::{block!, Error, Result}` to avoid version mismatches. These should be used instead of
20-
importing the `nb` crate directly in dependendent crates.
21+
importing the `nb` crate directly in dependent crates.
2122
- `blocking::Serial`: renamed `bwrite_all` to `write`, `bflush` to `flush.
2223
- Removed `prelude` to avoid method name conflicts between different flavors (blocking, nb) of the same trait. Traits must now be manually imported.
2324
- Removed the various `Default` marker traits.

src/adc.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//! Analog-digital conversion traits
2+
3+
/// Non-blocking ADC traits
4+
pub mod nb {
5+
/// A marker trait to identify MCU pins that can be used as inputs to an ADC channel.
6+
///
7+
/// This marker trait denotes an object, i.e. a GPIO pin, that is ready for use as an input to the
8+
/// ADC. As ADCs channels can be supplied by multiple pins, this trait defines the relationship
9+
/// between the physical interface and the ADC sampling buffer.
10+
///
11+
/// ```
12+
/// # use core::marker::PhantomData;
13+
/// # use embedded_hal::adc::nb::Channel;
14+
///
15+
/// struct Adc1; // Example ADC with single bank of 8 channels
16+
/// struct Gpio1Pin1<MODE>(PhantomData<MODE>);
17+
/// struct Analog(()); // marker type to denote a pin in "analog" mode
18+
///
19+
/// // GPIO 1 pin 1 can supply an ADC channel when it is configured in Analog mode
20+
/// impl Channel<Adc1> for Gpio1Pin1<Analog> {
21+
/// type ID = u8; // ADC channels are identified numerically
22+
///
23+
/// fn channel(&self) -> Self::ID {
24+
/// 7_u8 // GPIO pin 1 is connected to ADC channel 7
25+
/// }
26+
/// }
27+
///
28+
/// struct Adc2; // ADC with two banks of 16 channels
29+
/// struct Gpio2PinA<MODE>(PhantomData<MODE>);
30+
/// struct AltFun(()); // marker type to denote some alternate function mode for the pin
31+
///
32+
/// // GPIO 2 pin A can supply an ADC channel when it's configured in some alternate function mode
33+
/// impl Channel<Adc2> for Gpio2PinA<AltFun> {
34+
/// type ID = (u8, u8); // ADC channels are identified by bank number and channel number
35+
///
36+
/// fn channel(&self) -> Self::ID {
37+
/// (0, 3) // bank 0 channel 3
38+
/// }
39+
/// }
40+
/// ```
41+
pub trait Channel<ADC> {
42+
/// Channel ID type
43+
///
44+
/// A type used to identify this ADC channel. For example, if the ADC has eight channels, this
45+
/// might be a `u8`. If the ADC has multiple banks of channels, it could be a tuple, like
46+
/// `(u8: bank_id, u8: channel_id)`.
47+
type ID: Copy;
48+
49+
/// Get the specific ID that identifies this channel, for example `0_u8` for the first ADC
50+
/// channel, if Self::ID is u8.
51+
fn channel(&self) -> Self::ID;
52+
}
53+
54+
/// ADCs that sample on single channels per request, and do so at the time of the request.
55+
///
56+
/// This trait is the interface to an ADC that is configured to read a specific channel at the time
57+
/// of the request (in contrast to continuous asynchronous sampling).
58+
///
59+
/// ```
60+
/// use embedded_hal::adc::nb::{Channel, OneShot};
61+
///
62+
/// struct MyAdc; // 10-bit ADC, with 5 channels
63+
/// # impl MyAdc {
64+
/// # pub fn power_up(&mut self) {}
65+
/// # pub fn power_down(&mut self) {}
66+
/// # pub fn do_conversion(&mut self, chan: u8) -> u16 { 0xAA55_u16 }
67+
/// # }
68+
///
69+
/// impl<WORD, PIN> OneShot<MyAdc, WORD, PIN> for MyAdc
70+
/// where
71+
/// WORD: From<u16>,
72+
/// PIN: Channel<MyAdc, ID=u8>,
73+
/// {
74+
/// type Error = ();
75+
///
76+
/// fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
77+
/// let chan = 1 << pin.channel();
78+
/// self.power_up();
79+
/// let result = self.do_conversion(chan);
80+
/// self.power_down();
81+
/// Ok(result.into())
82+
/// }
83+
/// }
84+
/// ```
85+
pub trait OneShot<ADC, Word, Pin: Channel<ADC>> {
86+
/// Error type returned by ADC methods
87+
type Error;
88+
89+
/// Request that the ADC begin a conversion on the specified pin
90+
///
91+
/// This method takes a `Pin` reference, as it is expected that the ADC will be able to sample
92+
/// whatever channel underlies the pin.
93+
fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
94+
}
95+
}

src/blocking/delay.rs

-32
This file was deleted.

src/blocking/digital.rs

-155
This file was deleted.

0 commit comments

Comments
 (0)