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