SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
sensor/light_sensor/SensorCM32181.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include "../../SensorBuildOpt.h"
33#if !SENSORLIB_EXCLUDE_CM32181
34
35#include "../../platform/comm/I2CDeviceNoHal.hpp"
36
45static constexpr uint8_t CM32181_ADDR_PRIMARY = 0x10;
46
50static constexpr uint8_t CM32181_ADDR_SECONDARY = 0x48;
51
55static constexpr uint8_t CM32181_SLAVE_ADDRESS = 0x10;
56
73{
74public:
84 enum Sampling : uint8_t {
89 };
90
110
126
135
139 SensorCM32181() = default;
140
144 ~SensorCM32181() = default;
145
158 {
159 uint16_t data = 0;
160 readRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
161
162 // Sensitivity bits [12:11]
163 data &= (uint16_t)~ALS_CONF_SENS_MASK;
164 data |= (uint16_t)(sampling & 0x03u) << ALS_CONF_SENS_SHIFT;
165
166 // Integration time bits [9:6]
167 data &= (uint16_t)~ALS_CONF_IT_MASK;
168 data |= (uint16_t)(int_time & 0x0Fu) << ALS_CONF_IT_SHIFT;
169
170 // FIX: Write back configuration (original code mistakenly read instead of write)
171 writeRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
172 }
173
180 void setIntThreshold(uint16_t low_threshold, uint16_t high_threshold)
181 {
182 uint8_t buffer[2] = {0};
183
184 // High threshold window (REG_ALS_THDH)
185 buffer[1] = sensorlib::_highByte(high_threshold);
186 buffer[0] = sensorlib::_lowByte(high_threshold);
187 writeRegBuff(REG_ALS_THDH, buffer, 2);
188
189 // Low threshold window (REG_ALS_THDL)
190 buffer[1] = sensorlib::_highByte(low_threshold);
191 buffer[0] = sensorlib::_lowByte(low_threshold);
192 writeRegBuff(REG_ALS_THDL, buffer, 2);
193 }
194
205 void powerSave(PowerSaveMode mode, bool enable)
206 {
207 uint16_t data = 0;
208 readRegBuff(REG_ALS_PSM, (uint8_t *)&data, 2);
209
210 // Clear mode bits [2:1] and enable bit [0]
211 data &= (uint16_t)~ALS_PSM_MODE_MASK;
212 data &= (uint16_t)~ALS_PSM_EN_BIT;
213
214 // Set mode
215 data |= (uint16_t)(mode & 0x03u) << ALS_PSM_MODE_SHIFT;
216
217 // Set enable
218 if (enable) {
219 data |= ALS_PSM_EN_BIT;
220 }
221
222 writeRegBuff(REG_ALS_PSM, (uint8_t *)&data, 2);
223 }
224
231 {
232 uint16_t data = 0;
233 readRegBuff(REG_ALS_STATUS, (uint8_t *)&data, 2);
234
235 // Keep original mapping:
236 // bit15 => low threshold trigger
237 // bit14 => high threshold trigger
238 if (sensorlib::_bitRead(data, 15)) {
240 }
241 if (sensorlib::_bitRead(data, 14)) {
243 }
244 return ALS_EVENT_NULL;
245 }
246
251 {
252 uint16_t data = 0;
253 readRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
254 sensorlib::_bitWrite(data, 1, 1);
255 writeRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
256 }
257
262 {
263 uint16_t data = 0;
264 readRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
265 sensorlib::_bitWrite(data, 1, 0);
266 writeRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
267 }
268
272 void powerOn()
273 {
274 uint16_t data = 0;
275 readRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
276 sensorlib::_bitClear(data, 0);
277 writeRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
278 }
279
284 {
285 uint16_t data = 0;
286 readRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
287 sensorlib::_bitSet(data, 0);
288 writeRegBuff(REG_ALS_CONF, (uint8_t *)&data, 2);
289 }
290
296 uint16_t getRaw()
297 {
298 uint8_t buffer[2] = {0};
299 readRegBuff(REG_ALS_DATA, buffer, 2);
300 return (uint16_t)buffer[0] | (uint16_t)(buffer[1] << 8);
301 }
302
308 float getLux()
309 {
310 return getRaw() * calibration_factor;
311 }
312
319 {
320 uint8_t buffer[2] = {0};
321 readRegBuff(REG_ID, buffer, 2);
322 return sensorlib::_lowByte(buffer[0]);
323 }
324
325private:
326 // ---- Register addresses (datasheet command codes) ----
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;
334
335 // ---- Chip ID ----
336 static constexpr uint8_t CM32181_CHIP_ID = 0x81;
337
338 // ---- Bit field masks/shifts (from datasheet screenshot) ----
339 // REG_ALS_CONF (0x00): bits 12:11 sensitivity, bits 9:6 integration time
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);
342
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);
345
346 // REG_ALS_PSM (0x03): bits 2:1 mode, bit0 enable
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);
350
354 bool initImpl(uint8_t param) override
355 {
356 setAck(false);
357
358 int chipID = getChipID();
359 SENSORLIB_LOG_I("chipID:%d", chipID);
360
361 if (chipID < 0) {
362 return false;
363 }
364 if (chipID != CM32181_CHIP_ID) {
365 return false;
366 }
367 return true;
368 }
369
370protected:
374 const float calibration_factor = 0.286f;
375};
376
377#endif
#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).
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_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.
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)
int writeRegBuff(uint8_t reg, uint8_t *buf, size_t len)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
void setAck(bool enable)
void _bitClear(T &value, uint8_t bit)
Definition SensorLib.h:93
constexpr uint8_t _lowByte(uint16_t w)
Definition SensorLib.h:69
void _bitWrite(T &value, uint8_t bit, bool bitvalue)
Definition SensorLib.h:105
void _bitSet(T &value, uint8_t bit)
Definition SensorLib.h:87
constexpr uint8_t _highByte(uint16_t w)
Definition SensorLib.h:73
constexpr T _bitRead(T value, uint8_t bit)
Definition SensorLib.h:81