use core::convert::Infallible;
use regiface::{register, FromByteArray, ReadableRegister, ToByteArray, WritableRegister};
#[register(0x0819u16)]
#[derive(Debug, Clone, Copy, ReadableRegister)]
pub struct RandomNumber {
pub value: u32,
}
#[register(0x0889u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub struct TxModulation {
pub bw_500khz_opt: bool,
}
impl Default for TxModulation {
fn default() -> Self {
Self {
bw_500khz_opt: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InvalidGainMode(pub u8);
#[register(0x08ACu16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub enum RxGain {
PowerSaving,
Boosted,
}
impl Default for RxGain {
fn default() -> Self {
Self::PowerSaving
}
}
impl RxGain {
pub fn from_byte(value: u8) -> Result<Self, InvalidGainMode> {
match value {
0x94 => Ok(Self::PowerSaving),
0x96 => Ok(Self::Boosted),
invalid => Err(InvalidGainMode(invalid)),
}
}
pub fn to_byte(self) -> u8 {
match self {
Self::PowerSaving => 0x94,
Self::Boosted => 0x96,
}
}
}
#[register(0x08D8u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub struct TxClampConfig {
pub threshold: u8,
}
impl Default for TxClampConfig {
fn default() -> Self {
Self { threshold: 0x4 }
}
}
#[register(0x08E7u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub struct OcpConfiguration {
pub threshold: u8,
}
impl Default for OcpConfiguration {
fn default() -> Self {
Self {
threshold: 0x18, }
}
}
impl FromByteArray for RandomNumber {
type Error = Infallible;
type Array = [u8; 4];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
value: u32::from_be_bytes(bytes),
})
}
}
impl FromByteArray for TxModulation {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
bw_500khz_opt: bytes[0] & 0x04 != 0,
})
}
}
impl ToByteArray for TxModulation {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([if self.bw_500khz_opt { 0x04 } else { 0x00 }])
}
}
impl FromByteArray for RxGain {
type Error = InvalidGainMode;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Self::from_byte(bytes[0])
}
}
impl ToByteArray for RxGain {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self.to_byte()])
}
}
impl FromByteArray for TxClampConfig {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
threshold: (bytes[0] >> 1) & 0x0F,
})
}
}
impl ToByteArray for TxClampConfig {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([(self.threshold & 0x0F) << 1])
}
}
impl FromByteArray for OcpConfiguration {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
threshold: bytes[0],
})
}
}
impl ToByteArray for OcpConfiguration {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self.threshold])
}
}