Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to embedded-hal 1.0. #16

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, 1.31.0]
rust: [stable, 1.60.0]
reitermarkus marked this conversation as resolved.
Show resolved Hide resolved
TARGET:
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [1.52.1]
rust: [1.60.0]
TARGET:
- x86_64-unknown-linux-gnu

Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

...
- MSRV increased to 1.60.
- Updated `embedded-hal` to version `1`, `read` in one-shot mode is therefore only an inherent method.

## [0.2.2] - 2021-07-29

Expand Down
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ include = [
"/LICENSE-MIT",
"/LICENSE-APACHE",
]
edition = "2018"
edition = "2021"

[badges]
coveralls = { repository = "eldruin/ads1x1x-rs", branch = "master", service = "github" }

[dependencies]
nb = "1"
embedded-hal = { version = "0.2.2", features = ["unproven"] }
embedded-hal = "1"

[dev-dependencies]
linux-embedded-hal = "0.3"
embedded-hal-mock = "0.7"
embedded-hal-mock = { version = "0.10", default-features = false, features = ["eh1"] }

[target.'cfg(target_os = "linux")'.dev-dependencies]
eldruin marked this conversation as resolved.
Show resolved Hide resolved
linux-embedded-hal = "0.4"

[profile.release]
lto = true
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ Please find additional examples using hardware in this repository: [driver-examp
[driver-examples]: https://github.com/eldruin/driver-examples

```rust
use embedded_hal::adc::OneShot;
use linux_embedded_hal::I2cdev;
use nb::block;

Expand All @@ -87,7 +86,7 @@ fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut adc = Ads1x1x::new_ads1013(dev, address);
let value = block!(adc.read(&mut channel::DifferentialA0A1)).unwrap();
let value = block!(adc.read(channel::DifferentialA0A1)).unwrap();
println!("Measurement: {}", value);
// get I2C device back
let _dev = adc.destroy_ads1013();
Expand Down
9 changes: 4 additions & 5 deletions examples/all_channels.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use embedded_hal::adc::OneShot;
use linux_embedded_hal::I2cdev;
use nb::block;

Expand All @@ -9,10 +8,10 @@ fn main() {
let address = SlaveAddr::default();
let mut adc = Ads1x1x::new_ads1015(dev, address);
let values = [
block!(adc.read(&mut channel::SingleA0)).unwrap(),
block!(adc.read(&mut channel::SingleA1)).unwrap(),
block!(adc.read(&mut channel::SingleA2)).unwrap(),
block!(adc.read(&mut channel::SingleA3)).unwrap(),
block!(adc.read(channel::SingleA0)).unwrap(),
block!(adc.read(channel::SingleA1)).unwrap(),
block!(adc.read(channel::SingleA2)).unwrap(),
block!(adc.read(channel::SingleA3)).unwrap(),
];
for (channel, value) in values.iter().enumerate() {
println!("Channel {}: {}", channel, value);
Expand Down
3 changes: 1 addition & 2 deletions examples/linux.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use embedded_hal::adc::OneShot;
use linux_embedded_hal::I2cdev;
use nb::block;

Expand All @@ -8,7 +7,7 @@ fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut adc = Ads1x1x::new_ads1013(dev, address);
let value = block!(adc.read(&mut channel::DifferentialA0A1)).unwrap();
let value = block!(adc.read(channel::DifferentialA0A1)).unwrap();
println!("Measurement: {}", value);
// get I2C device back
let _dev = adc.destroy_ads1013();
Expand Down
5 changes: 2 additions & 3 deletions examples/typed.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// This example demonstrates the use of a type alias for the `Ads1x1x` struct
// to ease usage in signatures.

use embedded_hal::adc::OneShot;
use linux_embedded_hal::I2cdev;
use nb::block;

use ads1x1x::{
channel::SingleA0,
channel,
ic::{Ads1115, Resolution16Bit},
interface::I2cInterface,
Ads1x1x, SlaveAddr,
Expand All @@ -18,7 +17,7 @@ type Adc = Ads1x1x<I2cInterface<I2cdev>, Ads1115, Resolution16Bit, ads1x1x::mode
/// Read a single value from channel A.
/// Returns 0 on Error.
pub fn read(adc: &mut Adc) -> i16 {
block!(adc.read(&mut SingleA0)).unwrap_or(0)
block!(adc.read(channel::SingleA0)).unwrap_or(0)
}

fn main() {
Expand Down
98 changes: 98 additions & 0 deletions src/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//! ADC input channels
use crate::{ic, Ads1x1x, BitFlags as BF, Config};

mod private {
pub trait Sealed {}
}

/// Marker type for an ADC input channel.
pub trait ChannelId<T>: private::Sealed {
/// Get the channel.
fn channel_id() -> ChannelSelection;
}

macro_rules! impl_channels {
($(#[doc = $doc:expr] $CH:ident => [$($IC:ident),+]),+ $(,)?) => {
#[derive(Debug, Clone, Copy)]
/// ADC input channel selection.
pub enum ChannelSelection {
$(
#[doc = $doc]
$CH,
)+
}

$(
#[doc = $doc]
pub struct $CH;

impl private::Sealed for $CH {}

$(
impl<DI, CONV, MODE> ChannelId<Ads1x1x<DI, ic::$IC, CONV, MODE>> for $CH {
fn channel_id() -> ChannelSelection {
ChannelSelection::$CH
}
}
)+
)+
};
}

impl_channels!(
/// Measure signal on input channel 0 differentially to signal on input channel 1.
DifferentialA0A1 => [Ads1013, Ads1014, Ads1015, Ads1113, Ads1114, Ads1115],
/// Measure signal on input channel 0 differentially to signal on input channel 3.
DifferentialA0A3 => [Ads1015, Ads1115],
/// Measure signal on input channel 1 differentially to signal on input channel 3.
DifferentialA1A3 => [Ads1015, Ads1115],
/// Measure signal on input channel 3 differentially to signal on input channel 3.
DifferentialA2A3 => [Ads1015, Ads1115],
/// Measure single-ended signal on input channel 0.
SingleA0 => [Ads1015, Ads1115],
/// Measure single-ended signal on input channel 1.
SingleA1 => [Ads1015, Ads1115],
/// Measure single-ended signal on input channel 2.
SingleA2 => [Ads1015, Ads1115],
/// Measure single-ended signal on input channel 3.
SingleA3 => [Ads1015, Ads1115]
);

impl Config {
pub(crate) fn with_mux_bits(&self, ch: ChannelSelection) -> Self {
match ch {
ChannelSelection::DifferentialA0A1 => self
.with_low(BF::MUX2)
.with_low(BF::MUX1)
.with_low(BF::MUX0),
ChannelSelection::DifferentialA0A3 => self
.with_low(BF::MUX2)
.with_low(BF::MUX1)
.with_high(BF::MUX0),
ChannelSelection::DifferentialA1A3 => self
.with_low(BF::MUX2)
.with_high(BF::MUX1)
.with_low(BF::MUX0),
ChannelSelection::DifferentialA2A3 => self
.with_low(BF::MUX2)
.with_high(BF::MUX1)
.with_high(BF::MUX0),
ChannelSelection::SingleA0 => self
.with_high(BF::MUX2)
.with_low(BF::MUX1)
.with_low(BF::MUX0),
ChannelSelection::SingleA1 => self
.with_high(BF::MUX2)
.with_low(BF::MUX1)
.with_high(BF::MUX0),
ChannelSelection::SingleA2 => self
.with_high(BF::MUX2)
.with_high(BF::MUX1)
.with_low(BF::MUX0),
ChannelSelection::SingleA3 => self
.with_high(BF::MUX2)
.with_high(BF::MUX1)
.with_high(BF::MUX0),
}
}
}
121 changes: 0 additions & 121 deletions src/channels.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use crate::{
DEVICE_BASE_ADDRESS,
};
use core::marker::PhantomData;
use embedded_hal::blocking;

macro_rules! impl_new_destroy {
( $IC:ident, $create:ident, $destroy:ident, $conv:ty ) => {
impl<I2C, E> Ads1x1x<I2cInterface<I2C>, ic::$IC, $conv, mode::OneShot>
where
I2C: blocking::i2c::Write<Error = E> + blocking::i2c::WriteRead<Error = E>,
I2C: embedded_hal::i2c::I2c<Error = E>,
{
/// Create a new instance of the device in OneShot mode.
pub fn $create(i2c: I2C, address: SlaveAddr) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait ConvertThreshold<E>: private::Sealed {

impl<E> ConvertThreshold<E> for ic::Resolution12Bit {
fn convert_threshold(value: i16) -> Result<u16, Error<E>> {
if value < -2048 || value > 2047 {
if !(-2048..=2047).contains(&value) {
return Err(Error::InvalidInputData);
}
Ok((value << 4) as u16)
Expand Down
9 changes: 4 additions & 5 deletions src/devices/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ where
DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>,
{
pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> {
let config;
match mode {
OperatingMode::OneShot => config = self.config.with_high(BitFlags::OP_MODE),
OperatingMode::Continuous => config = self.config.with_low(BitFlags::OP_MODE),
}
let config = match mode {
OperatingMode::OneShot => self.config.with_high(BitFlags::OP_MODE),
OperatingMode::Continuous => self.config.with_low(BitFlags::OP_MODE),
};
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())
Expand Down
Loading
Loading