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
ISR 0:15a30802e719 1 /*
ISR 0:15a30802e719 2 Copyright (c) 2012 Vishay GmbH, www.vishay.com
ISR 0:15a30802e719 3 author: DS, version 1.21
ISR 0:15a30802e719 4
ISR 0:15a30802e719 5 Permission is hereby granted, free of charge, to any person obtaining a copy
ISR 0:15a30802e719 6 of this software and associated documentation files (the "Software"), to deal
ISR 0:15a30802e719 7 in the Software without restriction, including without limitation the rights
ISR 0:15a30802e719 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ISR 0:15a30802e719 9 copies of the Software, and to permit persons to whom the Software is
ISR 0:15a30802e719 10 furnished to do so, subject to the following conditions:
ISR 0:15a30802e719 11
ISR 0:15a30802e719 12 The above copyright notice and this permission notice shall be included in
ISR 0:15a30802e719 13 all copies or substantial portions of the Software.
ISR 0:15a30802e719 14
ISR 0:15a30802e719 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ISR 0:15a30802e719 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ISR 0:15a30802e719 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ISR 0:15a30802e719 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ISR 0:15a30802e719 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ISR 0:15a30802e719 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ISR 0:15a30802e719 21 THE SOFTWARE.
ISR 0:15a30802e719 22 */
ISR 0:15a30802e719 23
ISR 0:15a30802e719 24 #include "VCNL40x0.h"
ISR 0:15a30802e719 25
ISR 0:15a30802e719 26 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 27
ISR 0:15a30802e719 28 VCNL40x0::VCNL40x0(PinName sda, PinName scl, unsigned char addr) : _i2c(sda, scl), _addr(addr) {
ISR 0:15a30802e719 29 _i2c.frequency(1000000); // set I2C frequency to 1MHz
ISR 0:15a30802e719 30 }
ISR 0:15a30802e719 31
ISR 0:15a30802e719 32 VCNL40x0::~VCNL40x0() {
ISR 0:15a30802e719 33 }
ISR 0:15a30802e719 34
ISR 0:15a30802e719 35 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 36
ISR 0:15a30802e719 37 VCNL40x0Error_e VCNL40x0::SetCommandRegister (unsigned char Command) {
ISR 0:15a30802e719 38
ISR 0:15a30802e719 39 _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration reister
ISR 0:15a30802e719 40 _send[1] = Command;
ISR 0:15a30802e719 41 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 42
ISR 0:15a30802e719 43 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 44 }
ISR 0:15a30802e719 45
ISR 0:15a30802e719 46 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 47
ISR 0:15a30802e719 48 VCNL40x0Error_e VCNL40x0::ReadCommandRegister (unsigned char *Command) {
ISR 0:15a30802e719 49
ISR 0:15a30802e719 50 _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
ISR 0:15a30802e719 51 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR 0:15a30802e719 52 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR 0:15a30802e719 53
ISR 0:15a30802e719 54 *Command = (unsigned char)(_receive[0]);
ISR 0:15a30802e719 55
ISR 0:15a30802e719 56 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 57 }
ISR 0:15a30802e719 58
ISR 0:15a30802e719 59 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 60
ISR 0:15a30802e719 61 VCNL40x0Error_e VCNL40x0::ReadID (unsigned char *ID) {
ISR 0:15a30802e719 62
ISR 0:15a30802e719 63 _send[0] = REGISTER_ID; // VCNL40x0 product ID revision register
ISR 0:15a30802e719 64 _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
ISR 0:15a30802e719 65 _i2c.read(VCNL40x0_ADDRESS+1, _receive, 1); // Read 1 byte on I2C
ISR 0:15a30802e719 66
ISR 0:15a30802e719 67 *ID = (unsigned char)(_receive[0]);
ISR 0:15a30802e719 68
ISR 0:15a30802e719 69 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 70 }
ISR 0:15a30802e719 71
ISR 0:15a30802e719 72 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 73
ISR 0:15a30802e719 74 VCNL40x0Error_e VCNL40x0::SetCurrent (unsigned char Current) {
ISR 0:15a30802e719 75
ISR 0:15a30802e719 76 _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register
ISR 0:15a30802e719 77 _send[1] = Current;
ISR 0:15a30802e719 78 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 79
ISR 0:15a30802e719 80 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 81 }
ISR 0:15a30802e719 82
ISR 0:15a30802e719 83 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 84
ISR 0:15a30802e719 85 VCNL40x0Error_e VCNL40x0::ReadCurrent (unsigned char *Current) {
ISR 0:15a30802e719 86
ISR 0:15a30802e719 87 _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED current register
ISR 0:15a30802e719 88 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR 0:15a30802e719 89 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR 0:15a30802e719 90
ISR 0:15a30802e719 91 *Current = (unsigned char)(_receive[0]);
ISR 0:15a30802e719 92
ISR 0:15a30802e719 93 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 94 }
ISR 0:15a30802e719 95
ISR 0:15a30802e719 96 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 97
ISR 0:15a30802e719 98 VCNL40x0Error_e VCNL40x0::SetProximityRate (unsigned char ProximityRate) {
ISR 0:15a30802e719 99
ISR 0:15a30802e719 100 _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register
ISR 0:15a30802e719 101 _send[1] = ProximityRate;
ISR 0:15a30802e719 102 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 103
ISR 0:15a30802e719 104 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 105 }
ISR 0:15a30802e719 106
ISR 0:15a30802e719 107 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 108
ISR 0:15a30802e719 109 VCNL40x0Error_e VCNL40x0::SetAmbiConfiguration (unsigned char AmbiConfiguration) {
ISR 0:15a30802e719 110
ISR 0:15a30802e719 111 _send[0] = REGISTER_AMBI_PARAMETER; // VCNL40x0 Ambilight configuration
ISR 0:15a30802e719 112 _send[1] = AmbiConfiguration;
ISR 0:15a30802e719 113 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 114
ISR 0:15a30802e719 115 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 116 }
ISR 0:15a30802e719 117
ISR 0:15a30802e719 118 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 119
ISR 0:15a30802e719 120 VCNL40x0Error_e VCNL40x0::SetInterruptControl (unsigned char InterruptControl) {
ISR 0:15a30802e719 121
ISR 0:15a30802e719 122 _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
ISR 0:15a30802e719 123 _send[1] = InterruptControl;
ISR 0:15a30802e719 124 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 125
ISR 0:15a30802e719 126 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 127 }
ISR 0:15a30802e719 128
ISR 0:15a30802e719 129 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 130
ISR 0:15a30802e719 131 VCNL40x0Error_e VCNL40x0::ReadInterruptControl (unsigned char *InterruptControl) {
ISR 0:15a30802e719 132
ISR 0:15a30802e719 133 _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
ISR 0:15a30802e719 134 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR 0:15a30802e719 135 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR 0:15a30802e719 136
ISR 0:15a30802e719 137 *InterruptControl = (unsigned char)(_receive[0]);
ISR 0:15a30802e719 138
ISR 0:15a30802e719 139 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 140 }
ISR 0:15a30802e719 141
ISR 0:15a30802e719 142 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 143
ISR 0:15a30802e719 144 VCNL40x0Error_e VCNL40x0::SetInterruptStatus (unsigned char InterruptStatus) {
ISR 0:15a30802e719 145
ISR 0:15a30802e719 146 _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
ISR 0:15a30802e719 147 _send[1] = InterruptStatus;
ISR 0:15a30802e719 148 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 149
ISR 0:15a30802e719 150 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 151 }
ISR 0:15a30802e719 152
ISR 0:15a30802e719 153 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 154
ISR 0:15a30802e719 155 VCNL40x0Error_e VCNL40x0::SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment) {
ISR 0:15a30802e719 156
ISR 0:15a30802e719 157 _send[0] = REGISTER_PROX_TIMING; // VCNL40x0 Modulator Timing Adjustment register
ISR 0:15a30802e719 158 _send[1] = ModulatorTimingAdjustment;
ISR 0:15a30802e719 159 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 160
ISR 0:15a30802e719 161 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 162 }
ISR 0:15a30802e719 163
ISR 0:15a30802e719 164 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 165
ISR 0:15a30802e719 166 VCNL40x0Error_e VCNL40x0::ReadInterruptStatus (unsigned char *InterruptStatus) {
ISR 0:15a30802e719 167
ISR 0:15a30802e719 168 _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register
ISR 0:15a30802e719 169 _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C
ISR 0:15a30802e719 170 _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C
ISR 0:15a30802e719 171
ISR 0:15a30802e719 172 *InterruptStatus = (unsigned char)(_receive[0]);
ISR 0:15a30802e719 173
ISR 0:15a30802e719 174 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 175 }
ISR 0:15a30802e719 176
ISR 0:15a30802e719 177 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 178
ISR 0:15a30802e719 179 VCNL40x0Error_e VCNL40x0::ReadProxiValue (unsigned int *ProxiValue) {
ISR 0:15a30802e719 180
ISR 0:15a30802e719 181 _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register
ISR 0:15a30802e719 182 _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
ISR 0:15a30802e719 183 _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
ISR 0:15a30802e719 184 *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
ISR 0:15a30802e719 185
ISR 0:15a30802e719 186 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 187
ISR 0:15a30802e719 188 }
ISR 0:15a30802e719 189
ISR 0:15a30802e719 190 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 191
ISR 0:15a30802e719 192 VCNL40x0Error_e VCNL40x0::ReadAmbiValue (unsigned int *AmbiValue) {
ISR 0:15a30802e719 193
ISR 0:15a30802e719 194 _send[0] = REGISTER_AMBI_VALUE; // VCNL40x0 Ambient Light Value register
ISR 0:15a30802e719 195 _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C
ISR 0:15a30802e719 196 _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C
ISR 0:15a30802e719 197 *AmbiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
ISR 0:15a30802e719 198
ISR 0:15a30802e719 199 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 200
ISR 0:15a30802e719 201 }
ISR 0:15a30802e719 202
ISR 0:15a30802e719 203 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 204
ISR 0:15a30802e719 205 VCNL40x0Error_e VCNL40x0::SetLowThreshold (unsigned int LowThreshold) {
ISR 0:15a30802e719 206
ISR 0:15a30802e719 207 unsigned char LoByte=0, HiByte=0;
ISR 0:15a30802e719 208
ISR 0:15a30802e719 209 LoByte = (unsigned char)(LowThreshold & 0x00ff);
ISR 0:15a30802e719 210 HiByte = (unsigned char)((LowThreshold & 0xff00)>>8);
ISR 0:15a30802e719 211
ISR 0:15a30802e719 212 _send[0] = REGISTER_INTERRUPT_LOW_THRES; // VCNL40x0 Low Threshold Register, Hi Byte
ISR 0:15a30802e719 213 _send[1] = HiByte;
ISR 0:15a30802e719 214 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 215
ISR 0:15a30802e719 216 _send[0] = REGISTER_INTERRUPT_LOW_THRES+1; // VCNL40x0 Low Threshold Register, Lo Byte
ISR 0:15a30802e719 217 _send[1] = LoByte;
ISR 0:15a30802e719 218 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 219
ISR 0:15a30802e719 220 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 221
ISR 0:15a30802e719 222 }
ISR 0:15a30802e719 223
ISR 0:15a30802e719 224 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 225
ISR 0:15a30802e719 226 VCNL40x0Error_e VCNL40x0::SetHighThreshold (unsigned int HighThreshold) {
ISR 0:15a30802e719 227
ISR 0:15a30802e719 228 unsigned char LoByte=0, HiByte=0;
ISR 0:15a30802e719 229
ISR 0:15a30802e719 230 LoByte = (unsigned char)(HighThreshold & 0x00ff);
ISR 0:15a30802e719 231 HiByte = (unsigned char)((HighThreshold & 0xff00)>>8);
ISR 0:15a30802e719 232
ISR 0:15a30802e719 233 _send[0] = REGISTER_INTERRUPT_HIGH_THRES; // VCNL40x0 High Threshold Register, Hi Byte
ISR 0:15a30802e719 234 _send[1] = HiByte;
ISR 0:15a30802e719 235 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 236
ISR 0:15a30802e719 237 _send[0] = REGISTER_INTERRUPT_HIGH_THRES+1; // VCNL40x0 High Threshold Register, Lo Byte
ISR 0:15a30802e719 238 _send[1] = LoByte;
ISR 0:15a30802e719 239 _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C
ISR 0:15a30802e719 240
ISR 0:15a30802e719 241 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 242
ISR 0:15a30802e719 243 }
ISR 0:15a30802e719 244
ISR 0:15a30802e719 245 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 246 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 247 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 248
ISR 0:15a30802e719 249 VCNL40x0Error_e VCNL40x0::ReadProxiOnDemand (unsigned int *ProxiValue) {
ISR 0:15a30802e719 250
ISR 0:15a30802e719 251 unsigned char Command=0;
ISR 0:15a30802e719 252
ISR 0:15a30802e719 253 // enable prox value on demand
ISR 0:15a30802e719 254 SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
ISR 0:15a30802e719 255
ISR 0:15a30802e719 256 // wait on prox data ready bit
ISR 0:15a30802e719 257 do {
ISR 0:15a30802e719 258 ReadCommandRegister (&Command); // read command register
ISR 0:15a30802e719 259 } while (!(Command & COMMAND_MASK_PROX_DATA_READY));
ISR 0:15a30802e719 260
ISR 0:15a30802e719 261 ReadProxiValue (ProxiValue); // read prox value
ISR 0:15a30802e719 262
ISR 0:15a30802e719 263 SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand
ISR 0:15a30802e719 264
ISR 0:15a30802e719 265 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 266 }
ISR 0:15a30802e719 267
ISR 0:15a30802e719 268 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ISR 0:15a30802e719 269
ISR 0:15a30802e719 270 VCNL40x0Error_e VCNL40x0::ReadAmbiOnDemand (unsigned int *AmbiValue) {
ISR 0:15a30802e719 271
ISR 0:15a30802e719 272 unsigned char Command=0;
ISR 0:15a30802e719 273
ISR 0:15a30802e719 274 // enable ambi value on demand
ISR 0:15a30802e719 275 SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_AMBI_ON_DEMAND);
ISR 0:15a30802e719 276
ISR 0:15a30802e719 277 // wait on ambi data ready bit
ISR 0:15a30802e719 278 do {
ISR 0:15a30802e719 279 ReadCommandRegister (&Command); // read command register
ISR 0:15a30802e719 280 } while (!(Command & COMMAND_MASK_AMBI_DATA_READY));
ISR 0:15a30802e719 281
ISR 0:15a30802e719 282 ReadAmbiValue (AmbiValue); // read ambi value
ISR 0:15a30802e719 283
ISR 0:15a30802e719 284 SetCommandRegister (COMMAND_ALL_DISABLE); // stop ambi value on demand
ISR 0:15a30802e719 285
ISR 0:15a30802e719 286 return VCNL40x0_ERROR_OK;
ISR 0:15a30802e719 287 }
ISR 0:15a30802e719 288