Rick McConney
/
StarterKit
GSMA version
Fork of AvnetATT_shape_hackathon by
Proximity.cpp@39:3bbb3dbb531b, 2016-09-27 (annotated)
- Committer:
- elmkom
- Date:
- Tue Sep 27 15:45:21 2016 +0000
- Revision:
- 39:3bbb3dbb531b
- Parent:
- 38:532a0d929756
- Child:
- 44:60008ebffdd4
changed datastr
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elmkom | 38:532a0d929756 | 1 | #include "mbed.h" |
elmkom | 38:532a0d929756 | 2 | #include "Proximity.h" |
elmkom | 38:532a0d929756 | 3 | |
elmkom | 38:532a0d929756 | 4 | I2C *proximityi2c; |
elmkom | 38:532a0d929756 | 5 | short proximityData [NUM_PROXIMIY_SENSORS][3]; |
elmkom | 38:532a0d929756 | 6 | short lastProximityData [NUM_PROXIMIY_SENSORS][3]; |
elmkom | 38:532a0d929756 | 7 | char dataStr[NUM_PROXIMIY_SENSORS*32]; |
elmkom | 38:532a0d929756 | 8 | |
elmkom | 38:532a0d929756 | 9 | Proximity::Proximity(void) |
elmkom | 38:532a0d929756 | 10 | { |
elmkom | 38:532a0d929756 | 11 | |
elmkom | 38:532a0d929756 | 12 | |
elmkom | 38:532a0d929756 | 13 | } |
elmkom | 38:532a0d929756 | 14 | |
elmkom | 38:532a0d929756 | 15 | void Proximity::init(void) |
elmkom | 38:532a0d929756 | 16 | { |
elmkom | 38:532a0d929756 | 17 | proximityi2c = new I2C(PTE25, PTE24); |
elmkom | 38:532a0d929756 | 18 | proximityi2c->frequency(400000); |
elmkom | 38:532a0d929756 | 19 | } |
elmkom | 38:532a0d929756 | 20 | |
elmkom | 38:532a0d929756 | 21 | void Proximity::write_reg(char address,char reg, char cmd) |
elmkom | 38:532a0d929756 | 22 | { |
elmkom | 38:532a0d929756 | 23 | char txbuffer [2]; |
elmkom | 38:532a0d929756 | 24 | txbuffer[0] = reg; |
elmkom | 38:532a0d929756 | 25 | txbuffer[1] = cmd; |
elmkom | 38:532a0d929756 | 26 | |
elmkom | 38:532a0d929756 | 27 | proximityi2c->write(address<<1, txbuffer, 2,false ); |
elmkom | 38:532a0d929756 | 28 | |
elmkom | 38:532a0d929756 | 29 | } |
elmkom | 38:532a0d929756 | 30 | |
elmkom | 38:532a0d929756 | 31 | void Proximity::write(char address, char cmd) |
elmkom | 38:532a0d929756 | 32 | { |
elmkom | 38:532a0d929756 | 33 | char txbuffer [1]; |
elmkom | 38:532a0d929756 | 34 | txbuffer[0] = cmd; |
elmkom | 38:532a0d929756 | 35 | |
elmkom | 38:532a0d929756 | 36 | proximityi2c->write(address<<1, txbuffer, 1,false ); |
elmkom | 38:532a0d929756 | 37 | |
elmkom | 38:532a0d929756 | 38 | } |
elmkom | 38:532a0d929756 | 39 | |
elmkom | 38:532a0d929756 | 40 | unsigned char Proximity::read_reg(char address,char reg) |
elmkom | 38:532a0d929756 | 41 | { |
elmkom | 38:532a0d929756 | 42 | char txbuffer [1]; |
elmkom | 38:532a0d929756 | 43 | char rxbuffer [1]; |
elmkom | 38:532a0d929756 | 44 | rxbuffer[0] = 0; |
elmkom | 38:532a0d929756 | 45 | txbuffer[0] = reg; |
elmkom | 38:532a0d929756 | 46 | proximityi2c->write(address<<1, txbuffer, 1,false ); |
elmkom | 38:532a0d929756 | 47 | proximityi2c->read(address<<1, rxbuffer, 1 ); |
elmkom | 38:532a0d929756 | 48 | return (unsigned char)rxbuffer[0]; |
elmkom | 38:532a0d929756 | 49 | } |
elmkom | 38:532a0d929756 | 50 | void Proximity::off() |
elmkom | 38:532a0d929756 | 51 | { |
elmkom | 38:532a0d929756 | 52 | for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) |
elmkom | 38:532a0d929756 | 53 | { |
elmkom | 38:532a0d929756 | 54 | write(MUXADDRESS,1<<sensor); |
elmkom | 38:532a0d929756 | 55 | write_reg(PROXIMITYADDRESS,0x41,0x00); |
elmkom | 38:532a0d929756 | 56 | } |
elmkom | 38:532a0d929756 | 57 | } |
elmkom | 38:532a0d929756 | 58 | |
elmkom | 38:532a0d929756 | 59 | void Proximity::on() |
elmkom | 38:532a0d929756 | 60 | { |
elmkom | 38:532a0d929756 | 61 | for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) |
elmkom | 38:532a0d929756 | 62 | { |
elmkom | 38:532a0d929756 | 63 | |
elmkom | 38:532a0d929756 | 64 | lastProximityData[sensor][0] = 1000; |
elmkom | 38:532a0d929756 | 65 | write(MUXADDRESS,1<<sensor); |
elmkom | 38:532a0d929756 | 66 | write_reg(PROXIMITYADDRESS,0x41,Als400Ps400); // initiate ALS: and PS |
elmkom | 38:532a0d929756 | 67 | write_reg(PROXIMITYADDRESS,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma |
elmkom | 38:532a0d929756 | 68 | } |
elmkom | 38:532a0d929756 | 69 | wait(0.5); |
elmkom | 38:532a0d929756 | 70 | } |
elmkom | 38:532a0d929756 | 71 | |
elmkom | 38:532a0d929756 | 72 | void Proximity::scan() |
elmkom | 38:532a0d929756 | 73 | { |
elmkom | 38:532a0d929756 | 74 | for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) |
elmkom | 38:532a0d929756 | 75 | { |
elmkom | 38:532a0d929756 | 76 | write(MUXADDRESS,1<<sensor); |
elmkom | 38:532a0d929756 | 77 | //proximity_sensor_on(sensor); |
elmkom | 38:532a0d929756 | 78 | unsigned char prox_lsb = read_reg(PROXIMITYADDRESS,0x44); |
elmkom | 38:532a0d929756 | 79 | unsigned char prox_msb = read_reg(PROXIMITYADDRESS,0x45); |
elmkom | 38:532a0d929756 | 80 | unsigned char ALS_lsb = read_reg(PROXIMITYADDRESS,0x46); |
elmkom | 38:532a0d929756 | 81 | unsigned char ALS_msb = read_reg(PROXIMITYADDRESS,0x47); |
elmkom | 38:532a0d929756 | 82 | unsigned char IR_lsb = read_reg(PROXIMITYADDRESS,0x48); |
elmkom | 38:532a0d929756 | 83 | unsigned char IR_msb = read_reg(PROXIMITYADDRESS,0x49); |
elmkom | 38:532a0d929756 | 84 | |
elmkom | 38:532a0d929756 | 85 | short proximity = prox_msb*256+prox_lsb; |
elmkom | 38:532a0d929756 | 86 | short ALS = ALS_msb*256+ALS_lsb; |
elmkom | 38:532a0d929756 | 87 | short IR = IR_msb*256+IR_lsb; |
elmkom | 38:532a0d929756 | 88 | proximityData[sensor][0] = proximity; |
elmkom | 38:532a0d929756 | 89 | proximityData[sensor][1] = ALS; |
elmkom | 38:532a0d929756 | 90 | proximityData[sensor][2] = IR; |
elmkom | 38:532a0d929756 | 91 | //proximity_sensor_off(sensor); |
elmkom | 38:532a0d929756 | 92 | //pc.printf(GRN "Sensor %d = %d, %d, %d\n\r",sensor,proximity,ALS,IR); |
elmkom | 38:532a0d929756 | 93 | } |
elmkom | 38:532a0d929756 | 94 | |
elmkom | 38:532a0d929756 | 95 | } |
elmkom | 38:532a0d929756 | 96 | |
elmkom | 38:532a0d929756 | 97 | bool Proximity::changed(short delta) |
elmkom | 38:532a0d929756 | 98 | { |
elmkom | 38:532a0d929756 | 99 | for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) |
elmkom | 38:532a0d929756 | 100 | { |
elmkom | 38:532a0d929756 | 101 | if(abs(proximityData[sensor][0] - lastProximityData[sensor][0]) > delta) |
elmkom | 38:532a0d929756 | 102 | { |
elmkom | 38:532a0d929756 | 103 | return true; |
elmkom | 38:532a0d929756 | 104 | } |
elmkom | 38:532a0d929756 | 105 | } |
elmkom | 38:532a0d929756 | 106 | |
elmkom | 38:532a0d929756 | 107 | return false; |
elmkom | 38:532a0d929756 | 108 | } |
elmkom | 38:532a0d929756 | 109 | |
elmkom | 38:532a0d929756 | 110 | short Proximity::getProximity(int sensor) |
elmkom | 38:532a0d929756 | 111 | { |
elmkom | 38:532a0d929756 | 112 | return proximityData[sensor][0]; |
elmkom | 38:532a0d929756 | 113 | } |
elmkom | 38:532a0d929756 | 114 | short Proximity::getAmbientLight(int sensor) |
elmkom | 38:532a0d929756 | 115 | { |
elmkom | 38:532a0d929756 | 116 | return proximityData[sensor][1]; |
elmkom | 38:532a0d929756 | 117 | } |
elmkom | 38:532a0d929756 | 118 | short Proximity::getIR(int sensor) |
elmkom | 38:532a0d929756 | 119 | { |
elmkom | 38:532a0d929756 | 120 | return proximityData[sensor][2]; |
elmkom | 38:532a0d929756 | 121 | } |
elmkom | 38:532a0d929756 | 122 | |
elmkom | 38:532a0d929756 | 123 | char* Proximity::getDataStr() |
elmkom | 38:532a0d929756 | 124 | { |
elmkom | 38:532a0d929756 | 125 | int i=0; |
elmkom | 38:532a0d929756 | 126 | int index = 0; |
elmkom | 39:3bbb3dbb531b | 127 | index += sprintf(&dataStr[index],"["); |
elmkom | 38:532a0d929756 | 128 | for (i=0; i<NUM_PROXIMIY_SENSORS; i++) |
elmkom | 38:532a0d929756 | 129 | { |
elmkom | 38:532a0d929756 | 130 | if(i<NUM_PROXIMIY_SENSORS-1) |
elmkom | 38:532a0d929756 | 131 | index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d},", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]); |
elmkom | 38:532a0d929756 | 132 | else |
elmkom | 39:3bbb3dbb531b | 133 | index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d}]", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]); |
elmkom | 38:532a0d929756 | 134 | lastProximityData[i][0] = proximityData[i][0]; |
elmkom | 38:532a0d929756 | 135 | lastProximityData[i][1] = proximityData[i][1]; |
elmkom | 38:532a0d929756 | 136 | lastProximityData[i][2] = proximityData[i][2]; |
elmkom | 38:532a0d929756 | 137 | } |
elmkom | 38:532a0d929756 | 138 | return dataStr; |
elmkom | 38:532a0d929756 | 139 | } |
elmkom | 38:532a0d929756 | 140 | |
elmkom | 38:532a0d929756 | 141 | |
elmkom | 38:532a0d929756 | 142 | |
elmkom | 38:532a0d929756 | 143 |