Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Pin configuration change in loop #269

Closed
holotrack opened this issue May 12, 2022 · 6 comments
Closed

Pin configuration change in loop #269

holotrack opened this issue May 12, 2022 · 6 comments
Labels
question Further information is requested

Comments

@holotrack
Copy link

holotrack commented May 12, 2022

I need to switch pin configuration "on the fly" how to do that with this library? Standard in rust is what i read is something like that:

loop {
    let mut eled = pin!(pins, dig9).into_output();

    eled.set_low().unwrap();
    sleep.delay_ms(20);
    eled.set_high().unwrap();
    sleep.delay_ms(35);
    sprintln!("sent");

    let mut eled_input = eled.into_floating_input()

    while eled_input.is_high().unwrap() {};
    while eled_input.is_low().unwrap() {};
    while eled_input.is_high().unwrap() {};

}

But when i try it with this library im getting on eled.into_input(); "no such method". How it should be handled here?

@Rahix
Copy link
Owner

Rahix commented May 12, 2022

The methods are into_floating_input() and into_pull_up_input(), check the docs: https://rahix.github.io/avr-hal/avr_hal_generic/port/struct.Pin.html

But we're working on a more ergonomic way to do this, take a look at MR #205. If this fits your usecase and you can provide some feedback on the API, I can merge those changes so they become generally available.

@Rahix Rahix added the question Further information is requested label May 12, 2022
@holotrack
Copy link
Author

holotrack commented May 13, 2022

Sorry but not sure which should i use. Should i use solution from link or i should try to do something like this:

#[arduino_hal::entry]
fn main() -> ! {
    
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);
    let mut led = pins.d12.into_opendrain_high();

    loop {
        

        led.set_low();
        arduino_hal::delay_ms(20);
        led.set_high();
        arduino_hal::delay_ms(35);

        let mut ledin = led.into_pull_up_input();

        while ledin.is_high() {};

        let mut led = ledin.into_opendrain_high();

    }
}

what i see under this link this convention should work for rust or am i missing something?

@Rahix
Copy link
Owner

Rahix commented May 13, 2022

Both work. I would recommend using the PR #205 code I linked as this API is more flexible and can work in more situations.

But the code which you shared is not technically wrong either, it is just more restricted in what you can do with it.

@holotrack
Copy link
Author

First i want to test first solution but when im trying to compile it im getting:

error[E0382]: borrow of moved value: `led`
  --> src/main.rs:16:9
   |
11 |     let mut led = pins.d12.into_opendrain_high();
   |         ------- move occurs because `led` has type `avr_hal_generic::port::Pin<OpenDrain, PB4>`, which does not implement the `Copy` trait
...
16 |         led.set_low();
   |         ^^^ value borrowed here after move
...
21 |         let mut ledin = led.into_pull_up_input();
   |                         --- value moved here, in previous iteration of loop

error: aborting due to previous error; 3 warnings emitted

Some problem with borrow checker, but im creating everytime in loop new value so why it doesnt want to switch?

@Rahix
Copy link
Owner

Rahix commented May 13, 2022

Your last line is wrong I think. It needs to be just

led = ledin.into_opendrain_high();

(without let mut)

@holotrack
Copy link
Author

greate now its compiling, thank you. closing ticket, when i will have problem with second solution will update its ticket

Repository owner locked and limited conversation to collaborators Sep 2, 2022
@Rahix Rahix converted this issue into discussion #311 Sep 2, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants