Skip to content

Commit 8110970

Browse files
committed
add implementations of hal::blocking::delay traits
1 parent b8b17d4 commit 8110970

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repository = "https://github.com/japaric/linux-embedded-hal"
99
version = "0.1.0"
1010

1111
[dependencies]
12+
cast = "0.2.2"
1213
embedded-hal = "0.1.0"
1314
spidev = "0.3.0"
1415
sysfs_gpio = "0.5.1"

src/lib.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,76 @@
1313
#![deny(missing_docs)]
1414
#![deny(warnings)]
1515

16+
extern crate cast;
1617
extern crate embedded_hal as hal;
1718
pub extern crate spidev;
1819
pub extern crate sysfs_gpio;
1920

2021
use std::io::{self, Write};
21-
use std::ops;
2222
use std::path::Path;
23+
use std::time::Duration;
24+
use std::{ops, thread};
2325

26+
use cast::{u32, u64};
2427
use spidev::SpidevTransfer;
2528

29+
/// Empty struct that provides delay functionality on top of `thread::sleep`
30+
pub struct Delay;
31+
32+
impl hal::blocking::delay::DelayUs<u8> for Delay {
33+
fn delay_us(&mut self, n: u8) {
34+
thread::sleep(Duration::new(0, u32(n) * 1000))
35+
}
36+
}
37+
38+
impl hal::blocking::delay::DelayUs<u16> for Delay {
39+
fn delay_us(&mut self, n: u16) {
40+
thread::sleep(Duration::new(0, u32(n) * 1000))
41+
}
42+
}
43+
44+
impl hal::blocking::delay::DelayUs<u32> for Delay {
45+
fn delay_us(&mut self, n: u32) {
46+
let secs = n / 1_000_000;
47+
let nsecs = (n % 1_000_000) * 1_000;
48+
49+
thread::sleep(Duration::new(u64(secs), nsecs))
50+
}
51+
}
52+
53+
impl hal::blocking::delay::DelayUs<u64> for Delay {
54+
fn delay_us(&mut self, n: u64) {
55+
let secs = n / 1_000_000;
56+
let nsecs = ((n % 1_000_000) * 1_000) as u32;
57+
58+
thread::sleep(Duration::new(secs, nsecs))
59+
}
60+
}
61+
62+
impl hal::blocking::delay::DelayMs<u8> for Delay {
63+
fn delay_ms(&mut self, n: u8) {
64+
thread::sleep(Duration::from_millis(u64(n)))
65+
}
66+
}
67+
68+
impl hal::blocking::delay::DelayMs<u16> for Delay {
69+
fn delay_ms(&mut self, n: u16) {
70+
thread::sleep(Duration::from_millis(u64(n)))
71+
}
72+
}
73+
74+
impl hal::blocking::delay::DelayMs<u32> for Delay {
75+
fn delay_ms(&mut self, n: u32) {
76+
thread::sleep(Duration::from_millis(u64(n)))
77+
}
78+
}
79+
80+
impl hal::blocking::delay::DelayMs<u64> for Delay {
81+
fn delay_ms(&mut self, n: u64) {
82+
thread::sleep(Duration::from_millis(n))
83+
}
84+
}
85+
2686
/// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
2787
///
2888
/// [`sysfs_gpio::Pin`]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html

0 commit comments

Comments
 (0)