use core::convert::Infallible;
use regiface::{register, FromByteArray, ReadableRegister, ToByteArray, WritableRegister};
#[register(0x0580u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub struct DioOutputEnable {
pub dio1: bool,
pub dio2: bool,
pub dio3: bool,
}
#[register(0x0583u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub struct DioInputEnable {
pub dio1: bool,
pub dio2: bool,
pub dio3: bool,
}
#[register(0x0584u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub struct DioPullUpControl {
pub dio1: bool,
pub dio2: bool,
pub dio3: bool,
}
#[register(0x0585u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub struct DioPullDownControl {
pub dio1: bool,
pub dio2: bool,
pub dio3: bool,
}
#[derive(Debug)]
pub struct InvalidVoltageError {
pub value: u8,
}
#[register(0x0920u16)]
#[derive(Debug, Clone, Copy, ReadableRegister, WritableRegister)]
pub enum Dio3OutputVoltage {
V1_6 = 0x00,
V1_7 = 0x01,
V1_8 = 0x02,
V2_2 = 0x03,
V2_4 = 0x04,
V2_7 = 0x05,
V3_0 = 0x06,
V3_3 = 0x07,
}
impl FromByteArray for DioOutputEnable {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
dio1: bytes[0] & 0x01 != 0,
dio2: bytes[0] & 0x02 != 0,
dio3: bytes[0] & 0x04 != 0,
})
}
}
impl ToByteArray for DioOutputEnable {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([(self.dio1 as u8) | ((self.dio2 as u8) << 1) | ((self.dio3 as u8) << 2)])
}
}
impl FromByteArray for DioInputEnable {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
dio1: bytes[0] & 0x01 != 0,
dio2: bytes[0] & 0x02 != 0,
dio3: bytes[0] & 0x04 != 0,
})
}
}
impl ToByteArray for DioInputEnable {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([(self.dio1 as u8) | ((self.dio2 as u8) << 1) | ((self.dio3 as u8) << 2)])
}
}
impl FromByteArray for DioPullUpControl {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
dio1: bytes[0] & 0x01 != 0,
dio2: bytes[0] & 0x02 != 0,
dio3: bytes[0] & 0x04 != 0,
})
}
}
impl ToByteArray for DioPullUpControl {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([(self.dio1 as u8) | ((self.dio2 as u8) << 1) | ((self.dio3 as u8) << 2)])
}
}
impl FromByteArray for DioPullDownControl {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
dio1: bytes[0] & 0x01 != 0,
dio2: bytes[0] & 0x02 != 0,
dio3: bytes[0] & 0x04 != 0,
})
}
}
impl ToByteArray for DioPullDownControl {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([(self.dio1 as u8) | ((self.dio2 as u8) << 1) | ((self.dio3 as u8) << 2)])
}
}
impl FromByteArray for Dio3OutputVoltage {
type Error = InvalidVoltageError;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
match bytes[0] & 0x07 {
0x00 => Ok(Self::V1_6),
0x01 => Ok(Self::V1_7),
0x02 => Ok(Self::V1_8),
0x03 => Ok(Self::V2_2),
0x04 => Ok(Self::V2_4),
0x05 => Ok(Self::V2_7),
0x06 => Ok(Self::V3_0),
0x07 => Ok(Self::V3_3),
invalid => Err(InvalidVoltageError { value: invalid }),
}
}
}
impl ToByteArray for Dio3OutputVoltage {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self as u8])
}
}