SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
sensor/light_sensor/SensorLTR553.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include "../../SensorBuildOpt.h"
33#if !SENSORLIB_EXCLUDE_LTR553
34
35#include "../../platform/comm/I2CDeviceNoHal.hpp"
36
37// Unique I2C device address
38static constexpr uint8_t LTR553_SLAVE_ADDRESS = 0x23;
39
54{
55public:
63
75
89
105
117
131
147
171
193
197 SensorLTR553() = default;
198
202 ~SensorLTR553() = default;
203
210 {
211 if (level) {
213 } else {
215 }
216 }
217
224 {
225 // RESERVED:7:3
226 // Interrupt Polarity : 2
227 // Interrupt Mode : 1:0
228 updateBits(REG_INTERRUPT, 0x03, mode);
229 }
230
235 {
236 updateBits(REG_INTERRUPT, 0x03, 0x00);
237 }
238
239 // -----------------------
240 // ALS (Ambient Light)
241 // -----------------------
242
251 {
252 return getRegBit(REG_ALS_PS_STATUS, 0);
253 }
254
263 void setLightSensorThreshold(uint16_t low, uint16_t high)
264 {
265 uint8_t buffer[4] = {
268 };
270 }
271
282 void setLightSensorPersists(uint8_t count)
283 {
284 updateBits(REG_INTERRUPT_PERSIST, 0x0F, count - 1);
285 }
286
297 void setLightSensorRate(IntegrationTime alsIntegrationTime, MeasurementRate alsMeasurementRate)
298 {
299 uint8_t value = (uint8_t)(((alsIntegrationTime & 0x07) << 3) | (alsMeasurementRate & 0x07));
301 }
302
307 {
309 }
310
315 {
317 }
318
325 {
326 updateBits(REG_ALS_CONTR, 0x1C, gain << 2);
327 }
328
335 int getLightSensor(uint8_t ch)
336 {
337 uint8_t buffer[2] = {0};
338
339 // Check ALS Data is Valid (datasheet bit definition should be checked if needed)
340 if (getRegBit(REG_ALS_PS_STATUS, 7) != false) {
341 return -1;
342 }
343
344 int val = readRegBuff(ch == 1 ? REG_ALS_DATA_CH1_0 : REG_ALS_DATA_CH0_0, buffer, 2);
345 if (val == -1) {
346 return -1;
347 }
348
349 return (int)buffer[0] | ((int)buffer[1] << 8);
350 }
351
352 // -----------------------
353 // PS (Proximity)
354 // -----------------------
355
365 void setProximityPersists(uint8_t count)
366 {
367 uint8_t val = (count == 0) ? 0 : (count - 1);
368 if (val > 0x0F) {
369 val = 0x0F;
370 }
371 updateBits(REG_INTERRUPT_PERSIST, 0xF0, val << 4);
372 }
373
387
394 {
395 updateBits(REG_PS_MEAS_RATE, 0x0F, rate & 0x0F);
396 }
397
402 {
403 updateBits(REG_PS_CONTR, 0x03, 0x03);
404 }
405
410 {
411 updateBits(REG_PS_CONTR, 0x03, 0x00);
412 }
413
418 {
420 }
421
426 {
428 }
429
440 int getProximity(bool *saturated = NULL )
441 {
442 uint8_t buffer[2] = {0};
443 int val = readRegBuff(REG_PS_DATA_0, buffer, 2);
444 if (val == -1) {
445 return -1;
446 }
447 if (saturated) {
448 *saturated = (buffer[1] & 0x80) == 0x80;
449 }
450 return (int)buffer[0] | ((int)(buffer[1] & 0x07) << 8);
451 }
452
453 // -----------------------
454 // PS LED configuration
455 // -----------------------
456
463 {
464 updateBits(REG_PS_LED, 0xE0, static_cast<uint8_t>(period) << 5);
465 }
466
473 {
474 updateBits(REG_PS_LED, 0x18, static_cast<uint8_t>(duty) << 3);
475 }
476
483 {
484 updateBits(REG_PS_LED, 0x07, static_cast<uint8_t>(cur));
485 }
486
492 void setPsLedPulses(uint8_t pulesNum)
493 {
494 updateBits(REG_PS_N_PULSES, 0x0F, pulesNum & 0x0F);
495 }
496
497 // -----------------------
498 // Identification / reset
499 // -----------------------
500
507 {
508 int val = readReg(REG_PART_ID);
509 if (val == -1) {
510 return -1;
511 }
512 return (val >> 4) & 0x0F;
513 }
514
521 {
522 int val = readReg(REG_PART_ID);
523 if (val == -1) {
524 return -1;
525 }
526 return (val) & 0x0F;
527 }
528
537 {
538 // Manufacturer ID (0x05H)
539 return readReg(REG_MANUFAC_ID);
540 }
541
545 void reset()
546 {
548 }
549
550private:
558 bool initImpl(uint8_t param)
559 {
560 setAck(false);
561 reset();
563 }
564
565protected:
566 // Register map
567 static constexpr uint8_t REG_ALS_CONTR = 0x80;
568 static constexpr uint8_t REG_PS_CONTR = 0x81;
569 static constexpr uint8_t REG_PS_LED = 0x82;
570 static constexpr uint8_t REG_PS_N_PULSES = 0x83;
571 static constexpr uint8_t REG_PS_MEAS_RATE = 0x84;
572 static constexpr uint8_t REG_ALS_MEAS_RATE = 0x85;
573 static constexpr uint8_t REG_PART_ID = 0x86;
574 static constexpr uint8_t REG_MANUFAC_ID = 0x87;
575 static constexpr uint8_t REG_ALS_DATA_CH1_0 = 0x88;
576 static constexpr uint8_t REG_ALS_DATA_CH1_1 = 0x89;
577 static constexpr uint8_t REG_ALS_DATA_CH0_0 = 0x8A;
578 static constexpr uint8_t REG_ALS_DATA_CH0_1 = 0x8B;
579 static constexpr uint8_t REG_ALS_PS_STATUS = 0x8C;
580 static constexpr uint8_t REG_PS_DATA_0 = 0x8D;
581 static constexpr uint8_t REG_PS_DATA_1 = 0x8E;
582 static constexpr uint8_t REG_INTERRUPT = 0x8F;
583 static constexpr uint8_t REG_PS_THRES_UP_0 = 0x90;
584 static constexpr uint8_t REG_PS_THRES_UP_1 = 0x91;
585 static constexpr uint8_t REG_PS_THRES_LOW_0 = 0x92;
586 static constexpr uint8_t REG_PS_THRES_LOW_1 = 0x93;
587 static constexpr uint8_t REG_PS_OFFSET_1 = 0x94;
588 static constexpr uint8_t REG_PS_OFFSET_0 = 0x95;
589 static constexpr uint8_t REG_ALS_THRES_UP_0 = 0x97;
590 static constexpr uint8_t REG_ALS_THRES_UP_1 = 0x98;
591 static constexpr uint8_t REG_ALS_THRES_LOW_0 = 0x99;
592 static constexpr uint8_t REG_ALS_THRES_LOW_1 = 0x9A;
593 static constexpr uint8_t REG_INTERRUPT_PERSIST = 0x9E;
594
596 static constexpr uint8_t LTR553_DEFAULT_MAN_ID = 0x05;
597};
598
599#endif
uint8_t level
bool clrRegBit(uint8_t reg, uint8_t bit)
bool getRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
int writeRegBuff(uint8_t reg, uint8_t *buf, size_t len)
int writeReg(uint8_t reg, uint8_t val)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
bool setRegBit(uint8_t reg, uint8_t bit)
void setAck(bool enable)
Driver for the LTR-553ALS-01 ambient light + proximity sensor (I2C).
static constexpr uint8_t REG_PS_N_PULSES
static constexpr uint8_t REG_PS_DATA_0
void setProximityThreshold(uint16_t low, uint16_t high)
Set PS threshold window.
~SensorLTR553()=default
Destructor.
PsRate
PS measurement rate (REG_PS_MEAS_RATE field).
int getManufacturerID()
Read Manufacturer ID (REG_MANUFAC_ID).
static constexpr uint8_t REG_ALS_MEAS_RATE
static constexpr uint8_t REG_PS_THRES_UP_1
static constexpr uint8_t REG_ALS_PS_STATUS
static constexpr uint8_t REG_PS_DATA_1
void enablePsIndicator()
Enable PS indicator.
static constexpr uint8_t REG_ALS_DATA_CH1_1
void setIRQLevel(IrqLevel level)
Set interrupt pin polarity.
static constexpr uint8_t REG_ALS_DATA_CH1_0
static constexpr uint8_t REG_PS_THRES_LOW_1
PsLedDuty
PS LED duty cycle (REG_PS_LED bits 4:3).
static constexpr uint8_t REG_ALS_DATA_CH0_1
int getLightSensor(uint8_t ch)
Read ALS raw data from channel 0 or 1.
void setLightSensorThreshold(uint16_t low, uint16_t high)
Set ALS threshold window.
int getPartID()
Get Part ID (REG_PART_ID high nibble).
IrqLevel
Interrupt pin active level.
@ ALS_IRQ_ACTIVE_HIGH
INT pin is active-high.
@ ALS_IRQ_ACTIVE_LOW
INT pin is active-low (default)
static constexpr uint8_t REG_ALS_THRES_UP_1
void reset()
Software reset (REG_ALS_CONTR bit1).
void setLightSensorGain(LightSensorGain gain)
Set ALS gain.
static constexpr uint8_t REG_ALS_THRES_UP_0
static constexpr uint8_t REG_ALS_CONTR
static constexpr uint8_t REG_PS_LED
static constexpr uint8_t REG_ALS_DATA_CH0_0
static constexpr uint8_t REG_PS_THRES_UP_0
void setPsLedCurrent(PsLedCurrent cur)
Set PS LED peak current (REG_PS_LED bits 2:0).
void setPsLedPulsePeriod(PsLedPeriod period)
Set PS LED pulse modulation frequency (REG_PS_LED bits 7:5).
int getProximity(bool *saturated=NULL)
Read proximity raw data.
static constexpr uint8_t REG_PS_OFFSET_1
void setLightSensorPersists(uint8_t count)
Configure ALS interrupt persistence count.
PsLedPeriod
PS LED pulse modulation frequency (REG_PS_LED bits 7:5).
bool psAvailable()
Check PS data ready flag.
void disableProximity()
Disable proximity measurement.
IrqMode
Interrupt trigger source.
@ ALS_IRQ_BOTH
Both ALS and PS measurement can trigger interrupt.
@ ALS_IRQ_DISABLE
No measurement can trigger interrupt (default)
@ ALS_IRQ_ONLY_ALS
Only ALS measurement can trigger interrupt.
@ ALS_IRQ_ONLY_PS
Only PS measurement can trigger interrupt.
void disableLightSensor()
Disable ALS measurement.
void enableProximity()
Enable proximity measurement.
void disableIRQ()
Disable all interrupt sources (mode bits = 0).
void setLightSensorRate(IntegrationTime alsIntegrationTime, MeasurementRate alsMeasurementRate)
Configure ALS integration time and measurement repeat rate.
void setProximityRate(PsRate rate)
Set PS measurement rate.
IntegrationTime
ALS integration time (REG_ALS_MEAS_RATE bits 5:3).
static constexpr uint8_t REG_PS_OFFSET_0
SensorLTR553()=default
Construct an uninitialized driver instance.
static constexpr uint8_t REG_MANUFAC_ID
void disablePsIndicator()
Disable PS indicator.
static constexpr uint8_t REG_INTERRUPT
void enableIRQ(IrqMode mode)
Enable interrupts and select which measurement sources can trigger them.
static constexpr uint8_t REG_PS_THRES_LOW_0
void setPsLedDutyCycle(PsLedDuty duty)
Set PS LED duty cycle (REG_PS_LED bits 4:3).
static constexpr uint8_t REG_ALS_THRES_LOW_0
void setPsLedPulses(uint8_t pulesNum)
Set number of PS LED pulses (REG_PS_N_PULSES bits 3:0).
@ ALS_GAIN_1X
1 lux to 64k lux (default)
@ ALS_GAIN_48X
0.02 lux to 1.3k lux
@ ALS_GAIN_4X
0.25 lux to 16k lux
@ ALS_GAIN_8X
0.125 lux to 8k lux
@ ALS_GAIN_96X
0.01 lux to 600 lux
static constexpr uint8_t REG_PS_MEAS_RATE
static constexpr uint8_t LTR553_DEFAULT_MAN_ID
Default manufacturer ID.
void setProximityPersists(uint8_t count)
Configure PS interrupt persistence count.
static constexpr uint8_t REG_INTERRUPT_PERSIST
int getRevisionID()
Get Revision ID (REG_PART_ID low nibble).
void enableLightSensor()
Enable ALS measurement.
static constexpr uint8_t REG_PART_ID
MeasurementRate
ALS measurement repeat rate (REG_ALS_MEAS_RATE bits 2:0).
static constexpr uint8_t REG_PS_CONTR
PsLedCurrent
PS LED peak current selection (REG_PS_LED bits 2:0).
static constexpr uint8_t REG_ALS_THRES_LOW_1
constexpr uint8_t _lowByte(uint16_t w)
Definition SensorLib.h:69
constexpr uint8_t _highByte(uint16_t w)
Definition SensorLib.h:73