32#include "../../SensorBuildOpt.h"
33#if !SENSORLIB_EXCLUDE_CM32181
35#include "../../platform/comm/I2CDeviceNoHal.hpp"
45static constexpr uint8_t CM32181_ADDR_PRIMARY = 0x10;
50static constexpr uint8_t CM32181_ADDR_SECONDARY = 0x48;
55static constexpr uint8_t CM32181_SLAVE_ADDRESS = 0x10;
163 data &= (uint16_t)~ALS_CONF_SENS_MASK;
164 data |= (uint16_t)(sampling & 0x03u) << ALS_CONF_SENS_SHIFT;
167 data &= (uint16_t)~ALS_CONF_IT_MASK;
168 data |= (uint16_t)(int_time & 0x0Fu) << ALS_CONF_IT_SHIFT;
182 uint8_t buffer[2] = {0};
211 data &= (uint16_t)~ALS_PSM_MODE_MASK;
212 data &= (uint16_t)~ALS_PSM_EN_BIT;
215 data |= (uint16_t)(mode & 0x03u) << ALS_PSM_MODE_SHIFT;
219 data |= ALS_PSM_EN_BIT;
298 uint8_t buffer[2] = {0};
300 return (uint16_t)buffer[0] | (uint16_t)(buffer[1] << 8);
320 uint8_t buffer[2] = {0};
327 static constexpr uint8_t REG_ALS_CONF = 0x00;
328 static constexpr uint8_t REG_ALS_THDH = 0x01;
329 static constexpr uint8_t REG_ALS_THDL = 0x02;
330 static constexpr uint8_t REG_ALS_PSM = 0x03;
331 static constexpr uint8_t REG_ALS_DATA = 0x04;
332 static constexpr uint8_t REG_ALS_STATUS = 0x06;
333 static constexpr uint8_t REG_ID = 0x07;
336 static constexpr uint8_t CM32181_CHIP_ID = 0x81;
340 static constexpr uint16_t ALS_CONF_SENS_SHIFT = 11;
341 static constexpr uint16_t ALS_CONF_SENS_MASK = (uint16_t)(0x03u << ALS_CONF_SENS_SHIFT);
343 static constexpr uint16_t ALS_CONF_IT_SHIFT = 6;
344 static constexpr uint16_t ALS_CONF_IT_MASK = (uint16_t)(0x0Fu << ALS_CONF_IT_SHIFT);
347 static constexpr uint16_t ALS_PSM_EN_BIT = (uint16_t)(1u << 0);
348 static constexpr uint16_t ALS_PSM_MODE_SHIFT = 1;
349 static constexpr uint16_t ALS_PSM_MODE_MASK = (uint16_t)(0x03u << ALS_PSM_MODE_SHIFT);
354 bool initImpl(uint8_t param)
override
364 if (chipID != CM32181_CHIP_ID) {
#define SENSORLIB_LOG_I(...)
High Sensitivity I2C Ambient Light Sensor (CM32181) driver.
~SensorCM32181()=default
Destructor.
void setSampling(Sampling sampling=SAMPLING_X1, IntegrationTime int_time=INTEGRATION_TIME_200MS)
Configure ALS sensitivity and integration time.
InterruptEvent getIrqStatus()
Read IRQ status (and clear interrupt by reading status register).
void enableINT()
Enable interrupt function (REG_ALS_CONF bit1).
void disableINT()
Disable interrupt function (REG_ALS_CONF bit1).
SensorCM32181()=default
Construct a CM32181 driver instance.
void powerOn()
Power on sensor (REG_ALS_CONF bit0 = 0).
IntegrationTime
ALS integration time (REG_ALS_CONF bits 9:6).
@ INTEGRATION_TIME_800MS
800ms
@ INTEGRATION_TIME_50MS
25ms
@ INTEGRATION_TIME_400MS
400ms
@ INTEGRATION_TIME_200MS
200ms
@ INTEGRATION_TIME_100MS
100ms
@ INTEGRATION_TIME_25MS
25ms
void powerSave(PowerSaveMode mode, bool enable)
Configure power saving mode.
InterruptEvent
Interrupt event type reported by getIrqStatus().
@ ALS_EVENT_HIGH_TRIGGER
High threshold window interrupt.
@ ALS_EVENT_NULL
No interrupt event.
@ ALS_EVENT_LOW_TRIGGER
Low threshold window interrupt.
float getLux()
Convert raw ALS to lux (approximate).
const float calibration_factor
Fixed raw-to-lux conversion factor (datasheet/manual derived).
uint16_t getRaw()
Read raw ALS data (16-bit).
void powerDown()
Shutdown sensor (REG_ALS_CONF bit0 = 1).
PowerSaveMode
Power saving mode (REG_ALS_PSM bits 2:1).
void setIntThreshold(uint16_t low_threshold, uint16_t high_threshold)
Set ALS interrupt thresholds.
int getChipID()
Read chip ID.
Sampling
ALS sensitivity selection (REG_ALS_CONF bits 12:11).
@ SAMPLING_X1_8
ALS Sensitivity x(1/8)
@ SAMPLING_X1_4
ALS Sensitivity x(1/4)
@ SAMPLING_X2
ALS Sensitivity x2.
@ SAMPLING_X1
ALS Sensitivity x1.
int writeRegBuff(uint8_t reg, uint8_t *buf, size_t len)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
void _bitClear(T &value, uint8_t bit)
constexpr uint8_t _lowByte(uint16_t w)
void _bitWrite(T &value, uint8_t bit, bool bitvalue)
void _bitSet(T &value, uint8_t bit)
constexpr uint8_t _highByte(uint16_t w)
constexpr T _bitRead(T value, uint8_t bit)