GETTING CLOSER

Dependencies:   NeoStrip_mod VCNL4020_1 mbed

Committer:
ghostaudio
Date:
Sun Jun 01 19:21:57 2014 +0000
Revision:
1:49431e431e68
Parent:
0:5db0fea04a3d
think this is working, depending on hardware!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ghostaudio 0:5db0fea04a3d 1 #include "mbed.h"
ghostaudio 0:5db0fea04a3d 2 #include "VCNL40x0.h"
ghostaudio 0:5db0fea04a3d 3 #include "NeoStrip.h"
ghostaudio 0:5db0fea04a3d 4 #define NumPixels 4
ghostaudio 0:5db0fea04a3d 5
ghostaudio 0:5db0fea04a3d 6 I2C i2c(p28, p27);
ghostaudio 0:5db0fea04a3d 7 DigitalOut mled0(LED1);
ghostaudio 0:5db0fea04a3d 8 DigitalOut mled1(LED2);
ghostaudio 0:5db0fea04a3d 9 DigitalOut mled2(LED3);
ghostaudio 0:5db0fea04a3d 10 DigitalOut mled3(LED4);
ghostaudio 0:5db0fea04a3d 11 Serial pc(USBTX, USBRX);
ghostaudio 0:5db0fea04a3d 12 DigitalOut reset(p29);
ghostaudio 0:5db0fea04a3d 13 NeoStrip strip(p26, NumPixels);
ghostaudio 0:5db0fea04a3d 14 DigitalIn interrupt (p25);
ghostaudio 0:5db0fea04a3d 15
ghostaudio 0:5db0fea04a3d 16 const uint8_t MUX = 0xE0; // 11100000
ghostaudio 0:5db0fea04a3d 17 const uint8_t VCNL4020 = 0x26;
ghostaudio 0:5db0fea04a3d 18 unsigned int sensors[4];
ghostaudio 0:5db0fea04a3d 19 char _send[3];
ghostaudio 0:5db0fea04a3d 20 char _receive[2];
ghostaudio 0:5db0fea04a3d 21 char port[2];
ghostaudio 0:5db0fea04a3d 22 const uint8_t chan0 = 0x01;
ghostaudio 0:5db0fea04a3d 23 const uint8_t chan1 = 0x02;
ghostaudio 0:5db0fea04a3d 24 const uint8_t chan2 = 0x04;
ghostaudio 0:5db0fea04a3d 25 const uint8_t chan3 = 0x08;
ghostaudio 0:5db0fea04a3d 26 const uint8_t chan[] = {chan0,chan1,chan2,chan3};
ghostaudio 0:5db0fea04a3d 27 int mleds[] = {0,0,0,0};
ghostaudio 0:5db0fea04a3d 28
ghostaudio 0:5db0fea04a3d 29 void Setup(void);
ghostaudio 1:49431e431e68 30 void SetupAllChannels(void);
ghostaudio 0:5db0fea04a3d 31 void Setmleds(void);
ghostaudio 0:5db0fea04a3d 32 void SwitchChannel(uint8_t chan);
ghostaudio 1:49431e431e68 33 void ReadOne(uint8_t channel);
ghostaudio 0:5db0fea04a3d 34 void ReadAllSensors(void);
ghostaudio 0:5db0fea04a3d 35 void Setmleds(void);
ghostaudio 0:5db0fea04a3d 36 void SetProximityRate (unsigned char ProximityRate);
ghostaudio 0:5db0fea04a3d 37 void SetCommandRegister (unsigned char Command);
ghostaudio 0:5db0fea04a3d 38 void ReadCommandRegister (unsigned char *Command);
ghostaudio 0:5db0fea04a3d 39 void SetCurrent (unsigned char Current);
ghostaudio 0:5db0fea04a3d 40 void ReadCurrent (unsigned char *Current);
ghostaudio 0:5db0fea04a3d 41 void ReadProxiValue (unsigned int *ProxiValue);
ghostaudio 0:5db0fea04a3d 42 void SetProximityRate (unsigned char ProximityRate);
ghostaudio 1:49431e431e68 43 void SetInterruptControl (unsigned char InterruptControl);
ghostaudio 1:49431e431e68 44 void ReadInterruptControl (unsigned char *InterruptControl);
ghostaudio 0:5db0fea04a3d 45 void ReadProxiOnDemand (unsigned int *ProxiValue);
ghostaudio 1:49431e431e68 46
ghostaudio 0:5db0fea04a3d 47
ghostaudio 0:5db0fea04a3d 48 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 49
ghostaudio 0:5db0fea04a3d 50 int main() {
ghostaudio 1:49431e431e68 51 wait_ms(5);
ghostaudio 0:5db0fea04a3d 52 //SwitchChannel(chan1); // set channel
ghostaudio 0:5db0fea04a3d 53 //wait_ms(50);
ghostaudio 0:5db0fea04a3d 54 //Setup();
ghostaudio 1:49431e431e68 55 SetupAllChannels();
ghostaudio 0:5db0fea04a3d 56
ghostaudio 0:5db0fea04a3d 57 while (1) {
ghostaudio 0:5db0fea04a3d 58
ghostaudio 0:5db0fea04a3d 59 ReadAllSensors();
ghostaudio 0:5db0fea04a3d 60 //ReadOne(chan1);
ghostaudio 0:5db0fea04a3d 61 }
ghostaudio 0:5db0fea04a3d 62 }
ghostaudio 0:5db0fea04a3d 63
ghostaudio 1:49431e431e68 64 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 1:49431e431e68 65
ghostaudio 1:49431e431e68 66 // setup for VCNL4020.
ghostaudio 1:49431e431e68 67
ghostaudio 1:49431e431e68 68 void Setup(void) {
ghostaudio 1:49431e431e68 69 unsigned char Current=0;
ghostaudio 1:49431e431e68 70
ghostaudio 0:5db0fea04a3d 71
ghostaudio 1:49431e431e68 72
ghostaudio 1:49431e431e68 73 SetCurrent(20); // Set current to 200mA
ghostaudio 1:49431e431e68 74 ReadCurrent(&Current);
ghostaudio 1:49431e431e68 75 pc.printf("\n IR LED Current: %d\n", Current);
ghostaudio 1:49431e431e68 76 SetCommandRegister (COMMAND_ALL_DISABLE); // ready for prox rate change
ghostaudio 1:49431e431e68 77 SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s
ghostaudio 1:49431e431e68 78 SetInterruptControl (INTERRUPT_THRES_SEL_PROX |INTERRUPT_THRES_ENABLE |INTERRUPT_COUNT_EXCEED_1);
ghostaudio 1:49431e431e68 79 SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_SELFTIMED_MODE_ENABLE);
ghostaudio 1:49431e431e68 80 pc.printf("\r done\n");
ghostaudio 1:49431e431e68 81 wait(0.2);
ghostaudio 0:5db0fea04a3d 82 }
ghostaudio 1:49431e431e68 83
ghostaudio 0:5db0fea04a3d 84 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 1:49431e431e68 85
ghostaudio 1:49431e431e68 86 // Setup all channels sequentially.
ghostaudio 1:49431e431e68 87
ghostaudio 1:49431e431e68 88 void SetupAllChannels(void){
ghostaudio 0:5db0fea04a3d 89
ghostaudio 1:49431e431e68 90
ghostaudio 1:49431e431e68 91 pc.baud(115200);
ghostaudio 1:49431e431e68 92 i2c.frequency(1000000);
ghostaudio 0:5db0fea04a3d 93 SwitchChannel(chan0); // set channel
ghostaudio 0:5db0fea04a3d 94 wait_ms(20);
ghostaudio 0:5db0fea04a3d 95 Setup();
ghostaudio 0:5db0fea04a3d 96 SwitchChannel(chan1); // set channel
ghostaudio 0:5db0fea04a3d 97 wait_ms(20);
ghostaudio 0:5db0fea04a3d 98 Setup();
ghostaudio 0:5db0fea04a3d 99 SwitchChannel(chan2); // set channel
ghostaudio 0:5db0fea04a3d 100 wait_ms(20);
ghostaudio 0:5db0fea04a3d 101 Setup();
ghostaudio 0:5db0fea04a3d 102 SwitchChannel(chan3); // set channel
ghostaudio 0:5db0fea04a3d 103 wait_ms(20);
ghostaudio 0:5db0fea04a3d 104 Setup();
ghostaudio 0:5db0fea04a3d 105 pc.printf("\r setup ok \n ");
ghostaudio 0:5db0fea04a3d 106 }
ghostaudio 0:5db0fea04a3d 107
ghostaudio 1:49431e431e68 108 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 109
ghostaudio 1:49431e431e68 110 // mbed leds for some visual recognition.
ghostaudio 0:5db0fea04a3d 111
ghostaudio 0:5db0fea04a3d 112 void Setmleds(void){
ghostaudio 0:5db0fea04a3d 113 mled0 = mleds[0];
ghostaudio 0:5db0fea04a3d 114 mled1 = mleds[1];
ghostaudio 0:5db0fea04a3d 115 mled2 = mleds[2];
ghostaudio 0:5db0fea04a3d 116 mled3 = mleds[3];
ghostaudio 0:5db0fea04a3d 117 }
ghostaudio 0:5db0fea04a3d 118
ghostaudio 0:5db0fea04a3d 119 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 120
ghostaudio 1:49431e431e68 121 // reset MUX and choose channel.
ghostaudio 1:49431e431e68 122
ghostaudio 0:5db0fea04a3d 123 void SwitchChannel(uint8_t chan){
ghostaudio 1:49431e431e68 124 //i2c.start();
ghostaudio 0:5db0fea04a3d 125 reset = 0;
ghostaudio 0:5db0fea04a3d 126 wait_ms(10);
ghostaudio 0:5db0fea04a3d 127 reset = 1;
ghostaudio 0:5db0fea04a3d 128 wait_ms(10);
ghostaudio 0:5db0fea04a3d 129 port[0] = chan;
ghostaudio 0:5db0fea04a3d 130 i2c.write(MUX,port, 1);
ghostaudio 1:49431e431e68 131 //i2c.stop();
ghostaudio 0:5db0fea04a3d 132 }
ghostaudio 0:5db0fea04a3d 133
ghostaudio 0:5db0fea04a3d 134 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 135
ghostaudio 1:49431e431e68 136 // Choose on channel and read proximity from that channel.
ghostaudio 1:49431e431e68 137
ghostaudio 1:49431e431e68 138 void ReadOne(uint8_t channel){
ghostaudio 1:49431e431e68 139
ghostaudio 1:49431e431e68 140 unsigned int sensor = 0;
ghostaudio 1:49431e431e68 141 SwitchChannel(channel); // set channel
ghostaudio 1:49431e431e68 142 i2c.start();
ghostaudio 1:49431e431e68 143 mleds[0] = 1; // LED on
ghostaudio 1:49431e431e68 144 Setmleds();
ghostaudio 1:49431e431e68 145 ReadProxiOnDemand(&sensor); // read prox value on demand
ghostaudio 1:49431e431e68 146 mleds[0] = 0; // LED off
ghostaudio 1:49431e431e68 147 Setmleds();
ghostaudio 1:49431e431e68 148 pc.printf("\r %i \n", sensor);
ghostaudio 1:49431e431e68 149 i2c.stop();
ghostaudio 1:49431e431e68 150 }
ghostaudio 1:49431e431e68 151
ghostaudio 1:49431e431e68 152 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 1:49431e431e68 153
ghostaudio 1:49431e431e68 154 // Read all sensors sequentially
ghostaudio 1:49431e431e68 155
ghostaudio 0:5db0fea04a3d 156 void ReadAllSensors(void){
ghostaudio 0:5db0fea04a3d 157
ghostaudio 0:5db0fea04a3d 158 for (int i = 0; i < 4; i++){
ghostaudio 0:5db0fea04a3d 159
ghostaudio 0:5db0fea04a3d 160 unsigned int sensor = 0;
ghostaudio 0:5db0fea04a3d 161
ghostaudio 0:5db0fea04a3d 162 SwitchChannel(chan[i]); // set channel
ghostaudio 1:49431e431e68 163 //i2c.start();
ghostaudio 0:5db0fea04a3d 164 mleds[i] = 1; // LED on
ghostaudio 0:5db0fea04a3d 165 Setmleds();
ghostaudio 1:49431e431e68 166 //ReadProxiValue(&sensor);
ghostaudio 1:49431e431e68 167 ReadProxiOnDemand(&sensor); // read prox value on demand
ghostaudio 0:5db0fea04a3d 168 wait_ms(10);
ghostaudio 0:5db0fea04a3d 169 mleds[i] = 0; // LED off
ghostaudio 0:5db0fea04a3d 170 Setmleds();
ghostaudio 0:5db0fea04a3d 171 sensors[i] = sensor;
ghostaudio 0:5db0fea04a3d 172 pc.printf("\r %i ", sensors[i]);
ghostaudio 1:49431e431e68 173 //i2c.stop();
ghostaudio 0:5db0fea04a3d 174 }
ghostaudio 0:5db0fea04a3d 175
ghostaudio 0:5db0fea04a3d 176 pc.printf("\r \n ");
ghostaudio 0:5db0fea04a3d 177 wait(0.001);
ghostaudio 0:5db0fea04a3d 178 }
ghostaudio 0:5db0fea04a3d 179
ghostaudio 0:5db0fea04a3d 180 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 181
ghostaudio 0:5db0fea04a3d 182 void SetCommandRegister (unsigned char Command) {
ghostaudio 0:5db0fea04a3d 183
ghostaudio 1:49431e431e68 184 _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration reister
ghostaudio 0:5db0fea04a3d 185 _send[1] = Command;
ghostaudio 1:49431e431e68 186 i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C
ghostaudio 0:5db0fea04a3d 187 }
ghostaudio 0:5db0fea04a3d 188
ghostaudio 0:5db0fea04a3d 189 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 190
ghostaudio 0:5db0fea04a3d 191 void ReadCommandRegister (unsigned char *Command) {
ghostaudio 0:5db0fea04a3d 192
ghostaudio 1:49431e431e68 193 _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register
ghostaudio 1:49431e431e68 194 i2c.write(VCNL4020,_send, 1); // Write 1 byte on I2C
ghostaudio 1:49431e431e68 195 i2c.read(VCNL4020+1,_receive, 1); // Read 1 byte on I2C
ghostaudio 0:5db0fea04a3d 196
ghostaudio 0:5db0fea04a3d 197 *Command = (unsigned char)(_receive[0]);
ghostaudio 0:5db0fea04a3d 198 }
ghostaudio 0:5db0fea04a3d 199
ghostaudio 0:5db0fea04a3d 200 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 201
ghostaudio 0:5db0fea04a3d 202 void SetCurrent (unsigned char Current) {
ghostaudio 0:5db0fea04a3d 203
ghostaudio 0:5db0fea04a3d 204 _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register
ghostaudio 0:5db0fea04a3d 205 _send[1] = Current;
ghostaudio 0:5db0fea04a3d 206 i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C
ghostaudio 0:5db0fea04a3d 207
ghostaudio 0:5db0fea04a3d 208 }
ghostaudio 0:5db0fea04a3d 209
ghostaudio 0:5db0fea04a3d 210 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 211
ghostaudio 0:5db0fea04a3d 212 void ReadCurrent (unsigned char *Current) {
ghostaudio 0:5db0fea04a3d 213
ghostaudio 0:5db0fea04a3d 214 _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED current register
ghostaudio 0:5db0fea04a3d 215 i2c.write(VCNL4020,_send, 1); // Write 1 byte on I2C
ghostaudio 0:5db0fea04a3d 216 i2c.read(VCNL4020+1,_receive, 1); // Read 1 byte on I2C
ghostaudio 0:5db0fea04a3d 217
ghostaudio 0:5db0fea04a3d 218 *Current = (unsigned char)(_receive[0]);
ghostaudio 0:5db0fea04a3d 219 }
ghostaudio 0:5db0fea04a3d 220
ghostaudio 0:5db0fea04a3d 221 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 222
ghostaudio 0:5db0fea04a3d 223 void ReadProxiValue (unsigned int *ProxiValue) {
ghostaudio 0:5db0fea04a3d 224
ghostaudio 0:5db0fea04a3d 225 _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register
ghostaudio 0:5db0fea04a3d 226 i2c.write(VCNL4020, _send, 1); // Write 1 byte on I2C
ghostaudio 0:5db0fea04a3d 227 i2c.read(VCNL4020+1, _receive, 2); // Read 2 bytes on I2C
ghostaudio 0:5db0fea04a3d 228 *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
ghostaudio 0:5db0fea04a3d 229 }
ghostaudio 0:5db0fea04a3d 230
ghostaudio 0:5db0fea04a3d 231 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 232
ghostaudio 0:5db0fea04a3d 233 void SetProximityRate (unsigned char ProximityRate) {
ghostaudio 0:5db0fea04a3d 234
ghostaudio 0:5db0fea04a3d 235 _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register
ghostaudio 0:5db0fea04a3d 236 _send[1] = ProximityRate;
ghostaudio 0:5db0fea04a3d 237 i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C
ghostaudio 0:5db0fea04a3d 238 }
ghostaudio 0:5db0fea04a3d 239
ghostaudio 0:5db0fea04a3d 240 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 0:5db0fea04a3d 241
ghostaudio 1:49431e431e68 242 void SetInterruptControl (unsigned char InterruptControl) {
ghostaudio 1:49431e431e68 243
ghostaudio 1:49431e431e68 244 _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
ghostaudio 1:49431e431e68 245 _send[1] = InterruptControl;
ghostaudio 1:49431e431e68 246 i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C
ghostaudio 1:49431e431e68 247
ghostaudio 1:49431e431e68 248 }
ghostaudio 1:49431e431e68 249
ghostaudio 1:49431e431e68 250 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 1:49431e431e68 251
ghostaudio 1:49431e431e68 252 void ReadInterruptControl (unsigned char *InterruptControl) {
ghostaudio 1:49431e431e68 253
ghostaudio 1:49431e431e68 254 _send[0] = REGISTER_INTERRUPT_CONTROL; // VCNL40x0 Interrupt Control register
ghostaudio 1:49431e431e68 255 i2c.write(VCNL4020,_send, 1); // Write 1 byte on I2C
ghostaudio 1:49431e431e68 256 i2c.read(VCNL4020+1,_receive, 1); // Read 1 byte on I2C
ghostaudio 1:49431e431e68 257
ghostaudio 1:49431e431e68 258 *InterruptControl = (unsigned char)(_receive[0]);
ghostaudio 1:49431e431e68 259 }
ghostaudio 1:49431e431e68 260
ghostaudio 1:49431e431e68 261 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ghostaudio 1:49431e431e68 262
ghostaudio 0:5db0fea04a3d 263 void ReadProxiOnDemand (unsigned int *ProxiValue) {
ghostaudio 0:5db0fea04a3d 264
ghostaudio 0:5db0fea04a3d 265 unsigned char Command=0;
ghostaudio 1:49431e431e68 266
ghostaudio 1:49431e431e68 267 // SetCommandRegister (COMMAND_ALL_DISABLE);
ghostaudio 1:49431e431e68 268
ghostaudio 0:5db0fea04a3d 269 SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
ghostaudio 0:5db0fea04a3d 270
ghostaudio 0:5db0fea04a3d 271 // wait on prox data ready bit
ghostaudio 0:5db0fea04a3d 272 do {
ghostaudio 0:5db0fea04a3d 273 ReadCommandRegister (&Command); // read command register
ghostaudio 0:5db0fea04a3d 274 } while (!(Command & COMMAND_MASK_PROX_DATA_READY));
ghostaudio 0:5db0fea04a3d 275
ghostaudio 0:5db0fea04a3d 276 ReadProxiValue (ProxiValue); // read prox value
ghostaudio 0:5db0fea04a3d 277
ghostaudio 0:5db0fea04a3d 278 SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand
ghostaudio 0:5db0fea04a3d 279
ghostaudio 0:5db0fea04a3d 280 }