Skip to content

Commit

Permalink
Merge pull request #15 from fmckeogh/units
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckeogh authored May 23, 2024
2 parents dabad9d + c5139d6 commit a8c7ab6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 34 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pd-interceptor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ fusb302b = { path = "../fusb302b" }
usb-pd = { path = "../usb-pd" }
panic-probe = { version = "0.3.1", features = ["print-defmt"] }
aht20-async = "1.0.0"

uom = { version = "0.36.0", default-features = false, features = ["si"] }
17 changes: 10 additions & 7 deletions pd-interceptor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use {
embassy_time::Instant,
fusb302b::Fusb302b,
panic_probe as _,
uom::si::{electric_current::milliampere, electric_potential::millivolt},
usb_pd::{
messages::pdo::PowerDataObject,
sink::{Event, Request, Sink},
Expand Down Expand Up @@ -111,8 +112,8 @@ fn handle_event(event: Event) -> Option<Request> {
debug!(
"supply @ {}: {}mV {}mA",
i,
supply.voltage_mv(),
supply.max_current_ma()
supply.voltage().get::<millivolt>(),
supply.max_current().get::<milliampere>(),
);
Some((i, supply))
} else {
Expand All @@ -122,12 +123,14 @@ fn handle_event(event: Event) -> Option<Request> {
.max_by(|(_, x), (_, y)| x.raw_voltage().cmp(&y.raw_voltage()))
.unwrap();

info!("requesting supply {:?}@{}", supply, index);

return Some(Request::RequestPower {
let req = Request::RequestPower {
index,
current: supply.max_current_ma(),
});
current: supply.raw_max_current(),
};

info!("requesting {}", req);

return Some(req);
}
Event::PowerReady => info!("power ready"),
Event::ProtocolChanged => info!("protocol changed"),
Expand Down
1 change: 1 addition & 0 deletions usb-pd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ byteorder = { version = "1.5.0", default-features = false }
defmt = "0.3.6"
heapless = { version = "0.8.0", features = ["defmt-03"] }
embassy-time = "0.3.0"
uom = { version = "0.36.0", default-features = false, features = ["autoconvert", "si", "u8", "u16", "u32"] }
3 changes: 3 additions & 0 deletions usb-pd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![no_std]

#[macro_use]
extern crate uom;

pub mod header;
pub mod messages;
pub mod sink;
Expand Down
89 changes: 62 additions & 27 deletions usb-pd/src/messages/pdo.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
use {
_250milliwatts_mod::_250milliwatts,
_50milliamperes_mod::_50milliamperes,
_50millivolts_mod::_50millivolts,
byteorder::{ByteOrder, LittleEndian},
defmt::Format,
heapless::Vec,
proc_bitfield::bitfield,
uom::si::{self, electric_current::centiampere, electric_potential::decivolt, power::watt},
};

mod _50milliamperes_mod {
unit! {
system: uom::si;
quantity: uom::si::electric_current;

@_50milliamperes: 0.05; "_50mA", "_50milliamps", "_50milliamps";
}
}

mod _50millivolts_mod {
unit! {
system: uom::si;
quantity: uom::si::electric_potential;

@_50millivolts: 0.05; "_50mV", "_50millivolts", "_50millivolts";
}
}

mod _250milliwatts_mod {
unit! {
system: uom::si;
quantity: uom::si::power;

@_250milliwatts: 0.25; "_250mW", "_250milliwatts", "_250milliwatts";
}
}

#[derive(Clone, Copy, Debug, Format)]
pub enum PowerDataObject {
FixedSupply(FixedSupply),
Expand Down Expand Up @@ -50,12 +81,12 @@ bitfield! {
}

impl FixedSupply {
pub fn voltage_mv(&self) -> u16 {
self.raw_voltage() * 50
pub fn voltage(&self) -> si::u16::ElectricPotential {
si::u16::ElectricPotential::new::<_50millivolts>(self.raw_voltage())
}

pub fn max_current_ma(&self) -> u16 {
self.raw_max_current() * 10
pub fn max_current(&self) -> si::u16::ElectricCurrent {
si::u16::ElectricCurrent::new::<centiampere>(self.raw_max_current())
}
}

Expand All @@ -74,16 +105,16 @@ bitfield! {
}

impl Battery {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 50
pub fn max_voltage(&self) -> si::u16::ElectricPotential {
si::u16::ElectricPotential::new::<_50millivolts>(self.raw_max_voltage())
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 50
pub fn min_voltage(&self) -> si::u16::ElectricPotential {
si::u16::ElectricPotential::new::<_50millivolts>(self.raw_min_voltage())
}

pub fn max_power_mw(&self) -> u32 {
u32::from(self.raw_max_power()) * 250
pub fn max_power(&self) -> si::u16::Power {
si::u16::Power::new::<_250milliwatts>(self.raw_max_power())
}
}

Expand All @@ -102,16 +133,16 @@ bitfield! {
}

impl VariableSupply {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 50
pub fn max_voltage(&self) -> si::u16::ElectricPotential {
si::u16::ElectricPotential::new::<_50millivolts>(self.raw_max_voltage())
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 50
pub fn min_voltage(&self) -> si::u16::ElectricPotential {
si::u16::ElectricPotential::new::<_50millivolts>(self.raw_min_voltage())
}

pub fn max_current_ma(&self) -> u16 {
u16::from(self.raw_max_current()) * 10
pub fn max_current(&self) -> si::u16::ElectricCurrent {
si::u16::ElectricCurrent::new::<centiampere>(self.raw_max_current())
}
}

Expand Down Expand Up @@ -150,16 +181,16 @@ bitfield! {
}

impl SPRProgrammablePowerSupply {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 100
pub fn max_voltage(&self) -> si::u8::ElectricPotential {
si::u8::ElectricPotential::new::<decivolt>(self.raw_max_voltage())
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 100
pub fn min_voltage(&self) -> si::u8::ElectricPotential {
si::u8::ElectricPotential::new::<decivolt>(self.raw_min_voltage())
}

pub fn max_current_ma(&self) -> u16 {
u16::from(self.raw_max_current()) * 50
pub fn max_current(&self) -> si::u8::ElectricCurrent {
si::u8::ElectricCurrent::new::<_50milliamperes>(self.raw_max_current())
}
}

Expand All @@ -176,17 +207,21 @@ bitfield! {
/// Minimum Voltage in 100mV increments
pub raw_min_voltage: u8 @ 8..=15,
/// PDP in 1W increments
pub pd_power: u8 @ 0..=7,
pub raw_pd_power: u8 @ 0..=7,
}
}

impl EPRAdjustableVoltageSupply {
pub fn max_voltage_mv(&self) -> u16 {
u16::from(self.raw_max_voltage()) * 100
pub fn max_voltage(&self) -> si::u16::ElectricPotential {
si::u16::ElectricPotential::new::<decivolt>(self.raw_max_voltage())
}

pub fn min_voltage(&self) -> si::u8::ElectricPotential {
si::u8::ElectricPotential::new::<decivolt>(self.raw_min_voltage())
}

pub fn min_voltage_mv(&self) -> u16 {
u16::from(self.raw_min_voltage()) * 100
pub fn pd_power(&self) -> si::u8::Power {
si::u8::Power::new::<watt>(self.raw_pd_power())
}
}

Expand Down

0 comments on commit a8c7ab6

Please sign in to comment.