Skip to content

Commit

Permalink
Create PDO definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertZ2011 committed Feb 7, 2025
1 parent 4006447 commit c0f1d91
Show file tree
Hide file tree
Showing 4 changed files with 939 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
defmt = { version = "0.3", optional = true }
embedded-hal-async = "1.0.0"
bitfield = "0.17.0"

[features]
default = []
Expand Down
109 changes: 109 additions & 0 deletions src/pdo/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,110 @@
//! Power data object (PDO) definitions
//! This module defines source and sink PDOs. Each PDO type has a corresponding *Raw and *Data struct.
//! The raw struct just provides a structured version of the raw PDO data, while the data struct provides
//! a type-safe version.
use crate::PdError;

pub mod sink;
pub mod source;

/// 10 mA unit
const MA10_UNIT: u16 = 10;
/// 50 mV unit
const MV50_UNIT: u16 = 50;
/// 250 mV unit
const MW250_UNIT: u16 = 250;
/// 50 mA unit
const MA50_UNIT: u16 = 50;
/// 100 mV unit
const MV100_UNIT: u16 = 100;
/// 1000 mW unit
const MW1000_UNIT: u16 = 1000;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum PdoKind {
Fixed,
Battery,
Variable,
Augmented,
}

impl From<u32> for PdoKind {
fn from(pdo: u32) -> Self {
const PDO_KIND_SHIFT: u8 = 30;
PdoKind::from((pdo >> PDO_KIND_SHIFT) as u8)
}
}

impl From<u8> for PdoKind {
fn from(value: u8) -> Self {
const PDO_KIND_MASK: u8 = 0x3;
match value & PDO_KIND_MASK {
0x0 => PdoKind::Fixed,
0x1 => PdoKind::Battery,
0x2 => PdoKind::Variable,
0x3 => PdoKind::Augmented,
_ => unreachable!(),
}
}
}

impl From<PdoKind> for u8 {
fn from(value: PdoKind) -> Self {
match value {
PdoKind::Fixed => 0x0,
PdoKind::Battery => 0x1,
PdoKind::Variable => 0x2,
PdoKind::Augmented => 0x3,
}
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ApdoKind {
/// SPR Programable power supply
SprPps,
/// EPR Adjustable voltage supply
EprAvs,
/// SPR Adjustable voltage supply
SprAvs,
}

impl Into<u8> for ApdoKind {

Check failure on line 74 in src/pdo/mod.rs

View workflow job for this annotation

GitHub Actions / stable / clippy (c0f1d91b389fe849cfd6dc19f03f6281147811ab)

[clippy] reported by reviewdog 🐶 error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> src/pdo/mod.rs:74:1 | 74 | impl Into<u8> for ApdoKind { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into = note: `-F clippy::from-over-into` implied by `-F clippy::style` help: replace the `Into` implementation with `From<pdo::ApdoKind>` | 74 ~ impl From<ApdoKind> for u8 { 75 ~ fn from(val: ApdoKind) -> Self { 76 ~ match val { | Raw Output: src/pdo/mod.rs:74:1:e:error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> src/pdo/mod.rs:74:1 | 74 | impl Into<u8> for ApdoKind { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into = note: `-F clippy::from-over-into` implied by `-F clippy::style` help: replace the `Into` implementation with `From<pdo::ApdoKind>` | 74 ~ impl From<ApdoKind> for u8 { 75 ~ fn from(val: ApdoKind) -> Self { 76 ~ match val { | __END__

Check failure on line 74 in src/pdo/mod.rs

View workflow job for this annotation

GitHub Actions / beta / clippy (c0f1d91b389fe849cfd6dc19f03f6281147811ab)

[clippy] reported by reviewdog 🐶 error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> src/pdo/mod.rs:74:1 | 74 | impl Into<u8> for ApdoKind { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into = note: `-F clippy::from-over-into` implied by `-F clippy::style` help: replace the `Into` implementation with `From<pdo::ApdoKind>` | 74 ~ impl From<ApdoKind> for u8 { 75 ~ fn from(val: ApdoKind) -> Self { 76 ~ match val { | Raw Output: src/pdo/mod.rs:74:1:e:error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> src/pdo/mod.rs:74:1 | 74 | impl Into<u8> for ApdoKind { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into = note: `-F clippy::from-over-into` implied by `-F clippy::style` help: replace the `Into` implementation with `From<pdo::ApdoKind>` | 74 ~ impl From<ApdoKind> for u8 { 75 ~ fn from(val: ApdoKind) -> Self { 76 ~ match val { | __END__
fn into(self) -> u8 {
match self {
ApdoKind::SprPps => 0x0,
ApdoKind::EprAvs => 0x1,
ApdoKind::SprAvs => 0x2,
}
}
}

impl TryFrom<u8> for ApdoKind {
type Error = PdError;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x0 => Ok(ApdoKind::SprPps),
0x1 => Ok(ApdoKind::EprAvs),
0x2 => Ok(ApdoKind::SprAvs),
_ => Err(PdError::InvalidParams),
}
}
}

impl TryFrom<u32> for ApdoKind {
type Error = PdError;

fn try_from(value: u32) -> Result<Self, Self::Error> {
const APDO_KIND_SHIFT: u8 = 28;
const APDO_KIND_MASK: u32 = 0x3;
match (value >> APDO_KIND_SHIFT) & APDO_KIND_MASK {
0x0 => Ok(ApdoKind::SprPps),
0x1 => Ok(ApdoKind::EprAvs),
0x2 => Ok(ApdoKind::SprAvs),
_ => Err(PdError::InvalidParams),
}
}
}
Loading

0 comments on commit c0f1d91

Please sign in to comment.