diff --git a/.idea/misc.xml b/.idea/misc.xml index 90d8454..28a804d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,16 +1,6 @@ - - - - - - - \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index e753f38..8278a75 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "gpio" version = "0.4.1" diff --git a/src/main.rs b/src/main.rs index 360b318..06983a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ use std::net::{TcpStream, TcpListener}; use std::io::prelude::*; use std::io::{BufReader, Result}; +//use crate::protocol::Protocol; mod protocol; + fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result<()> { let mut outstream = stream.try_clone()?; let mut reader = BufReader::new(stream); @@ -13,19 +15,36 @@ fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result prot.start(); 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!"); + let len = match reader.read_line(&mut text) { + Ok(len) => len, + Err(_) => { + println!("Reading line from client failed!"); + 0 + }, + }; + if len == 0 { break; } println!("read: {}", text); match prot.parse(&text) { Ok(response) => { - writeln!(outstream,"{}",response).expect("could not write to TCP Stream!"); - //stream.write_line(response.as_bytes()); + match writeln!(outstream,"{}",response) { + Ok(_) => (), + Err(_) => { + prot.stop(); + println!("Write to TCP Stream failed") + }, + } }, Err(e) => { - writeln!(outstream,"{}",e).expect("could not write to TCP Stream!"); - //stream.write_line(e.as_bytes()); + match writeln!(outstream,"{}",e) { + Ok(_) => (), + Err(_) => { + prot.stop(); + println!("Write to TCP Stream failed") + }, + } } } text = String::from(""); @@ -39,7 +58,9 @@ fn main() -> Result<()> { let listener = TcpListener::bind(address).expect("could not start server"); let mut prot = protocol::Protocol::new(); println!("Initialize GPIO"); - prot.init(); + if protocol::RPI { + prot.init(); + } println!("Ready"); // accept connections and get a TcpStream for connection in listener.incoming() { diff --git a/src/protocol.rs b/src/protocol.rs index 99e4cd3..3646a06 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -2,6 +2,8 @@ extern crate gpio; use gpio::GpioOut; +pub static RPI: bool = true; + struct Segment { nextval: bool, port: gpio::sysfs::SysFsGpioOutput, @@ -26,7 +28,6 @@ impl Segment { } } - pub struct Protocol { segments: Vec, running: bool, @@ -49,13 +50,16 @@ impl Protocol { self.segments.push(Segment::new(21)); self.segments.push(Segment::new(20)); self.segments.push(Segment::new(16)); - } pub fn start(&mut self) { self.running = true; } + pub fn stop(&mut self) { + self.running = false; + } + pub fn is_running(&self) -> bool{ self.running } @@ -87,17 +91,24 @@ impl Protocol { "off" => false, _ => return Err("ERR no value for segment") }; - self.segments[index].set_nextval(value); + if RPI { + self.segments[index].set_nextval(value); + } else { + println!("segment {} = {}", index, value); + } } "display" => { //show the saved state on the display - for i in 0 .. 8 { - match self.segments[i].show() { - Ok(_) => (), - Err(e) => println!("{}", e), - + if RPI { + for i in 0..8 { + match self.segments[i].show() { + Ok(_) => (), + Err(e) => println!("{}", e), + } } + } else { + println!("showing segments") } println!(); }