Bouncy ball example

This commit is contained in:
Luke
2020-05-06 12:31:58 +12:00
parent 497ba61d22
commit e09dd345c0
14 changed files with 277 additions and 85 deletions

View File

@@ -3,14 +3,17 @@ use dbus::blocking::BlockingSender;
use dbus::channel::Sender;
use dbus::{blocking::Connection, Message};
use std::error::Error;
use std::sync::{Arc, Mutex};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::{thread, time::Duration};
/// Simplified way to write a effect block
pub struct AuraDbusWriter {
connection: Box<Connection>,
block_time: u64,
stop: Arc<Mutex<bool>>,
stop: Arc<AtomicBool>,
}
impl AuraDbusWriter {
@@ -20,7 +23,7 @@ impl AuraDbusWriter {
Ok(AuraDbusWriter {
connection: Box::new(connection),
block_time: 10,
stop: Arc::new(Mutex::new(false)),
stop: Arc::new(AtomicBool::new(false)),
})
}
@@ -35,9 +38,7 @@ impl AuraDbusWriter {
println!("GOT {:?}", msg);
if let Ok(stop) = msg.read1::<bool>() {
if stop {
if let Ok(mut lock) = stopper.lock() {
*lock = true;
}
stopper.store(true, Ordering::Relaxed);
}
}
true
@@ -75,10 +76,8 @@ impl AuraDbusWriter {
.append1(&group[10].to_vec());
self.connection.send(msg).unwrap();
thread::sleep(Duration::from_millis(self.block_time));
if let Ok(lock) = self.stop.try_lock() {
if *lock {
panic!("Go signal to stop!");
}
if self.stop.load(Ordering::Relaxed) {
panic!("Go signal to stop!");
}
Ok(())
}