Commit dcf452fd authored by Ralf's avatar Ralf

comment out unused code in main module

parent 6eb2f4e1
......@@ -46,180 +46,181 @@ mod brain;
// enum Event {
// Command(CommandEvent)
// }
struct State {
is_buzzing: Option<bool>,
is_door_closed: Option<bool>,
is_door_locked: Option<bool>,
is_switch_on: Option<bool>,
}
impl State {
pub fn new() -> State {
State {
is_buzzing: None,
is_door_closed: None,
is_door_locked: None,
is_switch_on: None,
}
}
}
#[derive(PartialEq,Eq)]
enum ShouldThreadRun {
ContinueRunning,
Terminate,
}
struct SleepInfo {
data: Mutex<ShouldThreadRun>,
notifyer: Condvar,
}
impl SleepInfo {
fn new_arc() -> Arc<SleepInfo> {
Arc::new(SleepInfo {
data: Mutex::new(ShouldThreadRun::ContinueRunning),
notifyer: Condvar::new(),
})
}
}
struct ActorThread<F>
where F: FnOnce(ActorThread<F>) -> Actor + Send
{
f: Option<F>,
sleep_info: Arc<SleepInfo>,
}
impl<F> ActorThread<F>
where F: FnOnce(ActorThread<F>) -> Actor + Send + 'static
{
fn new(f: F, sleep_info: Arc<SleepInfo>) -> ActorThread<F> {
ActorThread {
f: Some(f),
sleep_info: sleep_info,
}
}
fn is_started(&self) -> bool {
self.f.is_none()
}
fn spawn(mut self) -> Result<JoinHandle<Actor>, ()> {
if let Some(f) = self.f {
self.f = None;
Ok(thread::spawn(move || f(self)))
} else {
Err(())
}
}
fn sleep(&mut self, dur: Duration) -> WaitTimeoutResult {
// unwrap is okay because another thread must have already panicked for an
// error value to be returned here
let sleep_result = self.sleep_info
.notifyer
.wait_timeout(self.sleep_info.data.lock().unwrap(), dur)
.unwrap();
sleep_result.1
}
fn should_terminate(&self) -> bool {
*(self.sleep_info.data.lock().unwrap()) == ShouldThreadRun::Terminate
}
}
struct SphinxHead {
state: State,
}
impl SphinxHead {
fn new() -> SphinxHead {
SphinxHead { state: State::new() }
}
fn run(&mut self) {
let mut red_led = Actor::new(10, false);
let sleep_info = SleepInfo::new_arc();
let blinker = ActorThread::new(move |mut s| {
debug_assert!(s.is_started());
while !s.should_terminate() {
red_led.flip();
println!("waiting in thread");
let sleep_result =
s.sleep(Duration::from_millis(500));
println!("waiting in thread done");
println!("timeout has elapsed: {}",
sleep_result.timed_out());
}
red_led
},
sleep_info.clone())
.spawn()
.unwrap();
thread::sleep_ms(1750);
{
let mut endthread = sleep_info.data.lock().unwrap();
*endthread = ShouldThreadRun::Terminate;
sleep_info.notifyer.notify_one();
} // block is important so the lock is released again
println!("joining...");
red_led = blinker.join().unwrap();
println!("joined");
red_led.flip();
}
}
struct Actor {
pin: u32,
last_set_to: bool, // TODO string name for debugging
}
impl Actor {
pub fn new(pin: u32, initial_state: bool) -> Actor {
let mut ret = Actor {
pin: pin,
last_set_to: !initial_state,
};
ret.set(initial_state);
ret
}
pub fn set(&mut self, value: bool) {
if self.last_set_to != value {
self.force_set(value);
} else {
println!("GPIO was already {}", value);
}
}
pub fn force_set(&mut self, value: bool) {
// TODO GPIO stuff
println!("GPIO set to {}", value);
self.last_set_to = value;
}
pub fn flip(&mut self) -> bool {
let new_value = !self.last_set_to;
self.set(new_value);
debug_assert_eq!(new_value, self.last_set_to);
new_value
}
}
//
// struct State {
// is_buzzing: Option<bool>,
// is_door_closed: Option<bool>,
// is_door_locked: Option<bool>,
// is_switch_on: Option<bool>,
// }
//
// impl State {
// pub fn new() -> State {
// State {
// is_buzzing: None,
// is_door_closed: None,
// is_door_locked: None,
// is_switch_on: None,
// }
// }
// }
//
// #[derive(PartialEq,Eq)]
// enum ShouldThreadRun {
// ContinueRunning,
// Terminate,
// }
//
// struct SleepInfo {
// data: Mutex<ShouldThreadRun>,
// notifyer: Condvar,
// }
//
// impl SleepInfo {
// fn new_arc() -> Arc<SleepInfo> {
// Arc::new(SleepInfo {
// data: Mutex::new(ShouldThreadRun::ContinueRunning),
// notifyer: Condvar::new(),
// })
// }
// }
//
// struct ActorThread<F>
// where F: FnOnce(ActorThread<F>) -> Actor + Send
// {
// f: Option<F>,
// sleep_info: Arc<SleepInfo>,
// }
//
// impl<F> ActorThread<F>
// where F: FnOnce(ActorThread<F>) -> Actor + Send + 'static
// {
// fn new(f: F, sleep_info: Arc<SleepInfo>) -> ActorThread<F> {
// ActorThread {
// f: Some(f),
// sleep_info: sleep_info,
// }
// }
// fn is_started(&self) -> bool {
// self.f.is_none()
// }
// fn spawn(mut self) -> Result<JoinHandle<Actor>, ()> {
// if let Some(f) = self.f {
// self.f = None;
// Ok(thread::spawn(move || f(self)))
// } else {
// Err(())
// }
// }
// fn sleep(&mut self, dur: Duration) -> WaitTimeoutResult {
// unwrap is okay because another thread must have already panicked for an
// error value to be returned here
// let sleep_result = self.sleep_info
// .notifyer
// .wait_timeout(self.sleep_info.data.lock().unwrap(), dur)
// .unwrap();
// sleep_result.1
// }
// fn should_terminate(&self) -> bool {
// (self.sleep_info.data.lock().unwrap()) == ShouldThreadRun::Terminate
// }
// }
//
// struct SphinxHead {
// state: State,
// }
//
// impl SphinxHead {
// fn new() -> SphinxHead {
// SphinxHead { state: State::new() }
// }
// fn run(&mut self) {
// let mut red_led = Actor::new(10, false);
// let sleep_info = SleepInfo::new_arc();
// let blinker = ActorThread::new(move |mut s| {
// debug_assert!(s.is_started());
// while !s.should_terminate() {
// red_led.flip();
// println!("waiting in thread");
// let sleep_result =
// s.sleep(Duration::from_millis(500));
// println!("waiting in thread done");
// println!("timeout has elapsed: {}",
// sleep_result.timed_out());
// }
// red_led
// },
// sleep_info.clone())
// .spawn()
// .unwrap();
// thread::sleep_ms(1750);
// {
// let mut endthread = sleep_info.data.lock().unwrap();
// endthread = ShouldThreadRun::Terminate;
// sleep_info.notifyer.notify_one();
// } // block is important so the lock is released again
// println!("joining...");
// red_led = blinker.join().unwrap();
// println!("joined");
// red_led.flip();
// }
// }
//
// struct Actor {
// pin: u32,
// last_set_to: bool, // TODO string name for debugging
// }
//
// impl Actor {
// pub fn new(pin: u32, initial_state: bool) -> Actor {
// let mut ret = Actor {
// pin: pin,
// last_set_to: !initial_state,
// };
// ret.set(initial_state);
// ret
// }
// pub fn set(&mut self, value: bool) {
// if self.last_set_to != value {
// self.force_set(value);
// } else {
// println!("GPIO was already {}", value);
// }
// }
// pub fn force_set(&mut self, value: bool) {
// TODO GPIO stuff
// println!("GPIO set to {}", value);
// self.last_set_to = value;
// }
// pub fn flip(&mut self) -> bool {
// let new_value = !self.last_set_to;
// self.set(new_value);
// debug_assert_eq!(new_value, self.last_set_to);
// new_value
// }
// }
// fn sphinx (old_state: &State, new_state: &State) -> i32 {
// match new_state {
// (is_buzzing:_;is_door_closed:_;is_door_locked:_;is_switch_on:_) => 0
// }
// }
//
fn main() {
match logger::init() {
Ok(_) => (),
Err(e) => {
let mut stderr = std::io::stderr();
writeln!(&mut stderr,
"Logger initialization failed: {}\nThere will be no logging.",
e)
.unwrap_or_else(|_| {
println!("Logger init failed AND printing to stderr doesn't work. Seems like \
we're all out of luck today :-(");
});
}
}
debug!("Logging initialised");
let mut sphinx_head = SphinxHead::new();
sphinx_head.run();
// match logger::init() {
// Ok(_) => (),
// Err(e) => {
// let mut stderr = std::io::stderr();
// writeln!(&mut stderr,
// "Logger initialization failed: {}\nThere will be no logging.",
// e)
// .unwrap_or_else(|_| {
// println!("Logger init failed AND printing to stderr doesn't work. Seems like \
// we're all out of luck today :-(");
// });
// }
// }
// debug!("Logging initialised");
// let mut sphinx_head = SphinxHead::new();
// sphinx_head.run();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment