SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorCM32181 Class Reference

High Sensitivity I2C Ambient Light Sensor (CM32181) driver. More...

#include <SensorCM32181.hpp>

Inheritance diagram for SensorCM32181:
[legend]

Public Types

enum  Sampling : uint8_t { SAMPLING_X1 = 0 , SAMPLING_X2 = 1 , SAMPLING_X1_8 = 2 , SAMPLING_X1_4 = 3 }
 ALS sensitivity selection (REG_ALS_CONF bits 12:11). More...
 
enum  IntegrationTime : uint8_t {
  INTEGRATION_TIME_25MS = 0x0C , INTEGRATION_TIME_50MS = 0x08 , INTEGRATION_TIME_100MS = 0x00 , INTEGRATION_TIME_200MS = 0x01 ,
  INTEGRATION_TIME_400MS = 0x02 , INTEGRATION_TIME_800MS = 0x03
}
 ALS integration time (REG_ALS_CONF bits 9:6). More...
 
enum  PowerSaveMode : uint8_t { PowerSaveMode1 = 0 , PowerSaveMode2 = 1 , PowerSaveMode3 = 2 , PowerSaveMode4 = 3 }
 Power saving mode (REG_ALS_PSM bits 2:1). More...
 
enum  InterruptEvent : uint8_t { ALS_EVENT_LOW_TRIGGER = 0 , ALS_EVENT_HIGH_TRIGGER , ALS_EVENT_NULL }
 Interrupt event type reported by getIrqStatus(). More...
 

Public Member Functions

 SensorCM32181 ()=default
 Construct a CM32181 driver instance.
 
 ~SensorCM32181 ()=default
 Destructor.
 
void setSampling (Sampling sampling=SAMPLING_X1, IntegrationTime int_time=INTEGRATION_TIME_200MS)
 Configure ALS sensitivity and integration time.
 
void setIntThreshold (uint16_t low_threshold, uint16_t high_threshold)
 Set ALS interrupt thresholds.
 
void powerSave (PowerSaveMode mode, bool enable)
 Configure power saving mode.
 
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).
 
void powerOn ()
 Power on sensor (REG_ALS_CONF bit0 = 0).
 
void powerDown ()
 Shutdown sensor (REG_ALS_CONF bit0 = 1).
 
uint16_t getRaw ()
 Read raw ALS data (16-bit).
 
float getLux ()
 Convert raw ALS to lux (approximate).
 
int getChipID ()
 Read chip ID.
 
- Public Member Functions inherited from I2CDeviceNoHal
bool begin (SensorCommCustom::CustomCallback callback, uint8_t addr)
 Initialize the sensor using custom callback interface.
 
- Public Member Functions inherited from DeviceBeginCommon
virtual ~DeviceBeginCommon ()
 
uint8_t getAddress () const
 
uint8_t getInterface () const
 
- Public Member Functions inherited from SensorCommWrapper
 SensorCommWrapper ()=default
 
int readReg (uint8_t reg) const
 
int readRegBuff (uint8_t reg, uint8_t *buf, size_t len) const
 
int readBuff (uint8_t *buf, size_t len) const
 
int writeReg (uint8_t reg, uint8_t val)
 
int writeRegBuff (uint8_t reg, uint8_t *buf, size_t len)
 
int writeBuff (uint8_t *buf, size_t len)
 
int writeThenRead (const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len)
 
bool setRegBit (uint8_t reg, uint8_t bit)
 
bool clrRegBit (uint8_t reg, uint8_t bit)
 
bool getRegBit (uint8_t reg, uint8_t bit)
 
int updateBits (uint8_t reg, uint8_t mask, uint8_t value_shifted)
 
void setAddress (uint8_t address)
 
void setAck (bool enable)
 
- Public Member Functions inherited from SensorCommInterface
virtual ~SensorCommInterface ()=default
 

Protected Attributes

const float calibration_factor = 0.286f
 Fixed raw-to-lux conversion factor (datasheet/manual derived).
 
- Protected Attributes inherited from DeviceBeginCommon
std::unique_ptr< SensorCommBasecomm
 
std::unique_ptr< SensorHalhal
 
std::unique_ptr< SensorCommStaticstaticComm
 
uint8_t _addr = 0
 
uint8_t _iface = COMM_CUSTOM
 

Additional Inherited Members

