yarp

Dependencies:   NeoStrip_mod VCNL40x0_1 mbed

Fork of XYZ_PROX_redux_01 by Tom Fleet

Committer:
ghostaudio
Date:
Fri May 30 16:36:49 2014 +0000
Revision:
4:57de0704968d
Parent:
2:9252c8c48272
getting there but issue with sensors

Who changed what in which revision?

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