Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace embedded-time with fugit #54

Closed
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3fcecf5
Cargo.toml: replace embedded-time with fugit
andresv Nov 2, 2021
6640568
lib: introduce new Clock trait and Instant alias
andresv Nov 2, 2021
772cc44
replace embedded-time with fugit
andresv Nov 2, 2021
c8a8078
rename FREQ_HZ to TIMER_HZ
andresv Nov 4, 2021
8ad06cb
lib: export Clock from atat
andresv Nov 4, 2021
f77d95a
use Clock trait from atat lib
andresv Nov 4, 2021
c98cd14
examples: update common_lib to latest
andresv Nov 4, 2021
64db75c
examples: linux wip
andresv Nov 4, 2021
42859c5
Make sure the embedded-nal implementations only makes use of error-ty…
MathiasKoch Nov 5, 2021
7f373a4
test_helpers: update to WouldBlock based Clock
andresv Nov 5, 2021
138ad21
error: add helper to convert Clock error to Error::Generic
andresv Nov 5, 2021
143c38a
client: use nb::block to wait for timer
andresv Nov 5, 2021
074b42e
import atat from blackbirdhq master
andresv Nov 5, 2021
fa5abbc
Merge branch 'master' into replace-embedded-time-with-fugit
MathiasKoch Nov 5, 2021
0c39345
use fugit defmt feature to get Format impl
andresv Nov 5, 2021
a90c7bb
linux: set_socket_storage
andresv Nov 5, 2021
015f580
add IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr to prelude
andresv Nov 6, 2021
8964999
linux example: add socket read/write
andresv Nov 6, 2021
f04dfdf
reexport fugit
andresv Nov 6, 2021
9142608
add some general documentation about the crate
andresv Nov 6, 2021
4a4e3f6
make first examples buildable also on macos
andresv Nov 6, 2021
56f99e2
rename linux example to sockets, add args support
andresv Nov 6, 2021
8ca67ac
power: add nb::block! for blocking timer.waits
andresv Nov 9, 2021
3bf306e
use atat and ublox-sockets from crates.io
andresv Nov 9, 2021
5c32bf1
use fmt.rs to support log or defmt
andresv Nov 12, 2021
adf3aac
examples: add better logging
andresv Jan 14, 2022
01e7e0d
update to embedded-hal 1.0.0-alpha.6
andresv Jan 24, 2022
ca5cf7d
add .DS_Store to .gitignore
andresv Jan 24, 2022
7a8fd47
update atat and ublox-sockets
andresv Jan 27, 2022
f502481
Merge branch 'master' into replace-embedded-time-with-fugit
MathiasKoch Jan 27, 2022
2abfd06
Update ublox-cellular/Cargo.toml
MathiasKoch Jan 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ target/
**/*secrets*

Cargo.lock
.DS_Store
12 changes: 8 additions & 4 deletions examples/common_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ name = "common"
doctest = false

[dependencies]
serialport = "3.3.0"
embedded-hal = "0.2.3"
nb = "0.1.2"
void = { version = "1.0.2", default-features = false }
serialport = "4"
embedded-hal = "^1.0.0-alpha.6"
nb = "1"
log = { version = "0.4", default-features = false }
ublox-cellular-rs = { path = "../../ublox-cellular" }

[target.'cfg(target_os = "linux")'.dependencies]
linux-embedded-hal = "0.3"
83 changes: 83 additions & 0 deletions examples/common_lib/src/gpio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use embedded_hal::digital::blocking::{InputPin, OutputPin};

#[cfg(target_os = "linux")]
mod linux {
use super::*;
use linux_embedded_hal::{sysfs_gpio, Pin};

// implement newest embedded_hal traits
// linux_embedded_hal uses old ones
pub struct ExtPin(Pin);

impl OutputPin for ExtPin {
type Error = sysfs_gpio::Error;

fn set_low(&mut self) -> Result<(), Self::Error> {
if self.0.get_active_low()? {
self.0.set_value(1)
} else {
self.0.set_value(0)
}
}

fn set_high(&mut self) -> Result<(), Self::Error> {
if self.0.get_active_low()? {
self.0.set_value(0)
} else {
self.0.set_value(1)
}
}
}

impl InputPin for ExtPin {
type Error = sysfs_gpio::Error;

fn is_high(&self) -> Result<bool, Self::Error> {
if !self.0.get_active_low()? {
self.0.get_value().map(|val| val != 0)
} else {
self.0.get_value().map(|val| val == 0)
}
}

fn is_low(&self) -> Result<bool, Self::Error> {
self.is_high().map(|val| !val)
}
}
}

#[cfg(not(target_os = "linux"))]
mod other {
use super::*;

pub struct ExtPin;

impl OutputPin for ExtPin {
type Error = std::convert::Infallible;

fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(())
}

fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}

impl InputPin for ExtPin {
type Error = std::convert::Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
Ok(true)
}

fn is_low(&self) -> Result<bool, Self::Error> {
self.is_high().map(|val| !val)
}
}
}

#[cfg(target_os = "linux")]
pub use linux::ExtPin;
#[cfg(not(target_os = "linux"))]
pub use other::ExtPin;
1 change: 1 addition & 0 deletions examples/common_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod gpio;
pub mod serial;
pub mod timer;
12 changes: 6 additions & 6 deletions examples/common_lib/src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use std::io::{ErrorKind as IoErrorKind, Read, Write};
pub struct Serial(pub Box<dyn serialport::SerialPort>);

/// Helper to convert std::io::Error to the nb::Error
fn translate_io_errors(err: std::io::Error) -> nb::Error<IoErrorKind> {
fn translate_io_errors(err: std::io::Error) -> nb::Error<hal::serial::ErrorKind> {
match err.kind() {
IoErrorKind::WouldBlock | IoErrorKind::TimedOut | IoErrorKind::Interrupted => {
nb::Error::WouldBlock
}
err => nb::Error::Other(err),
_err => nb::Error::Other(hal::serial::ErrorKind::Other),
}
}

impl hal::serial::Read<u8> for Serial {
type Error = IoErrorKind;
impl hal::serial::nb::Read<u8> for Serial {
type Error = hal::serial::ErrorKind;

fn read(&mut self) -> nb::Result<u8, Self::Error> {
let mut buffer = [0; 1];
Expand All @@ -28,8 +28,8 @@ impl hal::serial::Read<u8> for Serial {
}
}

impl hal::serial::Write<u8> for Serial {
type Error = IoErrorKind;
impl hal::serial::nb::Write<u8> for Serial {
type Error = hal::serial::ErrorKind;

fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
self.0.write(&[word]).map_err(translate_io_errors)?;
Expand Down
98 changes: 72 additions & 26 deletions examples/common_lib/src/timer.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,87 @@
use embedded_hal::timer::CountDown;
use std::time::{Duration, Instant};
use ublox_cellular::fugit;
use ublox_cellular::prelude::*;

pub struct SysTimer {
start: Instant,
count: u32,
pub struct SysTimer<const TIMER_HZ: u32> {
description: String,
monotonic: std::time::Instant,
start: Option<std::time::Instant>,
duration: fugit::TimerDurationU32<TIMER_HZ>,
}

impl SysTimer {
pub fn new() -> SysTimer {
SysTimer {
start: Instant::now(),
count: 0,
impl<const TIMER_HZ: u32> SysTimer<TIMER_HZ> {
pub fn new(description: &str) -> Self {
Self {
description: description.into(),
monotonic: std::time::Instant::now(),
start: None,
duration: fugit::TimerDurationU32::millis(0),
}
}
}

impl CountDown for SysTimer {
type Time = u32;
impl<const TIMER_HZ: u32> Clock<TIMER_HZ> for SysTimer<TIMER_HZ> {
type Error = std::convert::Infallible;

fn start<T>(&mut self, count: T)
where
T: Into<Self::Time>,
{
self.start = Instant::now();
self.count = count.into();
fn now(&mut self) -> fugit::TimerInstantU32<TIMER_HZ> {
let millis = self.monotonic.elapsed().as_millis();
fugit::TimerInstantU32::from_ticks(millis as u32)
}

fn wait(&mut self) -> nb::Result<(), void::Void> {
if Instant::now() - self.start > Duration::from_millis(self.count as u64) {
Ok(())
fn start(&mut self, duration: fugit::TimerDurationU32<TIMER_HZ>) -> Result<(), Self::Error> {
self.start = Some(std::time::Instant::now());
self.duration = duration.convert();
log::debug!(
"[{}] start {:?} duration {:?}",
self.description,
self.start,
self.duration
);
Ok(())
}

fn cancel(&mut self) -> Result<(), Self::Error> {
if self.start.is_some() {
self.start = None;
}
Ok(())
}

fn wait(&mut self) -> nb::Result<(), Self::Error> {
if let Some(start) = self.start {
if std::time::Instant::now() - start
> std::time::Duration::from_millis(self.duration.ticks() as u64)
{
log::debug!(
"[{}] now {:?} start {:?} duration {:?} {:?}",
self.description,
std::time::Instant::now(),
self.start,
self.duration,
std::time::Duration::from_millis(self.duration.ticks() as u64)
);
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
} else {
Err(nb::Error::WouldBlock)
Ok(())
}
}
}

// #[cfg(test)]
// mod tests {
// extern crate nb;
// }
#[cfg(test)]
mod tests {
use super::*;
use fugit::ExtU32;

#[test]
fn sys_timer() {
let now = std::time::Instant::now();

let mut t: SysTimer<1000> = SysTimer::new("");
t.start(1.secs()).unwrap();
nb::block!(t.wait()).unwrap();

assert!(now.elapsed().as_millis() >= 1000);
}
}
143 changes: 0 additions & 143 deletions examples/linux/src/main.rs

This file was deleted.

Loading