You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB

extern crate gpio;
use gpio::GpioOut;
pub static RPI: bool = true;
pub static DEBUG: bool = false;
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<Segment>,
running: bool,
}
impl Protocol {
pub fn new() -> Protocol {
Protocol {
segments: Vec::new(),
running: false,
}
}
pub fn init(&mut self) {
self.segments.push(Segment::new(5));
self.segments.push(Segment::new(6));
self.segments.push(Segment::new(13));
self.segments.push(Segment::new(19));
self.segments.push(Segment::new(26));
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
}
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("ERR Missing arguments");
}
match arguments[0] {
"setsegment" => {
if arguments.len() != 3 {
self.running = false;
return Err("ERR Wrong number of arguments");
}
let index = match arguments[1].parse::<usize>() {
Ok(value) => {
if value < 8 {
value
} else {
return Err("ERR index out of bounds");
}
},
Err(_e) => return Err("ERR index not a number")
};
let value = match arguments[2] {
"on" => true,
"off" => false,
_ => return Err("ERR no value for segment")
};
if RPI {
self.segments[index].set_nextval(value);
} else {
if DEBUG {
println!("segment {} = {}", index, value);
}
}
}
"display" => {
//show the saved state on the display
if RPI {
for i in 0..8 {
match self.segments[i].show() {
Ok(_) => (),
Err(e) => {
if DEBUG {
println!("{}", e)
}
},
}
}
} else {
if DEBUG {
println!("showing segments");
}
}
if DEBUG {
println!();
}
}
"exit" => {
//close connection
println!("Closing connection.");
self.running = false;
}
_ => {
return Err("ERR Command not found!");
}
}
Ok("OK")
}
}