Skip to content

Commit 96c712e

Browse files
committed
Extract delay into separate module
1 parent 1c7312d commit 96c712e

File tree

2 files changed

+93
-85
lines changed

2 files changed

+93
-85
lines changed

src/delay.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Implementation of [`embedded-hal`] delay traits
2+
//!
3+
//! [`embedded-hal`]: https://docs.rs/embedded-hal
4+
5+
use cast::{u32, u64};
6+
use core::convert::Infallible;
7+
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
8+
use std::thread;
9+
use std::time::Duration;
10+
11+
/// Empty struct that provides delay functionality on top of `thread::sleep`
12+
pub struct Delay;
13+
14+
impl DelayUs<u8> for Delay {
15+
type Error = Infallible;
16+
17+
fn try_delay_us(&mut self, n: u8) -> Result<(), Self::Error> {
18+
thread::sleep(Duration::new(0, u32(n) * 1000));
19+
Ok(())
20+
}
21+
}
22+
23+
impl DelayUs<u16> for Delay {
24+
type Error = Infallible;
25+
26+
fn try_delay_us(&mut self, n: u16) -> Result<(), Self::Error> {
27+
thread::sleep(Duration::new(0, u32(n) * 1000));
28+
Ok(())
29+
}
30+
}
31+
32+
impl DelayUs<u32> for Delay {
33+
type Error = Infallible;
34+
35+
fn try_delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
36+
let secs = n / 1_000_000;
37+
let nsecs = (n % 1_000_000) * 1_000;
38+
39+
thread::sleep(Duration::new(u64(secs), nsecs));
40+
Ok(())
41+
}
42+
}
43+
44+
impl DelayUs<u64> for Delay {
45+
type Error = Infallible;
46+
47+
fn try_delay_us(&mut self, n: u64) -> Result<(), Self::Error> {
48+
let secs = n / 1_000_000;
49+
let nsecs = ((n % 1_000_000) * 1_000) as u32;
50+
51+
thread::sleep(Duration::new(secs, nsecs));
52+
Ok(())
53+
}
54+
}
55+
56+
impl DelayMs<u8> for Delay {
57+
type Error = Infallible;
58+
59+
fn try_delay_ms(&mut self, n: u8) -> Result<(), Self::Error> {
60+
thread::sleep(Duration::from_millis(u64(n)));
61+
Ok(())
62+
}
63+
}
64+
65+
impl DelayMs<u16> for Delay {
66+
type Error = Infallible;
67+
68+
fn try_delay_ms(&mut self, n: u16) -> Result<(), Self::Error> {
69+
thread::sleep(Duration::from_millis(u64(n)));
70+
Ok(())
71+
}
72+
}
73+
74+
impl DelayMs<u32> for Delay {
75+
type Error = Infallible;
76+
77+
fn try_delay_ms(&mut self, n: u32) -> Result<(), Self::Error> {
78+
thread::sleep(Duration::from_millis(u64(n)));
79+
Ok(())
80+
}
81+
}
82+
83+
impl DelayMs<u64> for Delay {
84+
type Error = Infallible;
85+
86+
fn try_delay_ms(&mut self, n: u64) -> Result<(), Self::Error> {
87+
thread::sleep(Duration::from_millis(n));
88+
Ok(())
89+
}
90+
}

src/lib.rs

Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
#![deny(missing_docs)]
1414

15-
use cast;
1615
pub use i2cdev;
1716
pub use nb;
1817
pub use serial_core;
@@ -25,13 +24,10 @@ pub use sysfs_gpio;
2524
#[cfg(feature = "gpio_cdev")]
2625
pub use gpio_cdev;
2726

28-
use core::convert::Infallible;
2927
use std::io::{self, Write};
28+
use std::ops;
3029
use std::path::Path;
31-
use std::time::Duration;
32-
use std::{ops, thread};
3330

34-
use cast::{u32, u64};
3531
use spidev::SpidevTransfer;
3632

3733
mod serial;
@@ -55,86 +51,8 @@ pub use cdev_pin::CdevPin;
5551
/// Sysfs pin re-export
5652
pub use sysfs_pin::SysfsPin;
5753

58-
/// Empty struct that provides delay functionality on top of `thread::sleep`
59-
pub struct Delay;
60-
61-
impl embedded_hal::blocking::delay::DelayUs<u8> for Delay {
62-
type Error = Infallible;
63-
64-
fn try_delay_us(&mut self, n: u8) -> Result<(), Self::Error> {
65-
thread::sleep(Duration::new(0, u32(n) * 1000));
66-
Ok(())
67-
}
68-
}
69-
70-
impl embedded_hal::blocking::delay::DelayUs<u16> for Delay {
71-
type Error = Infallible;
72-
73-
fn try_delay_us(&mut self, n: u16) -> Result<(), Self::Error> {
74-
thread::sleep(Duration::new(0, u32(n) * 1000));
75-
Ok(())
76-
}
77-
}
78-
79-
impl embedded_hal::blocking::delay::DelayUs<u32> for Delay {
80-
type Error = Infallible;
81-
82-
fn try_delay_us(&mut self, n: u32) -> Result<(), Self::Error> {
83-
let secs = n / 1_000_000;
84-
let nsecs = (n % 1_000_000) * 1_000;
85-
86-
thread::sleep(Duration::new(u64(secs), nsecs));
87-
Ok(())
88-
}
89-
}
90-
91-
impl embedded_hal::blocking::delay::DelayUs<u64> for Delay {
92-
type Error = Infallible;
93-
94-
fn try_delay_us(&mut self, n: u64) -> Result<(), Self::Error> {
95-
let secs = n / 1_000_000;
96-
let nsecs = ((n % 1_000_000) * 1_000) as u32;
97-
98-
thread::sleep(Duration::new(secs, nsecs));
99-
Ok(())
100-
}
101-
}
102-
103-
impl embedded_hal::blocking::delay::DelayMs<u8> for Delay {
104-
type Error = Infallible;
105-
106-
fn try_delay_ms(&mut self, n: u8) -> Result<(), Self::Error> {
107-
thread::sleep(Duration::from_millis(u64(n)));
108-
Ok(())
109-
}
110-
}
111-
112-
impl embedded_hal::blocking::delay::DelayMs<u16> for Delay {
113-
type Error = Infallible;
114-
115-
fn try_delay_ms(&mut self, n: u16) -> Result<(), Self::Error> {
116-
thread::sleep(Duration::from_millis(u64(n)));
117-
Ok(())
118-
}
119-
}
120-
121-
impl embedded_hal::blocking::delay::DelayMs<u32> for Delay {
122-
type Error = Infallible;
123-
124-
fn try_delay_ms(&mut self, n: u32) -> Result<(), Self::Error> {
125-
thread::sleep(Duration::from_millis(u64(n)));
126-
Ok(())
127-
}
128-
}
129-
130-
impl embedded_hal::blocking::delay::DelayMs<u64> for Delay {
131-
type Error = Infallible;
132-
133-
fn try_delay_ms(&mut self, n: u64) -> Result<(), Self::Error> {
134-
thread::sleep(Duration::from_millis(n));
135-
Ok(())
136-
}
137-
}
54+
mod delay;
55+
pub use crate::delay::Delay;
13856

13957
mod i2c;
14058
pub use crate::i2c::I2cdev;

0 commit comments

Comments
 (0)