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 embedded-hal requirement from 0.2.2 to 1.0.0-rc.1 #13

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ads1x1x"
version = "0.2.2" # remember to update html_root_url
version = "0.3.0" # remember to update html_root_url
authors = ["Diego Barrios Romero <[email protected]>"]
repository = "https://github.com/eldruin/ads1x1x-rs"
license = "MIT OR Apache-2.0"
Expand All @@ -18,14 +18,14 @@ 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.0.0-rc.1"

[dev-dependencies]
linux-embedded-hal = "0.3"
Expand Down
93 changes: 93 additions & 0 deletions src/adc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//! Analog-digital conversion traits

/// A marker trait to identify MCU pins that can be used as inputs to an ADC channel.
///
/// This marker trait denotes an object, i.e. a GPIO pin, that is ready for use as an input to the
/// ADC. As ADCs channels can be supplied by multiple pins, this trait defines the relationship
/// between the physical interface and the ADC sampling buffer.
///
/// ```
/// # use std::marker::PhantomData;
/// # use embedded_hal::adc::Channel;
///
/// struct Adc1; // Example ADC with single bank of 8 channels
/// struct Gpio1Pin1<MODE>(PhantomData<MODE>);
/// struct Analog(()); // marker type to denote a pin in "analog" mode
///
/// // GPIO 1 pin 1 can supply an ADC channel when it is configured in Analog mode
/// impl Channel<Adc1> for Gpio1Pin1<Analog> {
/// type ID = u8; // ADC channels are identified numerically
///
/// fn channel() -> u8 { 7_u8 } // GPIO pin 1 is connected to ADC channel 7
/// }
///
/// struct Adc2; // ADC with two banks of 16 channels
/// struct Gpio2PinA<MODE>(PhantomData<MODE>);
/// struct AltFun(()); // marker type to denote some alternate function mode for the pin
///
/// // GPIO 2 pin A can supply an ADC channel when it's configured in some alternate function mode
/// impl Channel<Adc2> for Gpio2PinA<AltFun> {
/// type ID = (u8, u8); // ADC channels are identified by bank number and channel number
///
/// fn channel() -> (u8, u8) { (0, 3) } // bank 0 channel 3
/// }
/// ```
pub trait Channel<ADC> {
/// Channel ID type
///
/// A type used to identify this ADC channel. For example, if the ADC has eight channels, this
/// might be a `u8`. If the ADC has multiple banks of channels, it could be a tuple, like
/// `(u8: bank_id, u8: channel_id)`.
type ID;

/// Get the specific ID that identifies this channel, for example `0_u8` for the first ADC
/// channel, if Self::ID is u8.
fn channel() -> Self::ID;

// `channel` is a function due to [this reported
// issue](https://github.com/rust-lang/rust/issues/54973). Something about blanket impls
// combined with `type ID; const CHANNEL: Self::ID;` causes problems.
//const CHANNEL: Self::ID;
}

/// ADCs that sample on single channels per request, and do so at the time of the request.
///
/// This trait is the interface to an ADC that is configured to read a specific channel at the time
/// of the request (in contrast to continuous asynchronous sampling).
///
/// ```
/// use embedded_hal::adc::{Channel, OneShot};
///
/// struct MyAdc; // 10-bit ADC, with 5 channels
/// # impl MyAdc {
/// # pub fn power_up(&mut self) {}
/// # pub fn power_down(&mut self) {}
/// # pub fn do_conversion(&mut self, chan: u8) -> u16 { 0xAA55_u16 }
/// # }
///
/// impl<WORD, PIN> OneShot<MyAdc, WORD, PIN> for MyAdc
/// where
/// WORD: From<u16>,
/// PIN: Channel<MyAdc, ID=u8>,
/// {
/// type Error = ();
///
/// fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
/// let chan = 1 << PIN::channel();
/// self.power_up();
/// let result = self.do_conversion(chan);
/// self.power_down();
/// Ok(result.into())
/// }
/// }
/// ```
pub trait OneShot<ADC, Word, Pin: Channel<ADC>> {
/// Error type returned by ADC methods
type Error;

/// Request that the ADC begin a conversion on the specified pin
///
/// This method takes a `Pin` reference, as it is expected that the ADC will be able to sample
/// whatever channel underlies the pin.
fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
}
2 changes: 1 addition & 1 deletion src/channels.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! ADC input channels
use crate::{ic, Ads1x1x, BitFlags as BF, Config};
use embedded_hal::adc;
use crate::adc;

