#![allow(non_upper_case_globals)]
#![allow(missing_docs)]
#![allow(missing_debug_implementations)]
#![allow(missing_copy_implementations)]
use std::mem;
use bitfield_register;
use bitfield_register_macro::register;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Register {
Convert = 0x00,
Config = 0x01,
Lowthresh = 0x02,
Hithresh = 0x03,
}
#[register()]
pub struct Config {
#[bitfield(at = 15)]
os: ConfigOs,
#[bitfield(from = 12, to = 14)]
mux: ConfigMux,
#[bitfield(from = 9, to = 11)]
pga: ConfigPga,
#[bitfield(at = 8)]
mode: ConfigMode,
#[bitfield(from = 5, to = 7)]
dr: ConfigDr,
#[bitfield(at = 4)]
cmode: ConfigCmode,
#[bitfield(at = 3)]
cpol: ConfigCpol,
#[bitfield(at = 2)]
clat: ConfigClat,
#[bitfield(from = 0, to = 1)]
cque: ConfigCque,
}
impl Into<u16> for Config {
fn into(self) -> u16 {
(self.0[0] as u16) << 8 | self.0[1] as u16
}
}
impl From<u16> for Config {
fn from(value: u16) -> Self {
Config([((value >> 8) & 0xff) as u8, (value & 0xff) as u8])
}
}
macro_rules! cast_u8_bitfield {
($t:ty,forbidden: [$($e:expr),*]) => {
impl bitfield_register::FromBitfield<[u8; 1]> for $t {
fn from_bitfield(value: [u8; 1]) -> Self {
unsafe { mem::transmute(value[0]) }
}
}
impl bitfield_register::IntoBitfield<[u8; 1]> for $t {
fn into_bitfield(self) -> [u8; 1] {
[self as u8]
}
}
};
($t:ty) => {
cast_u8_bitfield!($t, forbidden: []);
};
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigOs {
Noop = 0b0,
Single = 0b1,
}
cast_u8_bitfield!(ConfigOs);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigMux {
Diff0_1 = 0b000,
Diff0_3 = 0b001,
Diff1_3 = 0b010,
Diff2_3 = 0b011,
Single0 = 0b100,
Single1 = 0b101,
Single2 = 0b110,
Single3 = 0b111,
}
cast_u8_bitfield!(ConfigMux);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigPga {
_6_144V = 0b000,
_4_096V = 0b001,
_2_048V = 0b010,
_1_024V = 0b011,
_0_512V = 0b100,
_0_256V = 0b101,
}
cast_u8_bitfield!(ConfigPga);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigMode {
Contin = 0b0,
Single = 0b1,
}
cast_u8_bitfield!(ConfigMode);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigDr {
_128SPS = 0b000,
_250SPS = 0b001,
_490SPS = 0b010,
_920SPS = 0b011,
_1600SPS = 0b100,
_2400SPS = 0b101,
_3300SPS = 0b110,
}
cast_u8_bitfield!(ConfigDr);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigCmode {
Trad = 0b0,
Window = 0b1,
}
cast_u8_bitfield!(ConfigCmode);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigCpol {
Actvlow = 0b0,
Actvhi = 0b1,
}
cast_u8_bitfield!(ConfigCpol);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigClat {
Nonlat = 0b0,
Latch = 0b1,
}
cast_u8_bitfield!(ConfigClat);
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ConfigCque {
Conv1 = 0b00,
Conv2 = 0b01,
Conv4 = 0b10,
None = 0b11,
}
cast_u8_bitfield!(ConfigCque);