This program simulates a dual dice throw by pushing the 2 buttons on the QW dev kit. The results of the throw are shown in binary on the leds, printed in the console window and transmitted via Sigfox.
Fork of QW-BinaryDices by
QW Binary Dices
This program simulates a dual dice throw by pushing the 2 buttons on the QW dev kit. The results of the throw are shown in binary on the leds, printed in the console window and transmitted via Sigfox.
Code explanation
The program starts with the initialisation/declaration of the leds and pushbuttons. Also the necessary function prototypes and serial communications are declared. After that, the program enters an infinite loop and waits till both buttons have been pushed. Once both buttons have been pushed, the result is shown in binary on the leds (LED_3 = LSB), printed in the console window and transmitted via Sigfox. When the Sigfox message is transmitted, the program is ready to throw the dices again.
Sigfox message payload
First there is the "04", this is the Quicksand ID of the example program. This is used by Quicksand to keep track of our example programs. The second value that is transmitted is the number that was thrown by the dices.
More information and other example code can be found on the component page by clicking the link below: https://developer.mbed.org/components/QW-SIGFOX-Development-Kit/
VCNL4010.cpp@0:6c17d1a79f75, 2015-11-05 (annotated)
- 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?
User | Revision | Line number | New 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.21 |
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 | #include "VCNL4010.h" |
quicksand | 0:6c17d1a79f75 | 25 | |
quicksand | 0:6c17d1a79f75 | 26 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 27 | |
quicksand | 0:6c17d1a79f75 | 28 | VCNL40x0::VCNL40x0(PinName sda, PinName scl, unsigned char addr) : _i2c(sda, scl), _addr(addr) { |
quicksand | 0:6c17d1a79f75 | 29 | // _i2c.frequency(1000000); // set I2C frequency to 1MHz |
quicksand | 0:6c17d1a79f75 | 30 | } |
quicksand | 0:6c17d1a79f75 | 31 | |
quicksand | 0:6c17d1a79f75 | 32 | VCNL40x0::~VCNL40x0() { |
quicksand | 0:6c17d1a79f75 | 33 | } |
quicksand | 0:6c17d1a79f75 | 34 | |
quicksand | 0:6c17d1a79f75 | 35 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 36 | |
quicksand | 0:6c17d1a79f75 | 37 | VCNL40x0Error_e VCNL40x0::SetCommandRegister (unsigned char Command) { |
quicksand | 0:6c17d1a79f75 | 38 | |
quicksand | 0:6c17d1a79f75 | 39 | _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration reister |
quicksand | 0:6c17d1a79f75 | 40 | _send[1] = Command; |
quicksand | 0:6c17d1a79f75 | 41 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 42 | |
quicksand | 0:6c17d1a79f75 | 43 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 44 | } |
quicksand | 0:6c17d1a79f75 | 45 | |
quicksand | 0:6c17d1a79f75 | 46 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 47 | |
quicksand | 0:6c17d1a79f75 | 48 | VCNL40x0Error_e VCNL40x0::ReadCommandRegister (unsigned char *Command) { |
quicksand | 0:6c17d1a79f75 | 49 | |
quicksand | 0:6c17d1a79f75 | 50 | _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register |
quicksand | 0:6c17d1a79f75 | 51 | _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 52 | _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 53 | |
quicksand | 0:6c17d1a79f75 | 54 | *Command = (unsigned char)(_receive[0]); |
quicksand | 0:6c17d1a79f75 | 55 | |
quicksand | 0:6c17d1a79f75 | 56 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 57 | } |
quicksand | 0:6c17d1a79f75 | 58 | |
quicksand | 0:6c17d1a79f75 | 59 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 60 | |
quicksand | 0:6c17d1a79f75 | 61 | VCNL40x0Error_e VCNL40x0::ReadID (unsigned char *ID) { |
quicksand | 0:6c17d1a79f75 | 62 | |
quicksand | 0:6c17d1a79f75 | 63 | _send[0] = REGISTER_ID; // VCNL40x0 product ID revision register |
quicksand | 0:6c17d1a79f75 | 64 | _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 65 | _i2c.read(VCNL40x0_ADDRESS+1, _receive, 1); // Read 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 66 | |
quicksand | 0:6c17d1a79f75 | 67 | *ID = (unsigned char)(_receive[0]); |
quicksand | 0:6c17d1a79f75 | 68 | |
quicksand | 0:6c17d1a79f75 | 69 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 70 | } |
quicksand | 0:6c17d1a79f75 | 71 | |
quicksand | 0:6c17d1a79f75 | 72 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 73 | |
quicksand | 0:6c17d1a79f75 | 74 | VCNL40x0Error_e VCNL40x0::SetCurrent (unsigned char Current) { |
quicksand | 0:6c17d1a79f75 | 75 | |
quicksand | 0:6c17d1a79f75 | 76 | _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register |
quicksand | 0:6c17d1a79f75 | 77 | _send[1] = Current; |
quicksand | 0:6c17d1a79f75 | 78 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 79 | |
quicksand | 0:6c17d1a79f75 | 80 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 81 | } |
quicksand | 0:6c17d1a79f75 | 82 | |
quicksand | 0:6c17d1a79f75 | 83 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 84 | |
quicksand | 0:6c17d1a79f75 | 85 | VCNL40x0Error_e VCNL40x0::ReadCurrent (unsigned char *Current) { |
quicksand | 0:6c17d1a79f75 | 86 | |
quicksand | 0:6c17d1a79f75 | 87 | _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED current register |
quicksand | 0:6c17d1a79f75 | 88 | _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 89 | _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 90 | |
quicksand | 0:6c17d1a79f75 | 91 | *Current = (unsigned char)(_receive[0]); |
quicksand | 0:6c17d1a79f75 | 92 | |
quicksand | 0:6c17d1a79f75 | 93 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 94 | } |
quicksand | 0:6c17d1a79f75 | 95 | |
quicksand | 0:6c17d1a79f75 | 96 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 97 | |
quicksand | 0:6c17d1a79f75 | 98 | VCNL40x0Error_e VCNL40x0::SetProximityRate (unsigned char ProximityRate) { |
quicksand | 0:6c17d1a79f75 | 99 | |
quicksand | 0:6c17d1a79f75 | 100 | _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register |
quicksand | 0:6c17d1a79f75 | 101 | _send[1] = ProximityRate; |
quicksand | 0:6c17d1a79f75 | 102 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 103 | |
quicksand | 0:6c17d1a79f75 | 104 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 105 | } |
quicksand | 0:6c17d1a79f75 | 106 | |
quicksand | 0:6c17d1a79f75 | 107 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 108 | |
quicksand | 0:6c17d1a79f75 | 109 | VCNL40x0Error_e VCNL40x0::SetAmbiConfiguration (unsigned char AmbiConfiguration) { |
quicksand | 0:6c17d1a79f75 | 110 | |
quicksand | 0:6c17d1a79f75 | 111 | _send[0] = REGISTER_AMBI_PARAMETER; // VCNL40x0 Ambilight configuration |
quicksand | 0:6c17d1a79f75 | 112 | _send[1] = AmbiConfiguration; |
quicksand | 0:6c17d1a79f75 | 113 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 114 | |
quicksand | 0:6c17d1a79f75 | 115 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 116 | } |
quicksand | 0:6c17d1a79f75 | 117 | |
quicksand | 0:6c17d1a79f75 | 118 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 119 | |
quicksand | 0:6c17d1a79f75 | 120 | VCNL40x0Error_e VCNL40x0::SetInterruptControl (unsigned char InterruptControl) { |
quicksand | 0:6c17d1a79f75 | 121 | |
quicksand | 0:6c17d1a79f75 | 122 | _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register |
quicksand | 0:6c17d1a79f75 | 123 | _send[1] = InterruptControl; |
quicksand | 0:6c17d1a79f75 | 124 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 125 | |
quicksand | 0:6c17d1a79f75 | 126 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 127 | } |
quicksand | 0:6c17d1a79f75 | 128 | |
quicksand | 0:6c17d1a79f75 | 129 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 130 | |
quicksand | 0:6c17d1a79f75 | 131 | VCNL40x0Error_e VCNL40x0::ReadInterruptControl (unsigned char *InterruptControl) { |
quicksand | 0:6c17d1a79f75 | 132 | |
quicksand | 0:6c17d1a79f75 | 133 | _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register |
quicksand | 0:6c17d1a79f75 | 134 | _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 135 | _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 136 | |
quicksand | 0:6c17d1a79f75 | 137 | *InterruptControl = (unsigned char)(_receive[0]); |
quicksand | 0:6c17d1a79f75 | 138 | |
quicksand | 0:6c17d1a79f75 | 139 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 140 | } |
quicksand | 0:6c17d1a79f75 | 141 | |
quicksand | 0:6c17d1a79f75 | 142 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 143 | |
quicksand | 0:6c17d1a79f75 | 144 | VCNL40x0Error_e VCNL40x0::SetInterruptStatus (unsigned char InterruptStatus) { |
quicksand | 0:6c17d1a79f75 | 145 | |
quicksand | 0:6c17d1a79f75 | 146 | _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register |
quicksand | 0:6c17d1a79f75 | 147 | _send[1] = InterruptStatus; |
quicksand | 0:6c17d1a79f75 | 148 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 149 | |
quicksand | 0:6c17d1a79f75 | 150 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 151 | } |
quicksand | 0:6c17d1a79f75 | 152 | |
quicksand | 0:6c17d1a79f75 | 153 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 154 | |
quicksand | 0:6c17d1a79f75 | 155 | VCNL40x0Error_e VCNL40x0::SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment) { |
quicksand | 0:6c17d1a79f75 | 156 | |
quicksand | 0:6c17d1a79f75 | 157 | _send[0] = REGISTER_PROX_TIMING; // VCNL40x0 Modulator Timing Adjustment register |
quicksand | 0:6c17d1a79f75 | 158 | _send[1] = ModulatorTimingAdjustment; |
quicksand | 0:6c17d1a79f75 | 159 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 160 | |
quicksand | 0:6c17d1a79f75 | 161 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 162 | } |
quicksand | 0:6c17d1a79f75 | 163 | |
quicksand | 0:6c17d1a79f75 | 164 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 165 | |
quicksand | 0:6c17d1a79f75 | 166 | VCNL40x0Error_e VCNL40x0::ReadInterruptStatus (unsigned char *InterruptStatus) { |
quicksand | 0:6c17d1a79f75 | 167 | |
quicksand | 0:6c17d1a79f75 | 168 | _send[0] = REGISTER_INTERRUPT_STATUS; // VCNL40x0 Interrupt Status register |
quicksand | 0:6c17d1a79f75 | 169 | _i2c.write(VCNL40x0_ADDRESS,_send, 1); // Write 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 170 | _i2c.read(VCNL40x0_ADDRESS+1,_receive, 1); // Read 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 171 | |
quicksand | 0:6c17d1a79f75 | 172 | *InterruptStatus = (unsigned char)(_receive[0]); |
quicksand | 0:6c17d1a79f75 | 173 | |
quicksand | 0:6c17d1a79f75 | 174 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 175 | } |
quicksand | 0:6c17d1a79f75 | 176 | |
quicksand | 0:6c17d1a79f75 | 177 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 178 | |
quicksand | 0:6c17d1a79f75 | 179 | VCNL40x0Error_e VCNL40x0::ReadProxiValue (unsigned int *ProxiValue) { |
quicksand | 0:6c17d1a79f75 | 180 | |
quicksand | 0:6c17d1a79f75 | 181 | _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register |
quicksand | 0:6c17d1a79f75 | 182 | _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 183 | _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 184 | *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]); |
quicksand | 0:6c17d1a79f75 | 185 | |
quicksand | 0:6c17d1a79f75 | 186 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 187 | |
quicksand | 0:6c17d1a79f75 | 188 | } |
quicksand | 0:6c17d1a79f75 | 189 | |
quicksand | 0:6c17d1a79f75 | 190 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 191 | |
quicksand | 0:6c17d1a79f75 | 192 | VCNL40x0Error_e VCNL40x0::ReadAmbiValue (unsigned int *AmbiValue) { |
quicksand | 0:6c17d1a79f75 | 193 | |
quicksand | 0:6c17d1a79f75 | 194 | _send[0] = REGISTER_AMBI_VALUE; // VCNL40x0 Ambient Light Value register |
quicksand | 0:6c17d1a79f75 | 195 | _i2c.write(VCNL40x0_ADDRESS, _send, 1); // Write 1 byte on I2C |
quicksand | 0:6c17d1a79f75 | 196 | _i2c.read(VCNL40x0_ADDRESS+1, _receive, 2); // Read 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 197 | *AmbiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]); |
quicksand | 0:6c17d1a79f75 | 198 | |
quicksand | 0:6c17d1a79f75 | 199 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 200 | |
quicksand | 0:6c17d1a79f75 | 201 | } |
quicksand | 0:6c17d1a79f75 | 202 | |
quicksand | 0:6c17d1a79f75 | 203 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 204 | |
quicksand | 0:6c17d1a79f75 | 205 | VCNL40x0Error_e VCNL40x0::SetLowThreshold (unsigned int LowThreshold) { |
quicksand | 0:6c17d1a79f75 | 206 | |
quicksand | 0:6c17d1a79f75 | 207 | unsigned char LoByte=0, HiByte=0; |
quicksand | 0:6c17d1a79f75 | 208 | |
quicksand | 0:6c17d1a79f75 | 209 | LoByte = (unsigned char)(LowThreshold & 0x00ff); |
quicksand | 0:6c17d1a79f75 | 210 | HiByte = (unsigned char)((LowThreshold & 0xff00)>>8); |
quicksand | 0:6c17d1a79f75 | 211 | |
quicksand | 0:6c17d1a79f75 | 212 | _send[0] = REGISTER_INTERRUPT_LOW_THRES; // VCNL40x0 Low Threshold Register, Hi Byte |
quicksand | 0:6c17d1a79f75 | 213 | _send[1] = HiByte; |
quicksand | 0:6c17d1a79f75 | 214 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 215 | |
quicksand | 0:6c17d1a79f75 | 216 | _send[0] = REGISTER_INTERRUPT_LOW_THRES+1; // VCNL40x0 Low Threshold Register, Lo Byte |
quicksand | 0:6c17d1a79f75 | 217 | _send[1] = LoByte; |
quicksand | 0:6c17d1a79f75 | 218 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 219 | |
quicksand | 0:6c17d1a79f75 | 220 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 221 | |
quicksand | 0:6c17d1a79f75 | 222 | } |
quicksand | 0:6c17d1a79f75 | 223 | |
quicksand | 0:6c17d1a79f75 | 224 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 225 | |
quicksand | 0:6c17d1a79f75 | 226 | VCNL40x0Error_e VCNL40x0::SetHighThreshold (unsigned int HighThreshold) { |
quicksand | 0:6c17d1a79f75 | 227 | |
quicksand | 0:6c17d1a79f75 | 228 | unsigned char LoByte=0, HiByte=0; |
quicksand | 0:6c17d1a79f75 | 229 | |
quicksand | 0:6c17d1a79f75 | 230 | LoByte = (unsigned char)(HighThreshold & 0x00ff); |
quicksand | 0:6c17d1a79f75 | 231 | HiByte = (unsigned char)((HighThreshold & 0xff00)>>8); |
quicksand | 0:6c17d1a79f75 | 232 | |
quicksand | 0:6c17d1a79f75 | 233 | _send[0] = REGISTER_INTERRUPT_HIGH_THRES; // VCNL40x0 High Threshold Register, Hi Byte |
quicksand | 0:6c17d1a79f75 | 234 | _send[1] = HiByte; |
quicksand | 0:6c17d1a79f75 | 235 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 236 | |
quicksand | 0:6c17d1a79f75 | 237 | _send[0] = REGISTER_INTERRUPT_HIGH_THRES+1; // VCNL40x0 High Threshold Register, Lo Byte |
quicksand | 0:6c17d1a79f75 | 238 | _send[1] = LoByte; |
quicksand | 0:6c17d1a79f75 | 239 | _i2c.write(VCNL40x0_ADDRESS,_send, 2); // Write 2 bytes on I2C |
quicksand | 0:6c17d1a79f75 | 240 | |
quicksand | 0:6c17d1a79f75 | 241 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 242 | |
quicksand | 0:6c17d1a79f75 | 243 | } |
quicksand | 0:6c17d1a79f75 | 244 | |
quicksand | 0:6c17d1a79f75 | 245 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 246 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 247 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 248 | |
quicksand | 0:6c17d1a79f75 | 249 | VCNL40x0Error_e VCNL40x0::ReadProxiOnDemand (unsigned int *ProxiValue) { |
quicksand | 0:6c17d1a79f75 | 250 | |
quicksand | 0:6c17d1a79f75 | 251 | unsigned char Command=0; |
quicksand | 0:6c17d1a79f75 | 252 | |
quicksand | 0:6c17d1a79f75 | 253 | // enable prox value on demand |
quicksand | 0:6c17d1a79f75 | 254 | SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND); |
quicksand | 0:6c17d1a79f75 | 255 | |
quicksand | 0:6c17d1a79f75 | 256 | // wait on prox data ready bit |
quicksand | 0:6c17d1a79f75 | 257 | do { |
quicksand | 0:6c17d1a79f75 | 258 | ReadCommandRegister (&Command); // read command register |
quicksand | 0:6c17d1a79f75 | 259 | } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); |
quicksand | 0:6c17d1a79f75 | 260 | |
quicksand | 0:6c17d1a79f75 | 261 | ReadProxiValue (ProxiValue); // read prox value |
quicksand | 0:6c17d1a79f75 | 262 | |
quicksand | 0:6c17d1a79f75 | 263 | SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand |
quicksand | 0:6c17d1a79f75 | 264 | |
quicksand | 0:6c17d1a79f75 | 265 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 266 | } |
quicksand | 0:6c17d1a79f75 | 267 | |
quicksand | 0:6c17d1a79f75 | 268 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
quicksand | 0:6c17d1a79f75 | 269 | |
quicksand | 0:6c17d1a79f75 | 270 | VCNL40x0Error_e VCNL40x0::ReadAmbiOnDemand (unsigned int *AmbiValue) { |
quicksand | 0:6c17d1a79f75 | 271 | |
quicksand | 0:6c17d1a79f75 | 272 | unsigned char Command=0; |
quicksand | 0:6c17d1a79f75 | 273 | |
quicksand | 0:6c17d1a79f75 | 274 | // enable ambi value on demand |
quicksand | 0:6c17d1a79f75 | 275 | SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_AMBI_ON_DEMAND); |
quicksand | 0:6c17d1a79f75 | 276 | |
quicksand | 0:6c17d1a79f75 | 277 | // wait on ambi data ready bit |
quicksand | 0:6c17d1a79f75 | 278 | do { |
quicksand | 0:6c17d1a79f75 | 279 | ReadCommandRegister (&Command); // read command register |
quicksand | 0:6c17d1a79f75 | 280 | } while (!(Command & COMMAND_MASK_AMBI_DATA_READY)); |
quicksand | 0:6c17d1a79f75 | 281 | |
quicksand | 0:6c17d1a79f75 | 282 | ReadAmbiValue (AmbiValue); // read ambi value |
quicksand | 0:6c17d1a79f75 | 283 | |
quicksand | 0:6c17d1a79f75 | 284 | SetCommandRegister (COMMAND_ALL_DISABLE); // stop ambi value on demand |
quicksand | 0:6c17d1a79f75 | 285 | |
quicksand | 0:6c17d1a79f75 | 286 | return VCNL40x0_ERROR_OK; |
quicksand | 0:6c17d1a79f75 | 287 | } |