Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
golemparts committed Nov 28, 2024
1 parent 37daa69 commit f112be9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Add support for Raspberry Pi Compute Module 5.
* **Gpio**: Add `set_bias` to `InputPin` (contributed by @KronsyC).
* **Gpio**: Add shared state button example (contributed by @CosminPerRam).
* **Spi**: Fix embedded HAL `SimpleHalSpiDevice` transactions to keep CS low between operations (contributed by @whatisbyandby).
* **Spi**: (Breaking change) Change `SimpleHalSpiDevice::new()` to require an `Spi` instance, instead of a generic HAL bus (contributed by @whatisbyandby).

Expand Down
16 changes: 8 additions & 8 deletions examples/gpio_shared_button_state.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// gpio_shared_button_state.rs - Stops the program until a certain amount of event input changes via
// a non-global shared variable (that can be done using OnceCell for example), this requires a Mutex
// as it goes across threads and Arc to make sure we have the same entry everywhere.
// a non-global shared variable (that can be done using OnceCell for example). This requires a Mutex
// as it goes across threads, and Arc to make sure we have the same entry everywhere.

use rppal::gpio::{Event, Gpio, Trigger};
use std::error::Error;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use rppal::gpio::{Gpio, Event, Trigger};

const INPUT_PIN_GPIO: u8 = 27;
const STOP_AFTER_N_CHANGES: u8 = 5;

// The function we will run upon a Trigger
// The function we will run upon a Trigger.
fn input_callback(event: Event, my_data: Arc<Mutex<u8>>) {
println!("Event: {:?}", event);
*my_data.lock().unwrap() += 1;
Expand All @@ -23,26 +23,26 @@ fn main() -> Result<(), Box<dyn Error>> {
// Configure the input pin.
let mut input_pin = Gpio::new()?.get(INPUT_PIN_GPIO)?.into_input_pulldown();

// We need to clone this as set_async_interrupt will move it and cant be used afterward if so
// We need to clone this as set_async_interrupt will move it, which means it can't be accessed afterwards.
let shared_state_hold = shared_state.clone();
input_pin.set_async_interrupt(
Trigger::FallingEdge,
Some(Duration::from_millis(50)),
move |event| {
// Note: you can also add more parameters here!
// Note: you could add more parameters here!
input_callback(event, shared_state_hold.clone());
},
)?;

// We check constantly if we have reached our number of changes.
// We constantly check if we have reached our number of changes.
loop {
if *shared_state.lock().unwrap() >= STOP_AFTER_N_CHANGES {
// Reached it, exiting the program.
println!("Reached {STOP_AFTER_N_CHANGES} events, exiting...");
break;
}

// Suppose we do some work here that takes a second, the shorter the work takes, the quicker
// Suppose we do some work here that takes a second. The shorter the work takes, the quicker
// we will quit upon reaching our condition.
println!("Still waiting...");
std::thread::sleep(Duration::from_secs(1));
Expand Down

0 comments on commit f112be9

Please sign in to comment.