Commit ae6ed20b authored by Simon Wörner's avatar Simon Wörner

made measure a feature

parent b5377c63
......@@ -3,6 +3,9 @@ name = "kawaii"
version = "0.1.0"
authors = ["Simon Wörner <git@simon-woerner.de>"]
[features]
measure = []
[dependencies]
separator = "^0.3.1"
time = "^0.1.36"
......
......@@ -10,6 +10,7 @@ use std::path::Path;
use std::os::unix::io::RawFd;
#[cfg(feature = "measure")]
use measure::Measure;
fn duration_to_ms(duration: Duration) -> u64 {
......@@ -231,27 +232,18 @@ impl AsyncPort {
.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 {
fn get_timeout(timeout: Option<Duration>) -> i32 {
match timeout {
None => -1,
Some(t) => duration_to_ms(t) as i32,
};
// pause measure
if let Some(ref mut m) = measure {
m.pause();
}
}
let poll = nix::poll::poll(&mut self.fds, timeout);
// start measure
if let Some(ref mut m) = measure {
m.start();
}
fn nix_poll(&mut self, timeout: i32) -> nix::Result<i32> {
nix::poll::poll(&mut self.fds, timeout)
}
fn poll_read(&mut self, poll: nix::Result<i32>) -> std::io::Result<Option<Value>> {
let poll = poll.or(Err(Error::new(ErrorKind::Other, "poll failed")))?;
if poll == 0 {
......@@ -269,6 +261,26 @@ impl AsyncPort {
|v| Ok(Some(v)))
}
pub fn poll(&mut self, timeout: Option<Duration>) -> std::io::Result<Option<Value>> {
let poll = self.nix_poll(Self::get_timeout(timeout));
self.poll_read(poll)
}
#[cfg(feature = "measure")]
pub fn poll_measure(&mut self,
timeout: Option<Duration>,
measure: &mut Measure)
-> std::io::Result<Option<Value>> {
let timeout = Self::get_timeout(timeout);
measure.pause();
let poll = self.nix_poll(timeout);
measure.start();
self.poll_read(poll)
}
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())
......
pub mod gpio;
#[cfg(feature = "measure")]
pub mod measure;
......@@ -15,5 +15,5 @@ fn main() {
println!("trigger = {:?}", trigger.read());
trigger.write(Value::Low).expect("write failed");
println!("trigger = {:?}", trigger.read());
println!("echo = {:?}", echo.poll(Some(Duration::new(1, 0)), None));
println!("echo = {:?}", echo.poll(Some(Duration::new(1, 0))));
}
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