|
13 | 13 | #![deny(missing_docs)]
|
14 | 14 | #![deny(warnings)]
|
15 | 15 |
|
| 16 | +extern crate cast; |
16 | 17 | extern crate embedded_hal as hal;
|
17 | 18 | pub extern crate spidev;
|
18 | 19 | pub extern crate sysfs_gpio;
|
19 | 20 |
|
20 | 21 | use std::io::{self, Write};
|
21 |
| -use std::ops; |
22 | 22 | use std::path::Path;
|
| 23 | +use std::time::Duration; |
| 24 | +use std::{ops, thread}; |
23 | 25 |
|
| 26 | +use cast::{u32, u64}; |
24 | 27 | use spidev::SpidevTransfer;
|
25 | 28 |
|
| 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 | + |
26 | 86 | /// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
|
27 | 87 | ///
|
28 | 88 | /// [`sysfs_gpio::Pin`]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html
|
|
0 commit comments