Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
elmkom
Date:
Wed Nov 30 14:50:16 2016 +0000
Revision:
45:fe90f1fcb4e0
Parent:
44:60008ebffdd4
gsma version

Who changed what in which revision?

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