4 changed files with 172 additions and 36 deletions
@ -1,16 +1,6 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="CargoProjects"> |
|||
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" /> |
|||
</component> |
|||
<component name="JavaScriptSettings"> |
|||
<option name="languageLevel" value="ES6" /> |
|||
</component> |
|||
<component name="MacroExpansionManager"> |
|||
<option name="directoryName" value="lbsqolEk" /> |
|||
</component> |
|||
<component name="RustProjectSettings"> |
|||
<option name="toolchainHomeDirectory" value="/usr/bin" /> |
|||
<option name="version" value="2" /> |
|||
</component> |
|||
</project> |
|||
@ -0,0 +1,107 @@ |
|||
use std::vec::Vec; |
|||
use std::string::String; |
|||
|
|||
struct Sevensegment { |
|||
segments: Vec<bool>, |
|||
} |
|||
|
|||
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<String> { |
|||
let mut instructions:Vec<String> = 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 |
|||
} |
|||
} |
|||
|
|||
@ -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()); |
|||
} |
|||
|
|||
Loading…
Reference in new issue