Skip to content

Commit 0dc881b

Browse files
committed
Auto merge of rust-embedded#1 - japaric:delay, r=japaric
add implementations of hal::blocking::delay traits None
2 parents b8b17d4 + 850d88f commit 0dc881b

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

.travis.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Based on the "trust" template v0.1.2
2+
# https://github.com/japaric/trust/tree/v0.1.2
3+
4+
dist: trusty
5+
language: rust
6+
services: docker
7+
8+
matrix:
9+
include:
10+
# Linux
11+
- env: TARGET=armv7-unknown-linux-gnueabihf
12+
rust: nightly
13+
- env: TARGET=x86_64-unknown-linux-gnu
14+
rust: nightly
15+
16+
before_install:
17+
- set -e
18+
- rustup self update
19+
20+
install:
21+
- bash ci/install.sh
22+
23+
script:
24+
- bash ci/script.sh
25+
26+
after_script: set +e
27+
28+
before_deploy:
29+
- sh ci/before_deploy.sh
30+
31+
cache: cargo
32+
before_cache:
33+
# Travis can't cache files that are not readable by "others"
34+
- chmod -R a+r $HOME/.cargo
35+
36+
branches:
37+
only:
38+
- auto
39+
- master
40+
- try
41+
42+
notifications:
43+
email:
44+
on_success: never

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"

ci/install.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
set -ex
2+
3+
main() {
4+
# This fetches latest stable release of Cross
5+
local tag=$(git ls-remote --tags --refs --exit-code https://github.com/japaric/cross \
6+
| cut -d/ -f3 \
7+
| grep -E '^v[0.1.0-9.]+$' \
8+
| sort --version-sort \
9+
| tail -n1)
10+
11+
curl -LSfs https://japaric.github.io/trust/install.sh | \
12+
sh -s -- \
13+
--force \
14+
--git japaric/cross \
15+
--tag $tag
16+
}
17+
18+
main

ci/script.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This script takes care of testing your crate
2+
3+
set -euxo pipefail
4+
5+
main() {
6+
cross check --target $TARGET
7+
}
8+
9+
main

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)