|
|
@ -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 { |
|
|
pub struct Protocol { |
|
|
segments: Vec<bool>, |
|
|
segments: Vec<Segment>, |
|
|
running: bool, |
|
|
running: bool, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl Protocol { |
|
|
impl Protocol { |
|
|
pub fn new() -> Protocol { |
|
|
pub fn new() -> Protocol { |
|
|
Protocol { |
|
|
Protocol { |
|
|
segments: [false, false, false, false, false, false, false, false].to_vec(), |
|
|
segments: Vec::with_capacity(8), |
|
|
running: false, |
|
|
running: false, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn start(&mut self) { |
|
|
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; |
|
|
self.running = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -19,39 +56,44 @@ impl Protocol { |
|
|
self.running |
|
|
self.running |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn get_segment(&self, index: usize) -> Result<bool, &'static str> { |
|
|
|
|
|
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> { |
|
|
pub fn parse(&mut self, input : &str) -> Result<&'static str, &'static str> { |
|
|
let arguments:Vec<&str> = input.split_whitespace().collect(); |
|
|
let arguments:Vec<&str> = input.split_whitespace().collect(); |
|
|
if arguments.len() < 1 { |
|
|
if arguments.len() < 1 { |
|
|
self.running = false; |
|
|
self.running = false; |
|
|
return Err("No argument found!"); |
|
|
return Err("ERR Nmissing arguments"); |
|
|
} |
|
|
} |
|
|
match arguments[0] { |
|
|
match arguments[0] { |
|
|
"setsegment" => { |
|
|
"setsegment" => { |
|
|
if arguments.len() != 3 { |
|
|
if arguments.len() != 3 { |
|
|
self.running = false; |
|
|
self.running = false; |
|
|
return Err("Wrong number of arguments!"); |
|
|
return Err("ERR Wrong number of arguments"); |
|
|
|
|
|
} |
|
|
|
|
|
let index = match arguments[1].parse::<usize>() { |
|
|
|
|
|
Ok(value) => { |
|
|
|
|
|
if value < 8 { |
|
|
|
|
|
return Err("ERR index out of bounds"); |
|
|
|
|
|
} else { |
|
|
|
|
|
value |
|
|
} |
|
|
} |
|
|
let index = arguments[1].parse::<usize>().unwrap(); |
|
|
}, |
|
|
|
|
|
Err(_e) => return Err("ERR index not a number") |
|
|
|
|
|
}; |
|
|
let value = match arguments[2] { |
|
|
let value = match arguments[2] { |
|
|
"on" => true, |
|
|
"on" => true, |
|
|
"off" => false, |
|
|
"off" => false, |
|
|
_ => self.segments[index], |
|
|
_ => return Err("ERR no value for segment") |
|
|
}; |
|
|
}; |
|
|
self.segments[index] = value; |
|
|
self.segments[index].set_nextval(value); |
|
|
} |
|
|
} |
|
|
"display" => { |
|
|
"display" => { |
|
|
//show the saved state on the display
|
|
|
//show the saved state on the display
|
|
|
print!("Showing state on display: "); |
|
|
|
|
|
for seg in self.segments.clone() { |
|
|
for i in 0 .. 7 { |
|
|
print!("{}, ", seg); |
|
|
match self.segments[i].show() { |
|
|
|
|
|
Ok(_) => (), |
|
|
|
|
|
Err(e) => println!("{}", e), |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
println!(); |
|
|
println!(); |
|
|
} |
|
|
} |
|
|
@ -61,9 +103,7 @@ impl Protocol { |
|
|
self.running = false; |
|
|
self.running = false; |
|
|
} |
|
|
} |
|
|
_ => { |
|
|
_ => { |
|
|
println!("Err: Command not found, closing connection!"); |
|
|
return Err("ERR Command not found!"); |
|
|
self.running = false; |
|
|
|
|
|
return Err("Command not found!"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
Ok("OK") |
|
|
Ok("OK") |
|
|
|