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.21
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#include "VCNL40x0.h"
ISR0:15a30802e719 25
ISR0:15a30802e719 26////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 27
ISR0:15a30802e719 28VCNL40x0::VCNL40x0(PinName sda, PinName scl, unsigned char addr) : _i2c(sda, scl), _addr(addr) {
ISR0:15a30802e719 29 _i2c.frequency(1000000); // set I2C frequency to 1MHz
ISR0:15a30802e719 30}
ISR0:15a30802e719 31
ISR0:15a30802e719 32VCNL40x0::~VCNL40x0() {
ISR0:15a30802e719 33}
ISR0:15a30802e719 34
ISR0:15a30802e719 35////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 36
ISR0:15a30802e719 37VCNL40x0Error_e VCNL40x0::SetCommandRegister (unsigned char Command) {
ISR0:15a30802e719 38
ISR0:15a30802e719 39 _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration reister
ISR0:15a30802e719 40 _send[1] = Command;
ISR0:15a30802e719 41 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 42
ISR0:15a30802e719 43 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 44}
ISR0:15a30802e719 45
ISR0:15a30802e719 46////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 47
ISR0:15a30802e719 48VCNL40x0Error_e VCNL40x0::ReadCommandRegister (unsigned char *Command) {
ISR0:15a30802e719 49
ISR0:15a30802e719 50 _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
ISR0:15a30802e719 51 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR0:15a30802e719 52 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR0:15a30802e719 53
ISR0:15a30802e719 54 *Command = (unsigned char)(_receive[0]);
ISR0:15a30802e719 55
ISR0:15a30802e719 56 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 57}
ISR0:15a30802e719 58
ISR0:15a30802e719 59////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 60
ISR0:15a30802e719 61VCNL40x0Error_e VCNL40x0::ReadID (unsigned char *ID) {
ISR0:15a30802e719 62
ISR0:15a30802e719 63 _send[0] = REGISTER_ID; // VCNL40x0 product ID revision register
ISR0:15a30802e719 64 _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
ISR0:15a30802e719 65 _i2c.read(VCNL40x0_ADDRESS+1, _receive, 1); // Read 1 byte on I2C
ISR0:15a30802e719 66
ISR0:15a30802e719 67 *ID = (unsigned char)(_receive[0]);
ISR0:15a30802e719 68
ISR0:15a30802e719 69 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 70}
ISR0:15a30802e719 71
ISR0:15a30802e719 72////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 73
ISR0:15a30802e719 74VCNL40x0Error_e VCNL40x0::SetCurrent (unsigned char Current) {
ISR0:15a30802e719 75
ISR0:15a30802e719 76 _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register
ISR0:15a30802e719 77 _send[1] = Current;
ISR0:15a30802e719 78 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 79
ISR0:15a30802e719 80 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 81}
ISR0:15a30802e719 82
ISR0:15a30802e719 83////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 84
ISR0:15a30802e719 85VCNL40x0Error_e VCNL40x0::ReadCurrent (unsigned char *Current) {
ISR0:15a30802e719 86
ISR0:15a30802e719 87 _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED current register
ISR0:15a30802e719 88 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR0:15a30802e719 89 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR0:15a30802e719 90
ISR0:15a30802e719 91 *Current = (unsigned char)(_receive[0]);
ISR0:15a30802e719 92
ISR0:15a30802e719 93 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 94}
ISR0:15a30802e719 95
ISR0:15a30802e719 96////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 97
ISR0:15a30802e719 98VCNL40x0Error_e VCNL40x0::SetProximityRate (unsigned char ProximityRate) {
ISR0:15a30802e719 99
ISR0:15a30802e719 100 _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register
ISR0:15a30802e719 101 _send[1] = ProximityRate;
ISR0:15a30802e719 102 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 103
ISR0:15a30802e719 104 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 105}
ISR0:15a30802e719 106
ISR0:15a30802e719 107////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 108
ISR0:15a30802e719 109VCNL40x0Error_e VCNL40x0::SetAmbiConfiguration (unsigned char AmbiConfiguration) {
ISR0:15a30802e719 110
ISR0:15a30802e719 111 _send[0] = REGISTER_AMBI_PARAMETER; // VCNL40x0 Ambilight configuration
ISR0:15a30802e719 112 _send[1] = AmbiConfiguration;
ISR0:15a30802e719 113 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 114
ISR0:15a30802e719 115 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 116}
ISR0:15a30802e719 117
ISR0:15a30802e719 118////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 119
ISR0:15a30802e719 120VCNL40x0Error_e VCNL40x0::SetInterruptControl (unsigned char InterruptControl) {
ISR0:15a30802e719 121
ISR0:15a30802e719 122 _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
ISR0:15a30802e719 123 _send[1] = InterruptControl;
ISR0:15a30802e719 124 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 125
ISR0:15a30802e719 126 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 127}
ISR0:15a30802e719 128
ISR0:15a30802e719 129////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 130
ISR0:15a30802e719 131VCNL40x0Error_e VCNL40x0::ReadInterruptControl (unsigned char *InterruptControl) {
ISR0:15a30802e719 132
ISR0:15a30802e719 133 _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
ISR0:15a30802e719 134 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR0:15a30802e719 135 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR0:15a30802e719 136
ISR0:15a30802e719 137 *InterruptControl = (unsigned char)(_receive[0]);
ISR0:15a30802e719 138
ISR0:15a30802e719 139 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 140}
ISR0:15a30802e719 141
ISR0:15a30802e719 142////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 143
ISR0:15a30802e719 144VCNL40x0Error_e VCNL40x0::SetInterruptStatus (unsigned char InterruptStatus) {
ISR0:15a30802e719 145
ISR0:15a30802e719 146 _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
ISR0:15a30802e719 147 _send[1] = InterruptStatus;
ISR0:15a30802e719 148 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 149
ISR0:15a30802e719 150 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 151}
ISR0:15a30802e719 152
ISR0:15a30802e719 153////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 154
ISR0:15a30802e719 155VCNL40x0Error_e VCNL40x0::SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment) {
ISR0:15a30802e719 156
ISR0:15a30802e719 157 _send[0] = REGISTER_PROX_TIMING; // VCNL40x0 Modulator Timing Adjustment register
ISR0:15a30802e719 158 _send[1] = ModulatorTimingAdjustment;
ISR0:15a30802e719 159 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 160
ISR0:15a30802e719 161 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 162}
ISR0:15a30802e719 163
ISR0:15a30802e719 164////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 165
ISR0:15a30802e719 166VCNL40x0Error_e VCNL40x0::ReadInterruptStatus (unsigned char *InterruptStatus) {
ISR0:15a30802e719 167
ISR0:15a30802e719 168 _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
ISR0:15a30802e719 169 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR0:15a30802e719 170 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR0:15a30802e719 171
ISR0:15a30802e719 172 *InterruptStatus = (unsigned char)(_receive[0]);
ISR0:15a30802e719 173
ISR0:15a30802e719 174 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 175}
ISR0:15a30802e719 176
ISR0:15a30802e719 177////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 178
ISR0:15a30802e719 179VCNL40x0Error_e VCNL40x0::ReadProxiValue (unsigned int *ProxiValue) {
ISR0:15a30802e719 180
ISR0:15a30802e719 181 _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register
ISR0:15a30802e719 182 _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
ISR0:15a30802e719 183 _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
ISR0:15a30802e719 184 *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
ISR0:15a30802e719 185
ISR0:15a30802e719 186 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 187
ISR0:15a30802e719 188}
ISR0:15a30802e719 189
ISR0:15a30802e719 190////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 191
ISR0:15a30802e719 192VCNL40x0Error_e VCNL40x0::ReadAmbiValue (unsigned int *AmbiValue) {
ISR0:15a30802e719 193
ISR0:15a30802e719 194 _send[0] = REGISTER_AMBI_VALUE; // VCNL40x0 Ambient Light Value register
ISR0:15a30802e719 195 _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
ISR0:15a30802e719 196 _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
ISR0:15a30802e719 197 *AmbiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
ISR0:15a30802e719 198
ISR0:15a30802e719 199 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 200
ISR0:15a30802e719 201}
ISR0:15a30802e719 202
ISR0:15a30802e719 203////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 204
ISR0:15a30802e719 205VCNL40x0Error_e VCNL40x0::SetLowThreshold (unsigned int LowThreshold) {
ISR0:15a30802e719 206
ISR0:15a30802e719 207 unsigned char LoByte=0, HiByte=0;
ISR0:15a30802e719 208
ISR0:15a30802e719 209 LoByte = (unsigned char)(LowThreshold & 0x00ff);
ISR0:15a30802e719 210 HiByte = (unsigned char)((LowThreshold & 0xff00)>>8);
ISR0:15a30802e719 211
ISR0:15a30802e719 212 _send[0] = REGISTER_INTERRUPT_LOW_THRES; // VCNL40x0 Low Threshold Register, Hi Byte
ISR0:15a30802e719 213 _send[1] = HiByte;
ISR0:15a30802e719 214 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 215
ISR0:15a30802e719 216 _send[0] = REGISTER_INTERRUPT_LOW_THRES+1; // VCNL40x0 Low Threshold Register, Lo Byte
ISR0:15a30802e719 217 _send[1] = LoByte;
ISR0:15a30802e719 218 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 219
ISR0:15a30802e719 220 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 221
ISR0:15a30802e719 222}
ISR0:15a30802e719 223
ISR0:15a30802e719 224////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 225
ISR0:15a30802e719 226VCNL40x0Error_e VCNL40x0::SetHighThreshold (unsigned int HighThreshold) {
ISR0:15a30802e719 227
ISR0:15a30802e719 228 unsigned char LoByte=0, HiByte=0;
ISR0:15a30802e719 229
ISR0:15a30802e719 230 LoByte = (unsigned char)(HighThreshold & 0x00ff);
ISR0:15a30802e719 231 HiByte = (unsigned char)((HighThreshold & 0xff00)>>8);
ISR0:15a30802e719 232
ISR0:15a30802e719 233 _send[0] = REGISTER_INTERRUPT_HIGH_THRES; // VCNL40x0 High Threshold Register, Hi Byte
ISR0:15a30802e719 234 _send[1] = HiByte;
ISR0:15a30802e719 235 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 236
ISR0:15a30802e719 237 _send[0] = REGISTER_INTERRUPT_HIGH_THRES+1; // VCNL40x0 High Threshold Register, Lo Byte
ISR0:15a30802e719 238 _send[1] = LoByte;
ISR0:15a30802e719 239 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR0:15a30802e719 240
ISR0:15a30802e719 241 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 242
ISR0:15a30802e719 243}
ISR0:15a30802e719 244
ISR0:15a30802e719 245////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 246////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 247////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 248
ISR0:15a30802e719 249VCNL40x0Error_e VCNL40x0::ReadProxiOnDemand (unsigned int *ProxiValue) {
ISR0:15a30802e719 250
ISR0:15a30802e719 251 unsigned char Command=0;
ISR0:15a30802e719 252
ISR0:15a30802e719 253 // enable prox value on demand
ISR0:15a30802e719 254 SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
ISR0:15a30802e719 255
ISR0:15a30802e719 256 // wait on prox data ready bit
ISR0:15a30802e719 257 do {
ISR0:15a30802e719 258 ReadCommandRegister (&Command); // read command register
ISR0:15a30802e719 259 } while (!(Command & COMMAND_MASK_PROX_DATA_READY));
ISR0:15a30802e719 260
ISR0:15a30802e719 261 ReadProxiValue (ProxiValue); // read prox value
ISR0:15a30802e719 262
ISR0:15a30802e719 263 SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand
ISR0:15a30802e719 264
ISR0:15a30802e719 265 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 266}
ISR0:15a30802e719 267
ISR0:15a30802e719 268////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR0:15a30802e719 269
ISR0:15a30802e719 270VCNL40x0Error_e VCNL40x0::ReadAmbiOnDemand (unsigned int *AmbiValue) {
ISR0:15a30802e719 271
ISR0:15a30802e719 272 unsigned char Command=0;
ISR0:15a30802e719 273
ISR0:15a30802e719 274 // enable ambi value on demand
ISR0:15a30802e719 275 SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_AMBI_ON_DEMAND);
ISR0:15a30802e719 276
ISR0:15a30802e719 277 // wait on ambi data ready bit
ISR0:15a30802e719 278 do {
ISR0:15a30802e719 279 ReadCommandRegister (&Command); // read command register
ISR0:15a30802e719 280 } while (!(Command & COMMAND_MASK_AMBI_DATA_READY));
ISR0:15a30802e719 281
ISR0:15a30802e719 282 ReadAmbiValue (AmbiValue); // read ambi value
ISR0:15a30802e719 283
ISR0:15a30802e719 284 SetCommandRegister (COMMAND_ALL_DISABLE); // stop ambi value on demand
ISR0:15a30802e719 285
ISR0:15a30802e719 286 return VCNL40x0_ERROR_OK;
ISR0:15a30802e719 287}
ISR0:15a30802e719 288