/// ADC input channel selection
#[allow(dead_code)]
Expand Down
7 changes: 4 additions & 3 deletions src/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use crate::{
DEVICE_BASE_ADDRESS,
};
use core::marker::PhantomData;
use embedded_hal::blocking;
use embedded_hal::i2c::I2c;

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: I2c<Error = E>,
{
/// Create a new instance of the device in OneShot mode.
pub fn $create(i2c: I2C, address: SlaveAddr) -> Self {
Expand All @@ -29,7 +29,8 @@ macro_rules! impl_new_destroy {
}
}
}
impl<I2C, CONV, MODE> Ads1x1x<I2cInterface<I2C>, ic::$IC, CONV, MODE> {
impl<I2C, CONV, MODE> Ads1x1x<I2cInterface<I2C>, ic::$IC, CONV, MODE>
where I2C: embedded_hal::i2c::I2c {
/// Destroy driver instance, return I²C bus instance.
pub fn $destroy(self) -> I2C {
self.iface.i2c
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
2 changes: 1 addition & 1 deletion src/devices/mode/continuous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
Error, ModeChangeError, Register,
};
use core::marker::PhantomData;
use embedded_hal::adc;
use crate::adc;

impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous>
where
Expand Down
2 changes: 1 addition & 1 deletion src/devices/mode/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
Config, DynamicOneShot, Error, ModeChangeError, Register,
};
use core::marker::PhantomData;
use embedded_hal::adc;
use crate::adc;

impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot>
where
Expand Down
9 changes: 5 additions & 4 deletions src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! I2C interface

use crate::{private, Error};
use embedded_hal::blocking;
use embedded_hal::i2c::I2c;

/// I2C interface
#[derive(Debug, Default)]
pub struct I2cInterface<I2C> {
pub struct I2cInterface<I2C>
where I2C: I2c {
pub(crate) i2c: I2C,
pub(crate) address: u8,
}
Expand All @@ -20,7 +21,7 @@ pub trait WriteData: private::Sealed {

impl<I2C, E> WriteData for I2cInterface<I2C>
where
I2C: blocking::i2c::Write<Error = E>,
I2C: I2c<Error = E>,
{
type Error = E;
fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> {
Expand All @@ -39,7 +40,7 @@ pub trait ReadData: private::Sealed {

impl<I2C, E> ReadData for I2cInterface<I2C>
where
I2C: blocking::i2c::WriteRead<Error = E>,
I2C: I2c<Error = E>,
{
type Error = E;
fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> {
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
//! adc.set_comparator_latching(ComparatorLatching::Latching).unwrap();
//! ```

#![doc(html_root_url = "https://docs.rs/ads1x1x/0.2.2")]
#![doc(html_root_url = "https://docs.rs/ads1x1x/0.3.0")]
#![deny(unsafe_code)]
#![deny(missing_docs)]
#![no_std]
Expand Down Expand Up @@ -238,6 +238,7 @@ impl BitFlags {
}

mod channels;
pub mod adc;
pub use crate::channels::{channel, ChannelSelection};
mod construction;
mod conversion;
Expand All @@ -259,7 +260,8 @@ mod private {
use super::{ic, interface, Ads1x1x};
pub trait Sealed {}

impl<I2C> Sealed for interface::I2cInterface<I2C> {}
impl<I2C> Sealed for interface::I2cInterface<I2C>
where I2C: embedded_hal::i2c::I2c {}
impl<DI, IC, CONV, MODE> Sealed for Ads1x1x<DI, IC, CONV, MODE> {}

impl Sealed for ic::Resolution12Bit {}
Expand Down
8 changes: 3 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ pub enum ComparatorQueue {
/// The FSR is fixed at ±2.048 V in the ADS1x13.
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(non_camel_case_types)]
#[derive(Default)]
pub enum FullScaleRange {
/// The measurable range is ±6.144V.
Within6_144V,
/// The measurable range is ±4.096V.
Within4_096V,
/// The measurable range is ±2.048V. (default)
#[default]
Within2_048V,
/// The measurable range is ±1.024V.
Within1_024V,
Expand Down Expand Up @@ -241,11 +243,7 @@ impl Default for Config {
}
}

impl Default for FullScaleRange {
fn default() -> Self {
FullScaleRange::Within2_048V
}
}


/// ADS1x1x ADC driver
#[derive(Debug, Default)]
Expand Down