Passing analog Pin to function

Hi. I'm working on an FM radio project with an Arduino Mega 2560.
I have a btn variable that I'd like to pass to a function, but I can't figure out how to do it.

let mut adc = arduino_hal::Adc::new(dp.ADC, Default::default());
let btn = pins.a0.into_analog_input(&mut adc);
fn do_something<T>(adc: &arduino_hal::Adc, pin: &arduino_hal::port::Pin<arduino_hal::port::mode::Analog, T>)
 where T: arduino_hal::port::PinOps {
    pin.analog_read(&mut adc);
    todo!();
}

I'm getting the following error, which makes sense:
error[E0277]: the trait bound avr_hal_generic::port::Pin<Analog, T>: AdcChannel<_, _> is not satisfied
Instead of Pin<Analog, T> I should use something like Pin<Analog, PF0> but I don't know how to access PF0.

One thing I struggle with as a beginner in Rust is passing the right type, or even knowing what type something is. This is a nice little function to print out the type, then I copy and paste the output.

fn print_type_of<T>(_: &T) {
    println!("{}", std::any::type_name::<T>())
}

Here's an example of a gpio I pass into a function to be a button:

    let peripherals = Peripherals::take().unwrap();
    let btn = peripherals.pins.gpio6.into_input().unwrap();

fn button_thread(btn: gpio::Gpio6<gpio::Input>, tx: Sender<bool>) {
    let mut btn_state = true;
    loop {
        if btn.is_high().unwrap() {
            if !btn_state {
                btn_state = true;
                tx.send(btn_state).unwrap();
            }
        } else {
            btn_state = false;
        }

        thread::sleep(Duration::from_millis(100));
    }
}

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.