-
Notifications
You must be signed in to change notification settings - Fork 9
/
oneshot.rs
84 lines (79 loc) · 3.18 KB
/
oneshot.rs
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
73
74
75
76
77
78
79
80
81
82
83
84
//! Common functions
use super::super::OperatingMode;
use channels::ChannelSelection;
use core::marker::PhantomData;
use {conversion, hal, interface, nb};
use {mode, Ads1x1x, BitFlags, Config, Error, ModeChangeError, Register};
impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot>
where
DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>,
CONV: conversion::ConvertMeasurement,
{
/// Change operating mode to Continuous
pub fn into_continuous(
mut self,
) -> Result<Ads1x1x<DI, IC, CONV, mode::Continuous>, ModeChangeError<E, Self>> {
if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::Continuous) {
return Err(ModeChangeError::I2C(e, self));
}
Ok(Ads1x1x {
iface: self.iface,
config: self.config,
fsr: self.fsr,
a_conversion_was_started: true,
_conv: PhantomData,
_ic: PhantomData,
_mode: PhantomData,
})
}
fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error<E>> {
let config = config.with_high(BitFlags::OS);
self.iface.write_register(Register::CONFIG, config.bits)
}
}
impl<DI, IC, CONV, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, CONV, mode::OneShot>, i16, CH>
for Ads1x1x<DI, IC, CONV, mode::OneShot>
where
DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>,
CONV: conversion::ConvertMeasurement,
CH: hal::adc::Channel<Ads1x1x<DI, IC, CONV, mode::OneShot>, ID = ChannelSelection>,
{
type Error = 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.
fn read(&mut self, _channel: &mut CH) -> nb::Result<i16, Self::Error> {
if self
.is_measurement_in_progress()
.map_err(nb::Error::Other)?
{
return Err(nb::Error::WouldBlock);
}
let same_channel = self.config == self.config.with_mux_bits(CH::channel());
if self.a_conversion_was_started && same_channel {
// result is ready
let value = self
.iface
.read_register(Register::CONVERSION)
.map_err(nb::Error::Other)?;
self.a_conversion_was_started = false;
return Ok(CONV::convert_measurement(value));
}
let config = self.config.with_mux_bits(CH::channel());
self.trigger_measurement(&config)
.map_err(nb::Error::Other)?;
self.config = config;
self.a_conversion_was_started = true;
Err(nb::Error::WouldBlock)
}
}