Library for ISL29125 - I2C RGB ambient light sensor

Dependents:   ISL29125_demo FinalProject Final_NSR 4180FinalProject_copy ... more

Device documentation

Detailed information is available at Intersil

Calling the constructor

The constructor can be instantiaded in 3 ways:

Information

The first two pins (SDA and SCL) always need to be declared.

ISR mode

void RGBsensor_irq(void)    // User ISR
{
    ....
}

ISL29125 RGBsensor(PTE0, PTE1, PTD7, &RGBsensor_irq);          // sda, scl, irq, user isr

The user ISR is called whenever the upper/lower threshold is reached and/or when a conversion cycle is finished. The threshold (Threshold function) and end of conversion (IRQonCnvDone function) interrupts can be enabled separately.
In this mode, PTD7 is an interrupt input.

Limitation!

Calling the Read function at a high rate will result in a I2C failure.
Use a Ticker to call the Read function at regular intervals. Example : ISL29125_demo

SYNC mode

ISL29125 RGBsensor(PTE0, PTE1, PTD7);                        // sda, scl, sync input

In this mode, PTD7 is a digital output. Initially, the sensor is idle (no conversions) and can be started by calling the Run function.

Polling mode

ISL29125 RGBsensor(PTE0, PTE1);                              // sda, scl

In this mode, user defined functions are needed to poll the sensor status, read the sensor data, ....

