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 authored and joaoantoniocardoso committed Sep 16, 2023
1 parent c58c0f3 commit 3d7a020
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions 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 @@ -27,8 +27,31 @@ fn main() {
})
.expect("Failed to spawn the sensor reader thread");

// This code block could be also other thread, but just run on the main one.
// This could work as a server get handler, or an instance that can be used by different services to monitor and analysis.
// 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_data_cloned = sensor_data.clone();
let nav_cloned = nav.clone();
thread::Builder::new()
.name("Flip monitor".into())
.spawn(move || loop {
if let Ok(sensor_data) = sensor_data_cloned.read() {
if sensor_data.accelerometer.x.abs() > 8.00 {
nav_cloned.lock().unwrap().set_led(UserLed::Led1, true)
};
if sensor_data.accelerometer.y.abs() > 8.00 {
nav_cloned.lock().unwrap().set_led(UserLed::Led2, true)
};
if sensor_data.accelerometer.z < -8.00 {
nav_cloned.lock().unwrap().set_led(UserLed::Led3, true)
};
}
sleep(Duration::from_millis(5000));
})
.expect("Failed to spawn the flip monitor thread");

// This code block could also be another thread, but we are going to run in the main one.
// This could work as a server get handler, or an instance that could be used by different services for monitoring and analysis.
loop {
if let Ok(sensor_data) = sensor_data.read() {
println!("Read SensorData: {sensor_data:?}");
Expand Down

0 comments on commit 3d7a020

Please sign in to comment.