This program demonstrates ambient light sensing on the QW Shields.

Dependencies:   mbed

Components / QW SIGFOX Development Kit
The QW ecosystem is designed to rapidly explore the Sigfox network.

Committer:
quicksand
Date:
Thu Nov 05 09:40:10 2015 +0000
Revision:
0:6c17d1a79f75
This program demonstrates the ambient light sensing functionality of the VCNL4010 on the QW Shields.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quicksand 0:6c17d1a79f75 1 /*
quicksand 0:6c17d1a79f75 2 Copyright (c) 2012 Vishay GmbH, www.vishay.com
quicksand 0:6c17d1a79f75 3 author: DS, version 1.2
quicksand 0:6c17d1a79f75 4
quicksand 0:6c17d1a79f75 5 Permission is hereby granted, free of charge, to any person obtaining a copy
quicksand 0:6c17d1a79f75 6 of this software and associated documentation files (the "Software"), to deal
quicksand 0:6c17d1a79f75 7 in the Software without restriction, including without limitation the rights
quicksand 0:6c17d1a79f75 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
quicksand 0:6c17d1a79f75 9 copies of the Software, and to permit persons to whom the Software is
quicksand 0:6c17d1a79f75 10 furnished to do so, subject to the following conditions:
quicksand 0:6c17d1a79f75 11
quicksand 0:6c17d1a79f75 12 The above copyright notice and this permission notice shall be included in
quicksand 0:6c17d1a79f75 13 all copies or substantial portions of the Software.
quicksand 0:6c17d1a79f75 14
quicksand 0:6c17d1a79f75 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
quicksand 0:6c17d1a79f75 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
quicksand 0:6c17d1a79f75 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
quicksand 0:6c17d1a79f75 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
quicksand 0:6c17d1a79f75 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
quicksand 0:6c17d1a79f75 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
quicksand 0:6c17d1a79f75 21 THE SOFTWARE.
quicksand 0:6c17d1a79f75 22 */
quicksand 0:6c17d1a79f75 23
quicksand 0:6c17d1a79f75 24 #ifndef VCNL40x0_H
quicksand 0:6c17d1a79f75 25 #define VCNL40x0_H
quicksand 0:6c17d1a79f75 26
quicksand 0:6c17d1a79f75 27 #include "mbed.h"
quicksand 0:6c17d1a79f75 28
quicksand 0:6c17d1a79f75 29 // Library for the Vishay Proximity/Ambient Light Sensor VCNL4010/4020
quicksand 0:6c17d1a79f75 30 // The VCNL4x00 is a I2C digital Proximity and Ambient Light Sensor in a small SMD package
quicksand 0:6c17d1a79f75 31
quicksand 0:6c17d1a79f75 32 #define VCNL40x0_ADDRESS (0x26) // 001 0011 shifted left 1 bit = 0x26
quicksand 0:6c17d1a79f75 33
quicksand 0:6c17d1a79f75 34 // registers
quicksand 0:6c17d1a79f75 35 #define REGISTER_COMMAND (0x80)
quicksand 0:6c17d1a79f75 36 #define REGISTER_ID (0x81)
quicksand 0:6c17d1a79f75 37 #define REGISTER_PROX_RATE (0x82)
quicksand 0:6c17d1a79f75 38 #define REGISTER_PROX_CURRENT (0x83)
quicksand 0:6c17d1a79f75 39 #define REGISTER_AMBI_PARAMETER (0x84)
quicksand 0:6c17d1a79f75 40 #define REGISTER_AMBI_VALUE (0x85)
quicksand 0:6c17d1a79f75 41 #define REGISTER_PROX_VALUE (0x87)
quicksand 0:6c17d1a79f75 42 #define REGISTER_INTERRUPT_CONTROL (0x89)
quicksand 0:6c17d1a79f75 43 #define REGISTER_INTERRUPT_LOW_THRES (0x8a)
quicksand 0:6c17d1a79f75 44 #define REGISTER_INTERRUPT_HIGH_THRES (0x8c)
quicksand 0:6c17d1a79f75 45 #define REGISTER_INTERRUPT_STATUS (0x8e)
quicksand 0:6c17d1a79f75 46 #define REGISTER_PROX_TIMING (0x8f)
quicksand 0:6c17d1a79f75 47 #define REGISTER_AMBI_IR_LIGHT_LEVEL (0x90) // This register is not intended to be use by customer
quicksand 0:6c17d1a79f75 48
quicksand 0:6c17d1a79f75 49 // Bits in Command register (0x80)
quicksand 0:6c17d1a79f75 50 #define COMMAND_ALL_DISABLE (0x00)
quicksand 0:6c17d1a79f75 51 #define COMMAND_SELFTIMED_MODE_ENABLE (0x01)
quicksand 0:6c17d1a79f75 52 #define COMMAND_PROX_ENABLE (0x02)
quicksand 0:6c17d1a79f75 53 #define COMMAND_AMBI_ENABLE (0x04)
quicksand 0:6c17d1a79f75 54 #define COMMAND_PROX_ON_DEMAND (0x08)
quicksand 0:6c17d1a79f75 55 #define COMMAND_AMBI_ON_DEMAND (0x10)
quicksand 0:6c17d1a79f75 56 #define COMMAND_MASK_PROX_DATA_READY (0x20)
quicksand 0:6c17d1a79f75 57 #define COMMAND_MASK_AMBI_DATA_READY (0x40)
quicksand 0:6c17d1a79f75 58 #define COMMAND_MASK_LOCK (0x80)
quicksand 0:6c17d1a79f75 59
quicksand 0:6c17d1a79f75 60 // Bits in Product ID Revision Register (0x81)
quicksand 0:6c17d1a79f75 61 #define PRODUCT_MASK_REVISION_ID (0x0f)
quicksand 0:6c17d1a79f75 62 #define PRODUCT_MASK_PRODUCT_ID (0xf0)
quicksand 0:6c17d1a79f75 63
quicksand 0:6c17d1a79f75 64 // Bits in Prox Measurement Rate register (0x82)
quicksand 0:6c17d1a79f75 65 #define PROX_MEASUREMENT_RATE_2 (0x00) // DEFAULT
quicksand 0:6c17d1a79f75 66 #define PROX_MEASUREMENT_RATE_4 (0x01)
quicksand 0:6c17d1a79f75 67 #define PROX_MEASUREMENT_RATE_8 (0x02)
quicksand 0:6c17d1a79f75 68 #define PROX_MEASUREMENT_RATE_16 (0x03)
quicksand 0:6c17d1a79f75 69 #define PROX_MEASUREMENT_RATE_31 (0x04)
quicksand 0:6c17d1a79f75 70 #define PROX_MEASUREMENT_RATE_62 (0x05)
quicksand 0:6c17d1a79f75 71 #define PROX_MEASUREMENT_RATE_125 (0x06)
quicksand 0:6c17d1a79f75 72 #define PROX_MEASUREMENT_RATE_250 (0x07)
quicksand 0:6c17d1a79f75 73 #define PROX_MASK_MEASUREMENT_RATE (0x07)
quicksand 0:6c17d1a79f75 74
quicksand 0:6c17d1a79f75 75 // Bits in Procimity LED current setting (0x83)
quicksand 0:6c17d1a79f75 76 #define PROX_MASK_LED_CURRENT (0x3f) // DEFAULT = 2
quicksand 0:6c17d1a79f75 77 #define PROX_MASK_FUSE_PROG_ID (0xc0)
quicksand 0:6c17d1a79f75 78
quicksand 0:6c17d1a79f75 79 // Bits in Ambient Light Parameter register (0x84)
quicksand 0:6c17d1a79f75 80 #define AMBI_PARA_AVERAGE_1 (0x00)
quicksand 0:6c17d1a79f75 81 #define AMBI_PARA_AVERAGE_2 (0x01)
quicksand 0:6c17d1a79f75 82 #define AMBI_PARA_AVERAGE_4 (0x02)
quicksand 0:6c17d1a79f75 83 #define AMBI_PARA_AVERAGE_8 (0x03)
quicksand 0:6c17d1a79f75 84 #define AMBI_PARA_AVERAGE_16 (0x04)
quicksand 0:6c17d1a79f75 85 #define AMBI_PARA_AVERAGE_32 (0x05) // DEFAULT
quicksand 0:6c17d1a79f75 86 #define AMBI_PARA_AVERAGE_64 (0x06)
quicksand 0:6c17d1a79f75 87 #define AMBI_PARA_AVERAGE_128 (0x07)
quicksand 0:6c17d1a79f75 88 #define AMBI_MASK_PARA_AVERAGE (0x07)
quicksand 0:6c17d1a79f75 89
quicksand 0:6c17d1a79f75 90 #define AMBI_PARA_AUTO_OFFSET_ENABLE (0x08) // DEFAULT enable
quicksand 0:6c17d1a79f75 91 #define AMBI_MASK_PARA_AUTO_OFFSET (0x08)
quicksand 0:6c17d1a79f75 92
quicksand 0:6c17d1a79f75 93 #define AMBI_PARA_MEAS_RATE_1 (0x00)
quicksand 0:6c17d1a79f75 94 #define AMBI_PARA_MEAS_RATE_2 (0x10) // DEFAULT
quicksand 0:6c17d1a79f75 95 #define AMBI_PARA_MEAS_RATE_3 (0x20)
quicksand 0:6c17d1a79f75 96 #define AMBI_PARA_MEAS_RATE_4 (0x30)
quicksand 0:6c17d1a79f75 97 #define AMBI_PARA_MEAS_RATE_5 (0x40)
quicksand 0:6c17d1a79f75 98 #define AMBI_PARA_MEAS_RATE_6 (0x50)
quicksand 0:6c17d1a79f75 99 #define AMBI_PARA_MEAS_RATE_8 (0x60)
quicksand 0:6c17d1a79f75 100 #define AMBI_PARA_MEAS_RATE_10 (0x70)
quicksand 0:6c17d1a79f75 101 #define AMBI_MASK_PARA_MEAS_RATE (0x70)
quicksand 0:6c17d1a79f75 102
quicksand 0:6c17d1a79f75 103 #define AMBI_PARA_CONT_CONV_ENABLE (0x80)
quicksand 0:6c17d1a79f75 104 #define AMBI_MASK_PARA_CONT_CONV (0x80) // DEFAULT disable
quicksand 0:6c17d1a79f75 105
quicksand 0:6c17d1a79f75 106 // Bits in Interrupt Control Register (x89)
quicksand 0:6c17d1a79f75 107 #define INTERRUPT_THRES_SEL_PROX (0x00)
quicksand 0:6c17d1a79f75 108 #define INTERRUPT_THRES_SEL_ALS (0x01)
quicksand 0:6c17d1a79f75 109
quicksand 0:6c17d1a79f75 110 #define INTERRUPT_THRES_ENABLE (0x02)
quicksand 0:6c17d1a79f75 111
quicksand 0:6c17d1a79f75 112 #define INTERRUPT_ALS_READY_ENABLE (0x04)
quicksand 0:6c17d1a79f75 113
quicksand 0:6c17d1a79f75 114 #define INTERRUPT_PROX_READY_ENABLE (0x08)
quicksand 0:6c17d1a79f75 115
quicksand 0:6c17d1a79f75 116 #define INTERRUPT_COUNT_EXCEED_1 (0x00) // DEFAULT
quicksand 0:6c17d1a79f75 117 #define INTERRUPT_COUNT_EXCEED_2 (0x20)
quicksand 0:6c17d1a79f75 118 #define INTERRUPT_COUNT_EXCEED_4 (0x40)
quicksand 0:6c17d1a79f75 119 #define INTERRUPT_COUNT_EXCEED_8 (0x60)
quicksand 0:6c17d1a79f75 120 #define INTERRUPT_COUNT_EXCEED_16 (0x80)
quicksand 0:6c17d1a79f75 121 #define INTERRUPT_COUNT_EXCEED_32 (0xa0)
quicksand 0:6c17d1a79f75 122 #define INTERRUPT_COUNT_EXCEED_64 (0xc0)
quicksand 0:6c17d1a79f75 123 #define INTERRUPT_COUNT_EXCEED_128 (0xe0)
quicksand 0:6c17d1a79f75 124 #define INTERRUPT_MASK_COUNT_EXCEED (0xe0)
quicksand 0:6c17d1a79f75 125
quicksand 0:6c17d1a79f75 126 // Bits in Interrupt Status Register (x8e)
quicksand 0:6c17d1a79f75 127 #define INTERRUPT_STATUS_THRES_HI (0x01)
quicksand 0:6c17d1a79f75 128 #define INTERRUPT_STATUS_THRES_LO (0x02)
quicksand 0:6c17d1a79f75 129 #define INTERRUPT_STATUS_ALS_READY (0x04)
quicksand 0:6c17d1a79f75 130 #define INTERRUPT_STATUS_PROX_READY (0x08)
quicksand 0:6c17d1a79f75 131 #define INTERRUPT_MASK_STATUS_THRES_HI (0x01)
quicksand 0:6c17d1a79f75 132 #define INTERRUPT_MASK_THRES_LO (0x02)
quicksand 0:6c17d1a79f75 133 #define INTERRUPT_MASK_ALS_READY (0x04)
quicksand 0:6c17d1a79f75 134 #define INTERRUPT_MASK_PROX_READY (0x08)
quicksand 0:6c17d1a79f75 135
quicksand 0:6c17d1a79f75 136 typedef enum {
quicksand 0:6c17d1a79f75 137 VCNL40x0_ERROR_OK = 0, // Everything executed normally
quicksand 0:6c17d1a79f75 138 VCNL40x0_ERROR_I2CINIT, // Unable to initialise I2C
quicksand 0:6c17d1a79f75 139 VCNL40x0_ERROR_I2CBUSY, // I2C already in use
quicksand 0:6c17d1a79f75 140 VCNL40x0_ERROR_LAST
quicksand 0:6c17d1a79f75 141 } VCNL40x0Error_e;
quicksand 0:6c17d1a79f75 142
quicksand 0:6c17d1a79f75 143
quicksand 0:6c17d1a79f75 144 class VCNL40x0 {
quicksand 0:6c17d1a79f75 145 public:
quicksand 0:6c17d1a79f75 146 // Creates an instance of the class.
quicksand 0:6c17d1a79f75 147 // Connect module at I2C address addr using I2C port pins sda and scl.
quicksand 0:6c17d1a79f75 148
quicksand 0:6c17d1a79f75 149 VCNL40x0 (PinName sda, PinName scl, unsigned char addr);
quicksand 0:6c17d1a79f75 150
quicksand 0:6c17d1a79f75 151 // Destroys instance
quicksand 0:6c17d1a79f75 152
quicksand 0:6c17d1a79f75 153 ~VCNL40x0();
quicksand 0:6c17d1a79f75 154
quicksand 0:6c17d1a79f75 155 // public functions
quicksand 0:6c17d1a79f75 156
quicksand 0:6c17d1a79f75 157 VCNL40x0Error_e Init (void);
quicksand 0:6c17d1a79f75 158
quicksand 0:6c17d1a79f75 159 VCNL40x0Error_e SetCommandRegister (unsigned char Command);
quicksand 0:6c17d1a79f75 160 VCNL40x0Error_e SetCurrent (unsigned char CurrentValue);
quicksand 0:6c17d1a79f75 161 VCNL40x0Error_e SetProximityRate (unsigned char ProximityRate);
quicksand 0:6c17d1a79f75 162 VCNL40x0Error_e SetAmbiConfiguration (unsigned char AmbiConfiguration);
quicksand 0:6c17d1a79f75 163 VCNL40x0Error_e SetLowThreshold (unsigned int LowThreshold);
quicksand 0:6c17d1a79f75 164 VCNL40x0Error_e SetHighThreshold (unsigned int HighThreshold);
quicksand 0:6c17d1a79f75 165 VCNL40x0Error_e SetInterruptControl (unsigned char InterruptControl);
quicksand 0:6c17d1a79f75 166 VCNL40x0Error_e SetInterruptStatus (unsigned char InterruptStatus);
quicksand 0:6c17d1a79f75 167 VCNL40x0Error_e SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment);
quicksand 0:6c17d1a79f75 168
quicksand 0:6c17d1a79f75 169 VCNL40x0Error_e ReadID (unsigned char *ID);
quicksand 0:6c17d1a79f75 170 VCNL40x0Error_e ReadCurrent (unsigned char *CurrentValue);
quicksand 0:6c17d1a79f75 171 VCNL40x0Error_e ReadCommandRegister (unsigned char *Command);
quicksand 0:6c17d1a79f75 172 VCNL40x0Error_e ReadProxiValue (unsigned int *ProxiValue);
quicksand 0:6c17d1a79f75 173 VCNL40x0Error_e ReadAmbiValue (unsigned int *AmbiValue);
quicksand 0:6c17d1a79f75 174 VCNL40x0Error_e ReadInterruptStatus (unsigned char *InterruptStatus);
quicksand 0:6c17d1a79f75 175 VCNL40x0Error_e ReadInterruptControl (unsigned char *InterruptControl);
quicksand 0:6c17d1a79f75 176
quicksand 0:6c17d1a79f75 177 VCNL40x0Error_e ReadProxiOnDemand (unsigned int *ProxiValue);
quicksand 0:6c17d1a79f75 178 VCNL40x0Error_e ReadAmbiOnDemand (unsigned int *AmbiValue);
quicksand 0:6c17d1a79f75 179
quicksand 0:6c17d1a79f75 180 private:
quicksand 0:6c17d1a79f75 181 I2C _i2c;
quicksand 0:6c17d1a79f75 182 int _addr;
quicksand 0:6c17d1a79f75 183 char _send[3];
quicksand 0:6c17d1a79f75 184 char _receive[2];
quicksand 0:6c17d1a79f75 185
quicksand 0:6c17d1a79f75 186 };
quicksand 0:6c17d1a79f75 187
quicksand 0:6c17d1a79f75 188 #endif