Committer:
frankvnk
Date:
Wed May 28 19:19:06 2014 +0000
Revision:
2:f1b55929169a
Parent:
1:1c1ded8d719e
Child:
3:400edaa74b7d
Doxygen update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frankvnk 0:59ae5a71c902 1 /******************************************************************************************************************************
frankvnk 0:59ae5a71c902 2 ***** *****
frankvnk 0:59ae5a71c902 3 ***** Name: ISL29125.h *****
frankvnk 0:59ae5a71c902 4 ***** Date: 06/04/2014 *****
frankvnk 0:59ae5a71c902 5 ***** Auth: Frank Vannieuwkerke *****
frankvnk 0:59ae5a71c902 6 ***** Func: library for Intersil ISL29125 RGB Ambient light sensor with IR blocking filter *****
frankvnk 0:59ae5a71c902 7 ***** *****
frankvnk 0:59ae5a71c902 8 ***** Additional info is available at *****
frankvnk 0:59ae5a71c902 9 ***** http://www.intersil.com/en/products/optoelectronics/ambient-light-sensors/light-to-digital-sensors/ISL29125.html *****
frankvnk 0:59ae5a71c902 10 ***** *****
frankvnk 0:59ae5a71c902 11 ******************************************************************************************************************************/
frankvnk 0:59ae5a71c902 12
frankvnk 0:59ae5a71c902 13 #ifndef ISL29125_H
frankvnk 0:59ae5a71c902 14 #define ISL29125_H
frankvnk 0:59ae5a71c902 15
frankvnk 0:59ae5a71c902 16 #include "mbed.h"
frankvnk 0:59ae5a71c902 17
frankvnk 0:59ae5a71c902 18 // Common Controls Used with
frankvnk 0:59ae5a71c902 19 // Operating IRQ Status
frankvnk 0:59ae5a71c902 20 // Mode ADC assignment request
frankvnk 0:59ae5a71c902 21 #define ISL29125_G 0x01 // Green X X X
frankvnk 0:59ae5a71c902 22 #define ISL29125_R 0x02 // Red X X X
frankvnk 0:59ae5a71c902 23 #define ISL29125_B 0x03 // Blue X X X
frankvnk 0:59ae5a71c902 24 #define ISL29125_RG 0x06 // Red and Green X - -
frankvnk 0:59ae5a71c902 25 #define ISL29125_BG 0x07 // Blue and Green X - -
frankvnk 0:59ae5a71c902 26 #define ISL29125_RGB 0x05 // Red, Green and Blue X - X
frankvnk 0:59ae5a71c902 27 #define ISL29125_STBY 0x04 // Standby X - -
frankvnk 0:59ae5a71c902 28 #define ISL29125_OFF 0x00 // Switch OFF a control X X -
frankvnk 0:59ae5a71c902 29 // Unique Controls
frankvnk 0:59ae5a71c902 30 #define ISL29125_LTH_W 0x04 // Low interrupt threshold register
frankvnk 0:59ae5a71c902 31 #define ISL29125_HTH_W 0x06 // High interrupt threshold register
frankvnk 0:59ae5a71c902 32 #define ISL29125_LTH_R 0x02 // Low interrupt threshold register
frankvnk 0:59ae5a71c902 33 #define ISL29125_HTH_R 0x03 // High interrupt threshold register
frankvnk 0:59ae5a71c902 34 #define ISL29125_375LX 0x00 // Full scale range = 375 lux
frankvnk 0:59ae5a71c902 35 #define ISL29125_10KLX 0x08 // Full scale range = 10K lux
frankvnk 0:59ae5a71c902 36 #define ISL29125_16BIT 0x00 // ADC resolution = 16 bit
frankvnk 0:59ae5a71c902 37 #define ISL29125_12BIT 0x10 // ADC resolution = 12 bit
frankvnk 0:59ae5a71c902 38 #define ISL29125_PERS1 0x00 // IRQ when threshold is reached once
frankvnk 0:59ae5a71c902 39 #define ISL29125_PERS2 0x04 // IRQ when threshold is reached twice
frankvnk 0:59ae5a71c902 40 #define ISL29125_PERS4 0x08 // IRQ when threshold is reached 4 times
frankvnk 0:59ae5a71c902 41 #define ISL29125_PERS8 0x0C // IRQ when threshold is reached 8 times
frankvnk 0:59ae5a71c902 42
frankvnk 1:1c1ded8d719e 43 /** ISL29125 class.
frankvnk 1:1c1ded8d719e 44 */
frankvnk 1:1c1ded8d719e 45
frankvnk 0:59ae5a71c902 46 class ISL29125 {
frankvnk 0:59ae5a71c902 47 public:
frankvnk 0:59ae5a71c902 48 /**
frankvnk 2:f1b55929169a 49 * \brief Create a ISL29125 object connected to I2C bus, irq or sync pin and user-ISR pointer.\n
frankvnk 2:f1b55929169a 50 * \param sda SDA pin.\n
frankvnk 2:f1b55929169a 51 * \param scl SCL pin.\n
frankvnk 2:f1b55929169a 52 * \param irqsync (Optional) Interrupt pin when fptr is also declared.\n
frankvnk 2:f1b55929169a 53 * Sync output when fptr is not declared.\n
frankvnk 2:f1b55929169a 54 * \param fptr (Optional) Pointer to user-ISR (only used with Interrupt pin).\n
frankvnk 2:f1b55929169a 55 * \return none\n
frankvnk 0:59ae5a71c902 56 */
frankvnk 0:59ae5a71c902 57 ISL29125(PinName sda, PinName scl, PinName irqsync = NC, void (*fptr)(void) = NULL);
frankvnk 0:59ae5a71c902 58
frankvnk 0:59ae5a71c902 59 /**
frankvnk 2:f1b55929169a 60 * \brief Read status register.\n
frankvnk 2:f1b55929169a 61 * The interrupt status flag is cleared when the status register is read.\n
frankvnk 0:59ae5a71c902 62 * \param NONE
frankvnk 2:f1b55929169a 63 * \return Content of the entire status register.\n
frankvnk 2:f1b55929169a 64 * bit Description\n
frankvnk 2:f1b55929169a 65 * --- ---------------------------------------------------------\n
frankvnk 2:f1b55929169a 66 * 5,4 RGB conversion - 00: Inactive\n
frankvnk 2:f1b55929169a 67 * 01: Green\n
frankvnk 2:f1b55929169a 68 * 10: Red\n
frankvnk 2:f1b55929169a 69 * 11: Blue\n
frankvnk 2:f1b55929169a 70 * 2 Brownout status - 0: No brownout\n
frankvnk 2:f1b55929169a 71 * 1: Power down or brownout occured \n
frankvnk 2:f1b55929169a 72 * 1 Conversion status - 0: Conversion is pending or inactive\n
frankvnk 2:f1b55929169a 73 * 1: Conversion is completed\n
frankvnk 2:f1b55929169a 74 * 0 Interrupt status - 0: no interrupt occured\n
frankvnk 2:f1b55929169a 75 * 1: interrupt occured\n
frankvnk 0:59ae5a71c902 76 */
frankvnk 0:59ae5a71c902 77 uint8_t Status(void);
frankvnk 0:59ae5a71c902 78
frankvnk 0:59ae5a71c902 79 /**
frankvnk 2:f1b55929169a 80 * \brief Read the device identifier.\n
frankvnk 2:f1b55929169a 81 * \param none.\n
frankvnk 2:f1b55929169a 82 * \return 0x7D on success.\n
frankvnk 0:59ae5a71c902 83 */
frankvnk 0:59ae5a71c902 84 uint8_t WhoAmI(void);
frankvnk 0:59ae5a71c902 85
frankvnk 0:59ae5a71c902 86 /**
frankvnk 2:f1b55929169a 87 * \brief Read the channel values (12 or 16-bit - depends on resolution).\n
frankvnk 2:f1b55929169a 88 * \param color ISL29125_R Return Red channel.\n
frankvnk 2:f1b55929169a 89 * ISL29125_G Return Green channel.\n
frankvnk 2:f1b55929169a 90 * ISL29125_B Return Blue channel.\n
frankvnk 2:f1b55929169a 91 * ISL29125_RGB Return Red, Green and Blue channels. \n
frankvnk 2:f1b55929169a 92 * \param data Pointer to 16-bit array for storing the channel value(s).\n
frankvnk 2:f1b55929169a 93 * \ Array size: 1 for a single color (Red, Green or Blue).\n
frankvnk 2:f1b55929169a 94 * \ 3 for all colors.\n
frankvnk 2:f1b55929169a 95 * \return bool 1: new data available - 0: no new data available.\n
frankvnk 0:59ae5a71c902 96 */
frankvnk 0:59ae5a71c902 97 bool Read(uint8_t color, uint16_t * data);
frankvnk 0:59ae5a71c902 98
frankvnk 0:59ae5a71c902 99 /**
frankvnk 2:f1b55929169a 100 * \brief Read/Write the low/high interrupt threshold value.\n
frankvnk 2:f1b55929169a 101 * When setIRQonColor is activated, an interrupt will occur when the low or high threshold is exceeded.\n
frankvnk 2:f1b55929169a 102 * \param reg ISL29125_LTH_W Write 16-bit low threshold.\n
frankvnk 2:f1b55929169a 103 * ISL29125_HTH_W Write 16-bit high threshold.\n
frankvnk 2:f1b55929169a 104 * ISL29125_LTH_R Read 16-bit low threshold.\n
frankvnk 2:f1b55929169a 105 * ISL29125_HTH_R Read 16-bit high threshold.\n
frankvnk 2:f1b55929169a 106 * \param thres 16-bit threshold value (only needed when _W parameter is used).\n
frankvnk 2:f1b55929169a 107 * \return Written threshold value when called with _W parameter.\n
frankvnk 2:f1b55929169a 108 * Stored threshold value when called with _R parameter only.\n
frankvnk 0:59ae5a71c902 109 */
frankvnk 0:59ae5a71c902 110 uint16_t Threshold(uint8_t reg, uint16_t thres=0);
frankvnk 0:59ae5a71c902 111
frankvnk 0:59ae5a71c902 112 /**
frankvnk 2:f1b55929169a 113 * \brief Read/Write the RGB operating mode value (active ADC channels).\n
frankvnk 2:f1b55929169a 114 * \param mode ISL29125_G G channel only.\n
frankvnk 2:f1b55929169a 115 * ISL29125_R R channel only.\n
frankvnk 2:f1b55929169a 116 * ISL29125_B B channel only.\n
frankvnk 2:f1b55929169a 117 * ISL29125_RG R and G channel.\n
frankvnk 2:f1b55929169a 118 * ISL29125_BG B and G channel.\n
frankvnk 2:f1b55929169a 119 * ISL29125_RGB R, G and B channel.\n
frankvnk 2:f1b55929169a 120 * ISL29125_STBY Standby (No ADC conversion).\n
frankvnk 2:f1b55929169a 121 * ISL29125_OFF Power down ADC conversion.\n
frankvnk 2:f1b55929169a 122 * \return Written value is returned when called with valid parameter, otherwise 0xff is returned.\n
frankvnk 2:f1b55929169a 123 * Stored value is returned when called without parameter.\n
frankvnk 0:59ae5a71c902 124 */
frankvnk 0:59ae5a71c902 125 uint8_t RGBmode(uint8_t mode=0xff);
frankvnk 0:59ae5a71c902 126
frankvnk 0:59ae5a71c902 127 /**
frankvnk 2:f1b55929169a 128 * \brief Read/Write the sensing range parameter.\n
frankvnk 2:f1b55929169a 129 * \param range ISL29125_375LX Max. value corresponds to 375 lux.\n
frankvnk 2:f1b55929169a 130 * ISL29125_10KLX Max. value corresponds to 10000 lux.\n
frankvnk 2:f1b55929169a 131 * \return Written value is returned when called with valid parameter, otherwise 0xff is returned.\n
frankvnk 2:f1b55929169a 132 * Stored value is returned when called without parameter.\n
frankvnk 0:59ae5a71c902 133 */
frankvnk 0:59ae5a71c902 134 uint8_t Range(uint8_t range=0xff);
frankvnk 0:59ae5a71c902 135
frankvnk 0:59ae5a71c902 136 /**
frankvnk 2:f1b55929169a 137 * \brief Read/Write the ADC resolution parameter.\n
frankvnk 2:f1b55929169a 138 * \param range ISL29125_16BIT ADC resolution = 16 bit.\n
frankvnk 2:f1b55929169a 139 * ISL29125_12BIT ADC resolution = 12 bit.\n
frankvnk 2:f1b55929169a 140 * \return Written value is returned when called with valid parameter, otherwise 0xff is returned.\n
frankvnk 2:f1b55929169a 141 * Stored value is returned when called without parameter.\n
frankvnk 0:59ae5a71c902 142 */
frankvnk 0:59ae5a71c902 143 uint8_t Resolution(uint8_t resol=0xff);
frankvnk 0:59ae5a71c902 144
frankvnk 0:59ae5a71c902 145 /**
frankvnk 2:f1b55929169a 146 * \brief Read/Write the IRQ persistence parameter.\n
frankvnk 2:f1b55929169a 147 * \param persist ISL29125_PERS1 IRQ occurs when threshold is exceeded once.\n
frankvnk 2:f1b55929169a 148 * ISL29125_PERS2 IRQ occurs when threshold is exceeded twice.\n
frankvnk 2:f1b55929169a 149 * ISL29125_PERS4 IRQ occurs when threshold is exceeded 4 times.\n
frankvnk 2:f1b55929169a 150 * ISL29125_PERS8 IRQ occurs when threshold is exceeded 8 times.\n
frankvnk 2:f1b55929169a 151 * \return Written value is returned when called with valid parameter, otherwise 0xff is returned.\n
frankvnk 2:f1b55929169a 152 * Stored value is returned when called without parameter.\n
frankvnk 0:59ae5a71c902 153 */
frankvnk 0:59ae5a71c902 154 uint8_t Persist(uint8_t persist=0xff);
frankvnk 0:59ae5a71c902 155
frankvnk 0:59ae5a71c902 156 /**
frankvnk 2:f1b55929169a 157 * \brief Read/Write the IRQ on conversion done parameter.\n
frankvnk 2:f1b55929169a 158 * \param true: enabled.\n
frankvnk 2:f1b55929169a 159 * false: disabled.\n
frankvnk 2:f1b55929169a 160 * \return Written value is returned when called with valid parameter, otherwise 0xff is returned.\n
frankvnk 2:f1b55929169a 161 * Stored value is returned when called without parameter.\n
frankvnk 0:59ae5a71c902 162 */
frankvnk 0:59ae5a71c902 163 uint8_t IRQonCnvDone(uint8_t irqen=0xff);
frankvnk 0:59ae5a71c902 164
frankvnk 0:59ae5a71c902 165 /**
frankvnk 2:f1b55929169a 166 * \brief Read/Write the IRQ threshold to color assignment parameter.\n
frankvnk 2:f1b55929169a 167 * \param RGBmode ISL29125_OFF No interrupt.\n
frankvnk 2:f1b55929169a 168 * ISL29125_G Green interrupt.\n
frankvnk 2:f1b55929169a 169 * ISL29125_R Red interrupt.\n
frankvnk 2:f1b55929169a 170 * ISL29125_B Blue interrupt.\n
frankvnk 2:f1b55929169a 171 * \return Written value is returned when called with valid parameter, otherwise 0xff is returned.\n
frankvnk 2:f1b55929169a 172 * Stored value is returned when called without parameter.\n
frankvnk 0:59ae5a71c902 173 */
frankvnk 0:59ae5a71c902 174 uint8_t IRQonColor(uint8_t RGBmode=0xff);
frankvnk 0:59ae5a71c902 175
frankvnk 0:59ae5a71c902 176 /**
frankvnk 2:f1b55929169a 177 * \brief Read/Write the active IR compensation parameter.\n
frankvnk 2:f1b55929169a 178 * \param ircomp valid range: between 0..63 or 128..191.\n
frankvnk 2:f1b55929169a 179 * \return Written value is returned when called with valid parameter, otherwise 0xff is returned.\n
frankvnk 2:f1b55929169a 180 * Stored value is returned when called without parameter.\n
frankvnk 0:59ae5a71c902 181 */
frankvnk 0:59ae5a71c902 182 uint8_t IRcomp(uint8_t ircomp=0xff);
frankvnk 0:59ae5a71c902 183
frankvnk 0:59ae5a71c902 184 /**
frankvnk 2:f1b55929169a 185 * \brief Start ADC conversion.\n
frankvnk 2:f1b55929169a 186 * Only possible when SyncMode is activated.\n
frankvnk 2:f1b55929169a 187 * \param None.\n
frankvnk 2:f1b55929169a 188 * \return bool 1: success - 0: fail.\n
frankvnk 0:59ae5a71c902 189 */
frankvnk 0:59ae5a71c902 190 bool Run(void);
frankvnk 0:59ae5a71c902 191
frankvnk 0:59ae5a71c902 192 private:
frankvnk 0:59ae5a71c902 193 I2C _i2c;
frankvnk 0:59ae5a71c902 194 FunctionPointer _fptr;
frankvnk 0:59ae5a71c902 195 uint8_t _ismode; // 0: no irq/sync mode - 1: irq mode - 2: sync mode
frankvnk 0:59ae5a71c902 196 void _alsISR(void);
frankvnk 0:59ae5a71c902 197 void i2cfail(void);
frankvnk 0:59ae5a71c902 198 void readRegs(uint8_t addr, uint8_t * data, uint8_t len);
frankvnk 0:59ae5a71c902 199 uint8_t readReg(uint8_t addr);
frankvnk 0:59ae5a71c902 200 void writeRegs(uint8_t * data, uint8_t len);
frankvnk 0:59ae5a71c902 201 };
frankvnk 0:59ae5a71c902 202
frankvnk 0:59ae5a71c902 203 #endif