Skip to content

Commit

Permalink
Add correct unit types for 50mA, 50mV, and 250mW quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckeogh committed May 21, 2024
1 parent b8a1515 commit c5139d6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 39 deletions.
1 change: 1 addition & 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"] }
7 changes: 4 additions & 3 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 @@ -109,10 +110,10 @@ fn handle_event(event: Event) -> Option<Request> {
.filter_map(|(i, cap)| {
if let PowerDataObject::FixedSupply(supply) = cap {
debug!(
"supply @ {}: {}V {}A",
"supply @ {}: {}mV {}mA",
i,
supply.voltage().value,
supply.max_current().value
supply.voltage().get::<millivolt>(),
supply.max_current().get::<milliampere>(),
);
Some((i, supply))
} else {
Expand Down
2 changes: 1 addition & 1 deletion usb-pd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +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", "u16", "u32"] }
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
94 changes: 59 additions & 35 deletions usb-pd/src/messages/pdo.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
use {
_250milliwatts_mod::_250milliwatts,
_50milliamperes_mod::_50milliamperes,
_50millivolts_mod::_50millivolts,
byteorder::{ByteOrder, LittleEndian},
defmt::Format,
heapless::Vec,
proc_bitfield::bitfield,
uom::si::{
electric_current::milliampere,
electric_potential::millivolt,
power::{milliwatt, watt},
u16::{ElectricCurrent, ElectricPotential},
u32::Power,
},
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 @@ -57,12 +81,12 @@ bitfield! {
}

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

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

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

impl Battery {
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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(&self) -> Power {
Power::new::<milliwatt>(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 @@ -109,16 +133,16 @@ bitfield! {
}

impl VariableSupply {
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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(&self) -> ElectricCurrent {
ElectricCurrent::new::<milliampere>(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 @@ -157,16 +181,16 @@ bitfield! {
}

impl SPRProgrammablePowerSupply {
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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(&self) -> ElectricCurrent {
ElectricCurrent::new::<milliampere>(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 @@ -188,16 +212,16 @@ bitfield! {
}

impl EPRAdjustableVoltageSupply {
pub fn max_voltage(&self) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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) -> ElectricPotential {
ElectricPotential::new::<millivolt>(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 pd_power(&self) -> Power {
Power::new::<watt>(u32::from(self.raw_pd_power()))
pub fn pd_power(&self) -> si::u8::Power {
si::u8::Power::new::<watt>(self.raw_pd_power())
}
}

Expand Down

0 comments on commit c5139d6

Please sign in to comment.