Skip to content

Commit

Permalink
examples: raspberry-pi-thread: Add a thread service example that moni…
Browse files Browse the repository at this point in the history
…tors position and hold detections on UserLed
  • Loading branch information
RaulTrombin committed Sep 15, 2023
1 parent 644f2c6 commit 6f3bd5b
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion examples/raspberry-pi-threads.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use navigator_rs::Navigator;
use navigator_rs::{Navigator, UserLed};
use std::sync::{Arc, Mutex, RwLock};
use std::thread::{self, sleep};
use std::time::Duration;
Expand Down Expand Up @@ -26,6 +26,26 @@ fn main() {
}
});

// This code block creates a thread that updates the `UserLed` according to its position, based on gravity.
// It will keep the LED state on, indicating that the navigator has fully flipped to axes X and Y or turned upside down (Z-axis).
// You can use it to also monitor abrupt collisions!
let sensor_update_thread_lock = sensor_data_lock.clone();
let navigator_lock = nav.clone();
let _ = thread::spawn(move || loop {
if let Ok(lock) = sensor_update_thread_lock.read() {
if lock.accelerometer.x.abs() > 8.00 {
navigator_lock.lock().unwrap().set_led(UserLed::Led1, true)
};
if lock.accelerometer.y.abs() > 8.00 {
navigator_lock.lock().unwrap().set_led(UserLed::Led2, true)
};
if lock.accelerometer.z < -8.00 {
navigator_lock.lock().unwrap().set_led(UserLed::Led3, true)
};
}
sleep(Duration::from_millis(10000));
});

// This code block could be also other thread, but just run on the main one.
// This could could work as a server get handler, or a instance that can be used by different services to monitor and analysis.
loop {
Expand Down

0 comments on commit 6f3bd5b

Please sign in to comment.