ISR UC / ISR_Mini-explorer

Energy harvesting mobile robot. Developed at Institute of Systems and Robotics — University of Coimbra.

Dependents:   Mapping VirtualForces_debug OneFileToRuleThemAll VirtualForces_with_class ... more

Committer:
ISR
Date:
Thu Feb 02 12:21:11 2017 +0000
Revision:
0:15a30802e719
Initial commit.

Who changed what in which revision?

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