![](/media/cache/profiles/595e4edac4cf4a73d34d23041575d3e2.jpg.50x50_q85.jpg)
This program simply connects to a HTS221 I2C device to proximity sensor
Diff: main.cpp
- Revision:
- 38:532a0d929756
- Parent:
- 37:ee01f752524a
- Child:
- 39:3bbb3dbb531b
--- a/main.cpp Wed Sep 21 18:03:43 2016 +0000 +++ b/main.cpp Mon Sep 26 18:02:16 2016 +0000 @@ -6,10 +6,11 @@ #include "config_me.h" #include "wnc_control.h" #include "sensors.h" +#include "Proximity.h" #include "hardware.h" I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used -I2C proximityi2c(PTE25, PTE24); +Proximity proximityStrip; // comment out the following line if color is not supported on the terminal #define USE_COLOR @@ -67,105 +68,12 @@ #define MDM_ERR_TIMEOUT -1 #define MAX_AT_RSP_LEN 255 -#define NUM_PROXIMIY_SENSORS 8 -short proximityData [NUM_PROXIMIY_SENSORS][3]; -short lastProximityData [NUM_PROXIMIY_SENSORS][3]; + bool proximityChange = false; - -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 proximity_sensor_off(int sensor) -{ - prox_write(MUXADDRESS,1<<sensor); - prox_write_reg(PROXIMITYADDRESS,0x41,0x00); -} - -void proximity_sensor_on(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; - - - prox_write(MUXADDRESS,1<<sensor); - prox_write_reg(PROXIMITYADDRESS,0x41,Als400Ps400); // initiate ALS: and PS - prox_write_reg(PROXIMITYADDRESS,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma - wait(0.5); -} - -short* read_proximity(int sensor) -{ - short readings[3]; - - prox_write(MUXADDRESS,1<<sensor); - //proximity_sensor_on(sensor); - unsigned char prox_lsb = prox_read_reg(PROXIMITYADDRESS,0x44); - unsigned char prox_msb = prox_read_reg(PROXIMITYADDRESS,0x45); - unsigned char ALS_lsb = prox_read_reg(PROXIMITYADDRESS,0x46); - unsigned char ALS_msb = prox_read_reg(PROXIMITYADDRESS,0x47); - unsigned char IR_lsb = prox_read_reg(PROXIMITYADDRESS,0x48); - unsigned char IR_msb = prox_read_reg(PROXIMITYADDRESS,0x49); - - short proximity = prox_msb*256+prox_lsb; - short ALS = ALS_msb*256+ALS_lsb; - short IR = IR_msb*256+IR_lsb; - readings[0] = proximity; - readings[1] = ALS; - readings[2] = IR; - //proximity_sensor_off(sensor); - //pc.printf(GRN "Sensor %d = %d, %d, %d\n\r",sensor,proximity,ALS,IR); +bool powerSave = false; - return readings; -} + @@ -177,10 +85,19 @@ //******************************************************************************************************************************************** void SetLedColor(unsigned char ucColor) { - //Note that when an LED is on, you write a 0 to it: - led_red = !(ucColor & 0x1); //bit 0 - led_green = !(ucColor & 0x2); //bit 1 - led_blue = !(ucColor & 0x4); //bit 2 + if(powerSave) + { + led_red = !0; + led_green = !0; + led_blue = !0; + } + else + { + //Note that when an LED is on, you write a 0 to it: + led_red = !(ucColor & 0x1); //bit 0 + led_green = !(ucColor & 0x2); //bit 1 + led_blue = !(ucColor & 0x4); //bit 2 + } } //SetLedColor() ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) { @@ -331,7 +248,7 @@ } return false; } - +/* bool wakeModem() { const char * rsp_lst[] = { ok_str, error_str, NULL }; @@ -350,7 +267,12 @@ } return false; } - +bool rebootModem() +{ + mdm.printf("ATZ\r\n"); + return wakeModem(); +} +*/ int oldmdm_init(void) { // Hard reset the modem (doesn't go through // the signal level translator) @@ -504,21 +426,10 @@ { switch(iSensorsToReport) { - case PROXIMITY: + case PROXIMITY_ONLY: { - char dataStr[NUM_PROXIMIY_SENSORS*32]; - int i=0; - int index = 0; - for (i=0; i<NUM_PROXIMIY_SENSORS; i++) - { - if(i<NUM_PROXIMIY_SENSORS-1) - index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d},", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]); - else - index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d}", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]); - } - sprintf(modem_string, "GET %s%s?serial=%s&data=[%s] %s%s\r\n\r\n", - FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, dataStr, FLOW_URL_TYPE, MY_SERVER_URL); - + char* data = proximityStrip.getDataStr(); + sprintf(modem_string, "GET %s%s?serial=%s&data=[%s] %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, data, FLOW_URL_TYPE, MY_SERVER_URL); break; } case TEMP_HUMIDITY_ONLY: @@ -545,25 +456,6 @@ } //GenerateModemString -//Periodic timer -Ticker OneMsTicker; -volatile bool bTimerExpiredFlag = false; -int OneMsTicks = 0; -int iTimer1Interval_ms = 1000; -//******************************************************************************************************************************************** -//* Periodic 1ms timer tick -//******************************************************************************************************************************************** -void OneMsFunction() -{ - OneMsTicks++; - if ((OneMsTicks % iTimer1Interval_ms) == 0) - { - bTimerExpiredFlag = true; - } -} //OneMsFunction() - - - //******************************************************************************************************************************************** //* Process JSON response messages //******************************************************************************************************************************************** @@ -598,69 +490,133 @@ } } //extract_JSON -bool parse_JSON(char* json_string) +void setPowerSave(bool on) +{ + string * pRespStr; + if(on) + { + powerSave = true; + send_wnc_cmd("AT+CPSMS=1", &pRespStr, WNC_TIMEOUT_MS); + } + else + { + powerSave = false; + send_wnc_cmd("AT+CPSMS=0", &pRespStr, WNC_TIMEOUT_MS); + } +} + +void parse_JSON_PSM(char* json_string) { char* beginquote; - char token[] = "\"LED\":\""; + char token[] = "\"PSM\":\""; beginquote = strstr(json_string, token ); if ((beginquote != 0)) { - char cLedColor = beginquote[strlen(token)]; - printf(GRN "LED Found : %c" DEF "\r\n", cLedColor); - switch(cLedColor) + char mode = beginquote[strlen(token)]; + printf(GRN "PSM Found : %c" DEF "\r\n", mode); + switch(mode) { - case 'O': - { //Off - SetLedColor(0); + case 'T': + { setPowerSave(true); break; - } - case 'R': - { //Red - SetLedColor(1); - break; - } - case 'G': - { //Green - SetLedColor(2); - break; - } - case 'Y': - { //Yellow - SetLedColor(3); + } + case 'F': + { setPowerSave(false); break; - } - case 'B': - { //Blue - SetLedColor(4); - break; - } - case 'M': - { //Magenta - SetLedColor(5); - break; - } - case 'T': - { //Turquoise - SetLedColor(6); - break; - } - case 'W': - { //White - SetLedColor(7); - break; - } - default: - { - break; - } - } //switch(cLedColor) - return true; + } + } + } +} + + +int secToTau(int time) +{ + /* + 0 - value is incremented in multiples of 10 minutes + 1 - value is incremented in multiples of 1 hour + 2 - value is incremented in multiples of 10 hours + 3 - value is incremented in multiples of 2 seconds + 4 - value is incremented in multiples of 30 seconds + 5 - value is incremented in multiples of 1 minute +*/ + if(time/2 < 32) + { + return (0x3<<5)+time/2; + } + else if(time/30 < 32) + { + return (0x4<<5)+time/30; + } + else if(time/60 < 32) + { + return (0x5<<5)+time/60; + } + else if(time/3600 < 32) + { + return (0x1<<5)+time/3600; + } + else if(time/36000 < 32) + { + return (0x2<<5)+time/36000; } else + return (0x7<<5); + + +} +int secToActivity(int time) +{ + /* + 0 - value is incremented in multiples of 2 seconds + 1 - value is incremented in multiples of 1 minute + 2 - value is incremented in multiples of decihours + 7 - value indicates that the timer is deactivated. + */ + if(time/2 < 32) { - return false; + return (0x0<<5)+time/2; + } + else if(time/60 < 32) + { + return (0x1<<5)+time/60; + } + else if(time/36000 < 32) + { + return (0x2<<5)+time/36000; } -} //parse_JSON + else + return (0x7<<5); + +} +void setTauTimer(int time) +{ + string * pRespStr; + string cmd_str("AT%SETACFG=\"ecm.Mtc.PsmPTAU_T3412\",\""); + char str[15]; + sprintf(str, "%d", secToTau(time)); + pc.printf("TAU %d = %d",time,secToTau(time)); + cmd_str += str; + cmd_str += "\""; + send_wnc_cmd("AT%SETCFG=\"EXT_TAU_CAP_EN\",\"1\"", &pRespStr, WNC_TIMEOUT_MS); + send_wnc_cmd("AT%SETACFG=\"ecm.Mtc.PsmCpsmsEn\",\"true\"", &pRespStr, WNC_TIMEOUT_MS); + send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS); +} + +void setActivityTimer(int time) +{ + string * pRespStr; + string cmd_str("AT%SETACFG=\"ecm.Mtc.PsmActTime_T3324\",\""); + char str[15]; + sprintf(str, "%d", secToActivity(time)); + pc.printf("Activity %d = %d",time,secToActivity(time)); + cmd_str += str; + cmd_str += "\""; + send_wnc_cmd("AT%SETCFG=\"PSM_CAP_EN\",\"1\"", &pRespStr, WNC_TIMEOUT_MS); + send_wnc_cmd("AT%SETACFG=\"ecm.Mtc.PsmCpsmsEn\",\"true\"", &pRespStr, WNC_TIMEOUT_MS); + send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS); +} + + int main() { @@ -669,7 +625,7 @@ HTS221 hts221; pc.baud(115200); - proximityi2c.frequency(400000); + void hts221_init(void); @@ -688,37 +644,7 @@ printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature())); printf("Humid is: %02d %%\n\r",hts221.readHumidity()); - - - //test - /* - int count = 0; - while(count < 4) - { - if(count == 0) - proximity_sensor_on(i); - for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++) - { - - short* readings = read_proximity(i); - proximityData[i][0] = readings[0]; - proximityData[i][1] = readings[1]; - proximityData[i][2] = readings[2]; - } - proximity_sensor_off(i); - wait(2); - count++; - } -*/ - for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++) - { - proximity_sensor_on(i); - lastProximityData[i][0] = 1000; - } - - sensors_init(); - read_sensors(); // Initialize the modem printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n"); @@ -737,102 +663,81 @@ // Resolve URL to IP address to connect to resolve_mdm(); - //Create a 1ms timer tick function: - //OneMsTicker.attach(OneMsFunction, 0.001f) ; - - // iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS; - - // Open the socket (connect to the server) sockopen_mdm(); // Set LED BLUE for partial init SetLedColor(0x4); + + setTauTimer(12*60*60); + setActivityTimer(20); + proximityStrip.init(); + + proximityStrip.on(); + + int count = 0; // Send and receive data perpetually while(1) { - static unsigned ledOnce = 0; - if (true || bTimerExpiredFlag) - { + + + //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 - bTimerExpiredFlag = false; - 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 + SetLedColor(0x2); //green + + proximityStrip.scan(); + + SetLedColor(0); //off - SetLedColor(0x2); //green - for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++) - { - - short* readings = read_proximity(i); - proximityData[i][0] = readings[0]; - proximityData[i][1] = readings[1]; - proximityData[i][2] = readings[2]; - if(abs(proximityData[i][0] - lastProximityData[i][0]) > 50) - { - proximityChange = true; - } - } - + if(count >= 5*60 ||proximityStrip.changed(50)) + { + count = 0; + SetLedColor(0x04); //blue - SetLedColor(0); //off + char modem_string[512]; + GenerateModemString(&modem_string[0]); + printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string); + wakeModem(); + sockwrite_mdm(modem_string); + sockread_mdm(&MySocketData, 1024, 20); - if(proximityChange) - { - SetLedColor(0x04); //blue - proximityChange = false; - char modem_string[512]; - GenerateModemString(&modem_string[0]); - printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string); - wakeModem(); - sockwrite_mdm(modem_string); - sockread_mdm(&MySocketData, 1024, 20); - - // If any non-zero response from server, make it GREEN one-time - // then the actual FLOW responses will set the color. - if (MySocketData.length() > 0) - { + // If any non-zero response from server, make it GREEN one-time + // then the actual FLOW responses will set the color. + if (MySocketData.length() > 0) + { - SetLedColor(0x2); // green - //only copy on sucessful send - for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++) - { - lastProximityData[i][0] = proximityData[i][0]; - } - printf(BLU "Read back : %s" DEF "\r\n", &MySocketData[0]); - char myJsonResponse[512]; - if (extract_JSON(&MySocketData[0], &myJsonResponse[0])) - { - printf(GRN "JSON : %s" DEF "\r\n", &myJsonResponse[0]); - //parse_JSON(&myJsonResponse[0]); - } - else - { - printf(RED "JSON : %s" DEF "\r\n", &myJsonResponse[0]); //most likely an incomplete JSON string - //parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted - } - SetLedColor(0); // off + SetLedColor(0x2); // green + //only copy on sucessful send + + printf(BLU "Read back : %s" DEF "\r\n", &MySocketData[0]); + char myJsonResponse[512]; + if (extract_JSON(&MySocketData[0], &myJsonResponse[0])) + { + printf(GRN "JSON : %s" DEF "\r\n", &myJsonResponse[0]); + parse_JSON_PSM(&myJsonResponse[0]); } - else + SetLedColor(0); // off + } + else + { + SetLedColor(0x1); //red + // reset socket if read fails + if(sendAttemps < 2) { - SetLedColor(0x1); //red - // reset socket if read fails - if(sendAttemps < 2) - { - sendAttemps++; - sockclose_mdm(); - sockopen_mdm(); - } - else // give up and do full reset - { + sendAttemps++; + sockclose_mdm(); + sockopen_mdm(); + } + else // give up and do full reset + { + if(!rebootModem()) system_reset(); - } + } - } - - - } - wait(0.2); - } //bTimerExpiredFlag - + } + } + count++; + wait(0.2); } //forever loop }