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

ADC cannot read from a Pin<Analog> object. #33

Open
bbonenfant opened this issue Aug 3, 2020 · 7 comments
Open

ADC cannot read from a Pin<Analog> object. #33

bbonenfant opened this issue Aug 3, 2020 · 7 comments
Labels
hal-api API design for the different components of avr-hal

Comments

@bbonenfant
Copy link

Hello, this is a great project. When trying to perform the Adc.read method on a Pin<Analog> object, I receive the following error:

the trait bound `arduino_uno::atmega328p_hal::port::Pin<arduino_uno::atmega328p_hal::port::mode::Analog>: embedded_hal::adc::Channel<arduino_uno::adc::Adc>` is not satisfied
required because of the requirements on the impl of `arduino_uno::prelude::_embedded_hal_adc_OneShot<arduino_uno::adc::Adc, _, arduino_uno::atmega328p_hal::port::Pin<arduino_uno::atmega328p_hal::port::mode::Analog>>` for `arduino_uno::adc::Adc` rustc(E0277)

which cleaned up for you benefit becomes:

the trait bound `Pin<Analog>: Channel<Adc>` is not satisfied
required because of the requirements on the impl of `OneShot<Adc, _, Pin<Analog>>` for `Adc` rustc(E0277)

I believe that this just means that the downgraded/generic Pin<Analog> object needs this OneShot trait implemented? It's a little hard to figure out what is and isn't implemented with all of the procedural macros.


Here is a minimal reproducible example:

#![no_std]
#![no_main]
extern crate panic_halt;
use arduino_uno::prelude::*;
use nb;

#[arduino_uno::entry]
fn main() -> ! {
    let peripherals = arduino_uno::Peripherals::take().unwrap();
    let mut pins = arduino_uno::Pins::new(
        peripherals.PORTB, peripherals.PORTC, peripherals.PORTD
    );

    let mut adc = arduino_uno::adc::Adc::new(
        peripherals.ADC, arduino_uno::adc::AdcSettings::default()
    );
    let analog_pin = pins.a0.into_analog_input(&mut adc).downgrade();
    let val: u16 = nb::block!(adc.read(&mut analog_pin)).void_unwrap();
    loop {}
}
@couchand
Copy link
Contributor

couchand commented Aug 4, 2020

From what I can tell, this is not possible to implement due to limitations in embedded-hal. The OneShot trait itself would be easy enough (just delegate to the correct port variant), but OneShot has a bound on Channel, which isn't implementable for the generic Pin, see: rust-embedded/embedded-hal#110. It looks like there was buy-in a year and a half ago to change that API, but there's been no movement since then.

@couchand
Copy link
Contributor

couchand commented Aug 4, 2020

Correction: there has been movement in the API, but not in a direction that supports this usage. Ahead of an imminent 1.0 release, the channel() associated function has been removed entirely in favor of an associated constant.

@Rahix Rahix added the hal-impl label Aug 4, 2020
@Rahix
Copy link
Owner

Rahix commented Aug 5, 2020

We will address this issue when rust-embedded/embedded-hal#242 is merged and this project migrates to embedded-hal version 1.0.0.

@Rahix Rahix added the hal-api API design for the different components of avr-hal label Oct 4, 2020
Rahix added a commit that referenced this issue Mar 13, 2021
Add a type that can represent any channel for e.g. storing a list of
channels in an array.

Ref: #33
Rahix added a commit that referenced this issue Mar 13, 2021
Add a type that can represent any channel for e.g. storing a list of
channels in an array.

Ref: #33
@Rahix
Copy link
Owner

Rahix commented Mar 15, 2021

On the next branch we now have a "solution" to this. Check the mega2560-adc example: https://github.com/Rahix/avr-hal/blob/next/examples/arduino-mega2560/src/bin/mega2560-adc.rs

@davystrong
Copy link

davystrong commented Jun 6, 2021

Is there any workaround for this until the next branch stabilizes?

@Rahix
Copy link
Owner

Rahix commented Jun 9, 2021

@davystrong: I would consider the next branch just as stable (or unstable) as master at this point. So if you need this right now, I would suggest just switching to the next branch already. But of course you'll need to refactor your code quite a bit to work with the new design.

@davystrong
Copy link

Ok, thanks, I'll try that.

@Rahix Rahix removed the hal-impl label Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hal-api API design for the different components of avr-hal
Projects
None yet
Development

No branches or pull requests

4 participants