Skip to content

Commit

Permalink
update embedded hal to 1.0.0-rc.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankurte committed Oct 25, 2023
1 parent caef935 commit 8a5feb8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 84 deletions.
18 changes: 6 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,26 @@ features = [ "std", "nonblocking", "mock", "helpers" ]
std = [ ]
nonblocking = [ ]
mock = [ "embedded-hal-mock" ]
helpers = [ "structopt", "humantime", "std", "pcap-file", "libc", "byteorder", "rolling-stats" ]
helpers = [ "clap", "humantime", "std", "pcap-file", "libc", "byteorder", "rolling-stats" ]
default = [ ]

[dependencies]
embedded-hal = "1.0.0-alpha.7"
embedded-hal = "1.0.0-rc.1"
embedded-hal-mock = { version = "0.9.0", optional = true }
nb = "1.0.0"

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

chrono = { version = "0.4.19", default_features = false }
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.5.0", optional = true }
rolling-stats = { version = "0.7.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.8.0"
optional = true
clap = { version = "4.4.7", optional = true, features = [ "derive" ] }

[dev-dependencies]
anyhow = "1.0.44"
16 changes: 8 additions & 8 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
use core::fmt::Debug;
use core::time::Duration;

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

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

Check warning on line 15 in src/blocking.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `log::debug`

Check warning on line 15 in src/blocking.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `log::debug`

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

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

#[cfg(feature = "std")]
use std::string::ToString;
Expand All @@ -27,15 +27,15 @@ use crate::{Receive, State, Transmit};

/// BlockingOptions for blocking radio functions
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "structopt", derive(StructOpt))]
#[cfg_attr(feature = "clap", derive(Parser))]
#[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)))]
#[cfg_attr(feature="clap", clap(long, default_value="100us", value_parser=crate::duration_from_str))]
pub poll_interval: Duration,

/// Timeout for blocking operation
#[cfg_attr(feature="structopt", structopt(long, default_value="100ms", parse(try_from_str=crate::duration_from_str)))]
#[cfg_attr(feature="clap", clap(long, default_value="100ms", value_parser=crate::duration_from_str))]
pub timeout: Duration,
}

Expand Down Expand Up @@ -73,7 +73,7 @@ impl<E> From<E> for BlockingError<E> {
```
# use radio::*;
# use radio::mock::*;
use radio::blocking::{BlockingTransmit, BlockingOptions};
use radio::{BlockingTransmit, BlockingOptions};
# let mut radio = MockRadio::new(&[
# Transaction::start_transmit(vec![0xaa, 0xbb], None),
Expand Down Expand Up @@ -147,7 +147,7 @@ where
```
# use radio::*;
# use radio::mock::*;
use radio::blocking::{BlockingReceive, BlockingOptions};
use radio::{BlockingReceive, BlockingOptions};
let data = [0xaa, 0xbb];
let info = BasicInfo::new(-81, 0);
Expand Down
109 changes: 52 additions & 57 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,40 @@ use log::{debug, info};
#[cfg(feature = "defmt")]
use defmt::{debug, info};


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

use byteorder::{ByteOrder, NetworkEndian};
use pcap_file::{pcap::PcapHeader, DataLink, PcapWriter};
use rolling_stats::Stats;

use crate::blocking::*;
use crate::{Power, Receive, ReceiveInfo, Rssi, Transmit};
use crate::*;
use crate::{
blocking::{BlockingError, BlockingOptions, BlockingReceive, BlockingTransmit},
Power, Receive, ReceiveInfo, Rssi, Transmit,
};

/// Basic operations supported by the helpers package
#[derive(Clone, StructOpt, PartialEq, Debug)]
#[derive(Clone, Parser, PartialEq, Debug)]
pub enum Operation {
#[structopt(name = "tx")]
#[clap(name = "tx")]
/// Transmit a packet
Transmit(TransmitOptions),

#[structopt(name = "rx")]
#[clap(name = "rx")]
/// Receive a packet
Receive(ReceiveOptions),

#[structopt(name = "rssi")]
#[clap(name = "rssi")]
/// Poll RSSI on the configured channel
Rssi(RssiOptions),

#[structopt(name = "echo")]
#[clap(name = "echo")]
/// Echo back received messages (useful with Link Test mode)
Echo(EchoOptions),

#[structopt(name = "ping-pong")]
#[clap(name = "ping-pong")]
/// Link test (ping-pong) mode
LinkTest(PingPongOptions),
}
Expand All @@ -60,7 +62,7 @@ where
+ Receive<Info = I, Error = E>
+ Rssi<Error = E>
+ Power<Error = E>
+ DelayUs<Error = E>,
+ DelayUs,
I: ReceiveInfo + Default + std::fmt::Debug,
E: std::fmt::Debug,
{
Expand All @@ -80,27 +82,27 @@ where
}

/// Configuration for Transmit operation
#[derive(Clone, StructOpt, PartialEq, Debug)]
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct TransmitOptions {
/// Data to be transmitted
#[structopt(long)]
#[clap(long)]
pub data: Vec<u8>,

/// Power in dBm (range -18dBm to 13dBm)
#[structopt(long)]
#[clap(long)]
pub power: Option<i8>,

/// Specify period for repeated transmission
#[structopt(long)]
#[clap(long)]
pub period: Option<HumanDuration>,

#[structopt(flatten)]
#[clap(flatten)]
pub blocking_options: BlockingOptions,
}

pub fn do_transmit<T, E>(radio: &mut T, options: TransmitOptions) -> Result<(), BlockingError<E>>
where
T: Transmit<Error = E> + Power<Error = E> + DelayUs<Error = E>,
T: Transmit<Error = E> + Power<Error = E> + DelayUs,
E: core::fmt::Debug,
{
// Set output power if specified
Expand All @@ -114,7 +116,7 @@ where

// Delay for repeated transmission or exit
match &options.period {
Some(p) => radio.delay_us(p.as_micros() as u32).unwrap(),
Some(p) => radio.delay_us(p.as_micros() as u32),
None => break,
}
}
Expand All @@ -123,28 +125,28 @@ where
}

/// Configuration for Receive operation
#[derive(Clone, StructOpt, PartialEq, Debug)]
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct ReceiveOptions {
/// Run continuously
#[structopt(long = "continuous")]
#[clap(long = "continuous")]
pub continuous: bool,

#[structopt(flatten)]
#[clap(flatten)]
pub pcap_options: PcapOptions,

#[structopt(flatten)]
#[clap(flatten)]
pub blocking_options: BlockingOptions,
}

#[derive(Clone, StructOpt, PartialEq, Debug)]
#[derive(Clone, Parser, PartialEq, Debug)]

pub struct PcapOptions {
/// Create and write capture output to a PCAP file
#[structopt(long, group = "1")]
#[clap(long, group = "1")]
pub pcap_file: Option<String>,

/// Create and write to a unix pipe for connection to wireshark
#[structopt(long, group = "1")]
#[clap(long, group = "1")]
pub pcap_pipe: Option<String>,
}

Expand Down Expand Up @@ -216,7 +218,7 @@ pub fn do_receive<T, I, E>(
options: ReceiveOptions,
) -> Result<usize, E>
where
T: Receive<Info = I, Error = E> + DelayUs<Error = E>,
T: Receive<Info = I, Error = E> + DelayUs,
I: std::fmt::Debug,
E: std::fmt::Debug,
{
Expand Down Expand Up @@ -262,27 +264,25 @@ where
radio.start_receive()?;
}

radio
.delay_us(options.blocking_options.poll_interval.as_micros() as u32)
.unwrap();
radio.delay_us(options.blocking_options.poll_interval.as_micros() as u32);
}
}

/// Configuration for RSSI operation
#[derive(Clone, StructOpt, PartialEq, Debug)]
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct RssiOptions {
/// Specify period for RSSI polling
#[structopt(long = "period", default_value = "1s")]
#[clap(long = "period", default_value = "1s")]
pub period: HumanDuration,

/// Run continuously
#[structopt(long = "continuous")]
#[clap(long = "continuous")]
pub continuous: bool,
}

pub fn do_rssi<T, I, E>(radio: &mut T, options: RssiOptions) -> Result<(), E>
where
T: Receive<Info = I, Error = E> + Rssi<Error = E> + DelayUs<Error = E>,
T: Receive<Info = I, Error = E> + Rssi<Error = E> + DelayUs,
I: std::fmt::Debug,
E: std::fmt::Debug,
{
Expand All @@ -297,7 +297,7 @@ where

radio.check_receive(true)?;

radio.delay_us(options.period.as_micros() as u32).unwrap();
radio.delay_us(options.period.as_micros() as u32);

if !options.continuous {
break;
Expand All @@ -308,25 +308,25 @@ where
}

/// Configuration for Echo operation
#[derive(Clone, StructOpt, PartialEq, Debug)]
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct EchoOptions {
/// Run continuously
#[structopt(long = "continuous")]
#[clap(long = "continuous")]
pub continuous: bool,

/// Power in dBm (range -18dBm to 13dBm)
#[structopt(long = "power")]
#[clap(long = "power")]
pub power: Option<i8>,

/// Specify delay for response message
#[structopt(long = "delay", default_value = "100ms")]
#[clap(long = "delay", default_value = "100ms")]
pub delay: HumanDuration,

/// Append RSSI and LQI to repeated message
#[structopt(long = "append-info")]
#[clap(long = "append-info")]
pub append_info: bool,

#[structopt(flatten)]
#[clap(flatten)]
pub blocking_options: BlockingOptions,
}

Expand All @@ -336,10 +336,7 @@ pub fn do_echo<T, I, E>(
options: EchoOptions,
) -> Result<usize, BlockingError<E>>
where
T: Receive<Info = I, Error = E>
+ Transmit<Error = E>
+ Power<Error = E>
+ DelayUs<Error = E>,
T: Receive<Info = I, Error = E> + Transmit<Error = E> + Power<Error = E> + DelayUs,
I: ReceiveInfo + std::fmt::Debug,
E: std::fmt::Debug,
{
Expand Down Expand Up @@ -372,7 +369,7 @@ where
}

// Wait for turnaround delay
radio.delay_us(options.delay.as_micros() as u32).unwrap();
radio.delay_us(options.delay.as_micros() as u32);

// Transmit respobnse
radio.do_transmit(&buff[..n], options.blocking_options.clone())?;
Expand All @@ -384,33 +381,31 @@ where
}

// Wait for poll delay
radio
.delay_us(options.blocking_options.poll_interval.as_micros() as u32)
.unwrap();
radio.delay_us(options.blocking_options.poll_interval.as_micros() as u32);
}
}

/// Configuration for Echo operation
#[derive(Clone, StructOpt, PartialEq, Debug)]
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct PingPongOptions {
/// Specify the number of rounds to tx/rx
#[structopt(long, default_value = "100")]
#[clap(long, default_value = "100")]
pub rounds: u32,

/// Power in dBm (range -18dBm to 13dBm)
#[structopt(long)]
#[clap(long)]
pub power: Option<i8>,

/// Specify delay for response message
#[structopt(long, default_value = "100ms")]
#[clap(long, default_value = "100ms")]
pub delay: HumanDuration,

/// Parse RSSI and other info from response messages
/// (echo server must have --append-info set)
#[structopt(long)]
#[clap(long)]
pub parse_info: bool,

#[structopt(flatten)]
#[clap(flatten)]
pub blocking_options: BlockingOptions,
}

Expand All @@ -426,7 +421,7 @@ pub fn do_ping_pong<T, I, E>(
options: PingPongOptions,
) -> Result<LinkTestInfo, BlockingError<E>>
where
T: Receive<Info = I, Error = E> + Transmit<Error = E> + Power<Error = E> + DelayUs<Error = E>,
T: Receive<Info = I, Error = E> + Transmit<Error = E> + Power<Error = E> + DelayUs,
I: ReceiveInfo,
E: std::fmt::Debug,
{
Expand Down Expand Up @@ -490,7 +485,7 @@ where
}

// Wait for send delay
radio.delay_us(options.delay.as_micros() as u32).unwrap();
radio.delay_us(options.delay.as_micros() as u32);
}

Ok(link_info)
Expand Down
Loading

0 comments on commit 8a5feb8

Please sign in to comment.