- Protected Member Functions inherited from DeviceBeginCommon
 DeviceBeginCommon ()=default
 
virtual bool ensureValid () const override
 
virtual SensorCommBasegetComm () const override
 
virtual void beforeBegin ()
 
virtual void afterCommReady ()
 
virtual void afterInitSuccess (uint8_t param)
 
virtual void onBeginFail ()
 
bool fail ()
 
void clearCommState ()
 
template<typename CreateComm >
bool beginLifecycle (uint8_t addr, uint8_t iface, uint8_t initParam, CreateComm createComm)
 

Detailed Description

High Sensitivity I2C Ambient Light Sensor (CM32181) driver.

This driver provides:

  • ALS configuration (sensitivity / integration time)
  • Power on/off
  • Interrupt threshold window configuration and interrupt enable/disable
  • IRQ status read (also clears IRQ per datasheet behavior)
  • Raw ALS read and approximate lux conversion
Note
Lux conversion uses a fixed calibration factor, because the datasheet/manual does not provide a readable calibration register.
Warning
Always call begin() and ensure it returns true before using other APIs.

Definition at line 72 of file sensor/light_sensor/SensorCM32181.hpp.

Member Enumeration Documentation

◆ IntegrationTime

ALS integration time (REG_ALS_CONF bits 9:6).

Datasheet mapping (bits 9:6):

  • 1100: 25ms (0x0C)
  • 1000: 50ms (0x08)
  • 0000: 100ms (0x00)
  • 0001: 200ms (0x01)
  • 0010: 400ms (0x02)
  • 0011: 800ms (0x03)
Enumerator
INTEGRATION_TIME_25MS 

25ms

INTEGRATION_TIME_50MS 

25ms

INTEGRATION_TIME_100MS 

100ms

INTEGRATION_TIME_200MS 

200ms

INTEGRATION_TIME_400MS 

400ms

INTEGRATION_TIME_800MS 

800ms

Definition at line 102 of file sensor/light_sensor/SensorCM32181.hpp.

◆ InterruptEvent

Interrupt event type reported by getIrqStatus().

Enumerator
ALS_EVENT_LOW_TRIGGER 

Low threshold window interrupt.

ALS_EVENT_HIGH_TRIGGER 

High threshold window interrupt.

ALS_EVENT_NULL 

No interrupt event.

Definition at line 130 of file sensor/light_sensor/SensorCM32181.hpp.

◆ PowerSaveMode

Power saving mode (REG_ALS_PSM bits 2:1).

Datasheet mapping:

  • 00: Mode1
  • 01: Mode2
  • 10: Mode3
  • 11: Mode4
Enumerator
PowerSaveMode1 
PowerSaveMode2 
PowerSaveMode3 
PowerSaveMode4 

Definition at line 120 of file sensor/light_sensor/SensorCM32181.hpp.

◆ Sampling

enum SensorCM32181::Sampling : uint8_t

ALS sensitivity selection (REG_ALS_CONF bits 12:11).

Datasheet mapping:

  • 00: x1
  • 01: x2
  • 10: x1/8
  • 11: x1/4
Enumerator
SAMPLING_X1 

ALS Sensitivity x1.

SAMPLING_X2 

ALS Sensitivity x2.

SAMPLING_X1_8 

ALS Sensitivity x(1/8)

SAMPLING_X1_4 

ALS Sensitivity x(1/4)

Definition at line 84 of file sensor/light_sensor/SensorCM32181.hpp.

Constructor & Destructor Documentation

◆ SensorCM32181()

SensorCM32181::SensorCM32181 ( )
default

Construct a CM32181 driver instance.

◆ ~SensorCM32181()

SensorCM32181::~SensorCM32181 ( )
default

Destructor.

Deinitializes communication backend if created.

Member Function Documentation

◆ disableINT()

void SensorCM32181::disableINT ( )
inline

Disable interrupt function (REG_ALS_CONF bit1).

Definition at line 261 of file sensor/light_sensor/SensorCM32181.hpp.

References sensorlib::_bitWrite(), SensorCommWrapper::readRegBuff(), and SensorCommWrapper::writeRegBuff().

Referenced by loop().

◆ enableINT()

void SensorCM32181::enableINT ( )
inline

Enable interrupt function (REG_ALS_CONF bit1).

Definition at line 250 of file sensor/light_sensor/SensorCM32181.hpp.

