Skip to content

Commit

Permalink
Remove DynamicOneShot trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 19, 2024
1 parent 5cd3613 commit bad978d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 83 deletions.
24 changes: 0 additions & 24 deletions examples/trait.rs

This file was deleted.

24 changes: 13 additions & 11 deletions src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
//! ADC input channels
use crate::{ic, Ads1x1x, BitFlags as BF, Config};

mod private {
pub trait Sealed {}
}
use private::ChannelSelection;

/// Marker type for an ADC input channel.
pub trait ChannelId<T>: private::Sealed {
pub trait ChannelId<T> {
/// 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,
)+
mod private {
pub trait Sealed {}

#[derive(Debug, Clone, Copy)]
/// ADC input channel selection.
pub enum ChannelSelection {
$(
#[doc = $doc]
$CH,
)+
}
}

$(
Expand Down
52 changes: 18 additions & 34 deletions src/devices/mode/oneshot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Common functions
use crate::{
conversion, devices::OperatingMode, mode, Ads1x1x, BitFlags, ChannelId, ChannelSelection,
Config, DynamicOneShot, Error, ModeChangeError, Register,
conversion, devices::OperatingMode, mode, Ads1x1x, BitFlags, ChannelId, Config, Error,
ModeChangeError, Register,
};
use core::marker::PhantomData;

Expand Down Expand Up @@ -40,14 +40,28 @@ where
I2C: embedded_hal::i2c::I2c<Error = E>,
CONV: conversion::ConvertMeasurement,
{
fn read_inner(&mut self, channel: ChannelSelection) -> nb::Result<i16, Error<E>> {
/// Request that the ADC begin a conversion on the specified channel.
///
/// The output value will be within `[2047..-2048]` for 12-bit devices
/// (`ADS101x`) and within `[32767..-32768]` for 16-bit devices (`ADS111x`).
/// The voltage that these values correspond to must be calculated using
/// the full-scale range selected.
/// See [`FullScaleRange`](enum.FullScaleRange.html).
///
/// Returns `nb::Error::WouldBlock` while a measurement is in progress.
///
/// In case a measurement was requested and after is it is finished a
/// measurement on a different channel is requested, a new measurement on
/// using the new channel selection is triggered.
#[allow(unused_variables)]
pub fn read<CH: ChannelId<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> {
if self
.is_measurement_in_progress()
.map_err(nb::Error::Other)?
{
return Err(nb::Error::WouldBlock);
}
let config = self.config.with_mux_bits(channel);
let config = self.config.with_mux_bits(CH::channel_id());
let same_channel = self.config == config;
if self.a_conversion_was_started && same_channel {
// result is ready
Expand All @@ -63,34 +77,4 @@ where
self.a_conversion_was_started = true;
Err(nb::Error::WouldBlock)
}

/// Request that the ADC begin a conversion on the specified channel.
///
/// The output value will be within `[2047..-2048]` for 12-bit devices
/// (`ADS101x`) and within `[32767..-32768]` for 16-bit devices (`ADS111x`).
/// The voltage that these values correspond to must be calculated using
/// the full-scale range selected.
/// See [`FullScaleRange`](enum.FullScaleRange.html).
///
/// Returns `nb::Error::WouldBlock` while a measurement is in progress.
///
/// In case a measurement was requested and after is it is finished a
/// measurement on a different channel is requested, a new measurement on
/// using the new channel selection is triggered.
#[allow(unused_variables)]
pub fn read<CH: ChannelId<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> {
self.read_inner(CH::channel_id())
}
}

impl<I2C, IC, CONV, E> DynamicOneShot for Ads1x1x<I2C, IC, CONV, mode::OneShot>
where
I2C: embedded_hal::i2c::I2c<Error = E>,
CONV: conversion::ConvertMeasurement,
{
type Error = Error<E>;

fn read(&mut self, channel: ChannelSelection) -> nb::Result<i16, Self::Error> {
self.read_inner(channel)
}
}
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl BitFlags {
}

pub mod channel;
pub use channel::{ChannelId, ChannelSelection};
pub use channel::ChannelId;
mod construction;
mod conversion;
pub use crate::conversion::{ConvertMeasurement, ConvertThreshold};
Expand All @@ -246,8 +246,7 @@ mod types;
use crate::types::Config;
pub use crate::types::{
mode, Ads1x1x, ComparatorLatching, ComparatorMode, ComparatorPolarity, ComparatorQueue,
DataRate12Bit, DataRate16Bit, DynamicOneShot, Error, FullScaleRange, ModeChangeError,
SlaveAddr,
DataRate12Bit, DataRate16Bit, Error, FullScaleRange, ModeChangeError, SlaveAddr,
};

mod private {
Expand Down
11 changes: 0 additions & 11 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
use core::marker::PhantomData;

use crate::{private, ChannelSelection};

/// Errors in this crate
#[derive(Debug)]
pub enum Error<E> {
Expand Down Expand Up @@ -256,15 +254,6 @@ pub struct Ads1x1x<I2C, IC, CONV, MODE> {
pub(crate) _mode: PhantomData<MODE>,
}

/// Multi channel One-shot ADC
pub trait DynamicOneShot: private::Sealed {
/// Error type
type Error;

/// Read a measurement
fn read(&mut self, channel: ChannelSelection) -> nb::Result<i16, Self::Error>;
}

#[cfg(test)]
mod tests {
use crate::DEVICE_BASE_ADDRESS as ADDR;
Expand Down

0 comments on commit bad978d

Please sign in to comment.