From 3d7a0205c02dde1614cdcfb93ea10e880701a8c3 Mon Sep 17 00:00:00 2001 From: Raul Victor Trombin Date: Fri, 15 Sep 2023 19:28:02 -0300 Subject: [PATCH] examples: raspberry-pi-thread: Add a thread service example that monitors position and hold detections on UserLed --- examples/raspberry-pi-threads.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/examples/raspberry-pi-threads.rs b/examples/raspberry-pi-threads.rs index 063c7ee011..b8202e465c 100644 --- a/examples/raspberry-pi-threads.rs +++ b/examples/raspberry-pi-threads.rs @@ -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; @@ -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:?}");