Ken Parish
/
XYZ_PROX_01_1
compiling aside from neopixel lib?
main.cpp@2:e97394fe87c4, 2014-05-30 (annotated)
- Committer:
- ghostaudio
- Date:
- Fri May 30 12:46:47 2014 +0000
- Revision:
- 2:e97394fe87c4
- Parent:
- 0:09dd83d78a0a
again...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ghostaudio | 0:09dd83d78a0a | 1 | #include "mbed.h" |
ghostaudio | 0:09dd83d78a0a | 2 | #include "VCNL40x0.h" |
ghostaudio | 2:e97394fe87c4 | 3 | //#include "NeoStrip.h" |
ghostaudio | 2:e97394fe87c4 | 4 | //#define NumPixels 4 |
ghostaudio | 0:09dd83d78a0a | 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 | 2:e97394fe87c4 | 13 | //NeoStrip strip(p26, NumPixels); |
ghostaudio | 0:09dd83d78a0a | 14 | |
ghostaudio | 0:09dd83d78a0a | 15 | const uint8_t MUX = 0xE0; // 11100000 |
ghostaudio | 0:09dd83d78a0a | 16 | const uint8_t VCNL4020 = 0x26; |
ghostaudio | 0:09dd83d78a0a | 17 | unsigned int sensors[4]; |
ghostaudio | 0:09dd83d78a0a | 18 | char _send[3]; |
ghostaudio | 0:09dd83d78a0a | 19 | char _receive[2]; |
ghostaudio | 0:09dd83d78a0a | 20 | char port[2]; |
ghostaudio | 0:09dd83d78a0a | 21 | const uint8_t chan[] = {0x01,0x02,0x04,0x08}; |
ghostaudio | 0:09dd83d78a0a | 22 | int mleds[] = {0,0,0,0}; |
ghostaudio | 0:09dd83d78a0a | 23 | |
ghostaudio | 0:09dd83d78a0a | 24 | void Setup(void); |
ghostaudio | 0:09dd83d78a0a | 25 | void Setmleds(void); |
ghostaudio | 0:09dd83d78a0a | 26 | void SwitchChannel(uint8_t chan); |
ghostaudio | 0:09dd83d78a0a | 27 | void ReadAllSensors(void); |
ghostaudio | 0:09dd83d78a0a | 28 | void Setmleds(void); |
ghostaudio | 0:09dd83d78a0a | 29 | void SetProximityRate (unsigned char ProximityRate); |
ghostaudio | 0:09dd83d78a0a | 30 | void SetCommandRegister (unsigned char Command); |
ghostaudio | 0:09dd83d78a0a | 31 | void ReadCommandRegister (unsigned char *Command); |
ghostaudio | 0:09dd83d78a0a | 32 | void SetCurrent (unsigned char Current); |
ghostaudio | 0:09dd83d78a0a | 33 | void ReadProxiValue (unsigned int *ProxiValue); |
ghostaudio | 0:09dd83d78a0a | 34 | void SetProximityRate (unsigned char ProximityRate); |
ghostaudio | 0:09dd83d78a0a | 35 | void ReadProxiOnDemand (unsigned int *ProxiValue); |
ghostaudio | 0:09dd83d78a0a | 36 | void Setup(void); |
ghostaudio | 0:09dd83d78a0a | 37 | |
ghostaudio | 0:09dd83d78a0a | 38 | |
ghostaudio | 0:09dd83d78a0a | 39 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 40 | |
ghostaudio | 0:09dd83d78a0a | 41 | int main() { |
ghostaudio | 0:09dd83d78a0a | 42 | |
ghostaudio | 0:09dd83d78a0a | 43 | Setup(); |
ghostaudio | 0:09dd83d78a0a | 44 | |
ghostaudio | 0:09dd83d78a0a | 45 | while (1) { |
ghostaudio | 0:09dd83d78a0a | 46 | |
ghostaudio | 0:09dd83d78a0a | 47 | ReadAllSensors(); |
ghostaudio | 0:09dd83d78a0a | 48 | |
ghostaudio | 0:09dd83d78a0a | 49 | } |
ghostaudio | 0:09dd83d78a0a | 50 | } |
ghostaudio | 0:09dd83d78a0a | 51 | |
ghostaudio | 0:09dd83d78a0a | 52 | |
ghostaudio | 0:09dd83d78a0a | 53 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 54 | |
ghostaudio | 0:09dd83d78a0a | 55 | void Setup(void) { |
ghostaudio | 0:09dd83d78a0a | 56 | |
ghostaudio | 0:09dd83d78a0a | 57 | pc.baud(115200); |
ghostaudio | 0:09dd83d78a0a | 58 | // set BAUD |
ghostaudio | 0:09dd83d78a0a | 59 | for (int i = 0; i < 4; i++){ |
ghostaudio | 0:09dd83d78a0a | 60 | |
ghostaudio | 0:09dd83d78a0a | 61 | reset = 0; |
ghostaudio | 0:09dd83d78a0a | 62 | wait(0.001); |
ghostaudio | 0:09dd83d78a0a | 63 | reset = 1; |
ghostaudio | 0:09dd83d78a0a | 64 | wait(0.001); |
ghostaudio | 0:09dd83d78a0a | 65 | port[0] = chan[i]; |
ghostaudio | 0:09dd83d78a0a | 66 | i2c.write(MUX,port, 1); |
ghostaudio | 0:09dd83d78a0a | 67 | SetCurrent(20); // Set current to 200mA |
ghostaudio | 0:09dd83d78a0a | 68 | SetCommandRegister (COMMAND_ALL_DISABLE); // ready for prox rate change |
ghostaudio | 0:09dd83d78a0a | 69 | SetProximityRate (PROX_MEASUREMENT_RATE_31); // 31 measurements/s |
ghostaudio | 0:09dd83d78a0a | 70 | wait(0.2); |
ghostaudio | 0:09dd83d78a0a | 71 | |
ghostaudio | 0:09dd83d78a0a | 72 | } |
ghostaudio | 0:09dd83d78a0a | 73 | } |
ghostaudio | 0:09dd83d78a0a | 74 | |
ghostaudio | 0:09dd83d78a0a | 75 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 76 | |
ghostaudio | 0:09dd83d78a0a | 77 | void Setmleds(void){ |
ghostaudio | 0:09dd83d78a0a | 78 | mled0 = mleds[0]; |
ghostaudio | 0:09dd83d78a0a | 79 | mled1 = mleds[1]; |
ghostaudio | 0:09dd83d78a0a | 80 | mled2 = mleds[2]; |
ghostaudio | 0:09dd83d78a0a | 81 | mled3 = mleds[3]; |
ghostaudio | 0:09dd83d78a0a | 82 | } |
ghostaudio | 0:09dd83d78a0a | 83 | |
ghostaudio | 0:09dd83d78a0a | 84 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 85 | |
ghostaudio | 0:09dd83d78a0a | 86 | void SwitchChannel(uint8_t chan){ |
ghostaudio | 0:09dd83d78a0a | 87 | reset = 0; |
ghostaudio | 0:09dd83d78a0a | 88 | wait(0.001); |
ghostaudio | 0:09dd83d78a0a | 89 | reset = 1; |
ghostaudio | 0:09dd83d78a0a | 90 | wait(0.001); |
ghostaudio | 0:09dd83d78a0a | 91 | port[0] = chan; |
ghostaudio | 0:09dd83d78a0a | 92 | i2c.write(MUX,port, 1); |
ghostaudio | 0:09dd83d78a0a | 93 | } |
ghostaudio | 0:09dd83d78a0a | 94 | |
ghostaudio | 0:09dd83d78a0a | 95 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 96 | |
ghostaudio | 0:09dd83d78a0a | 97 | void ReadAllSensors(void){ |
ghostaudio | 0:09dd83d78a0a | 98 | |
ghostaudio | 0:09dd83d78a0a | 99 | for (int i = 0; i < 4; i++){ |
ghostaudio | 0:09dd83d78a0a | 100 | |
ghostaudio | 0:09dd83d78a0a | 101 | SwitchChannel(chan[i]); // set channel |
ghostaudio | 0:09dd83d78a0a | 102 | mleds[i] = 1; // LED on |
ghostaudio | 0:09dd83d78a0a | 103 | Setmleds(); |
ghostaudio | 0:09dd83d78a0a | 104 | ReadProxiOnDemand(&sensors[i]); // read prox value on demand |
ghostaudio | 0:09dd83d78a0a | 105 | mleds[i] = 0; // LED off |
ghostaudio | 0:09dd83d78a0a | 106 | Setmleds(); |
ghostaudio | 0:09dd83d78a0a | 107 | } |
ghostaudio | 0:09dd83d78a0a | 108 | |
ghostaudio | 0:09dd83d78a0a | 109 | for (int i = 0; i < 4; i++){ |
ghostaudio | 0:09dd83d78a0a | 110 | pc.printf("\r %i ", sensors[i]); |
ghostaudio | 0:09dd83d78a0a | 111 | } |
ghostaudio | 2:e97394fe87c4 | 112 | pc.printf("\r \n "); |
ghostaudio | 0:09dd83d78a0a | 113 | } |
ghostaudio | 0:09dd83d78a0a | 114 | |
ghostaudio | 0:09dd83d78a0a | 115 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 116 | |
ghostaudio | 0:09dd83d78a0a | 117 | void SetCommandRegister (unsigned char Command) { |
ghostaudio | 0:09dd83d78a0a | 118 | |
ghostaudio | 0:09dd83d78a0a | 119 | //unsigned char send[2]; |
ghostaudio | 0:09dd83d78a0a | 120 | |
ghostaudio | 0:09dd83d78a0a | 121 | _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration reister |
ghostaudio | 0:09dd83d78a0a | 122 | _send[1] = Command; |
ghostaudio | 0:09dd83d78a0a | 123 | i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C |
ghostaudio | 0:09dd83d78a0a | 124 | } |
ghostaudio | 0:09dd83d78a0a | 125 | |
ghostaudio | 0:09dd83d78a0a | 126 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 127 | |
ghostaudio | 0:09dd83d78a0a | 128 | void ReadCommandRegister (unsigned char *Command) { |
ghostaudio | 0:09dd83d78a0a | 129 | |
ghostaudio | 0:09dd83d78a0a | 130 | //unsigned char send[2]; |
ghostaudio | 0:09dd83d78a0a | 131 | //unsigned char receive[2]; |
ghostaudio | 0:09dd83d78a0a | 132 | |
ghostaudio | 0:09dd83d78a0a | 133 | _send[0] = REGISTER_COMMAND; // VCNL40x0 Configuration register |
ghostaudio | 0:09dd83d78a0a | 134 | i2c.write(VCNL4020,_send, 1); // Write 1 byte on I2C |
ghostaudio | 0:09dd83d78a0a | 135 | i2c.read(VCNL4020+1,_receive, 1); // Read 1 byte on I2C |
ghostaudio | 0:09dd83d78a0a | 136 | |
ghostaudio | 0:09dd83d78a0a | 137 | *Command = (unsigned char)(_receive[0]); |
ghostaudio | 0:09dd83d78a0a | 138 | } |
ghostaudio | 0:09dd83d78a0a | 139 | |
ghostaudio | 0:09dd83d78a0a | 140 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 141 | |
ghostaudio | 0:09dd83d78a0a | 142 | void SetCurrent (unsigned char Current) { |
ghostaudio | 0:09dd83d78a0a | 143 | |
ghostaudio | 0:09dd83d78a0a | 144 | _send[0] = REGISTER_PROX_CURRENT; // VCNL40x0 IR LED Current register |
ghostaudio | 0:09dd83d78a0a | 145 | _send[1] = Current; |
ghostaudio | 0:09dd83d78a0a | 146 | i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C |
ghostaudio | 0:09dd83d78a0a | 147 | |
ghostaudio | 0:09dd83d78a0a | 148 | } |
ghostaudio | 0:09dd83d78a0a | 149 | |
ghostaudio | 0:09dd83d78a0a | 150 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 151 | |
ghostaudio | 0:09dd83d78a0a | 152 | void ReadProxiValue (unsigned int *ProxiValue) { |
ghostaudio | 0:09dd83d78a0a | 153 | |
ghostaudio | 0:09dd83d78a0a | 154 | _send[0] = REGISTER_PROX_VALUE; // VCNL40x0 Proximity Value register |
ghostaudio | 0:09dd83d78a0a | 155 | i2c.write(VCNL4020, _send, 1); // Write 1 byte on I2C |
ghostaudio | 0:09dd83d78a0a | 156 | i2c.read(VCNL4020+1, _receive, 2); // Read 2 bytes on I2C |
ghostaudio | 0:09dd83d78a0a | 157 | *ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]); |
ghostaudio | 0:09dd83d78a0a | 158 | } |
ghostaudio | 0:09dd83d78a0a | 159 | |
ghostaudio | 0:09dd83d78a0a | 160 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 161 | |
ghostaudio | 0:09dd83d78a0a | 162 | void SetProximityRate (unsigned char ProximityRate) { |
ghostaudio | 0:09dd83d78a0a | 163 | |
ghostaudio | 0:09dd83d78a0a | 164 | _send[0] = REGISTER_PROX_RATE; // VCNL40x0 Proximity rate register |
ghostaudio | 0:09dd83d78a0a | 165 | _send[1] = ProximityRate; |
ghostaudio | 0:09dd83d78a0a | 166 | i2c.write(VCNL4020,_send, 2); // Write 2 bytes on I2C |
ghostaudio | 0:09dd83d78a0a | 167 | } |
ghostaudio | 0:09dd83d78a0a | 168 | |
ghostaudio | 0:09dd83d78a0a | 169 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ghostaudio | 0:09dd83d78a0a | 170 | |
ghostaudio | 0:09dd83d78a0a | 171 | void ReadProxiOnDemand (unsigned int *ProxiValue) { |
ghostaudio | 0:09dd83d78a0a | 172 | |
ghostaudio | 0:09dd83d78a0a | 173 | unsigned char Command=0; |
ghostaudio | 0:09dd83d78a0a | 174 | |
ghostaudio | 0:09dd83d78a0a | 175 | // enable prox value on demand |
ghostaudio | 0:09dd83d78a0a | 176 | SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND); |
ghostaudio | 0:09dd83d78a0a | 177 | |
ghostaudio | 0:09dd83d78a0a | 178 | // wait on prox data ready bit |
ghostaudio | 0:09dd83d78a0a | 179 | do { |
ghostaudio | 0:09dd83d78a0a | 180 | ReadCommandRegister (&Command); // read command register |
ghostaudio | 0:09dd83d78a0a | 181 | } while (!(Command & COMMAND_MASK_PROX_DATA_READY)); |
ghostaudio | 0:09dd83d78a0a | 182 | |
ghostaudio | 0:09dd83d78a0a | 183 | ReadProxiValue (ProxiValue); // read prox value |
ghostaudio | 0:09dd83d78a0a | 184 | |
ghostaudio | 0:09dd83d78a0a | 185 | SetCommandRegister (COMMAND_ALL_DISABLE); // stop prox value on demand |
ghostaudio | 0:09dd83d78a0a | 186 | |
ghostaudio | 0:09dd83d78a0a | 187 | } |