diff --git a/.idea/misc.xml b/.idea/misc.xml index 5f6c4c2..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 f8af725..4174b8b 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 = "untitled" version = "0.1.0" diff --git a/src/encoder.rs b/src/encoder.rs new file mode 100644 index 0000000..74c1ced --- /dev/null +++ b/src/encoder.rs @@ -0,0 +1,107 @@ +use std::vec::Vec; +use std::string::String; + +struct Sevensegment { + segments: Vec, +} + +impl Sevensegment { + pub fn new() -> Sevensegment { + Sevensegment { + segments: [false, false, false, false, false, false, false, false].to_vec(), + } + } + +// --a-- +// | | +// f b +// | | +// --g-- +// | | +// e c +// | | +// --d-- h + + pub fn encode(&mut self, val: char) { + self.segments = match val { + '0' => [true, true, true, true, true, true, false, false].to_vec(), + '1' => [false, true, true, false, false, false, false, false].to_vec(), + '2' => [true, true, false, true, true, false, true, false].to_vec(), + '3' => [true, true, true, true, false, false, true, false].to_vec(), + '4' => [false, true, true, false, false, true, true, false].to_vec(), + '5' => [true, false, true, true, false, true, true, false].to_vec(), + '6' => [true, false, true, true, true, true, true, false].to_vec(), + '7' => [true, true, true, false, false, false, false, false].to_vec(), + '8' => [true, true, true, true, true, true, true, false].to_vec(), + '9' => [true, true, true, true, false, true, true, false].to_vec(), + 'a'|'A' => [true, true, true, false, true, true, true, false].to_vec(), + 'b'|'B' => [false, false, true, true, true, true, true, false].to_vec(), + 'c'|'C' => [true, false, false, true, true, true, false, false].to_vec(), + 'd'|'D' => [false, true, true, true, true, false, true, false].to_vec(), + 'e'|'E' => [true, false, false, true, true, true, true, false].to_vec(), + 'f'|'F' => [true, false, false, false, true, true, true, false].to_vec(), + 'h'|'H' => [false, true, true, false, true, true, true, false].to_vec(), + 'i'|'I' => [false, false, true, false, false, false, false, false].to_vec(), + 'j'|'J' => [false, true, true, true, false, false, false, false].to_vec(), + 'l'|'L' => [false, false, false, true, true, true, false, false].to_vec(), + 'n'|'N' => [false, false, true, false, true, false, true, false].to_vec(), + 'o'|'O' => [false, false, true, true, true, false, true, false].to_vec(), + 'p'|'P' => [true, true, false, false, true, true, true, false].to_vec(), + 't'|'T' => [false, false, false, true, true, true, true, false].to_vec(), + 'u'|'U' => [false, false, true, true, true, false, false, false].to_vec(), + 'y'|'Y' => [false, true, true, true, false, true, true, false].to_vec(), + _ => [false, false, false, false, false, false, false, false].to_vec(), + }; + } + + pub fn set_segment(&mut self, index: usize, val: bool) { + if index < self.segments.len() { + self.segments[index] = val; + } else { + panic!("index out of bounds!"); + } + } + + pub fn get_segment(&self, index: usize) -> bool { + if index < self.segments.len() { + self.segments[index] + } else { + panic!("index out of bounds!") + } + } +} + +pub struct Encodestate { + current: Sevensegment, + next: Sevensegment, +} + +impl Encodestate { + pub fn new() -> Encodestate { + Encodestate { + current: Sevensegment::new(), + next: Sevensegment::new(), + } + } + + pub fn encode_next_char(&mut self, c: char) -> Vec { + let mut instructions:Vec = Vec::new(); + self.next.encode(c); + for i in 0 .. 8 { + if self.current.get_segment(i) != self.next.get_segment(i) { + let mut text = String::new(); + text.push_str("setsegment "); + text.push(std::char::from_u32(('0' as u32) + i as u32).unwrap()); + text.push(' '); + text.push_str(match self.next.get_segment(i) { + true => "on", + false => "off", + }); + self.current.set_segment(i, self.next.get_segment(i)); + instructions.push(text); + } + } + instructions + } +} + diff --git a/src/main.rs b/src/main.rs index 38d98ab..a5a890f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,36 +1,73 @@ -use std::net::{TcpStream, TcpListener,SocketAddr}; +use std::net::{TcpStream}; use std::io::prelude::*; -use std::io; +use std::io::{BufReader}; use std::time::{Duration}; use std::thread; +mod encoder; + +use encoder::Encodestate; + +pub static DEBUG: bool = false; + fn main() { let address = "192.168.1.77:8000"; - let mut text = String::new(); - let mut stream: TcpStream = TcpStream::connect(address).expect("connection failed"); + let stream: TcpStream = TcpStream::connect(address).expect("connection failed"); let mut outstream = stream.try_clone().unwrap(); let mut reader = BufReader::new(stream); + let mut instructions; + let input = String::from("0123456789aBcDeFhijlnOpTuY"); + + let mut state = Encodestate::new(); + + let mut headline = String::new(); + reader.read_line(&mut headline).expect("Reading line failed"); + println!("{}", headline); + + for c in input.chars() { + instructions = state.encode_next_char(c); + for inst in instructions { + let mut response = String::new(); + if DEBUG { + print!("{} ",inst); + } + writeln!(outstream, "{}", inst).expect("write failed"); + reader.read_line(&mut response).expect("Reading line failed"); + if DEBUG { + println!("{} ",response); + } + if !response.trim_end().contains("OK") { + return; + } + } + let mut response = String::new(); + let inst = String::from("display"); + if DEBUG { + print!("{} ",inst); + } + writeln!(outstream, "{}", inst).expect("write failed"); + reader.read_line(&mut response).expect("Reading line failed"); + if DEBUG { + println!("{}", response); + } + if !response.trim_end().contains("OK") { + return; + } + //thread::sleep(Duration::from_millis(500)); + } + let mut response = String::new(); + let inst = String::from("exit"); + if DEBUG { + print!("{} ",inst); + } + writeln!(outstream, "{}", inst).expect("write failed"); + reader.read_line(&mut response).expect("Reading line failed"); + if DEBUG { + println!("{}", response); + } + + if !response.trim_end().contains("OK") { + return; + } - write!(outstream,"setsegment 3 on\n").expect("write failed"); - stream.read_to_string(&mut text).expect("read failed"); - println!("received: '{}'", text.trim_end()); - write!(outstream,"setsegment 4 on\n").expect("write failed"); - stream.read_to_string(&mut text).expect("read failed"); - println!("received: '{}'", text.trim_end()); - write!(outstream,"setsegment 5 on\n").expect("write failed"); - stream.read_to_string(&mut text).expect("read failed"); - println!("received: '{}'", text.trim_end()); - write!(outstream,"display\n").expect("write failed"); - stream.read_to_string(&mut text).expect("read failed"); - println!("received: '{}'", text.trim_end()); - thread::sleep(Duration::from_millis(1000)); - write!(outstream,"setsegment 0 on\n").expect("write failed"); - stream.read_to_string(&mut text).expect("read failed"); - println!("received: '{}'", text.trim_end()); - write!(outstream,"setsegment 6 on\n").expect("write failed"); - stream.read_to_string(&mut text).expect("read failed"); - println!("received: '{}'", text.trim_end()); - write!(outstream,"display\n").expect("write failed"); - stream.read_to_string(&mut text).expect("read failed"); - println!("received: '{}'", text.trim_end()); }