sat-rs/satrs-core/src/res_code.rs

61 lines
1.4 KiB
Rust
Raw Normal View History

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use spacepackets::ecss::{EcssEnumU16, EcssEnumeration};
2023-07-03 00:42:20 +02:00
use spacepackets::util::UnsignedEnum;
use spacepackets::{ByteConversionError, SizeMissmatch};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ResultU16 {
group_id: u8,
unique_id: u8,
}
impl ResultU16 {
pub const fn const_new(group_id: u8, unique_id: u8) -> Self {
Self {
group_id,
unique_id,
}
}
pub fn raw(&self) -> u16 {
((self.group_id as u16) << 8) | self.unique_id as u16
}
pub fn group_id(&self) -> u8 {
self.group_id
}
pub fn unique_id(&self) -> u8 {
self.unique_id
}
}
impl From<ResultU16> for EcssEnumU16 {
fn from(v: ResultU16) -> Self {
EcssEnumU16::new(v.raw())
}
}
2023-07-03 00:42:20 +02:00
impl UnsignedEnum for ResultU16 {
fn size(&self) -> usize {
core::mem::size_of::<u16>()
}
2023-07-03 00:42:20 +02:00
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
if buf.len() < 2 {
return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: buf.len(),
expected: 2,
}));
}
buf[0] = self.group_id;
buf[1] = self.unique_id;
2023-07-03 00:42:20 +02:00
Ok(self.size())
}
}
impl EcssEnumeration for ResultU16 {
fn pfc(&self) -> u8 {
16
}
}