diff --git a/Cargo.lock b/Cargo.lock index 0f8ee74..e753f38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,8 @@ +[[package]] +name = "gpio" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "1.3.0" @@ -12,6 +17,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "noobctf-server" version = "0.1.0" dependencies = [ + "gpio 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -25,6 +31,7 @@ dependencies = [ ] [metadata] +"checksum gpio 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4fe6783270536547ac473c9d2ae5a7e0e715ea43f29004ced47fbd1c1372d2c7" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917" "checksum rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "47fa3e09d34f2992f47f3d4315f8ee8cfffc1fe1912d4d08258b66cc18dc3b00" diff --git a/Cargo.toml b/Cargo.toml index 3fbdbc1..11d9bae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ authors = ["Michael Preisach "] edition = "2018" [dependencies] -rppal = "0.11.1" \ No newline at end of file +rppal = "0.11.1" +gpio = "0.4.1" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 388d420..ee5152e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,14 +5,14 @@ use std::io::{BufReader, Result}; mod protocol; -fn handle_connection(mut stream: TcpStream) -> Result<()> { +fn handle_connection(stream: TcpStream) -> Result<()> { let mut outstream = stream.try_clone()?; let mut reader = BufReader::new(stream); let mut text = String::new(); let mut prot = protocol::Protocol::new(); prot.start(); - write!(outstream,"Hello to 7seg-writer!\n"); + write!(outstream,"Hello to 7seg-writer!\n").expect("could not write to TCP Stream!"); while prot.is_running() { let len = reader.read_line(&mut text).expect("Reading line from client failed!"); if len == 0 { @@ -21,11 +21,11 @@ fn handle_connection(mut stream: TcpStream) -> Result<()> { println!("read: {}", text); match prot.parse(&text) { Ok(response) => { - writeln!(outstream,"{}",response); + writeln!(outstream,"{}",response).expect("could not write to TCP Stream!"); //stream.write_line(response.as_bytes()); }, Err(e) => { - writeln!(outstream,"{}",e); + writeln!(outstream,"{}",e).expect("could not write to TCP Stream!"); //stream.write_line(e.as_bytes()); } } diff --git a/src/protocol.rs b/src/protocol.rs index 9bc78d0..e20cd11 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -1,17 +1,54 @@ +extern crate gpio; + +use gpio::GpioOut; + +struct Segment { + nextval: bool, + port: gpio::sysfs::SysFsGpioOutput, +} + +impl Segment { + pub fn new(portnum: u16) -> Segment { + Segment { + nextval: false, + port: gpio::sysfs::SysFsGpioOutput::open(portnum) + .expect("Startup failed, could not open all GPIOs"), + } + } + pub fn set_nextval(&mut self, value: bool) { + self.nextval = value; + } + pub fn show(&mut self) -> Result<(), &'static str> { + match self.port.set_value(self.nextval) { + Ok(_) => Ok(()), + Err(_) => Err("Could not write to GPIO port"), + } + } +} + + pub struct Protocol { - segments: Vec, + segments: Vec, running: bool, } impl Protocol { pub fn new() -> Protocol { Protocol { - segments: [false, false, false, false, false, false, false, false].to_vec(), + segments: Vec::with_capacity(8), running: false, } } pub fn start(&mut self) { + self.segments[0] = Segment::new(5); + self.segments[1] = Segment::new(6); + self.segments[2] = Segment::new(13); + self.segments[3] = Segment::new(19); + self.segments[4] = Segment::new(26); + self.segments[5] = Segment::new(21); + self.segments[6] = Segment::new(20); + self.segments[7] = Segment::new(16); self.running = true; } @@ -19,39 +56,44 @@ impl Protocol { self.running } - pub fn get_segment(&self, index: usize) -> Result { - if index < 8 && index >= 0 { - Ok(self.segments[index]) - } else { - Err("get_segment: Index out of bounds") - } - } - pub fn parse(&mut self, input : &str) -> Result<&'static str, &'static str> { let arguments:Vec<&str> = input.split_whitespace().collect(); if arguments.len() < 1 { self.running = false; - return Err("No argument found!"); + return Err("ERR Nmissing arguments"); } match arguments[0] { "setsegment" => { if arguments.len() != 3 { self.running = false; - return Err("Wrong number of arguments!"); + return Err("ERR Wrong number of arguments"); } - let index = arguments[1].parse::().unwrap(); + let index = match arguments[1].parse::() { + Ok(value) => { + if value < 8 { + return Err("ERR index out of bounds"); + } else { + value + } + }, + Err(_e) => return Err("ERR index not a number") + }; let value = match arguments[2] { "on" => true, "off" => false, - _ => self.segments[index], + _ => return Err("ERR no value for segment") }; - self.segments[index] = value; + self.segments[index].set_nextval(value); } "display" => { //show the saved state on the display - print!("Showing state on display: "); - for seg in self.segments.clone() { - print!("{}, ", seg); + + for i in 0 .. 7 { + match self.segments[i].show() { + Ok(_) => (), + Err(e) => println!("{}", e), + + } } println!(); } @@ -61,9 +103,7 @@ impl Protocol { self.running = false; } _ => { - println!("Err: Command not found, closing connection!"); - self.running = false; - return Err("Command not found!"); + return Err("ERR Command not found!"); } } Ok("OK")