Another suggestion, you can remove all the "return" key words of the button_pressed function (don't forget to remove also semicolon). It is more idiomatic Rust, but also less readable for the new comer.
fn buttonpressed(but: &PinDriver<', Gpio3, Input>, del: &u32) -> u32 { // Check if Button has been pressed // If not pressed, return the delay value unchanged if but.is_low() { // if the value of the delay passed is less of equal to 50 then reset it to initial value // else subtract 50 from the passed delay println!("Button Pressed!"); if del <= &50_u32 { 200_u32 } else { 50_u32 } } else { *del } }
I think you should use an array instead of declaring 10 variables (one variable by pin!). Then you will be able to iterate the array with a for loop.
// Configure all LED pins to digital outputs let mut leds = [ PinDriver::output(dp.pins.gpio1.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio10.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio19.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio18.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio4.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio5.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio6.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio7.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio8.downgrade_output()).unwrap(), PinDriver::output(dp.pins.gpio9.downgrade_output()).unwrap(), ];
// Put the following code in the loop for mut led in &mut leds { // LED i led.set_high().unwrap(); blinkdelay = button_pressed(&button, &blinkdelay); FreeRtos::delay_ms(blinkdelay); led.set_low().unwrap(); FreeRtos::delay_ms(100_u32); }
// I have tested the result on wokwi
Igor Cavalcante
Backend Software Crafter
Great article. I love rust and electronic, but I never found a way to use rust with microcontrolers without a lot of boilerplate. I will try this approach. ty