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

use Duration for poll timeout

parent 7f69c3cb
extern crate std;
extern crate nix;
use std::time::Duration;
use std::io::prelude::*;
use std::io::{Error, ErrorKind, SeekFrom};
use std::fs::File;
......@@ -8,6 +9,10 @@ use std::path::Path;
use std::os::unix::io::RawFd;
fn duration_to_ms(duration: Duration) -> u64 {
duration.as_secs() * 1_000u64 + duration.subsec_nanos() as u64 / 1_000_000u64
}
#[derive(Clone, Copy, Debug)]
pub enum Direction {
Out,
......@@ -192,11 +197,11 @@ impl AsyncPort {
.or(Err(Error::new(ErrorKind::Other, "open failed")))
}
pub fn poll(&mut self, timeout: Option<i32>) -> std::io::Result<Value> {
pub fn poll(&mut self, timeout: Option<Duration>) -> std::io::Result<Value> {
let mut fds = [nix::poll::PollFd::new(self.file, nix::poll::POLLPRI, nix::poll::EventFlags::empty())];
let mut buffer: [u8; 1] = [0; 1];
nix::poll::poll(&mut fds, match timeout { None => -1, Some(t) => t})
nix::poll::poll(&mut fds, match timeout { None => -1, Some(t) => duration_to_ms(t) as i32})
.or(Err(Error::new(ErrorKind::Other, "poll failed")))?;
nix::unistd::lseek(self.file, 0, nix::unistd::Whence::SeekSet)
......
extern crate kawaii;
use std::time::Duration;
use kawaii::gpio::{SyncPort, AsyncPort, Direction, Edge, Value};
fn main() {
......@@ -13,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(1000)));
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