-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4006447
commit c0f1d91
Showing
4 changed files
with
939 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
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), | ||
} | ||
} | ||
} |
Oops, something went wrong.