pub(crate) trait RegisterBits {
fn mask() -> u8;
fn value(&self) -> u8;
}
#[derive(Copy, Clone, Debug)]
#[allow(non_camel_case_types)]
pub enum AccelDataRate {
_10_Hz = 0x10,
_50_Hz = 0x20,
_100_Hz = 0x30,
_200_Hz = 0x40,
_400_Hz = 0x50,
_800_Hz = 0x60,
}
impl Default for AccelDataRate {
fn default() -> Self {
AccelDataRate::_100_Hz
}
}
impl RegisterBits for AccelDataRate {
fn mask() -> u8 {
AccelDataRate::_800_Hz.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
#[derive(Clone, Copy, Debug)]
#[allow(non_camel_case_types)]
pub enum AccelScale {
_2G,
_4G,
_8G,
}
impl Default for AccelScale {
fn default() -> Self {
AccelScale::_2G
}
}
impl RegisterBits for AccelScale {
fn mask() -> u8 {
AccelScale::_8G.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
impl AccelScale {
pub(crate) fn resolution(&self) -> f32 {
match self {
AccelScale::_2G => 2.0 / 32768.0,
AccelScale::_4G => 4.0 / 32768.0,
AccelScale::_8G => 8.0 / 32768.0,
}
}
}
#[derive(Clone, Copy, Debug)]
#[allow(non_camel_case_types)]
pub enum MagDataRate {
_0_625_Hz = 0x00,
_1_25_Hz = 0x04,
_2_5_Hz = 0x08,
_5_Hz = 0x0C,
_10_Hz = 0x10,
_20_Hz = 0x14,
_40_Hz = 0x18,
_80_Hz = 0x1C,
}
impl Default for MagDataRate {
fn default() -> Self {
MagDataRate::_40_Hz
}
}
impl RegisterBits for MagDataRate {
fn mask() -> u8 {
MagDataRate::_80_Hz.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
#[derive(Debug, Clone, Copy)]
#[allow(non_camel_case_types)]
pub enum MagScale {
_16_Ga = 0x60,
}
impl Default for MagScale {
fn default() -> Self {
MagScale::_16_Ga
}
}
impl RegisterBits for MagScale {
fn mask() -> u8 {
MagScale::_16_Ga.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
impl MagScale {
pub(crate) fn resolution(&self) -> f32 {
match self {
MagScale::_16_Ga => 0.58,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum MagMode {
Continous = 0x00,
Single = 0x01,
PowerDown1 = 0x02,
PowerDown2 = 0x03,
}
impl RegisterBits for MagMode {
fn mask() -> u8 {
MagMode::PowerDown2.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
impl Default for MagMode {
fn default() -> Self {
MagMode::Continous
}
}
#[derive(Debug, Clone, Copy)]
pub enum MagXYOperativeMode {
LowPower = 0x00,
MediumPerformance = 0x20,
HighPerformance = 0x40,
UltraHighPerformance = 0x60,
}
impl RegisterBits for MagXYOperativeMode {
fn mask() -> u8 {
MagXYOperativeMode::UltraHighPerformance.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
impl Default for MagXYOperativeMode {
fn default() -> Self {
MagXYOperativeMode::HighPerformance
}
}
#[derive(Debug, Clone, Copy)]
pub enum MagZOperativeMode {
LowPower = 0x00,
MediumPerformance = 0x04,
HighPerformance = 0x08,
UltraHighPerformance = 0x0C,
}
impl RegisterBits for MagZOperativeMode {
fn mask() -> u8 {
MagZOperativeMode::UltraHighPerformance.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
impl Default for MagZOperativeMode {
fn default() -> Self {
MagZOperativeMode::HighPerformance
}
}
#[derive(Debug, Clone, Copy)]
pub enum TempControl {
Enable = 0x80,
Disable = 0x00,
}
impl Default for TempControl {
fn default() -> Self {
TempControl::Enable
}
}
impl RegisterBits for TempControl {
fn mask() -> u8 {
TempControl::Enable.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
#[derive(Debug, Clone, Copy)]
pub enum MagBlockDataUpdate {
Enable = 0x40,
Disable = 0x00,
}
impl Default for MagBlockDataUpdate {
fn default() -> Self {
MagBlockDataUpdate::Enable
}
}
impl RegisterBits for MagBlockDataUpdate {
fn mask() -> u8 {
MagBlockDataUpdate::Enable.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
#[derive(Debug, Clone, Copy)]
pub enum AccelBlockDataUpdate {
Enable = 0x08,
Disable = 0x00,
}
impl Default for AccelBlockDataUpdate {
fn default() -> Self {
AccelBlockDataUpdate::Enable
}
}
impl RegisterBits for AccelBlockDataUpdate {
fn mask() -> u8 {
AccelBlockDataUpdate::Enable.value()
}
fn value(&self) -> u8 {
*self as u8
}
}
#[derive(Debug, Clone, Copy)]
pub struct AccelAxesControl {
x_axis: bool,
y_axis: bool,
z_axis: bool,
}
impl AccelAxesControl {
pub fn all_enabled() -> Self {
AccelAxesControl { x_axis: true,
y_axis: true,
z_axis: true }
}
pub fn all_disabled() -> Self {
AccelAxesControl { x_axis: true,
y_axis: true,
z_axis: true }
}
pub fn custom(x_axis: bool, y_axis: bool, z_axis: bool) -> Self {
AccelAxesControl { x_axis,
y_axis,
z_axis }
}
}
impl Default for AccelAxesControl {
fn default() -> Self {
AccelAxesControl::all_enabled()
}
}
impl RegisterBits for AccelAxesControl {
fn mask() -> u8 {
AccelAxesControl::all_enabled().value()
}
fn value(&self) -> u8 {
let mut res: u8 = 0;
if self.x_axis {
res |= 0x01;
}
if self.y_axis {
res |= 0x02;
}
if self.z_axis {
res |= 0x04;
}
res
}
}
#[derive(Copy, Clone, Debug)]
pub struct LsmConfig {
pub(crate) accel_scale: Option<AccelScale>,
pub(crate) mag_scale: Option<MagScale>,
pub(crate) accel_data_rate: Option<AccelDataRate>,
pub(crate) mag_data_rate: Option<MagDataRate>,
pub(crate) accel_block_data_update: Option<AccelBlockDataUpdate>,
pub(crate) mag_block_data_update: Option<MagBlockDataUpdate>,
pub(crate) mag_mode: Option<MagMode>,
pub(crate) mag_xy_operative_mode: Option<MagXYOperativeMode>,
pub(crate) mag_z_operative_mode: Option<MagZOperativeMode>,
pub(crate) accel_axes_control: Option<AccelAxesControl>,
pub(crate) temp_control: Option<TempControl>,
}
impl LsmConfig {
pub fn new() -> Self {
LsmConfig { accel_scale: None,
mag_scale: None,
accel_data_rate: None,
mag_data_rate: None,
accel_block_data_update: None,
mag_block_data_update: None,
mag_mode: None,
mag_xy_operative_mode: None,
mag_z_operative_mode: None,
accel_axes_control: None,
temp_control: None }
}
pub fn accel_scale(&mut self, accel_scale: AccelScale) -> &mut Self {
self.accel_scale = Some(accel_scale);
self
}
pub fn mag_scale(&mut self, mag_scale: MagScale) -> &mut Self {
self.mag_scale = Some(mag_scale);
self
}
pub fn accel_data_rate(&mut self,
accel_data_rate: AccelDataRate)
-> &mut Self {
self.accel_data_rate = Some(accel_data_rate);
self
}
pub fn mag_data_rate(&mut self, mag_data_rate: MagDataRate) -> &mut Self {
self.mag_data_rate = Some(mag_data_rate);
self
}
pub fn accel_block_data_update(&mut self,
accel_block_data_update: AccelBlockDataUpdate)
-> &mut Self {
self.accel_block_data_update = Some(accel_block_data_update);
self
}
pub fn mag_block_data_update(&mut self,
mag_block_data_update: MagBlockDataUpdate)
-> &mut Self {
self.mag_block_data_update = Some(mag_block_data_update);
self
}
pub fn mag_mode(&mut self, mag_mode: MagMode) -> &mut Self {
self.mag_mode = Some(mag_mode);
self
}
pub fn mag_xy_operative_mode(&mut self,
operative_mode: MagXYOperativeMode)
-> &mut Self {
self.mag_xy_operative_mode = Some(operative_mode);
self
}
pub fn mag_z_operative_mode(&mut self,
operative_mode: MagZOperativeMode)
-> &mut Self {
self.mag_z_operative_mode = Some(operative_mode);
self
}
pub fn accel_axes_control(&mut self,
axes_control: AccelAxesControl)
-> &mut Self {
self.accel_axes_control = Some(axes_control);
self
}
pub fn temp_control(&mut self, temp_control: TempControl) -> &mut Self {
self.temp_control = Some(temp_control);
self
}
}