main.rs 4.72 KB
Newer Older
Ralf's avatar
Ralf committed
1
// FIXME: While we are still prototyping, allow dead code
Ralf's avatar
Ralf committed
2
#![allow(dead_code)]
Constantin Berhard's avatar
logging  
Constantin Berhard committed
3

Constantin Berhard's avatar
rustfmt  
Constantin Berhard committed
4 5
#[macro_use]
extern crate log;
Constantin Berhard's avatar
logging  
Constantin Berhard committed
6

Constantin Berhard's avatar
Constantin Berhard committed
7 8 9
#[macro_use]
extern crate chan;

Constantin Berhard's avatar
logging  
Constantin Berhard committed
10 11
mod logger;

12 13 14
mod util;
mod input;
mod actors;
Ralf's avatar
Ralf committed
15
mod brain;
16

Ralf's avatar
Ralf committed
17
// FIXME everything below here is just a sketch
Ralf's avatar
Ralf committed
18

Constantin Berhard's avatar
rustfmt  
Constantin Berhard committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// struct Commander {
// TODO connection information
// dummy: i32,
// }
//
// impl Commander {
// fn send(statuscode:i32, message:&str) {
// TODO
// }
// }
//
// enum CommandType {
// CmdUnlock,
// CmdLock,
// CmdBuzz,
// CmdFallbackOn,
// CmdFallbackOff,
// }
//
// struct CommandEvent {
// commander: Commander,
// command_type: CommandType,
// }
//
// enum Event {
// Command(CommandEvent)
// }
Ralf's avatar
Ralf committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
//
// 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
// }
// }
Constantin Berhard's avatar
rustfmt  
Constantin Berhard committed
199 200 201 202 203
// fn sphinx (old_state: &State, new_state: &State) -> i32 {
// match new_state {
// (is_buzzing:_;is_door_closed:_;is_door_locked:_;is_switch_on:_) => 0
// }
// }
Ralf's avatar
Ralf committed
204
//
Constantin Berhard's avatar
Constantin Berhard committed
205 206

fn main() {
Ralf's avatar
Ralf committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    //    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();
Constantin Berhard's avatar
Constantin Berhard committed
223
}