Rick McConney
/
StarterKit
GSMA version
Fork of AvnetATT_shape_hackathon by
Revision 46:733edf15f9e8, committed 2017-01-09
- Comitter:
- elmkom
- Date:
- Mon Jan 09 18:23:37 2017 +0000
- Parent:
- 45:fe90f1fcb4e0
- Commit message:
- tcp test code
Changed in this revision
diff -r fe90f1fcb4e0 -r 733edf15f9e8 HTS221.h --- a/HTS221.h Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ - -#ifndef HTS221_H_ -#define HTS221_H_ - -class HTS221 { -public: - HTS221(void); - int begin(void); - int activate(void); - int deactivate(void); - - int bduActivate(void); - int bduDeactivate(void); - - int readHumidity(void); - double readTemperature(void); -private: - int storeCalibration(void); - unsigned char _h0_rH, _h1_rH; - unsigned int _T0_degC, _T1_degC; - unsigned int _H0_T0, _H1_T0; - unsigned int _T0_OUT, _T1_OUT; - double _temperature; - int _humidity; - unsigned char _address; - - unsigned char readRegister(unsigned char slaveAddress, unsigned char regToRead); - int writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite); -}; - -#define HTS221_ADDRESS 0xBF - -//Define a few of the registers that we will be accessing on the HTS221 -#define WHO_AM_I 0x0F -#define WHO_AM_I_RETURN 0xBC //This read-only register contains the device identifier, set to BCh - -#define AVERAGE_REG 0x10 // To configure humidity/temperature average. -#define AVERAGE_DEFAULT 0x1B - -/* - * [7] PD: power down control - * (0: power-down mode; 1: active mode) - * - * [6:3] Reserved - * - * [2] BDU: block data update - * (0: continuous update; 1: output registers not updated until MSB and LSB reading) -The BDU bit is used to inhibit the output register update between the reading of the upper -and lower register parts. In default mode (BDU = ?0?), the lower and upper register parts are -updated continuously. If it is not certain whether the read will be faster than output data rate, -it is recommended to set the BDU bit to ?1?. In this way, after the reading of the lower (upper) -register part, the content of that output register is not updated until the upper (lower) part is -read also. - * - * [1:0] ODR1, ODR0: output data rate selection (see table 17) - */ -#define CTRL_REG1 0x20 -#define POWER_UP 0x80 -#define BDU_SET 0x4 -#define ODR0_SET 0x1 // setting sensor reading period 1Hz - -#define CTRL_REG2 0x21 -#define CTRL_REG3 0x22 -#define REG_DEFAULT 0x00 - -#define STATUS_REG 0x27 -#define TEMPERATURE_READY 0x1 -#define HUMIDITY_READY 0x2 - -#define HUMIDITY_L_REG 0x28 -#define HUMIDITY_H_REG 0x29 -#define TEMP_L_REG 0x2A -#define TEMP_H_REG 0x2B -/* - * calibration registry should be read for temperature and humidity calculation. - * Before the first calculation of temperature and humidity, - * the master reads out the calibration coefficients. - * will do at init phase - */ -#define CALIB_START 0x30 -#define CALIB_END 0x3F -/* -#define CALIB_T0_DEGC_X8 0x32 -#define CALIB_T1_DEGC_X8 0x33 -#define CALIB_T1_T0_MSB 0x35 -#define CALIB_T0_OUT_L 0x3C -#define CALIB_T0_OUT_H 0x3D -#define CALIB_T1_OUT_L 0x3E -#define CALIB_T1_OUT_H 0x3F - */ - -#endif
diff -r fe90f1fcb4e0 -r 733edf15f9e8 Proximity.cpp --- a/Proximity.cpp Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,139 +0,0 @@ -#include "mbed.h" -#include "Proximity.h" - - - -Proximity::Proximity(void) -{ -} - -void Proximity::init(I2C *i2c, char mAddress) -{ - muxAddress = mAddress; - proximityi2c = i2c; - proximityi2c->frequency(100000); -} - -void Proximity::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 Proximity::write(char address, char cmd) -{ - char txbuffer [1]; - txbuffer[0] = cmd; - - proximityi2c->write(address<<1, txbuffer, 1,false ); - -} - -unsigned char Proximity::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::off() -{ - for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) - { - write(muxAddress,1<<sensor); - write_reg(PROXIMITYADDRESS,0x41,0x00); - } -} - -void Proximity::on() -{ - for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) - { - - lastProximityData[sensor][0] = 1000; - write(muxAddress,1<<sensor); - write_reg(PROXIMITYADDRESS,0x41,Als400Ps400); // initiate ALS: and PS - write_reg(PROXIMITYADDRESS,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma - } - wait(0.5); -} - -void Proximity::scan() -{ - for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) - { - write(muxAddress,1<<sensor); - //proximity_sensor_on(sensor); - unsigned char prox_lsb = read_reg(PROXIMITYADDRESS,0x44); - unsigned char prox_msb = read_reg(PROXIMITYADDRESS,0x45); - unsigned char ALS_lsb = read_reg(PROXIMITYADDRESS,0x46); - unsigned char ALS_msb = read_reg(PROXIMITYADDRESS,0x47); - unsigned char IR_lsb = read_reg(PROXIMITYADDRESS,0x48); - unsigned char IR_msb = 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; - proximityData[sensor][0] = proximity; - proximityData[sensor][1] = ALS; - proximityData[sensor][2] = IR; - //proximity_sensor_off(sensor); - //pc.printf(GRN "Sensor %d = %d, %d, %d\n\r",sensor,proximity,ALS,IR); - } - -} - -bool Proximity::changed(short delta) -{ - for(int sensor = 0;sensor<NUM_PROXIMIY_SENSORS;sensor++) - { - if(abs(proximityData[sensor][0] - lastProximityData[sensor][0]) > delta) - { - return true; - } - } - - return false; -} - -short Proximity::getProximity(int sensor) -{ - return proximityData[sensor][0]; -} -short Proximity::getAmbientLight(int sensor) -{ - return proximityData[sensor][1]; -} -short Proximity::getIR(int sensor) -{ - return proximityData[sensor][2]; -} - -char* Proximity::getDataStr() -{ - int i=0; - int index = 0; - index += sprintf(&dataStr[index],"["); - 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]); - lastProximityData[i][0] = proximityData[i][0]; - lastProximityData[i][1] = proximityData[i][1]; - lastProximityData[i][2] = proximityData[i][2]; - } - return dataStr; -} - - - -
diff -r fe90f1fcb4e0 -r 733edf15f9e8 Proximity.h --- a/Proximity.h Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#ifndef Proximity_ -#define Proximity_ - - -#define PROXIMITYADDRESS 0x39 -#define NUM_PROXIMIY_SENSORS 5 - -#define C25ma 0x00 -#define C50ma 0x01 -#define C100m1 0x02 -#define C200ma 0x03 - -#define GainAls1Ir1 (0x00<<2) -#define GainAls2Ir1 (0x04<<2) -#define GainAls2Ir2 (0x05<<2) -#define GainAls64Ir64 (0x0A<<2) -#define GainAls128Ir64 (0x0D<<2) -#define GainAls128Ir128 (0x0F<<2) - -#define Als0Ps0 0x00 -#define Als0Ps10 0x01 -#define Als0Ps40 0x02 -#define Als0Ps100 0x03 -#define Als0Ps400 0x04 - -#define Als100Ps0 0x05 -#define Als100Ps100 0x06 -#define Als100Ps400 0x07 - -#define Als401Ps0 0x08 -#define Als401Ps100 0x09 -#define Als400Ps0 0x0A -#define Als400Ps400 0x0B - -#define Als50Ps50 0x0C - - -class Proximity { -private: -I2C *proximityi2c; -short proximityData [NUM_PROXIMIY_SENSORS][3]; -short lastProximityData [NUM_PROXIMIY_SENSORS][3]; -char dataStr[NUM_PROXIMIY_SENSORS*32]; -char muxAddress; -public: - Proximity(void); - void init(I2C *i2c, char mAddress); - void write_reg(char address,char reg, char cmd); - void write(char address, char cmd); - unsigned char read_reg(char address,char reg); - void off(); - void on(); - void scan(); - bool changed(short delta); - short getProximity(int sensor); - short getAmbientLight(int sensor); - short getIR(int sensor); - char* getDataStr(); -}; - -#endif \ No newline at end of file
diff -r fe90f1fcb4e0 -r 733edf15f9e8 Wnc.cpp --- a/Wnc.cpp Wed Nov 30 14:50:16 2016 +0000 +++ b/Wnc.cpp Mon Jan 09 18:23:37 2017 +0000 @@ -393,15 +393,15 @@ reply = send("AT%PDNSET?", WNC_WAIT_TIME_MS); - // pc.printf("Wiating ...\r\n"); - // wait(10); + //pc.printf("Wiating ...\r\n"); + //wait(10); reply = send("AT@INTERNET=1", WNC_WAIT_TIME_MS); // Internet services enabled if(cmdFailed(reply,"Failed to start INTERNET")) { return false; } - reply = send("AT@SOCKDIAL=1", WNC_WAIT_TIME_MS); + reply = send("AT@SOCKDIAL=1", 4*WNC_WAIT_TIME_MS); if(cmdFailed(reply,"SOCKDIAL Failed")) { // passthrough();
diff -r fe90f1fcb4e0 -r 733edf15f9e8 config_me.h --- a/config_me.h Wed Nov 30 14:50:16 2016 +0000 +++ b/config_me.h Mon Jan 09 18:23:37 2017 +0000 @@ -6,7 +6,7 @@ // This is the server's base URL name. Example "www.google.com" // Note that when you Fork a FLOW, it will typically assign either // "run-east.att.io" or "run-west.att.io", so be sure to check this. -static const char * MY_SERVER_URL = "run-east.att.io"; +static const char * MY_SERVER_URL = "runm-west.att.io"; // These are FLOW fields from the Endpoints tab: #define FLOW_BASE_URL "/a80556051c438/f4cd2ed65f73/7ecb40fcd115f7a/in/flow" @@ -38,10 +38,10 @@ static int iSensorsToReport = PROXIMITY_ONLY; //TEMP_HUMIDITY_ONLY; //modify this to change your selection // This is the APN name for the cellular network, you will need to change this, check the instructions included with your SIM card kit: -//#define MY_APN_STR "m2m.com.attz" -#define MY_APN_STR "attiotdemo" +#define MY_APN_STR "m2m.com.attz" +//#define MY_APN_STR "attiotdemo" //#define MY_APN_STR "nxtgenphone" //This is for normal HTTP. If you want to use TCP to a specific port, change that here: -static int MY_PORT = 80; +static int MY_PORT = 33971; #endif
diff -r fe90f1fcb4e0 -r 733edf15f9e8 hardware.h --- a/hardware.h Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -#ifndef Hardware_H_ -#define Hardware_H_ -extern I2C i2c; //SDA, SCL -- define the I2C pins being used - -#endif
diff -r fe90f1fcb4e0 -r 733edf15f9e8 hts221_driver.cpp --- a/hts221_driver.cpp Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,249 +0,0 @@ - -#include "HTS221.h" - - -// ------------------------------------------------------------------------------ -//jmf -- define I2C pins and functions to read & write to I2C device - -#include <string> -#include "mbed.h" - -#include "hardware.h" -//I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used. Defined in a -//common locatioin since sensors also use I2C - -// Read a single unsigned char from addressToRead and return it as a unsigned char -unsigned char HTS221::readRegister(unsigned char slaveAddress, unsigned char ToRead) -{ - char data = ToRead; - - i2c.write(slaveAddress, &data, 1, 1); - i2c.read(slaveAddress, &data, 1, 0); - return data; -} - -// Writes a single unsigned char (dataToWrite) into regToWrite -int HTS221::writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite) -{ - const char data[] = {regToWrite, dataToWrite}; - - return i2c.write(slaveAddress,data,2,0); -} - - -//jmf end -// ------------------------------------------------------------------------------ - -//static inline int humidityReady(uint8_t data) { -// return (data & 0x02); -//} -//static inline int temperatureReady(uint8_t data) { -// return (data & 0x01); -//} - - -HTS221::HTS221(void) : _address(HTS221_ADDRESS) -{ - _temperature = 0; - _humidity = 0; -} - - -int HTS221::begin(void) -{ - uint8_t data; - - data = readRegister(_address, WHO_AM_I); - if (data == WHO_AM_I_RETURN){ - if (activate()){ - storeCalibration(); - return data; - } - } - - return 0; -} - -int -HTS221::storeCalibration(void) -{ - uint8_t data; - uint16_t tmp; - - for (int reg=CALIB_START; reg<=CALIB_END; reg++) { - if ((reg!=CALIB_START+8) && (reg!=CALIB_START+9) && (reg!=CALIB_START+4)) { - - data = readRegister(HTS221_ADDRESS, reg); - - switch (reg) { - case CALIB_START: - _h0_rH = data; - break; - case CALIB_START+1: - _h1_rH = data; - break; - case CALIB_START+2: - _T0_degC = data; - break; - case CALIB_START+3: - _T1_degC = data; - break; - - case CALIB_START+5: - tmp = _T0_degC; - _T0_degC = (data&0x3)<<8; - _T0_degC |= tmp; - - tmp = _T1_degC; - _T1_degC = ((data&0xC)>>2)<<8; - _T1_degC |= tmp; - break; - case CALIB_START+6: - _H0_T0 = data; - break; - case CALIB_START+7: - _H0_T0 |= data<<8; - break; - case CALIB_START+0xA: - _H1_T0 = data; - break; - case CALIB_START+0xB: - _H1_T0 |= data <<8; - break; - case CALIB_START+0xC: - _T0_OUT = data; - break; - case CALIB_START+0xD: - _T0_OUT |= data << 8; - break; - case CALIB_START+0xE: - _T1_OUT = data; - break; - case CALIB_START+0xF: - _T1_OUT |= data << 8; - break; - - - case CALIB_START+8: - case CALIB_START+9: - case CALIB_START+4: - //DO NOTHING - break; - - // to cover any possible error - default: - return false; - } /* switch */ - } /* if */ - } /* for */ - return true; -} - - -int -HTS221::activate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data |= POWER_UP; - data |= ODR0_SET; - writeRegister(_address, CTRL_REG1, data); - - return true; -} - - -int HTS221::deactivate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data &= ~POWER_UP; - writeRegister(_address, CTRL_REG1, data); - return true; -} - - -int -HTS221::bduActivate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data |= BDU_SET; - writeRegister(_address, CTRL_REG1, data); - - return true; -} - - -int -HTS221::bduDeactivate(void) -{ - uint8_t data; - - data = readRegister(_address, CTRL_REG1); - data &= ~BDU_SET; - writeRegister(_address, CTRL_REG1, data); - return true; -} - - -int -HTS221::readHumidity(void) -{ - uint8_t data = 0; - uint16_t h_out = 0; - double h_temp = 0.0; - double hum = 0.0; - - data = readRegister(_address, STATUS_REG); - - if (data & HUMIDITY_READY) { - data = readRegister(_address, HUMIDITY_H_REG); - h_out = data << 8; // MSB - data = readRegister(_address, HUMIDITY_L_REG); - h_out |= data; // LSB - - // Decode Humidity - hum = ((int16_t)(_h1_rH) - (int16_t)(_h0_rH))/2.0; // remove x2 multiple - - // Calculate humidity in decimal of grade centigrades i.e. 15.0 = 150. - h_temp = (((int16_t)h_out - (int16_t)_H0_T0) * hum) / ((int16_t)_H1_T0 - (int16_t)_H0_T0); - hum = ((int16_t)_h0_rH) / 2.0; // remove x2 multiple - _humidity = (int16_t)((hum + h_temp)); // provide signed % measurement unit - } - return _humidity; -} - - - -double -HTS221::readTemperature(void) -{ - uint8_t data = 0; - uint16_t t_out = 0; - double t_temp = 0.0; - double deg = 0.0; - - data = readRegister(_address, STATUS_REG); - - if (data & TEMPERATURE_READY) { - - data= readRegister(_address, TEMP_H_REG); - t_out = data << 8; // MSB - data = readRegister(_address, TEMP_L_REG); - t_out |= data; // LSB - - // Decode Temperature - deg = ((int16_t)(_T1_degC) - (int16_t)(_T0_degC))/8.0; // remove x8 multiple - - // Calculate Temperature in decimal of grade centigrades i.e. 15.0 = 150. - t_temp = (((int16_t)t_out - (int16_t)_T0_OUT) * deg) / ((int16_t)_T1_OUT - (int16_t)_T0_OUT); - deg = ((int16_t)_T0_degC) / 8.0; // remove x8 multiple - _temperature = deg + t_temp; // provide signed celsius measurement unit - } - - return _temperature; -}
diff -r fe90f1fcb4e0 -r 733edf15f9e8 main.cpp --- a/main.cpp Wed Nov 30 14:50:16 2016 +0000 +++ b/main.cpp Mon Jan 09 18:23:37 2017 +0000 @@ -1,30 +1,14 @@ #include "mbed.h" #include <cctype> #include "SerialBuffered.h" -#include "HTS221.h" #include "config_me.h" -#include "sensors.h" -#include "Proximity.h" #include "Wnc.h" -#include "hardware.h" -#include "att160826.h" - extern SerialBuffered mdm; Wnc wnc; - -I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used -I2C *myi2c; - -#define PROXIMITYON 1 +int seqNum =0; +int msgLen = 10; -#if PROXIMITYON == 1 -att160826_data_t strip_data[2]; -att160826_data_t last_data[2]; -ATT160826 *strips[2]; -#elif PROXIMITYON == 2 -Proximity proximityStrip[2]; -#endif // comment out the following line if color is not supported on the terminal #define USE_COLOR @@ -55,10 +39,6 @@ - - - - int mdm_dbgmask = MDM_DBG_OFF; Serial pc(USBTX, USBRX); @@ -71,16 +51,13 @@ -DigitalIn slot1(PTB3,PullUp); -DigitalIn slot2(PTB10,PullUp); -DigitalIn slot3(PTB11,PullUp); +//DigitalIn slot1(PTB3,PullUp); +//DigitalIn slot2(PTB10,PullUp); +//DigitalIn slot3(PTB11,PullUp); DigitalIn sw2(SW2); -int lastSlot1; -int lastSlot2; -int lastSlot3; -char *iccid; + #define TOUPPER(a) (a) //toupper(a) @@ -90,7 +67,7 @@ #define MAX_AT_RSP_LEN 255 bool toggleLed = false; -int seqNum; + //******************************************************************************************************************************************** @@ -99,277 +76,39 @@ //******************************************************************************************************************************************** void SetLedColor(unsigned char ucColor) { - if(wnc.isPowerSaveOn()) - { - led_red = !0; - led_green = !0; - led_blue = !(ucColor & 0x4); - } - 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 - } + + //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() void system_reset() { - //wnc.passthrough(); printf(RED "\n\rSystem resetting..." DEF "\n"); NVIC_SystemReset(); } - - -#define CTOF(x) ((x)*1.8+32) - -//******************************************************************************************************************************************** -//* Create string with sensor readings that can be sent to flow as an HTTP get -//******************************************************************************************************************************************** -K64F_Sensors_t SENSOR_DATA = -{ - .Temperature = "0", - .Humidity = "0", - .AccelX = "0", - .AccelY = "0", - .AccelZ = "0", - .MagnetometerX = "0", - .MagnetometerY = "0", - .MagnetometerZ = "0", - .AmbientLightVis = "0", - .AmbientLightIr = "0", - .UVindex = "0", - .Proximity = "0", - .Temperature_Si7020 = "0", - .Humidity_Si7020 = "0" -}; - - - -void GenerateModemString(char * modem_string,int sensor,int strip) -{ - switch(sensor) - { -#if PROXIMITYON == 1 - - case PROXIMITY_ONLY: - { - if(strip == -1) - { - seqNum++; - sprintf(modem_string, "GET %s%s?ver=cat1&strip=%d&serial=%s&seq=%d&psm=%d&T=%d&t=%d %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", strip, iccid, seqNum, wnc.isPowerSaveOn(),wnc.gett3412Timer(),wnc.gett3324Timer(),FLOW_URL_TYPE, MY_SERVER_URL); - break; - } - int stripIndex = 0; - if(strip == 3) - stripIndex = 1; - char data[256]; - int i=0; - int index = 0; - index += sprintf(&data[index],"["); - for (i=1; i<SENSORS_PER_STRIP; i++) - { - short p = last_data[stripIndex].sensor[i].sample.prox; - short l = last_data[stripIndex].sensor[i].sample.als_vis; - short r = last_data[stripIndex].sensor[i].sample.als_ir; - if(i<SENSORS_PER_STRIP-1) - index += snprintf(&data[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d},", i,p,l,r); - else - index += snprintf(&data[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d}]", i,p,l,r); - - } - - - seqNum++; - sprintf(modem_string, "GET %s%s?ver=cat1&strip=%d&serial=%s&seq=%d&psm=%d&T=%d&t=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", strip, iccid, seqNum, wnc.isPowerSaveOn(),wnc.gett3412Timer(),wnc.gett3324Timer(), data, FLOW_URL_TYPE, MY_SERVER_URL); - break; - } -#elif PROXIMITYON == 2 - case PROXIMITY_ONLY: - { - - char* data0 = proximityStrip[0].getDataStr(); - char* data1 = proximityStrip[1].getDataStr(); - seqNum++; - sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s&data1=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", iccid, seqNum, data0,data1, FLOW_URL_TYPE, MY_SERVER_URL); - break; - } -#endif - case SWITCH_ONLY: - { - char data[32]; - sprintf(data,"[{\"p\":%d},{\"p\":%d},{\"p\":%d}]",lastSlot1*26,lastSlot2*26,lastSlot3*26); - seqNum++; - sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/car", iccid, seqNum, data, 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); - break; - } - case TEMP_HUMIDITY_ACCELEROMETER: - { - sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, FLOW_URL_TYPE, MY_SERVER_URL); - break; - } - case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS: - { - sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, FLOW_URL_TYPE, MY_SERVER_URL); - break; - } - default: - { - sprintf(modem_string, "Invalid sensor selected\r\n\r\n"); - break; - } - } //switch(iSensorsToReport) -} //GenerateModemString - - -//******************************************************************************************************************************************** -//* Process JSON response messages -//******************************************************************************************************************************************** -bool extract_reply(char* search_field, char* found_string) -{ - char* beginquote; - char* endquote; - beginquote = strstr(search_field, "\r\n\r\n"); //start of data - endquote = strchr(search_field, '\0'); - if (beginquote != 0) - { - uint16_t ifoundlen; - if (endquote != 0) - { - ifoundlen = (uint16_t) (endquote - beginquote) + 1; - strncpy(found_string, beginquote, ifoundlen ); - found_string[ifoundlen] = 0; //null terminate - return true; - } - } - - return false; - -} //extract_reply - - -void parse_reply(char* reply) -{ - char *tokens[5]; - int index = 0; - tokens[index++] = strtok(reply," "); - while( index < 5 ) - { - char* token = strtok(NULL, " "); - if(token == NULL) break; - tokens[index++] = token; - } - if(strcmp("PSM",tokens[1]) == 0) - { - - int t3412 = atoi(tokens[3]); - int t3324 = atoi(tokens[4]); - pc.printf("t3412 %d t3324 %d\r\n",t3412,t3324); - // setTauTimer(t3412); - //setActivityTimer(t3324); - - if(strcmp("true",tokens[2]) == 0) - { - pc.printf("PSM ON\r\n"); - wnc.setPowerSave(true,t3412,t3324); - } - else - { - pc.printf("PSM OFF\r\n"); - wnc.setPowerSave(false,t3412,t3324); - } - } + +void GenerateTestString(char * modem_string,int len) +{ + seqNum++; + char data[len+1]; + for(int i = 0;i<len;i++) + data[i] = 'a'; + data[len] = 0; + sprintf(modem_string, "%s", data); } -bool checkSlots() -{ - bool changed = false; - int s1 = !slot1; - int s2 = !slot2; - int s3 = !slot3; - if(lastSlot1 != s1 || lastSlot2 != s2 ||lastSlot3 != s3) - { - pc.printf("slot 1 %d\r\n",s1); - pc.printf("slot 2 %d\r\n",s2); - pc.printf("slot 3 %d\r\n",s3); - changed = true; - } - lastSlot1 = s1; - lastSlot2 = s2; - lastSlot3 = s3; - return changed; -} -#if PROXIMITYON == 1 -bool scanStrip(int id) -{ - bool stripChanged = false; - ATT160826& strip = *strips[id]; - att160826_data_t *data = &strip_data[id]; - bool ok = strip.scan(data); - - if(ok) - { - // printf("%d\r\n",id); - for (int i = 1; i < SENSORS_PER_STRIP;i++) { // ignore first sensor it is blocked - - /* - if (strip_data[id].sensor[i].present) { - printf(" %3d", strip_data[id].sensor[i].sample.prox); - } else { - printf(" %3s", "---"); - } - */ - if (strip_data[id].sensor[i].present) { - - if (abs(last_data[id].sensor[i].sample.prox - strip_data[id].sensor[i].sample.prox) > 50) - { - stripChanged = true; - last_data[id].sensor[i].sample.prox = strip_data[id].sensor[i].sample.prox; - last_data[id].sensor[i].sample.als_vis = strip_data[id].sensor[i].sample.als_vis; - last_data[id].sensor[i].sample.als_ir = strip_data[id].sensor[i].sample.als_ir; - } - - } - } - // printf("\r\n"); - } - else - printf("scan of strip %d failed",id); - return stripChanged; -} -#endif - int main() { - int i; - - HTS221 hts221; pc.baud(115200); - void hts221_init(void); - // Set LED to RED until init finishes SetLedColor(0x1); - pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r"); - pc.printf(GRN "Initialize the HTS221\n\r"); - - i = hts221.begin(); - if( i ) - pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i); - else - pc.printf(RED "HTS221 NOT DETECTED!!\n\r"); - - printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature())); - printf("Humid is: %02d %%\n\r",hts221.readHumidity()); - + pc.printf(BLU "Hello World\r\n\n\r"); // Initialize the modem printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n"); @@ -378,9 +117,8 @@ pc.printf(RED "Modem initialization failed!" DEF "\n"); system_reset(); } - - - iccid = wnc.getIccid(); + + char* iccid = wnc.getIccid(); printf(GRN "ICCID = %s" DEF "\r\n",iccid); @@ -400,136 +138,25 @@ // Set LED BLUE for partial init SetLedColor(0x4); - wnc.setPowerSave(true,wnc.gett3412Timer(),wnc.gett3324Timer()); - -#if PROXIMITYON == 1 - bool stripChanged[2]; - myi2c = new I2C(PTE25, PTE24); - //myi2c = new I2C(PTC11, PTC10); - myi2c->frequency(100000); - ATT160826 strip0 (myi2c, 0); - ATT160826 strip1 (myi2c, 3); - strips[0] = &strip0; - strips[1] = &strip1; - -#elif PROXIMITYON == 2 - - myi2c = new I2C(PTE25, PTE24); + char modem_string[512]; - myi2c->frequency(100000); - Proximity strip0; - Proximity strip1; - proximityStrip[0] = strip0; - proximityStrip[1] = strip1; - proximityStrip[0].init(myi2c, 0x70); - proximityStrip[1].init(myi2c, 0x73); - proximityStrip[0].on(); - proximityStrip[1].on(); - -#endif - - int count = 5*120; - // Send and receive data perpetually - while(1) { - - wnc.checkPassthrough(); - //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 + while(true) + { toggleLed = !toggleLed; if(toggleLed) SetLedColor(0x2); //green else - SetLedColor(0); //off - -#if PROXIMITYON == 1 - - if(stripChanged[0] == false) - stripChanged[0] = scanStrip(0); - if(stripChanged[1] == false) - stripChanged[1] = scanStrip(1); - -#elif PROXIMITYON == 2 - - printf("scan %d\r\n",count); - bool stripChanged = false; - proximityStrip[0].scan(); - printf("scan0 done %d\r\n",count); - proximityStrip[1].scan(); - printf("scan1 done %d\r\n",count); - stripChanged |= proximityStrip[0].changed(50); - printf("scan0 change %d\r\n",stripChanged); - stripChanged |= proximityStrip[1].changed(50); - printf("scan1 change %d\r\n",stripChanged); - -#else - bool slotsChanged = checkSlots(); -#endif - - char modem_string[512]; - -#if PROXIMITYON == 1 -/* - if(count >= 5*120) - { - stripChanged[0] = true; - stripChanged[1] = true; - } - */ + SetLedColor(0); //off + if(sw2 == 0) - { - stripChanged[0] = true; - stripChanged[1] = true; - } - if(stripChanged[0] || stripChanged[1]) - - -#elif PROXIMITYON == 2 - if(count >= 5*60 || stripChanged ) -#else - if(count >= 5*60 || slotsChanged) -#endif - { - if(wnc.isPowerSaveOn()) - { - wnc.wakeFromPowerSave(); - //wnc.ping("108.244.165.22"); - } - count = 0; - SetLedColor(0x04); //blue - - - -#if PROXIMITYON == 1 - if(stripChanged[0]) - { - GenerateModemString(&modem_string[0],PROXIMITY_ONLY,0); - stripChanged[0] = false; - } - else if (stripChanged[1]) - { - GenerateModemString(&modem_string[0],PROXIMITY_ONLY,3); - stripChanged[1] = false; - } - else - GenerateModemString(&modem_string[0],PROXIMITY_ONLY,-1); -#elif PROXIMITYON == 2 - GenerateModemString(&modem_string[0],PROXIMITY_ONLY,0); -#else - GenerateModemString(&modem_string[0],SWITCH_ONLY,0); -#endif - + { + GenerateTestString(modem_string,msgLen); printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string); - - //if(wnc.connect("108.244.165.22",5005)) // node-red - - + if(wnc.connect(ip,MY_PORT)) // shelf - //if(wnc.connect("52.33.231.251",80)) // car { if(wnc.writeSocket(modem_string)) - { - + { char* mydata; int tries = 8; while(tries > 0) // wait for reply @@ -543,25 +170,11 @@ } if (strlen(mydata) > 0) - { - + { SetLedColor(0x2); // green - //only copy on sucessful send - printf(BLU "Read back : [%s]" DEF "\r\n", mydata); - char datareply[512]; - if (extract_reply(mydata, datareply)) - { - printf(GRN "JSON : %s" DEF "\r\n", datareply); - parse_reply(datareply); - } - SetLedColor(0); // off + sscanf(mydata,"LEN=%d",&msgLen); } - else // no reply reset - { - SetLedColor(0x1); //red - system_reset(); - } } wnc.disconnect(); } @@ -569,11 +182,8 @@ { SetLedColor(0x1); //red system_reset(); - } - if(wnc.isPowerSaveOn()) - wnc.resumePowerSave(); + } } - count++; wait(0.2); } //forever loop }
diff -r fe90f1fcb4e0 -r 733edf15f9e8 sensors.cpp --- a/sensors.cpp Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,375 +0,0 @@ -#include "mbed.h" -#include "sensors.h" - -//I2C for pmod sensors: -#define Si1145_PMOD_I2C_ADDR 0xC0 //this is for 7-bit addr 0x60 for the Si7020 -#define Si7020_PMOD_I2C_ADDR 0x80 //this is for 7-bit addr 0x4 for the Si7020 - -#include "hardware.h" - -#include "FXOS8700CQ.h" -// Storage for the data from the motion sensor -SRAWDATA accel_data; -SRAWDATA magn_data; -//InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F -InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt -bool fxos_int2_triggered = false; -void trigger_fxos_int2(void) -{ - fxos_int2_triggered = true; -} - -/*------------------------------------------------------------------------------ - * Perform I2C single read. - *------------------------------------------------------------------------------*/ -unsigned char I2C_ReadSingleByte(unsigned char ucDeviceAddress) -{ - char rxbuffer [1]; - i2c.read(ucDeviceAddress, rxbuffer, 1 ); - return (unsigned char)rxbuffer[0]; -} //I2C_ReadSingleByte() - -/*------------------------------------------------------------------------------ - * Perform I2C single read from address. - *------------------------------------------------------------------------------*/ -unsigned char I2C_ReadSingleByteFromAddr(unsigned char ucDeviceAddress, unsigned char Addr) -{ - char txbuffer [1]; - char rxbuffer [1]; - txbuffer[0] = (char)Addr; - i2c.write(ucDeviceAddress, txbuffer, 1 ); - i2c.read(ucDeviceAddress, rxbuffer, 1 ); - return (unsigned char)rxbuffer[0]; -} //I2C_ReadSingleByteFromAddr() - -/*------------------------------------------------------------------------------ - * Perform I2C read of more than 1 byte. - *------------------------------------------------------------------------------*/ -int I2C_ReadMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength) -{ - int status; - status = i2c.read(ucDeviceAddress, ucData, ucLength); - return status; -} //I2C_ReadMultipleBytes() - -/*------------------------------------------------------------------------------ - * Perform I2C write of a single byte. - *------------------------------------------------------------------------------*/ -int I2C_WriteSingleByte(unsigned char ucDeviceAddress, unsigned char Data, bool bSendStop) -{ - int status; - char txbuffer [1]; - txbuffer[0] = (char)Data; //data - status = i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop - return status; -} //I2C_WriteSingleByte() - -/*------------------------------------------------------------------------------ - * Perform I2C write of 1 byte to an address. - *------------------------------------------------------------------------------*/ -int I2C_WriteSingleByteToAddr(unsigned char ucDeviceAddress, unsigned char Addr, unsigned char Data, bool bSendStop) -{ - int status; - char txbuffer [2]; - txbuffer[0] = (char)Addr; //address - txbuffer[1] = (char)Data; //data - //status = i2c.write(ucDeviceAddress, txbuffer, 2, false); //stop at end - status = i2c.write(ucDeviceAddress, txbuffer, 2, !bSendStop); //true: do not send stop - return status; -} //I2C_WriteSingleByteToAddr() - -/*------------------------------------------------------------------------------ - * Perform I2C write of more than 1 byte. - *------------------------------------------------------------------------------*/ -int I2C_WriteMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength, bool bSendStop) -{ - int status; - status = i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop - return status; -} //I2C_WriteMultipleBytes() - -bool bSi7020_present = false; -void Init_Si7020(void) -{ - char SN_7020 [8]; - //SN part 1: - I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFA, 0x0F, false); - I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[0], 4); - - //SN part 1: - I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFC, 0xC9, false); - I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[4], 4); - - char Ver_7020 [2]; - //FW version: - I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0x84, 0xB8, false); - I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Ver_7020[0], 2); - - if (SN_7020[4] != 0x14) - { - bSi7020_present = false; - printf("Si7020 sensor not found\n"); - } - else - { - bSi7020_present = true; - printf("Si7020 SN = 0x%02X%02X%02X%02X%02X%02X%02X%02X\n", SN_7020[0], SN_7020[1], SN_7020[2], SN_7020[3], SN_7020[4], SN_7020[5], SN_7020[6], SN_7020[7]); - printf("Si7020 Version# = 0x%02X\n", Ver_7020[0]); - } //bool bSi7020_present = true - -} //Init_Si7020() - -void Read_Si7020(void) -{ - if (bSi7020_present) - { - char Humidity [2]; - char Temperature [2]; - //Command to measure humidity (temperature also gets measured): - I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xF5, false); //no hold, must do dummy read - I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 1); //dummy read, should get an nack until it is done - wait (0.05); //wait for measurement. Can also keep reading until no NACK is received - //I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE5, false); //Hold mod, the device does a clock stretch on the read until it is done (crashes the I2C bus... - I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 2); //read humidity - //printf("Read Si7020 Humidity = 0x%02X%02X\n", Humidity[0], Humidity[1]); - int rh_code = (Humidity[0] << 8) + Humidity[1]; - float fRh = (125.0*rh_code/65536.0) - 6.0; //from datasheet - //printf("Si7020 Humidity = %*.*f %%\n", 4, 2, fRh); //double % sign for escape //printf("%*.*f\n", myFieldWidth, myPrecision, myFloatValue); - sprintf(SENSOR_DATA.Humidity_Si7020, "%0.2f", fRh); - - //Command to read temperature when humidity is already done: - I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE0, false); - I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Temperature[0], 2); //read temperature - //printf("Read Si7020 Temperature = 0x%02X%02X\n", Temperature[0], Temperature[1]); - int temp_code = (Temperature[0] << 8) + Temperature[1]; - float fTemp = (175.72*temp_code/65536.0) - 46.85; //from datasheet in Celcius - //printf("Si7020 Temperature = %*.*f deg C\n", 4, 2, fTemp); - sprintf(SENSOR_DATA.Temperature_Si7020, "%0.2f", fTemp); - } //bool bSi7020_present = true - -} //Read_Si7020() - -/*------------------------------------------------------------------------------ - * The following are aliases so that the Si1145 coding examples can be used as-is. - *------------------------------------------------------------------------------*/ -unsigned char ReadFrom_Si1145_Register(unsigned char reg) //returns byte from I2C Register 'reg' -{ - unsigned char result = I2C_ReadSingleByteFromAddr(Si1145_PMOD_I2C_ADDR, reg); - return (result); -} //ReadFrom_Si1145_Register() - -void WriteTo_Si1145_Register(unsigned char reg, unsigned char value) //writes 'value' into I2C Register reg' -{ - I2C_WriteSingleByteToAddr(Si1145_PMOD_I2C_ADDR, reg, value, true); -} //WriteTo_Si1145_Register() - -#define REG_PARAM_WR 0x17 -#define REG_PARAM_RD 0x2E -#define REG_COMMAND 0x18 -#define REG_RESPONSE 0x20 -#define REG_HW_KEY 0x07 -#define HW_KEY_VAL0 0x17 -#define REG_MEAS_RATE_LSB 0x08 -#define REG_MEAS_RATE_MSB 0x09 -#define REG_PS_LED21 0x0F -#define REG_PS_LED3 0x10 -#define MAX_LED_CURRENT 0xF -#define PARAM_CH_LIST 0x01 -#define REG_ALS_VIS_DATA0 0x22 -#define REG_ALS_VIS_DATA1 0x23 -#define REG_ALS_IR_DATA0 0x24 -#define REG_ALS_IR_DATA1 0x25 -#define REG_PS1_DATA0 0x26 -#define REG_PS1_DATA1 0x27 -#define REG_PS2_DATA0 0x28 -#define REG_PS2_DATA1 0x29 -#define REG_PS3_DATA0 0x2A -#define REG_PS3_DATA1 0x2B -#define REG_UVINDEX0 0x2C -#define REG_UVINDEX1 0x2D -int Si1145_ParamSet(unsigned char address, unsigned char value) //writes 'value' into Parameter 'address' -{ - char txbuffer [3]; - txbuffer[0] = (char)REG_PARAM_WR; //destination - txbuffer[1] = (char)value; - txbuffer[2] = (char)(0xA0 + (address & 0x1F)); - int retval; - //if((retval = _waitUntilSleep(si114x_handle))!=0) return retval; - retval = I2C_WriteMultipleBytes(Si1145_PMOD_I2C_ADDR, &txbuffer[0], 3, true); - if(retval!=0) return retval; - while(1) - { - retval=ReadFrom_Si1145_Register(REG_PARAM_RD); - if (retval==value) break; - } - return (0); -} //Si1145_ParamSet() - -void PsAlsForce(void) //equivalent to WriteTo_Si1145_Register(REG_COMMAND,0x07). This forces PS and ALS measurements -{ - WriteTo_Si1145_Register(REG_COMMAND,0x07); -} //PsAlsForce() - -bool bSi1145_present = false; -void Init_Si1145(void) -{ - unsigned char readbyte; - //Read Si1145 part ID: - readbyte = ReadFrom_Si1145_Register(0x00); - if (readbyte != 0x45) - { - bSi1145_present = false; - printf("Si1145 sensor not found\n"); - } - else - { - bSi1145_present = true; - printf("Si1145 Part ID : 0x%02X\n", readbyte); - //Initialize Si1145 by writing to HW_KEY (I2C Register 0x07 = 0x17) - WriteTo_Si1145_Register(REG_HW_KEY, HW_KEY_VAL0); - - // Initialize LED Current - // I2C Register 0x0F = 0xFF - // I2C Register 0x10 = 0x0F - WriteTo_Si1145_Register(REG_PS_LED21,(MAX_LED_CURRENT<<4) + MAX_LED_CURRENT); - WriteTo_Si1145_Register(REG_PS_LED3, MAX_LED_CURRENT); - - // Parameter 0x01 = 0x37 - //Si1145_ParamSet(PARAM_CH_LIST, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK); - //Si1145_ParamSet(0x01, 0x37); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, some) - Si1145_ParamSet(0x01, 0x7F); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, all but UV. One can only use AUX or UV but here we use AUX because UV does not work...) - // I2C Register 0x18 = 0x0x07 //This is PSALS_FORCE to the Command register => Force a single PS (proximity sensor) and ALS (ambient light sensor) reading - The factory code has this as 0x05 which only does PS... - PsAlsForce(); // can also be written as WriteTo_Si1145_Register(REG_COMMAND,0x07); - WriteTo_Si1145_Register(REG_COMMAND, 0x0F);//command to put it into auto mode - //Set MES_RATE to 0x1000. I.e. the device will automatically wake up every 16 * 256* 31.25 us = 0.128 seconds to measure - WriteTo_Si1145_Register(REG_MEAS_RATE_LSB, 0x00); - WriteTo_Si1145_Register(REG_MEAS_RATE_MSB, 0x10); - } //bSi1145_present = true -} //Init_Si1145() - -void Read_Si1145(void) -{ - if (bSi1145_present) - { - // Once the measurements are completed, here is how to reconstruct them - // Note very carefully that 16-bit registers are in the 'Little Endian' byte order - // It may be more efficient to perform block I2C Reads, but this example shows - // individual reads of registers - - int PS1 = ReadFrom_Si1145_Register(REG_PS1_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS1_DATA1); - int PS2 = ReadFrom_Si1145_Register(REG_PS2_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS2_DATA1); - int PS3 = ReadFrom_Si1145_Register(REG_PS3_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS3_DATA1); - //printf("PS1_Data = %d\n", PS1); - //printf("PS2_Data = %d\n", PS2); - //printf("PS3_Data = %d\n", PS3); - //OBJECT PRESENT? -#if (0) - if(PS1 < 22000){ - //printf("Object Far\n"); - sprintf(SENSOR_DATA.Proximity, "Object Far\0"); - } - else if(PS1 < 24000) - { - //printf("Object in Vicinity\n"); - sprintf(SENSOR_DATA.Proximity, "Object in Vicinity\0"); - } - else if (PS1 < 30000) - { - //printf("Object Near\n"); - sprintf(SENSOR_DATA.Proximity, "Object Near\0"); - } - else - { - //printf("Object Very Near\n"); - sprintf(SENSOR_DATA.Proximity, "Object Very Near\0"); - } -#else - sprintf(SENSOR_DATA.Proximity, "%d\0", PS1); -#endif - - //Force ALS read: - //WriteTo_Si1145_Register(REG_COMMAND, 0x06); - //wait (0.1); - int ALS_VIS = ReadFrom_Si1145_Register(REG_ALS_VIS_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_VIS_DATA1); - int ALS_IR = ReadFrom_Si1145_Register(REG_ALS_IR_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_IR_DATA1); - int UV_INDEX = ReadFrom_Si1145_Register(REG_UVINDEX0) + 256 * ReadFrom_Si1145_Register(REG_UVINDEX1); - //printf("ALS_VIS_Data = %d\n", ALS_VIS); - //printf("ALS_IR_Data = %d\n", ALS_IR); - //printf("UV_INDEX_Data = %d\n", UV_INDEX); - - //printf("Ambient Light Visible Sensor = %d\n", ALS_VIS); - sprintf(SENSOR_DATA.AmbientLightVis, "%d", ALS_VIS); - //printf("Ambient Light Infrared Sensor = %d\n", ALS_IR); - sprintf(SENSOR_DATA.AmbientLightIr, "%d", ALS_IR); - //float fUV_value = (UV_INDEX -50.0)/10000.0; - float fUV_value = (UV_INDEX)/100.0; //this is the aux reading - //printf("UV_Data = %0.2f\n", fUV_value); - sprintf(SENSOR_DATA.UVindex, "%0.2f", fUV_value); - } //bSi1145_present = true -} //Read_Si1145() - -//******************************************************************************************************************************************** -//* Read the FXOS8700CQ - 6-axis combo Sensor Accelerometer and Magnetometer -//******************************************************************************************************************************************** -bool bMotionSensor_present = false; -void init_motion_sensor() -{ - // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus... - // Class instantiation with pin names for the motion sensor on the FRDM-K64F board: - FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1) - int iWhoAmI = fxos.get_whoami(); - printf("FXOS8700CQ WhoAmI = %X\r\n", iWhoAmI); - // Iterrupt for active-low interrupt line from FXOS - // Configured with only one interrupt on INT2 signaling Data-Ready - //fxos_int2.fall(&trigger_fxos_int2); - if (iWhoAmI != 0xC7) - { - bMotionSensor_present = false; - printf("FXOS8700CQ motion sensor not found\n"); - } - else - { - bMotionSensor_present = true; - fxos.enable(); - } -} //init_motion_sensor - -void read_motion_sensor() -{ - // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus... - // Class instantiation with pin names for the motion sensor on the FRDM-K64F board: - FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1) - if (bMotionSensor_present) - { - fxos.enable(); - fxos.get_data(&accel_data, &magn_data); - //printf("Roll=%5d, Pitch=%5d, Yaw=%5d;\r\n", magn_data.x, magn_data.y, magn_data.z); - sprintf(SENSOR_DATA.MagnetometerX, "%5d", magn_data.x); - sprintf(SENSOR_DATA.MagnetometerY, "%5d", magn_data.y); - sprintf(SENSOR_DATA.MagnetometerZ, "%5d", magn_data.z); - - //Try to normalize (/2048) the values so they will match the eCompass output: - float fAccelScaled_x, fAccelScaled_y, fAccelScaled_z; - fAccelScaled_x = (accel_data.x/2048.0); - fAccelScaled_y = (accel_data.y/2048.0); - fAccelScaled_z = (accel_data.z/2048.0); - //printf("Acc: X=%2.3f Y=%2.3f Z=%2.3f;\r\n", fAccelScaled_x, fAccelScaled_y, fAccelScaled_z); - sprintf(SENSOR_DATA.AccelX, "%2.3f", fAccelScaled_x); - sprintf(SENSOR_DATA.AccelY, "%2.3f", fAccelScaled_y); - sprintf(SENSOR_DATA.AccelZ, "%2.3f", fAccelScaled_z); - } //bMotionSensor_present -} //read_motion_sensor - -void sensors_init(void) -{ - Init_Si7020(); - Init_Si1145(); - init_motion_sensor(); -} //sensors_init - -void read_sensors(void) -{ - Read_Si7020(); - Read_Si1145(); - read_motion_sensor(); -} //read_sensors
diff -r fe90f1fcb4e0 -r 733edf15f9e8 sensors.h --- a/sensors.h Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -#ifndef __SENSORS_H_ -#define __SENSORS_H_ - -void sensors_init(void); -void read_sensors(void); - -#define SENSOR_FIELD_LEN_LIMIT 32 -typedef struct -{ - char Temperature[SENSOR_FIELD_LEN_LIMIT]; - char Humidity[SENSOR_FIELD_LEN_LIMIT]; - char AccelX[SENSOR_FIELD_LEN_LIMIT]; - char AccelY[SENSOR_FIELD_LEN_LIMIT]; - char AccelZ[SENSOR_FIELD_LEN_LIMIT]; - char MagnetometerX[SENSOR_FIELD_LEN_LIMIT]; - char MagnetometerY[SENSOR_FIELD_LEN_LIMIT]; - char MagnetometerZ[SENSOR_FIELD_LEN_LIMIT]; - char AmbientLightVis[SENSOR_FIELD_LEN_LIMIT]; - char AmbientLightIr[SENSOR_FIELD_LEN_LIMIT]; - char UVindex[SENSOR_FIELD_LEN_LIMIT]; - char Proximity[SENSOR_FIELD_LEN_LIMIT]; - char Temperature_Si7020[SENSOR_FIELD_LEN_LIMIT]; - char Humidity_Si7020[SENSOR_FIELD_LEN_LIMIT]; -} K64F_Sensors_t ; - -extern K64F_Sensors_t SENSOR_DATA; - -#endif \ No newline at end of file
diff -r fe90f1fcb4e0 -r 733edf15f9e8 sfh7779.lib --- a/sfh7779.lib Wed Nov 30 14:50:16 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/elmkom/code/sfh7779/#5e15a306a518