#![cfg_attr(not(test), no_std)]
#[cfg(feature = "async")]
pub mod r#async;
pub mod blocking;
pub const BH1730FVC_ADDR: u8 = 0x29;
const BH1730FVC_RESET_CMD: u8 = 0xE4;
const TINT_US: f32 = 2.8;
fn calculate_lux(data0: u16, data1: u16, gain: f32, integration_time_ms: f32) -> f32 {
let data0 = data0 as f32;
let data1 = data1 as f32;
let ratio = data1 / data0;
let lux = if ratio < 0.26 {
(1.290 * data0 - 2.733 * data1) / (gain * 102.6 / integration_time_ms)
} else if ratio < 0.55 {
(0.795 * data0 - 0.859 * data1) / (gain * 102.6 / integration_time_ms)
} else if ratio < 1.09 {
(0.510 * data0 - 0.345 * data1) / (gain * 102.6 / integration_time_ms)
} else if ratio < 2.13 {
(0.276 * data0 - 0.130 * data1) / (gain * 102.6 / integration_time_ms)
} else {
0.0
};
lux
}
fn itime_ms_to_itime(itime_ms: f32) -> u8 {
let itime = 256.0 - (itime_ms * 1000.0 / (TINT_US * 964.0));
itime as u8
}
fn itime_to_itime_ms(itime: u8) -> f32 {
let itime_ms = (TINT_US * 964.0 * (256.0 - itime as f32)) / 1000.0;
itime_ms
}
type Result<T> = core::result::Result<T, BH1730FVCError>;
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum BH1730FVCError {
ReadI2CError,
WriteI2CError,
NoDataAvailable,
}
pub enum DataRegister {
Control = 0x00,
Timing = 0x01,
Interrupt = 0x02,
InterruptLowThresholdLow = 0x03,
InterruptLowThresholdHigh = 0x04,
InterruptHighThresholdLow = 0x05,
InterruptHighThresholdHigh = 0x06,
Gain = 0x07,
ID = 0x12,
Data0Low = 0x14,
Data0High = 0x15,
Data1Low = 0x16,
Data1High = 0x17,
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
pub enum Gain {
X1 = 0x0,
X2 = 0x1,
X64 = 0x2,
X128 = 0x3,
}
impl From<Gain> for f32 {
fn from(gain: Gain) -> Self {
match gain {
Gain::X1 => 1.0,
Gain::X2 => 2.0,
Gain::X64 => 64.0,
Gain::X128 => 128.0,
}
}
}
impl Gain {
pub fn into_reg_value(self) -> u8 {
self as u8
}
}
#[derive(Copy, Clone, Debug)]
pub enum Mode {
PowerDown = 0x00,
SingleShot = 0x01,
Continuous = 0x02,
}