Skip to content

Commit

Permalink
Merge pull request #23 from rust-iot/fix/std
Browse files Browse the repository at this point in the history
Cleanup std/no-std features
  • Loading branch information
ryankurte authored Nov 17, 2021
2 parents df588e3 + 524dd06 commit 1172a8a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,34 @@ version = "0.10.0"
all-features = true

[features]
std = [ "log" ]
nonblocking = [ ]
mock = [ "embedded-hal-mock" ]
std = []
helpers = [ "structopt", "humantime", "std", "pcap-file", "libc", "byteorder", "rolling-stats" ]
default = []

[dependencies]
embedded-hal = "1.0.0-alpha.5"
nb = "1.0.0"
log = "0.4.14"

log = { version = "0.4.14", optional = true, default_features = false }
defmt = { version = "0.3.0", optional = true }

humantime = { version = "2.0.1", optional = true }
pcap-file = { version = "1.1.1", optional = true }
structopt = { version = "0.3.14", optional = true }
async-std = { version = "1.4.0", optional = true }
libc = { version = "0.2.71", optional = true }
byteorder = { version = "1.3.4", optional = true }
rolling-stats = { version = "0.3.0", optional = true }
rolling-stats = { version = "0.5.0", optional = true }
thiserror = { version = "1.0.30", optional = true }

[dependencies.chrono]
version = "0.4.19"
default_features = false

[dependencies.embedded-hal-mock]
version = "0.7.1"
version = "0.8.0"
optional = true

[dev-dependencies]
Expand Down
14 changes: 13 additions & 1 deletion src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ use core::time::Duration;

use embedded_hal::delay::blocking::DelayUs;

#[cfg(feature = "log")]
use log::debug;

#[cfg(feature = "defmt")]
use defmt::debug;

#[cfg(feature = "structopt")]
use structopt::StructOpt;

#[cfg(feature = "std")]
use crate::std::string::ToString;
use std::string::ToString;

use crate::{Receive, State, Transmit};

/// BlockingOptions for blocking radio functions
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "structopt", derive(StructOpt))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct BlockingOptions {
/// Interval for polling for device state
#[cfg_attr(feature="structopt", structopt(long, default_value="100us", parse(try_from_str=crate::duration_from_str)))]
Expand All @@ -44,6 +51,7 @@ impl Default for BlockingOptions {
/// BlockingError wraps radio error type to provie a `Timeout` variant
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "thiserror", derive(thiserror::Error))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum BlockingError<E> {
#[cfg_attr(feature = "thiserror", error("Inner: {0}"))]
Inner(E),
Expand Down Expand Up @@ -110,13 +118,15 @@ where
loop {
// Check for transmit complete
if self.check_transmit()? {
#[cfg(any(feature = "log", feature = "defmt"))]
debug!("Blocking send complete");
break;
}

// Update poll time and timeout if overrun
c += tx_options.poll_interval.as_micros();
if c > t {
#[cfg(any(feature = "log", feature = "defmt"))]
debug!("Blocking send timeout");
return Err(BlockingError::Timeout);
}
Expand Down Expand Up @@ -200,6 +210,7 @@ where

c += rx_options.poll_interval.as_micros();
if c > t {
#[cfg(any(feature = "log", feature = "defmt"))]
debug!("Blocking receive timeout");
return Err(BlockingError::Timeout);
}
Expand Down Expand Up @@ -247,6 +258,7 @@ where
// Timeout eventually
c += options.poll_interval.as_micros();
if c > t {
#[cfg(any(feature = "log", feature = "defmt"))]
debug!("Blocking receive timeout");
return Err(BlockingError::Timeout);
}
Expand Down
11 changes: 6 additions & 5 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
//! ## https://github.com/ryankurte/rust-radio
//! ## Copyright 2020 Ryan Kurte
use embedded_hal::delay::blocking::DelayUs;
use humantime::Duration as HumanDuration;
use structopt::StructOpt;

extern crate std;
use std::ffi::CString;
use std::fs::{File, OpenOptions};
use std::prelude::v1::*;
Expand All @@ -16,6 +11,12 @@ use std::time::SystemTime;

use libc::{self};

use log::{debug, info};

use embedded_hal::delay::blocking::DelayUs;
use humantime::Duration as HumanDuration;
use structopt::StructOpt;

use byteorder::{ByteOrder, NetworkEndian};
use pcap_file::{pcap::PcapHeader, DataLink, PcapWriter};
use rolling_stats::Stats;
Expand Down
23 changes: 6 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@
//! ## https://github.com/ryankurte/rust-radio
//! ## Copyright 2020 Ryan Kurte
#![no_std]
// Set `no_std` where `std` feature is disabled
#![cfg_attr(not(feature = "std"), no_std)]

use core::convert::TryFrom;
use core::fmt::Debug;

extern crate chrono;
extern crate nb;

#[macro_use]
extern crate log;

extern crate embedded_hal;

#[cfg(feature = "std")]
extern crate std;

pub mod config;

pub mod blocking;
pub mod config;

#[cfg(feature = "helpers")]
pub mod helpers;
Expand Down Expand Up @@ -261,10 +250,10 @@ pub trait Registers<Word> {
}
}

#[cfg(feature = "structopt")]
use crate::std::str::FromStr;
#[cfg(feature = "std")]
use std::str::FromStr;

#[cfg(feature = "structopt")]
#[cfg(feature = "std")]
fn duration_from_str(s: &str) -> Result<core::time::Duration, humantime::DurationError> {
let d = humantime::Duration::from_str(s)?;
Ok(*d)
Expand Down

0 comments on commit 1172a8a

Please sign in to comment.