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, ....
ISL29125.h@1:1c1ded8d719e, 2014-05-28 (annotated)
- Committer:
- frankvnk
- Date:
- Wed May 28 18:44:54 2014 +0000
- Revision:
- 1:1c1ded8d719e
- Parent:
- 0:59ae5a71c902
- Child:
- 2:f1b55929169a
update doxygen
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:59ae5a71c902 | 49 | * \brief Create a ISL29125 object connected to I2C bus, irq or sync pin and user-ISR pointer. |
frankvnk | 0:59ae5a71c902 | 50 | * \param sda SDA pin. |
frankvnk | 0:59ae5a71c902 | 51 | * \param scl SCL pin. |
frankvnk | 0:59ae5a71c902 | 52 | * \param irqsync (Optional) Interrupt pin when fptr is also declared. |
frankvnk | 0:59ae5a71c902 | 53 | * Sync output when fptr is not declared. |
frankvnk | 0:59ae5a71c902 | 54 | * \param fptr (Optional) Pointer to user-ISR (only used with Interrupt pin). |
frankvnk | 0:59ae5a71c902 | 55 | * \return none |
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 | 0:59ae5a71c902 | 60 | * \brief Read status register. |
frankvnk | 0:59ae5a71c902 | 61 | * The interrupt status flag is cleared when the status register is read. |
frankvnk | 0:59ae5a71c902 | 62 | * \param NONE |
frankvnk | 0:59ae5a71c902 | 63 | * \return Content of the entire status register. |
frankvnk | 0:59ae5a71c902 | 64 | * bit Description |
frankvnk | 0:59ae5a71c902 | 65 | * --- --------------------------------------------------------- |
frankvnk | 0:59ae5a71c902 | 66 | * 5,4 RGB conversion - 00: Inactive |
frankvnk | 0:59ae5a71c902 | 67 | * 01: Green |
frankvnk | 0:59ae5a71c902 | 68 | * 10: Red |
frankvnk | 0:59ae5a71c902 | 69 | * 11: Blue |
frankvnk | 0:59ae5a71c902 | 70 | * 2 Brownout status - 0: No brownout |
frankvnk | 0:59ae5a71c902 | 71 | * 1: Power down or brownout occured |
frankvnk | 0:59ae5a71c902 | 72 | * 1 Conversion status - 0: Conversion is pending or inactive |
frankvnk | 0:59ae5a71c902 | 73 | * 1: Conversion is completed |
frankvnk | 0:59ae5a71c902 | 74 | * 0 Interrupt status - 0: no interrupt occured |
frankvnk | 0:59ae5a71c902 | 75 | * 1: interrupt occured |
frankvnk | 0:59ae5a71c902 | 76 | */ |
frankvnk | 0:59ae5a71c902 | 77 | uint8_t Status(void); |
frankvnk | 0:59ae5a71c902 | 78 | |
frankvnk | 0:59ae5a71c902 | 79 | /** |
frankvnk | 0:59ae5a71c902 | 80 | * \brief Read the device identifier. |
frankvnk | 0:59ae5a71c902 | 81 | * \param none. |
frankvnk | 0:59ae5a71c902 | 82 | * \return 0x7D on success. |
frankvnk | 0:59ae5a71c902 | 83 | */ |
frankvnk | 0:59ae5a71c902 | 84 | uint8_t WhoAmI(void); |
frankvnk | 0:59ae5a71c902 | 85 | |
frankvnk | 0:59ae5a71c902 | 86 | /** |
frankvnk | 0:59ae5a71c902 | 87 | * \brief Read the channel values (12 or 16-bit - depends on resolution). |
frankvnk | 0:59ae5a71c902 | 88 | * \param color ISL29125_R Return Red channel. |
frankvnk | 0:59ae5a71c902 | 89 | * ISL29125_G Return Green channel. |
frankvnk | 0:59ae5a71c902 | 90 | * ISL29125_B Return Blue channel. |
frankvnk | 0:59ae5a71c902 | 91 | * ISL29125_RGB Return Red, Green and Blue channels. |
frankvnk | 0:59ae5a71c902 | 92 | * \param data Pointer to 16-bit array for storing the channel value(s). |
frankvnk | 0:59ae5a71c902 | 93 | * \ Array size: 1 for a single color (Red, Green or Blue). |
frankvnk | 0:59ae5a71c902 | 94 | * \ 3 for all colors. |
frankvnk | 0:59ae5a71c902 | 95 | * \return bool 1: new data available - 0: no new data available. |
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 | 0:59ae5a71c902 | 100 | * \brief Read/Write the low/high interrupt threshold value. |
frankvnk | 0:59ae5a71c902 | 101 | * When setIRQonColor is activated, an interrupt will occur when the low or high threshold is exceeded. |
frankvnk | 0:59ae5a71c902 | 102 | * \param reg ISL29125_LTH_W Write 16-bit low threshold. |
frankvnk | 0:59ae5a71c902 | 103 | * ISL29125_HTH_W Write 16-bit high threshold. |
frankvnk | 0:59ae5a71c902 | 104 | * ISL29125_LTH_R Read 16-bit low threshold. |
frankvnk | 0:59ae5a71c902 | 105 | * ISL29125_HTH_R Read 16-bit high threshold. |
frankvnk | 0:59ae5a71c902 | 106 | * \param thres 16-bit threshold value (only needed when _W parameter is used). |
frankvnk | 0:59ae5a71c902 | 107 | * \return Written threshold value when called with _W parameter. |
frankvnk | 0:59ae5a71c902 | 108 | * Stored threshold value when called with _R parameter only. |
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 | 0:59ae5a71c902 | 113 | * \brief Read/Write the RGB operating mode value (active ADC channels). |
frankvnk | 0:59ae5a71c902 | 114 | * \param mode ISL29125_G G channel only. |
frankvnk | 0:59ae5a71c902 | 115 | * ISL29125_R R channel only. |
frankvnk | 0:59ae5a71c902 | 116 | * ISL29125_B B channel only. |
frankvnk | 0:59ae5a71c902 | 117 | * ISL29125_RG R and G channel. |
frankvnk | 0:59ae5a71c902 | 118 | * ISL29125_BG B and G channel. |
frankvnk | 0:59ae5a71c902 | 119 | * ISL29125_RGB R, G and B channel. |
frankvnk | 0:59ae5a71c902 | 120 | * ISL29125_STBY Standby (No ADC conversion). |
frankvnk | 0:59ae5a71c902 | 121 | * ISL29125_OFF Power down ADC conversion. |
frankvnk | 0:59ae5a71c902 | 122 | * \return Written value is returned when called with valid parameter, otherwise 0xff is returned. |
frankvnk | 0:59ae5a71c902 | 123 | * Stored value is returned when called without parameter. |
frankvnk | 0:59ae5a71c902 | 124 | */ |
frankvnk | 0:59ae5a71c902 | 125 | uint8_t RGBmode(uint8_t mode=0xff); |
frankvnk | 0:59ae5a71c902 | 126 | |
frankvnk | 0:59ae5a71c902 | 127 | /** |
frankvnk | 0:59ae5a71c902 | 128 | * \brief Read/Write the sensing range parameter. |
frankvnk | 0:59ae5a71c902 | 129 | * \param range ISL29125_375LX Max. value corresponds to 375 lux. |
frankvnk | 0:59ae5a71c902 | 130 | * ISL29125_10KLX Max. value corresponds to 10000 lux. |
frankvnk | 0:59ae5a71c902 | 131 | * \return Written value is returned when called with valid parameter, otherwise 0xff is returned. |
frankvnk | 0:59ae5a71c902 | 132 | * Stored value is returned when called without parameter. |
frankvnk | 0:59ae5a71c902 | 133 | */ |
frankvnk | 0:59ae5a71c902 | 134 | uint8_t Range(uint8_t range=0xff); |
frankvnk | 0:59ae5a71c902 | 135 | |
frankvnk | 0:59ae5a71c902 | 136 | /** |
frankvnk | 0:59ae5a71c902 | 137 | * \brief Read/Write the ADC resolution parameter. |
frankvnk | 0:59ae5a71c902 | 138 | * \param range ISL29125_16BIT ADC resolution = 16 bit. |
frankvnk | 0:59ae5a71c902 | 139 | * ISL29125_12BIT ADC resolution = 12 bit. |
frankvnk | 0:59ae5a71c902 | 140 | * \return Written value is returned when called with valid parameter, otherwise 0xff is returned. |
frankvnk | 0:59ae5a71c902 | 141 | * Stored value is returned when called without parameter. |
frankvnk | 0:59ae5a71c902 | 142 | */ |
frankvnk | 0:59ae5a71c902 | 143 | uint8_t Resolution(uint8_t resol=0xff); |
frankvnk | 0:59ae5a71c902 | 144 | |
frankvnk | 0:59ae5a71c902 | 145 | /** |
frankvnk | 0:59ae5a71c902 | 146 | * \brief Read/Write the IRQ persistence parameter. |
frankvnk | 0:59ae5a71c902 | 147 | * \param persist ISL29125_PERS1 IRQ occurs when threshold is exceeded once. |
frankvnk | 0:59ae5a71c902 | 148 | * ISL29125_PERS2 IRQ occurs when threshold is exceeded twice. |
frankvnk | 0:59ae5a71c902 | 149 | * ISL29125_PERS4 IRQ occurs when threshold is exceeded 4 times. |
frankvnk | 0:59ae5a71c902 | 150 | * ISL29125_PERS8 IRQ occurs when threshold is exceeded 8 times. |
frankvnk | 0:59ae5a71c902 | 151 | * \return Written value is returned when called with valid parameter, otherwise 0xff is returned. |
frankvnk | 0:59ae5a71c902 | 152 | * Stored value is returned when called without parameter. |
frankvnk | 0:59ae5a71c902 | 153 | */ |
frankvnk | 0:59ae5a71c902 | 154 | uint8_t Persist(uint8_t persist=0xff); |
frankvnk | 0:59ae5a71c902 | 155 | |
frankvnk | 0:59ae5a71c902 | 156 | /** |
frankvnk | 0:59ae5a71c902 | 157 | * \brief Read/Write the IRQ on conversion done parameter. |
frankvnk | 0:59ae5a71c902 | 158 | * \param true: enabled. |
frankvnk | 0:59ae5a71c902 | 159 | * false: disabled. |
frankvnk | 0:59ae5a71c902 | 160 | * \return Written value is returned when called with valid parameter, otherwise 0xff is returned. |
frankvnk | 0:59ae5a71c902 | 161 | * Stored value is returned when called without parameter. |
frankvnk | 0:59ae5a71c902 | 162 | */ |
frankvnk | 0:59ae5a71c902 | 163 | uint8_t IRQonCnvDone(uint8_t irqen=0xff); |
frankvnk | 0:59ae5a71c902 | 164 | |
frankvnk | 0:59ae5a71c902 | 165 | /** |
frankvnk | 0:59ae5a71c902 | 166 | * \brief Read/Write the IRQ threshold to color assignment parameter. |
frankvnk | 0:59ae5a71c902 | 167 | * \param RGBmode ISL29125_OFF No interrupt. |
frankvnk | 0:59ae5a71c902 | 168 | * ISL29125_G Green interrupt. |
frankvnk | 0:59ae5a71c902 | 169 | * ISL29125_R Red interrupt. |
frankvnk | 1:1c1ded8d719e | 170 | * ISL29125_B Blue interrupt. |
frankvnk | 0:59ae5a71c902 | 171 | * \return Written value is returned when called with valid parameter, otherwise 0xff is returned. |
frankvnk | 0:59ae5a71c902 | 172 | * Stored value is returned when called without parameter. |
frankvnk | 0:59ae5a71c902 | 173 | */ |
frankvnk | 0:59ae5a71c902 | 174 | uint8_t IRQonColor(uint8_t RGBmode=0xff); |
frankvnk | 0:59ae5a71c902 | 175 | |
frankvnk | 0:59ae5a71c902 | 176 | /** |
frankvnk | 0:59ae5a71c902 | 177 | * \brief Read/Write the active IR compensation parameter. |
frankvnk | 0:59ae5a71c902 | 178 | * \param ircomp valid range: between 0..63 or 128..191. |
frankvnk | 0:59ae5a71c902 | 179 | * \return Written value is returned when called with valid parameter, otherwise 0xff is returned. |
frankvnk | 0:59ae5a71c902 | 180 | * Stored value is returned when called without parameter. |
frankvnk | 0:59ae5a71c902 | 181 | */ |
frankvnk | 0:59ae5a71c902 | 182 | uint8_t IRcomp(uint8_t ircomp=0xff); |
frankvnk | 0:59ae5a71c902 | 183 | |
frankvnk | 0:59ae5a71c902 | 184 | /** |
frankvnk | 0:59ae5a71c902 | 185 | * \brief Start ADC conversion. |
frankvnk | 0:59ae5a71c902 | 186 | * Only possible when SyncMode is activated. |
frankvnk | 0:59ae5a71c902 | 187 | * \param None. |
frankvnk | 0:59ae5a71c902 | 188 | * \return bool 1: success - 0: fail. |
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 |