References sensorlib::_bitWrite(), SensorCommWrapper::readRegBuff(), and SensorCommWrapper::writeRegBuff().

Referenced by loop(), and setup().

◆ getChipID()

int SensorCM32181::getChipID ( )
inline

Read chip ID.

Returns
Chip ID (8-bit).

Definition at line 318 of file sensor/light_sensor/SensorCM32181.hpp.

References sensorlib::_lowByte(), and SensorCommWrapper::readRegBuff().

Referenced by setup().

◆ getIrqStatus()

InterruptEvent SensorCM32181::getIrqStatus ( )
inline

Read IRQ status (and clear interrupt by reading status register).

Returns
Interrupt event type.

Definition at line 230 of file sensor/light_sensor/SensorCM32181.hpp.

References sensorlib::_bitRead(), ALS_EVENT_HIGH_TRIGGER, ALS_EVENT_LOW_TRIGGER, ALS_EVENT_NULL, and SensorCommWrapper::readRegBuff().

Referenced by loop().

◆ getLux()

float SensorCM32181::getLux ( )
inline

Convert raw ALS to lux (approximate).

Returns
Lux value.

Definition at line 308 of file sensor/light_sensor/SensorCM32181.hpp.

References calibration_factor, and getRaw().

Referenced by loop().

◆ getRaw()

uint16_t SensorCM32181::getRaw ( )
inline

Read raw ALS data (16-bit).

Returns
Raw data.

Definition at line 296 of file sensor/light_sensor/SensorCM32181.hpp.

References SensorCommWrapper::readRegBuff().

Referenced by getLux(), and loop().

◆ powerDown()

void SensorCM32181::powerDown ( )
inline

Shutdown sensor (REG_ALS_CONF bit0 = 1).

Definition at line 283 of file sensor/light_sensor/SensorCM32181.hpp.

References sensorlib::_bitSet(), SensorCommWrapper::readRegBuff(), and SensorCommWrapper::writeRegBuff().

◆ powerOn()

void SensorCM32181::powerOn ( )
inline

Power on sensor (REG_ALS_CONF bit0 = 0).

Definition at line 272 of file sensor/light_sensor/SensorCM32181.hpp.

References sensorlib::_bitClear(), SensorCommWrapper::readRegBuff(), and SensorCommWrapper::writeRegBuff().

Referenced by setup().

◆ powerSave()

void SensorCM32181::powerSave ( PowerSaveMode  mode,
bool  enable 
)
inline

Configure power saving mode.

REG_ALS_PSM (0x03):

  • bits 2:1: mode (Mode1..Mode4)
  • bit 0: enable power saving
Parameters
modePower saving mode.
enabletrue to enable power saving, false to disable.

Definition at line 205 of file sensor/light_sensor/SensorCM32181.hpp.

References SensorCommWrapper::readRegBuff(), and SensorCommWrapper::writeRegBuff().

◆ setIntThreshold()

void SensorCM32181::setIntThreshold ( uint16_t  low_threshold,
uint16_t  high_threshold 
)
inline

Set ALS interrupt thresholds.

Parameters
low_thresholdLow threshold (raw 16-bit).
high_thresholdHigh threshold (raw 16-bit).

Definition at line 180 of file sensor/light_sensor/SensorCM32181.hpp.

References sensorlib::_highByte(), sensorlib::_lowByte(), and SensorCommWrapper::writeRegBuff().

Referenced by setup().

◆ setSampling()

void SensorCM32181::setSampling ( Sampling  sampling = SAMPLING_X1,
IntegrationTime  int_time = INTEGRATION_TIME_200MS 
)
inline

Configure ALS sensitivity and integration time.

This updates REG_ALS_CONF bits:

  • bits 12:11: sensitivity
  • bits 9:6: integration time
Parameters
samplingALS sensitivity.
int_timeIntegration time.

Definition at line 156 of file sensor/light_sensor/SensorCM32181.hpp.

References SensorCommWrapper::readRegBuff(), and SensorCommWrapper::writeRegBuff().

Referenced by setup().

Member Data Documentation

◆ calibration_factor

const float SensorCM32181::calibration_factor = 0.286f
protected

Fixed raw-to-lux conversion factor (datasheet/manual derived).

Definition at line 374 of file sensor/light_sensor/SensorCM32181.hpp.

Referenced by getLux().


The documentation for this class was generated from the following file: