Commit 8ac542d4 authored by Simon Wörner's avatar Simon Wörner

rustfmt

parent 9d4a349e
......@@ -7,7 +7,7 @@ use std::path::Path;
#[derive(Debug)]
pub enum Direction {
Out,
In
In,
}
impl Direction {
pub fn from_str(s: &str) -> Option<Direction> {
......@@ -31,7 +31,7 @@ pub enum Edge {
None,
Rising,
Falling,
Both
Both,
}
impl Edge {
pub fn from_str(s: &str) -> Option<Edge> {
......@@ -57,7 +57,7 @@ impl Edge {
#[derive(Debug)]
pub enum Value {
High,
Low
Low,
}
impl Value {
pub fn from_str(s: &str) -> Option<Value> {
......@@ -89,14 +89,14 @@ impl Port {
let path = Path::new(path.as_str());
let file = match direction {
Direction::Out => File::create(path)?,
Direction::In => File::open(path)?
Direction::In => File::open(path)?,
};
let port = Port {
number: number,
direction: direction,
edge: edge,
file: file
file: file,
};
port.export().ok();
......@@ -111,7 +111,8 @@ impl Port {
}
fn write_path(path: &str, value: &str) -> std::io::Result<()> {
File::create(Path::new(path))?.write_all(value.as_bytes())
File::create(Path::new(path))?
.write_all(value.as_bytes())
}
pub fn read(&mut self) -> std::io::Result<Value> {
......@@ -119,7 +120,8 @@ impl Port {
self.file.read_to_string(&mut contents)?;
Value::from_str(contents.as_str()).ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "Unrecognized GPIO Value"))
Value::from_str(contents.as_str())
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "Unrecognized GPIO Value"))
}
pub fn write(&mut self, value: Value) -> std::io::Result<()> {
self.file.write_all(value.as_str().as_bytes())
......@@ -133,10 +135,12 @@ impl Port {
}
fn set_direction(&self) -> std::io::Result<()> {
Port::write_path(format!("/sys/class/gpio/gpio{}/direction", self.number).as_str(), self.direction.as_str())
Port::write_path(format!("/sys/class/gpio/gpio{}/direction", self.number).as_str(),
self.direction.as_str())
}
fn set_edge(&self) -> std::io::Result<()> {
Port::write_path(format!("/sys/class/gpio/gpio{}/edge", self.number).as_str(), self.edge.as_str())
Port::write_path(format!("/sys/class/gpio/gpio{}/edge", self.number).as_str(),
self.edge.as_str())
}
}
\ No newline at end of file
}
pub mod gpio;
\ No newline at end of file
pub mod gpio;
......@@ -5,7 +5,8 @@ use kawaii::gpio::{Port, Direction, Edge, Value};
fn main() {
println!("Hello, world!");
let mut trigger = Port::new(27, Direction::Out, Edge::None).expect("Create Trigger GPIO failed");
let mut trigger =
Port::new(27, Direction::Out, Edge::None).expect("Create Trigger GPIO failed");
let mut echo = Port::new(28, Direction::In, Edge::Both).expect("Create Echo GPIO failed");
println!("trigger = {:?}", trigger.read());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment