Skip to content

Commit 0ca4b8a

Browse files
committed
move cli package to example, add comments to it.
1 parent 4b45cca commit 0ca4b8a

File tree

8 files changed

+66
-54
lines changed

8 files changed

+66
-54
lines changed

Cargo.toml

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1-
[workspace]
2-
members = [
3-
"cli",
4-
"ppk2"
5-
]
1+
[package]
2+
name = "ppk2"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Henk Oordt <[email protected]>"]
6+
description = "A driver for Nordic's Power Profiler Kit 2"
7+
readme = "README.md"
8+
repository = "https://github.com/hdoordt/ppk2-rs"
9+
license = "MIT"
10+
keywords = ["PPK2", "Power", "Profiler", "Nordic", "Serial"]
11+
categories = ["embedded", "development-tools::profiling"]
12+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
13+
14+
[dependencies]
15+
num_enum = "0.5.7"
16+
serialport = "4.2.0"
17+
thiserror = "1.0.32"
18+
tracing = "0.1.36"
19+
20+
[dev-dependencies]
21+
anyhow = { version = "1.0.60", features = ["backtrace"] }
22+
ctrlc = "3.2.2"
23+
tracing-subscriber = "0.3.15"
24+
clap = { version = "3.2.20", features = ["derive", "env"] }
25+
26+
[badges]
27+
maintenance = { status = "passively-maintained" }

cli/Cargo.toml

-18
This file was deleted.

cli/src/main.rs examples/cli.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct Args {
6363
}
6464

6565
fn main() -> Result<()> {
66+
// Setup stuff
6667
let args = Args::parse();
6768

6869
let subscriber = FmtSubscriber::builder()
@@ -75,20 +76,28 @@ fn main() -> Result<()> {
7576
None => try_find_ppk2_port()?,
7677
};
7778

79+
// Connect to PPK2 and initialize
7880
let mut ppk2 = Ppk2::new(ppk2_port, args.mode)?;
79-
8081
ppk2.set_source_voltage(args.voltage)?;
8182
ppk2.set_device_power(args.power)?;
83+
84+
// Set up pin pattern for matching
85+
// This particular setup will only
86+
// match measurements if pin 0 is low.
8287
let mut levels = [Level::Either; 8];
8388
levels[0] = Level::Low;
8489
let pins = LogicPortPins::with_levels(levels);
85-
let (rx, kill) = ppk2.start_measuring_while_matches(pins, args.sps)?;
8690

87-
let mut kill = Some(kill);
91+
// Start measuring.
92+
let (rx, kill) = ppk2.start_measurement_matching(pins, args.sps)?;
8893

94+
// Set up sigkill handler.
95+
let mut kill = Some(kill);
8996
ctrlc::set_handler(move || {
9097
kill.take().unwrap()().unwrap();
9198
})?;
99+
100+
// Receive measurements
92101
let mut count = 0usize;
93102
let start = Instant::now();
94103
let r: Result<()> = loop {
@@ -97,10 +106,10 @@ fn main() -> Result<()> {
97106
use MeasurementMatch::*;
98107
match rcv_res {
99108
Ok(Match(m)) => {
100-
debug!("Last: {:.4} μA", m.micro_amps);
109+
debug!("Last chunk average: {:.4} μA", m.micro_amps);
101110
}
102111
Ok(NoMatch) => {
103-
debug!("No match");
112+
debug!("No match in the last chunk of measurements");
104113
}
105114
Err(RecvTimeoutError::Disconnected) => break Ok(()),
106115
Err(e) => {

ppk2/Cargo.toml

-21
This file was deleted.

ppk2/src/cmd.rs src/cmd.rs

File renamed without changes.

ppk2/src/lib.rs src/lib.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#![doc = include_str!("../../README.md")]
1+
#![doc = include_str!("../README.md")]
22
#![deny(missing_docs)]
33

4-
use measurement::{Measurement, MeasurementAccumulator, MeasurementIterExt, MeasurementMatch};
4+
use measurement::{MeasurementAccumulator, MeasurementIterExt, MeasurementMatch};
55
use serialport::{ClearBuffer::Input, FlowControl, SerialPort};
66
use std::str::Utf8Error;
77
use std::sync::mpsc::{self, Receiver, SendError, TryRecvError};
@@ -105,12 +105,24 @@ impl Ppk2 {
105105
Ok(())
106106
}
107107

108+
/// Start measurements. Returns a tuple of:
109+
/// - [Ppk2<Measuring>],
110+
/// - [Receiver] of [measurement::MeasurementMatch], and
111+
/// - A closure that can be called to stop the measurement parsing pipeline and return the
112+
/// device.
113+
pub fn start_measurement(
114+
self,
115+
sps: usize,
116+
) -> Result<(Receiver<MeasurementMatch>, impl FnOnce() -> Result<Self>)> {
117+
self.start_measurement_matching(LogicPortPins::default(), sps)
118+
}
119+
108120
/// Start measurements. Returns a tuple of:
109121
/// - [Ppk2<Measuring>],
110122
/// - [Receiver] of [measurement::Result], and
111123
/// - A closure that can be called to stop the measurement parsing pipeline and return the
112124
/// device.
113-
pub fn start_measuring_while_matches(
125+
pub fn start_measurement_matching(
114126
mut self,
115127
pins: LogicPortPins,
116128
sps: usize,

ppk2/src/measurement.rs src/measurement.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,16 @@ pub enum MeasurementMatch {
177177

178178
/// Extension trait for VecDeque<Measurement>
179179
pub trait MeasurementIterExt {
180-
/// Combine items into a single [Measurement], if there are items.
180+
/// Combine items into a single [MeasurementMatch::Match], if there are items.
181+
/// If there are none, [MeasurementMatch::NoMatch] is returned.
182+
/// Set combined logic port pin high if and only if more than half
183+
/// of the measurements indicate the pin was high
181184
fn combine(self, missed: usize) -> MeasurementMatch;
182185

183-
/// Combine items with matching logic port pins into a single [Measurement], if any.
186+
/// Combine items with matching logic port state into a single [MeasurementMatch::Match],
187+
/// if there are items. If there are none, [MeasurementMatch::NoMatch] is returned.
188+
/// Set combined logic port pin high if and only if more than half
189+
/// of the measurements indicate the pin was high
184190
fn combine_matching(self, missed: usize, matching_pins: LogicPortPins) -> MeasurementMatch;
185191
}
186192

@@ -205,6 +211,8 @@ impl<I: Iterator<Item = Measurement>> MeasurementIterExt for I {
205211
return MeasurementMatch::NoMatch;
206212
}
207213

214+
// Set combined pin high if and only if more than half
215+
// of the measurements indicate the pin was high
208216
let mut pins = [false; 8];
209217
pin_high_count
210218
.into_iter()

ppk2/src/types.rs src/types.rs

File renamed without changes.

0 commit comments

Comments
 (0)