Commit 065a2d02 authored by Simon Wörner's avatar Simon Wörner

rustfmt

parent c6c6e08b
......@@ -88,7 +88,7 @@ impl Value {
match c {
48 => Some(Value::Low), // '0'
49 => Some(Value::High), // '1'
_ => None
_ => None,
}
}
......@@ -103,27 +103,31 @@ impl Value {
#[derive(Debug)]
pub struct Port {
pub number: u8,
pub direction: Direction
pub direction: Direction,
}
#[derive(Debug)]
pub struct SyncPort {
pub port: Port,
file: File,
buffer: [u8; 1]
buffer: [u8; 1],
}
pub struct AsyncPort {
pub port: Port,
pub edge: Edge,
file: RawFd,
fds: [nix::poll::PollFd; 1],
buffer: [u8; 1]
buffer: [u8; 1],
}
impl fmt::Debug for AsyncPort {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "AsyncPort {{ port: {:?}, edge: {:?}, file: {:?}, fds: [?], buffer: {:?} }}",
self.port, self.edge, self.file, self.buffer)
write!(f,
"AsyncPort {{ port: {:?}, edge: {:?}, file: {:?}, fds: [?], buffer: {:?} }}",
self.port,
self.edge,
self.file,
self.buffer)
}
}
......@@ -131,7 +135,7 @@ impl Port {
pub fn new(number: u8, direction: Direction) -> std::io::Result<Port> {
let port = Port {
number: number,
direction: direction
direction: direction,
};
port.init()?;
......@@ -162,17 +166,18 @@ 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())
}
}
impl SyncPort {
pub fn new(number: u8, direction: Direction) -> std::io::Result<SyncPort> {
Ok(SyncPort {
port: Port::new(number, direction)?,
file: SyncPort::open(number, direction)?,
buffer: [0; 1]
})
port: Port::new(number, direction)?,
file: SyncPort::open(number, direction)?,
buffer: [0; 1],
})
}
fn open(number: u8, direction: Direction) -> std::io::Result<File> {
......@@ -180,17 +185,17 @@ impl SyncPort {
let path = Path::new(path.as_str());
Ok(match direction {
Direction::Out => File::create(path)?,
Direction::In => File::open(path)?,
})
Direction::Out => File::create(path)?,
Direction::In => File::open(path)?,
})
}
pub fn read(&mut self) -> std::io::Result<Value> {
self.file.seek(SeekFrom::Start(0))?;
self.file.read_exact(&mut self.buffer)?;
Value::from_buffer(&self.buffer)
.ok_or(Error::new(ErrorKind::InvalidData, "Unrecognized GPIO Value"))
Value::from_buffer(&self.buffer).ok_or(Error::new(ErrorKind::InvalidData,
"Unrecognized GPIO Value"))
}
pub fn write(&mut self, value: Value) -> std::io::Result<()> {
......@@ -205,8 +210,8 @@ impl AsyncPort {
port: Port::new(number, Direction::In)?,
edge: edge,
file: file,
fds:[nix::poll::PollFd::new(file, nix::poll::POLLPRI, nix::poll::EventFlags::empty())],
buffer: [0; 1]
fds: [nix::poll::PollFd::new(file, nix::poll::POLLPRI, nix::poll::EventFlags::empty())],
buffer: [0; 1],
};
port.init()?;
......@@ -219,12 +224,20 @@ impl AsyncPort {
}
fn open(number: u8) -> std::io::Result<RawFd> {
nix::fcntl::open(format!("/sys/class/gpio/gpio{}/value", number).as_str(), nix::fcntl::O_RDONLY, nix::sys::stat::Mode::empty())
.or(Err(Error::new(ErrorKind::Other, "open failed")))
nix::fcntl::open(format!("/sys/class/gpio/gpio{}/value", number).as_str(),
nix::fcntl::O_RDONLY,
nix::sys::stat::Mode::empty())
.or(Err(Error::new(ErrorKind::Other, "open failed")))
}
pub fn poll(&mut self, timeout: Option<Duration>, mut measure: Option<&mut Measure>) -> std::io::Result<Option<Value>> {
let timeout = match timeout { None => -1, Some(t) => duration_to_ms(t) as i32};
pub fn poll(&mut self,
timeout: Option<Duration>,
mut measure: Option<&mut Measure>)
-> std::io::Result<Option<Value>> {
let timeout = match timeout {
None => -1,
Some(t) => duration_to_ms(t) as i32,
};
// pause measure
if let Some(ref mut m) = measure {
......@@ -250,11 +263,13 @@ impl AsyncPort {
nix::unistd::read(self.file, &mut self.buffer)
.or(Err(Error::new(ErrorKind::Other, "read failed")))?;
Value::from_buffer(&self.buffer)
.map_or(Err(Error::new(ErrorKind::InvalidData, "Unrecognized GPIO Value")), |v| Ok(Some(v)))
Value::from_buffer(&self.buffer).map_or(Err(Error::new(ErrorKind::InvalidData,
"Unrecognized GPIO Value")),
|v| Ok(Some(v)))
}
fn set_edge(&self) -> std::io::Result<()> {
Port::write_path(format!("/sys/class/gpio/gpio{}/edge", self.port.number).as_str(), self.edge.as_str())
Port::write_path(format!("/sys/class/gpio/gpio{}/edge", self.port.number).as_str(),
self.edge.as_str())
}
}
pub mod gpio;
pub mod measure;
\ No newline at end of file
pub mod measure;
......@@ -10,7 +10,7 @@ pub struct Measure {
pub max: u64,
time: u64,
last: u64,
pub name: String
pub name: String,
}
impl Measure {
......@@ -20,7 +20,7 @@ impl Measure {
max: 0u64,
time: 0u64,
last: 0u64,
name: name
name: name,
}
}
......
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