Typically, working with a one shot ADC looks like this
let mut adc = Adc::adc1(dp.ADC1);
let sample = adc.read(&channel);
print!("sample: {}", sample);
(Playground)
Is it possible to borrow the ADC channel to a structure to use like this
let mut thermometer1 = MyAdcChannel::new(&mut pin1);
let mut thermometer2 = MyAdcChannel::new(&mut pin2);
let temp1 = thermometer1.read();
let temp2 = thermometer2.read();
print!("temperature1: {}, temperature2: {}", temp1, temp2);
Is it possible to do this with hal like stm32f4xx-hal? Or should erasing the ADC channel be implemented in hal?
I'm using rtic so I can pass the ADC in the shares, but I'd like to avoid this.
Was able to solve this problem similar to embedded-hal-bus
pub struct RefCellChannel<'a, T, ADC, Pin>
where
T: OneShot<ADC, u16, Pin>,
Pin: Channel<ADC>,
{
adc: &'a RefCell<T>,
pin: Pin,
_adc: PhantomData<ADC>,
}
impl<'a, T, Pin, ADC> RefCellChannel<'a, T, ADC, Pin>
where
T: OneShot<ADC, u16, Pin>,
Pin: Channel<ADC>,
{
/// Create a new `RefCellChannel`.
#[inline]
pub fn new(adc: &'a RefCell<T>, pin: Pin) -> Self {
Self {
adc,
pin,
_adc: PhantomData,
}
}
pub fn read(&mut self) -> nb::Result<u16, <T as OneShot<ADC, u16, Pin>>::Error> {
let adc = &mut *self.adc.borrow_mut();
adc.read(&mut self.pin)
}
}
2 Likes
system
Closed
3
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.