Rick McConney
/
stripReader
Proximity strip reader
Fork of StarterKit by
Diff: main.cpp
- Revision:
- 35:2e864bae3af0
- Parent:
- 34:029e07b67a41
- Child:
- 36:f8d96ff1dd1b
--- a/main.cpp Sat Jul 23 01:10:53 2016 +0000 +++ b/main.cpp Mon Sep 19 16:16:17 2016 +0000 @@ -9,7 +9,7 @@ #include "hardware.h" I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used - +I2C proximityi2c(PTE25, PTE24); // comment out the following line if color is not supported on the terminal #define USE_COLOR #ifdef USE_COLOR @@ -63,6 +63,149 @@ #define MAX_AT_RSP_LEN 255 +short proximity = 0; + +void prox_write_reg(char address,char reg, char cmd) +{ + char txbuffer [2]; + txbuffer[0] = reg; + txbuffer[1] = cmd; + proximityi2c.write(address<<1, txbuffer, 2,false ); +} + +void prox_write(char address, char cmd) +{ + char txbuffer [1]; + txbuffer[0] = cmd; + proximityi2c.write(address<<1, txbuffer, 1,false ); +} + +unsigned char prox_read_reg(char address,char reg) +{ + char txbuffer [1]; + char rxbuffer [1]; + rxbuffer[0] = 0; + txbuffer[0] = reg; + proximityi2c.write(address<<1, txbuffer, 1,false ); + proximityi2c.read(address<<1, rxbuffer, 1 ); + return (unsigned char)rxbuffer[0]; +} + +void init_proximity_sensor(int sensor) +{ + char C25ma = 0x00; + char C50ma = 0x01; + char C100m1 = 0x02; + char C200ma = 0x03; + + char GainAls1Ir1 = 0x00<<2; + char GainAls2Ir1 = 0x04<<2; + char GainAls2Ir2 = 0x05<<2; + char GainAls64Ir64 = 0x0A<<2; + char GainAls128Ir64 = 0x0D<<2; + char GainAls128Ir128 = 0x0F<<2; + + char Als0Ps0 = 0x00; + char Als0Ps10 = 0x01; + char Als0Ps40 = 0x02; + char Als0Ps100 = 0x03; + char Als0Ps400 = 0x04; + + char Als100Ps0 = 0x05; + char Als100Ps100 = 0x06; + char Als100Ps400 = 0x07; + + char Als401Ps0 = 0x08; + char Als401Ps100 = 0x09; + char Als400Ps0 = 0x0A; + char Als400Ps400 = 0x0B; + + char Als50Ps50 = 0x0C; + + char muxaddress = 0x70; + char proxaddress = 0x39; + prox_write(muxaddress,sensor); // sensor 2 + prox_write_reg(proxaddress,0x41,Als0Ps400); // initiate ALS: and PS + prox_write_reg(proxaddress,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma +} + +short read_proximity(int sensor) +{ + + char muxaddress = 0x70; + char proxaddress = 0x39; + prox_write(muxaddress,sensor); // sensor 2 + unsigned char prox_lsb = prox_read_reg(proxaddress,0x44); + unsigned char prox_msb = prox_read_reg(proxaddress,0x45); + unsigned char ALS_lsb = prox_read_reg(proxaddress,0x46); + unsigned char ALS_msb = prox_read_reg(proxaddress,0x47); + unsigned char IR_lsb = prox_read_reg(proxaddress,0x48); + unsigned char IR_msb = prox_read_reg(proxaddress,0x49); + + short proximity = prox_msb*256+prox_lsb; + short ALS = ALS_msb*256+ALS_lsb; + short IR = IR_msb*256+IR_lsb; + pc.printf(GRN "Sensor %d\n\r",sensor); + pc.printf(GRN "Prox %d\n\r",proximity); + pc.printf(GRN "ALS %d\n\r",ALS); + pc.printf(GRN "IR %d\n\r",IR); + return proximity; +} + + + /* +I2C_w_3 (sfh_address*2, 0x41, 0x08); // initiate ALS: 400ms rep rate, T_int=100ms +I2C_w_3 (sfh_address*2, 0x42, 0x28); // set ALS_VIS=ALS_IR GAIN = 64 +I2C_w_2_r_1 (sfh_address*2, 0x46); // read lsb of ALS_VIS, register 0x46 +Content1 = Content; +I2C_w_2_r_1 (sfh_address*2, 0x47); // read msb of ALS_VIS, register 0x47 +ALS_VIS = (Content * 256 + Content1); // combining LSB+MSB byte to decimal value +I2C_w_2_r_1 (sfh_address*2, 0x48); // read lsb of ALS_IR, register 0x48 +Content1 = Content; +I2C_w_2_r_1 (sfh_address*2, 0x49); // read msb of ALS_IR, register 0x49 +ALS_IR = (Content * 256 + Content1); // combining LSB+MSB byte to decimal value +// Lux Calculation based on ALS Gain = 64 and ALS_Int_Time = 100 ms +// Lux value in front of sensor, no cover glass +IF ((ALS_IR / ALS_VIS) < 0.109) + {LUX = (1.534 * ALS_VIS / 64 - 3.759 * ALS_IR / 64) * 1}; +ELSE IF ((ALS_IR / ALS_VIS) < 0.429) + {LUX = (1.339 * ALS_VIS / 64 – 1.972 * ALS_IR / 64) * 1}; +ELSE IF ((ALS_IR/ALS_VIS) < (0.95 * 1.45)) + {LUX = (0.701 * ALS_VIS / 64 – 0.483 * ALS_IR / 64) * 1}; +ELSE IF ((ALS_IR/ALS_VIS) < (1.5 * 1.45)) + {LUX = (2 * 0.701 * ALS_VIS / 64 – 1.18 * 0.483 * ALS_IR / 64) * 1}; +ELSE IF ((ALS_IR/ALS_VIS) < (2.5 * 1.45)) +{LUX = (4 * 0.701 * ALS_VIS / 64 – 1.33 * 0.483 * ALS_IR / 64) * 1}; +Else {LUX = 8 * 0.701 * ALS_VIS / 64}; +*/ +short oldread_proximity( void ) +{ + char muxaddress = (0x70<<1); + char proxaddress = (0x39<<1); + char reg = 0x40; + short muxvalue = 0; + short value = 0; + char sensor = 0x02; + + char txbuffer [1]; + char rxbuffer [1]; + rxbuffer[0] = 0; + txbuffer[0] = sensor; + proximityi2c.write(muxaddress, txbuffer, 1,false ); + proximityi2c.read(muxaddress, rxbuffer, 1 ); + muxvalue = (unsigned char)rxbuffer[0]; + + rxbuffer[0] = 0; + txbuffer[0] = reg; + proximityi2c.write(proxaddress, txbuffer, 1,false ); + proximityi2c.read(proxaddress, rxbuffer, 1 ); + value = (unsigned char)rxbuffer[0]; + pc.printf(GRN "Mux %d\n\r",muxvalue); + pc.printf(GRN "Proximity %d\n\r",value); + return value; + +} + ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) { int cin = -1; int cin_last; @@ -133,8 +276,66 @@ } return MDM_OK; } +int mdm_init(void) { + // disable signal level translator (necessary + // for the modem to boot properly) + shield_3v3_1v8_sig_trans_ena = 0; -int mdm_init(void) { + // Hard reset the modem (doesn't go through + // the signal level translator) + mdm_reset = 1; + + // wait a moment for the modem to react + wait_ms(10); + + // Let modem boot + mdm_reset = 0; + + // wait a moment for the modem to react + wait(1.0); + + // power modem on //off + mdm_power_on = 0; //1; + + // insure modem boots into normal operating mode + // and does not go to sleep when powered on + mdm_uart2_rx_boot_mode_sel = 1; + mdm_wakeup_in = 1; + + // initialze comm with the modem + mdm.baud(115200); + // clear out potential garbage + while (mdm.readable()) + mdm.getc(); + + mdm_uart1_cts = 0; + + // wait a moment for the modem to react to signal + // conditions while the level translator is disabled + // (sorry, don't have enough information to know + // what exactly the modem is doing with the current + // pin settings) + wait(1.0); + + // enable the signal level translator to start + // modem reset process (modem will be powered down) + shield_3v3_1v8_sig_trans_ena = 1; + + // Give the modem 60 secons to start responding by + // sending simple 'AT' commands to modem once per second. + Timer timer; + timer.start(); + while (timer.read() < 60) { + const char * rsp_lst[] = { ok_str, error_str, NULL }; + int rc = mdm_sendAtCmd("AT", rsp_lst, 500); + if (rc == 0) + return true; //timer.read(); + wait_ms(1000 - (timer.read_ms() % 1000)); + pc.printf("\r%d",timer.read_ms()/1000); + } + return false; +} +int oldmdm_init(void) { // Hard reset the modem (doesn't go through // the signal level translator) mdm_reset = 0; @@ -279,6 +480,11 @@ { switch(iSensorsToReport) { + case PROXIMITY: + { + sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&proximity=%d %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity,proximity, FLOW_URL_TYPE, MY_SERVER_URL); + break; + } case TEMP_HUMIDITY_ONLY: { sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL); @@ -434,6 +640,9 @@ int i; HTS221 hts221; pc.baud(115200); + proximityi2c.frequency(400000); + for(int i = 0;i<8;i++) + init_proximity_sensor(i); void hts221_init(void); @@ -452,6 +661,7 @@ printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature())); printf("Humid is: %02d %%\n\r",hts221.readHumidity()); + sensors_init(); read_sensors(); @@ -490,6 +700,8 @@ sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature())); sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity()); read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor + for(int i = 0;i<8;i++) + proximity = read_proximity(i); char modem_string[512]; GenerateModemString(&modem_string[0]); printf(BLU "Sending to modem : %s" DEF "\n", modem_string);