AT&T IoT hackster.io contest entry: Carpal2

Dependencies:   FXOS8700CQ Pubnub_mbed2_sync WNCInterface mbed-rtos mbed

Committer:
cswiger
Date:
Sat Dec 24 17:31:00 2016 +0000
Revision:
2:fe8e935b9342
Parent:
0:d2425a595807
added Ignition awareness so Carpal2 will resume working after car has been turned off and back on.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cswiger 0:d2425a595807 1 /* ===================================================================
cswiger 0:d2425a595807 2 Copyright © 2016, AVNET Inc.
cswiger 0:d2425a595807 3
cswiger 0:d2425a595807 4 Licensed under the Apache License, Version 2.0 (the "License");
cswiger 0:d2425a595807 5 you may not use this file except in compliance with the License.
cswiger 0:d2425a595807 6 You may obtain a copy of the License at
cswiger 0:d2425a595807 7
cswiger 0:d2425a595807 8 http://www.apache.org/licenses/LICENSE-2.0
cswiger 0:d2425a595807 9
cswiger 0:d2425a595807 10 Unless required by applicable law or agreed to in writing,
cswiger 0:d2425a595807 11 software distributed under the License is distributed on an
cswiger 0:d2425a595807 12 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
cswiger 0:d2425a595807 13 either express or implied. See the License for the specific
cswiger 0:d2425a595807 14 language governing permissions and limitations under the License.
cswiger 0:d2425a595807 15
cswiger 0:d2425a595807 16 ======================================================================== */
cswiger 0:d2425a595807 17
cswiger 0:d2425a595807 18 #include "mbed.h"
cswiger 0:d2425a595807 19 #include "sensors.h" // has anything needed from config_me.h
cswiger 0:d2425a595807 20 #include "hardware.h"
cswiger 0:d2425a595807 21 //#include "config_me.h"
cswiger 0:d2425a595807 22 #include "FXOS8700CQ.h"
cswiger 0:d2425a595807 23 #include "HTS221.h"
cswiger 0:d2425a595807 24 #include "xadow_gps.h"
cswiger 0:d2425a595807 25 #include <string>
cswiger 0:d2425a595807 26
cswiger 0:d2425a595807 27 //I2C for pmod sensors:
cswiger 0:d2425a595807 28 #define Si1145_PMOD_I2C_ADDR 0xC0 //this is for 7-bit addr 0x60 for the Si7020
cswiger 0:d2425a595807 29 #define Si7020_PMOD_I2C_ADDR 0x80 //this is for 7-bit addr 0x4 for the Si7020
cswiger 0:d2425a595807 30
cswiger 0:d2425a595807 31 // Storage for the data from the motion sensor
cswiger 0:d2425a595807 32 SRAWDATA accel_data;
cswiger 0:d2425a595807 33 SRAWDATA magn_data;
cswiger 0:d2425a595807 34 //InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
cswiger 0:d2425a595807 35 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
cswiger 0:d2425a595807 36 bool fxos_int2_triggered = false;
cswiger 0:d2425a595807 37 void trigger_fxos_int2(void)
cswiger 0:d2425a595807 38 {
cswiger 0:d2425a595807 39 fxos_int2_triggered = true;
cswiger 0:d2425a595807 40
cswiger 0:d2425a595807 41 }
cswiger 0:d2425a595807 42
cswiger 0:d2425a595807 43 /*------------------------------------------------------------------------------
cswiger 0:d2425a595807 44 * Perform I2C single read.
cswiger 0:d2425a595807 45 *------------------------------------------------------------------------------*/
cswiger 0:d2425a595807 46 unsigned char I2C_ReadSingleByte(unsigned char ucDeviceAddress)
cswiger 0:d2425a595807 47 {
cswiger 0:d2425a595807 48 char rxbuffer [1];
cswiger 0:d2425a595807 49 i2c.read(ucDeviceAddress, rxbuffer, 1 );
cswiger 0:d2425a595807 50 return (unsigned char)rxbuffer[0];
cswiger 0:d2425a595807 51 } //I2C_ReadSingleByte()
cswiger 0:d2425a595807 52
cswiger 0:d2425a595807 53 /*------------------------------------------------------------------------------
cswiger 0:d2425a595807 54 * Perform I2C single read from address.
cswiger 0:d2425a595807 55 *------------------------------------------------------------------------------*/
cswiger 0:d2425a595807 56 unsigned char I2C_ReadSingleByteFromAddr(unsigned char ucDeviceAddress, unsigned char Addr)
cswiger 0:d2425a595807 57 {
cswiger 0:d2425a595807 58 char txbuffer [1];
cswiger 0:d2425a595807 59 char rxbuffer [1];
cswiger 0:d2425a595807 60 txbuffer[0] = (char)Addr;
cswiger 0:d2425a595807 61 i2c.write(ucDeviceAddress, txbuffer, 1 );
cswiger 0:d2425a595807 62 i2c.read(ucDeviceAddress, rxbuffer, 1 );
cswiger 0:d2425a595807 63 return (unsigned char)rxbuffer[0];
cswiger 0:d2425a595807 64 } //I2C_ReadSingleByteFromAddr()
cswiger 0:d2425a595807 65
cswiger 0:d2425a595807 66 /*------------------------------------------------------------------------------
cswiger 0:d2425a595807 67 * Perform I2C read of more than 1 byte.
cswiger 0:d2425a595807 68 *------------------------------------------------------------------------------*/
cswiger 0:d2425a595807 69 int I2C_ReadMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength)
cswiger 0:d2425a595807 70 {
cswiger 0:d2425a595807 71 int status;
cswiger 0:d2425a595807 72 status = i2c.read(ucDeviceAddress, ucData, ucLength);
cswiger 0:d2425a595807 73 return status;
cswiger 0:d2425a595807 74 } //I2C_ReadMultipleBytes()
cswiger 0:d2425a595807 75
cswiger 0:d2425a595807 76 /*------------------------------------------------------------------------------
cswiger 0:d2425a595807 77 * Perform I2C write of a single byte.
cswiger 0:d2425a595807 78 *------------------------------------------------------------------------------*/
cswiger 0:d2425a595807 79 int I2C_WriteSingleByte(unsigned char ucDeviceAddress, unsigned char Data, bool bSendStop)
cswiger 0:d2425a595807 80 {
cswiger 0:d2425a595807 81 int status;
cswiger 0:d2425a595807 82 char txbuffer [1];
cswiger 0:d2425a595807 83 txbuffer[0] = (char)Data; //data
cswiger 0:d2425a595807 84 status = i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop
cswiger 0:d2425a595807 85 return status;
cswiger 0:d2425a595807 86 } //I2C_WriteSingleByte()
cswiger 0:d2425a595807 87
cswiger 0:d2425a595807 88 /*------------------------------------------------------------------------------
cswiger 0:d2425a595807 89 * Perform I2C write of 1 byte to an address.
cswiger 0:d2425a595807 90 *------------------------------------------------------------------------------*/
cswiger 0:d2425a595807 91 int I2C_WriteSingleByteToAddr(unsigned char ucDeviceAddress, unsigned char Addr, unsigned char Data, bool bSendStop)
cswiger 0:d2425a595807 92 {
cswiger 0:d2425a595807 93 int status;
cswiger 0:d2425a595807 94 char txbuffer [2];
cswiger 0:d2425a595807 95 txbuffer[0] = (char)Addr; //address
cswiger 0:d2425a595807 96 txbuffer[1] = (char)Data; //data
cswiger 0:d2425a595807 97 //status = i2c.write(ucDeviceAddress, txbuffer, 2, false); //stop at end
cswiger 0:d2425a595807 98 status = i2c.write(ucDeviceAddress, txbuffer, 2, !bSendStop); //true: do not send stop
cswiger 0:d2425a595807 99 return status;
cswiger 0:d2425a595807 100 } //I2C_WriteSingleByteToAddr()
cswiger 0:d2425a595807 101
cswiger 0:d2425a595807 102 /*------------------------------------------------------------------------------
cswiger 0:d2425a595807 103 * Perform I2C write of more than 1 byte.
cswiger 0:d2425a595807 104 *------------------------------------------------------------------------------*/
cswiger 0:d2425a595807 105 int I2C_WriteMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength, bool bSendStop)
cswiger 0:d2425a595807 106 {
cswiger 0:d2425a595807 107 int status;
cswiger 0:d2425a595807 108 status = i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop
cswiger 0:d2425a595807 109 return status;
cswiger 0:d2425a595807 110 } //I2C_WriteMultipleBytes()
cswiger 0:d2425a595807 111
cswiger 0:d2425a595807 112 bool bSi7020_present = false;
cswiger 0:d2425a595807 113 void Init_Si7020(void)
cswiger 0:d2425a595807 114 {
cswiger 0:d2425a595807 115 char SN_7020 [8];
cswiger 0:d2425a595807 116 //SN part 1:
cswiger 0:d2425a595807 117 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFA, 0x0F, false);
cswiger 0:d2425a595807 118 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[0], 4);
cswiger 0:d2425a595807 119
cswiger 0:d2425a595807 120 //SN part 1:
cswiger 0:d2425a595807 121 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFC, 0xC9, false);
cswiger 0:d2425a595807 122 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[4], 4);
cswiger 0:d2425a595807 123
cswiger 0:d2425a595807 124 char Ver_7020 [2];
cswiger 0:d2425a595807 125 //FW version:
cswiger 0:d2425a595807 126 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0x84, 0xB8, false);
cswiger 0:d2425a595807 127 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Ver_7020[0], 2);
cswiger 0:d2425a595807 128
cswiger 0:d2425a595807 129 if (SN_7020[4] != 0x14)
cswiger 0:d2425a595807 130 {
cswiger 0:d2425a595807 131 bSi7020_present = false;
cswiger 0:d2425a595807 132 PRINTF("Si7020 sensor not found\r\n");
cswiger 0:d2425a595807 133 }
cswiger 0:d2425a595807 134 else
cswiger 0:d2425a595807 135 {
cswiger 0:d2425a595807 136 bSi7020_present = true;
cswiger 0:d2425a595807 137 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]);
cswiger 0:d2425a595807 138 PRINTF("Si7020 Version# = 0x%02X\n", Ver_7020[0]);
cswiger 0:d2425a595807 139 } //bool bSi7020_present = true
cswiger 0:d2425a595807 140
cswiger 0:d2425a595807 141 } //Init_Si7020()
cswiger 0:d2425a595807 142
cswiger 0:d2425a595807 143 void Read_Si7020(void)
cswiger 0:d2425a595807 144 {
cswiger 0:d2425a595807 145 if (bSi7020_present)
cswiger 0:d2425a595807 146 {
cswiger 0:d2425a595807 147 char Humidity [2];
cswiger 0:d2425a595807 148 char Temperature [2];
cswiger 0:d2425a595807 149 //Command to measure humidity (temperature also gets measured):
cswiger 0:d2425a595807 150 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xF5, false); //no hold, must do dummy read
cswiger 0:d2425a595807 151 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 1); //dummy read, should get an nack until it is done
cswiger 0:d2425a595807 152 wait (0.05); //wait for measurement. Can also keep reading until no NACK is received
cswiger 0:d2425a595807 153 //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...
cswiger 0:d2425a595807 154 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 2); //read humidity
cswiger 0:d2425a595807 155 //PRINTF("Read Si7020 Humidity = 0x%02X%02X\n", Humidity[0], Humidity[1]);
cswiger 0:d2425a595807 156 int rh_code = (Humidity[0] << 8) + Humidity[1];
cswiger 0:d2425a595807 157 float fRh = (125.0*rh_code/65536.0) - 6.0; //from datasheet
cswiger 0:d2425a595807 158 //PRINTF("Si7020 Humidity = %*.*f %%\n", 4, 2, fRh); //double % sign for escape //PRINTF("%*.*f\n", myFieldWidth, myPrecision, myFloatValue);
cswiger 0:d2425a595807 159 sprintf(SENSOR_DATA.Humidity_Si7020, "%0.2f", fRh);
cswiger 0:d2425a595807 160
cswiger 0:d2425a595807 161 //Command to read temperature when humidity is already done:
cswiger 0:d2425a595807 162 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE0, false);
cswiger 0:d2425a595807 163 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Temperature[0], 2); //read temperature
cswiger 0:d2425a595807 164 //PRINTF("Read Si7020 Temperature = 0x%02X%02X\n", Temperature[0], Temperature[1]);
cswiger 0:d2425a595807 165 int temp_code = (Temperature[0] << 8) + Temperature[1];
cswiger 0:d2425a595807 166 float fTemp = (175.72*temp_code/65536.0) - 46.85; //from datasheet in Celcius
cswiger 0:d2425a595807 167 //PRINTF("Si7020 Temperature = %*.*f deg C\n", 4, 2, fTemp);
cswiger 0:d2425a595807 168 sprintf(SENSOR_DATA.Temperature_Si7020, "%0.2f", fTemp);
cswiger 0:d2425a595807 169 } //bool bSi7020_present = true
cswiger 0:d2425a595807 170
cswiger 0:d2425a595807 171 } //Read_Si7020()
cswiger 0:d2425a595807 172
cswiger 0:d2425a595807 173 /*------------------------------------------------------------------------------
cswiger 0:d2425a595807 174 * The following are aliases so that the Si1145 coding examples can be used as-is.
cswiger 0:d2425a595807 175 *------------------------------------------------------------------------------*/
cswiger 0:d2425a595807 176 unsigned char ReadFrom_Si1145_Register(unsigned char reg) //returns byte from I2C Register 'reg'
cswiger 0:d2425a595807 177 {
cswiger 0:d2425a595807 178 unsigned char result = I2C_ReadSingleByteFromAddr(Si1145_PMOD_I2C_ADDR, reg);
cswiger 0:d2425a595807 179 return (result);
cswiger 0:d2425a595807 180 } //ReadFrom_Si1145_Register()
cswiger 0:d2425a595807 181
cswiger 0:d2425a595807 182 void WriteTo_Si1145_Register(unsigned char reg, unsigned char value) //writes 'value' into I2C Register reg'
cswiger 0:d2425a595807 183 {
cswiger 0:d2425a595807 184 I2C_WriteSingleByteToAddr(Si1145_PMOD_I2C_ADDR, reg, value, true);
cswiger 0:d2425a595807 185 } //WriteTo_Si1145_Register()
cswiger 0:d2425a595807 186
cswiger 0:d2425a595807 187 #define REG_PARAM_WR 0x17
cswiger 0:d2425a595807 188 #define REG_PARAM_RD 0x2E
cswiger 0:d2425a595807 189 #define REG_COMMAND 0x18
cswiger 0:d2425a595807 190 #define REG_RESPONSE 0x20
cswiger 0:d2425a595807 191 #define REG_HW_KEY 0x07
cswiger 0:d2425a595807 192 #define HW_KEY_VAL0 0x17
cswiger 0:d2425a595807 193 #define REG_MEAS_RATE_LSB 0x08
cswiger 0:d2425a595807 194 #define REG_MEAS_RATE_MSB 0x09
cswiger 0:d2425a595807 195 #define REG_PS_LED21 0x0F
cswiger 0:d2425a595807 196 #define REG_PS_LED3 0x10
cswiger 0:d2425a595807 197 #define MAX_LED_CURRENT 0xF
cswiger 0:d2425a595807 198 #define PARAM_CH_LIST 0x01
cswiger 0:d2425a595807 199 #define REG_ALS_VIS_DATA0 0x22
cswiger 0:d2425a595807 200 #define REG_ALS_VIS_DATA1 0x23
cswiger 0:d2425a595807 201 #define REG_ALS_IR_DATA0 0x24
cswiger 0:d2425a595807 202 #define REG_ALS_IR_DATA1 0x25
cswiger 0:d2425a595807 203 #define REG_PS1_DATA0 0x26
cswiger 0:d2425a595807 204 #define REG_PS1_DATA1 0x27
cswiger 0:d2425a595807 205 #define REG_PS2_DATA0 0x28
cswiger 0:d2425a595807 206 #define REG_PS2_DATA1 0x29
cswiger 0:d2425a595807 207 #define REG_PS3_DATA0 0x2A
cswiger 0:d2425a595807 208 #define REG_PS3_DATA1 0x2B
cswiger 0:d2425a595807 209 #define REG_UVINDEX0 0x2C
cswiger 0:d2425a595807 210 #define REG_UVINDEX1 0x2D
cswiger 0:d2425a595807 211 int Si1145_ParamSet(unsigned char address, unsigned char value) //writes 'value' into Parameter 'address'
cswiger 0:d2425a595807 212 {
cswiger 0:d2425a595807 213 char txbuffer [3];
cswiger 0:d2425a595807 214 txbuffer[0] = (char)REG_PARAM_WR; //destination
cswiger 0:d2425a595807 215 txbuffer[1] = (char)value;
cswiger 0:d2425a595807 216 txbuffer[2] = (char)(0xA0 + (address & 0x1F));
cswiger 0:d2425a595807 217 int retval;
cswiger 0:d2425a595807 218 //if((retval = _waitUntilSleep(si114x_handle))!=0) return retval;
cswiger 0:d2425a595807 219 retval = I2C_WriteMultipleBytes(Si1145_PMOD_I2C_ADDR, &txbuffer[0], 3, true);
cswiger 0:d2425a595807 220 if(retval!=0) return retval;
cswiger 0:d2425a595807 221 while(1)
cswiger 0:d2425a595807 222 {
cswiger 0:d2425a595807 223 retval=ReadFrom_Si1145_Register(REG_PARAM_RD);
cswiger 0:d2425a595807 224 if (retval==value) break;
cswiger 0:d2425a595807 225 }
cswiger 0:d2425a595807 226 return (0);
cswiger 0:d2425a595807 227 } //Si1145_ParamSet()
cswiger 0:d2425a595807 228
cswiger 0:d2425a595807 229 void PsAlsForce(void) //equivalent to WriteTo_Si1145_Register(REG_COMMAND,0x07). This forces PS and ALS measurements
cswiger 0:d2425a595807 230 {
cswiger 0:d2425a595807 231 WriteTo_Si1145_Register(REG_COMMAND,0x07);
cswiger 0:d2425a595807 232 } //PsAlsForce()
cswiger 0:d2425a595807 233
cswiger 0:d2425a595807 234 bool bSi1145_present = false;
cswiger 0:d2425a595807 235 void Init_Si1145(void)
cswiger 0:d2425a595807 236 {
cswiger 0:d2425a595807 237 unsigned char readbyte;
cswiger 0:d2425a595807 238 //Read Si1145 part ID:
cswiger 0:d2425a595807 239 readbyte = ReadFrom_Si1145_Register(0x00);
cswiger 0:d2425a595807 240 if (readbyte != 0x45)
cswiger 0:d2425a595807 241 {
cswiger 0:d2425a595807 242 bSi1145_present = false;
cswiger 0:d2425a595807 243 PRINTF("Si1145 sensor not found\r\n");
cswiger 0:d2425a595807 244 }
cswiger 0:d2425a595807 245 else
cswiger 0:d2425a595807 246 {
cswiger 0:d2425a595807 247 bSi1145_present = true;
cswiger 0:d2425a595807 248 PRINTF("Si1145 Part ID : 0x%02X\n", readbyte);
cswiger 0:d2425a595807 249 //Initialize Si1145 by writing to HW_KEY (I2C Register 0x07 = 0x17)
cswiger 0:d2425a595807 250 WriteTo_Si1145_Register(REG_HW_KEY, HW_KEY_VAL0);
cswiger 0:d2425a595807 251
cswiger 0:d2425a595807 252 // Initialize LED Current
cswiger 0:d2425a595807 253 // I2C Register 0x0F = 0xFF
cswiger 0:d2425a595807 254 // I2C Register 0x10 = 0x0F
cswiger 0:d2425a595807 255 WriteTo_Si1145_Register(REG_PS_LED21,(MAX_LED_CURRENT<<4) + MAX_LED_CURRENT);
cswiger 0:d2425a595807 256 WriteTo_Si1145_Register(REG_PS_LED3, MAX_LED_CURRENT);
cswiger 0:d2425a595807 257
cswiger 0:d2425a595807 258 // Parameter 0x01 = 0x37
cswiger 0:d2425a595807 259 //Si1145_ParamSet(PARAM_CH_LIST, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
cswiger 0:d2425a595807 260 //Si1145_ParamSet(0x01, 0x37); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, some)
cswiger 0:d2425a595807 261 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...)
cswiger 0:d2425a595807 262 // 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...
cswiger 0:d2425a595807 263 PsAlsForce(); // can also be written as WriteTo_Si1145_Register(REG_COMMAND,0x07);
cswiger 0:d2425a595807 264 WriteTo_Si1145_Register(REG_COMMAND, 0x0F);//command to put it into auto mode
cswiger 0:d2425a595807 265 //Set MES_RATE to 0x1000. I.e. the device will automatically wake up every 16 * 256* 31.25 us = 0.128 seconds to measure
cswiger 0:d2425a595807 266 WriteTo_Si1145_Register(REG_MEAS_RATE_LSB, 0x00);
cswiger 0:d2425a595807 267 WriteTo_Si1145_Register(REG_MEAS_RATE_MSB, 0x10);
cswiger 0:d2425a595807 268 } //bSi1145_present = true
cswiger 0:d2425a595807 269 } //Init_Si1145()
cswiger 0:d2425a595807 270
cswiger 0:d2425a595807 271 void Read_Si1145(void)
cswiger 0:d2425a595807 272 {
cswiger 0:d2425a595807 273 if (bSi1145_present)
cswiger 0:d2425a595807 274 {
cswiger 0:d2425a595807 275 // Once the measurements are completed, here is how to reconstruct them
cswiger 0:d2425a595807 276 // Note very carefully that 16-bit registers are in the 'Little Endian' byte order
cswiger 0:d2425a595807 277 // It may be more efficient to perform block I2C Reads, but this example shows
cswiger 0:d2425a595807 278 // individual reads of registers
cswiger 0:d2425a595807 279
cswiger 0:d2425a595807 280 int PS1 = ReadFrom_Si1145_Register(REG_PS1_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS1_DATA1);
cswiger 0:d2425a595807 281 int PS2 = ReadFrom_Si1145_Register(REG_PS2_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS2_DATA1);
cswiger 0:d2425a595807 282 int PS3 = ReadFrom_Si1145_Register(REG_PS3_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS3_DATA1);
cswiger 0:d2425a595807 283 //PRINTF("PS1_Data = %d\n", PS1);
cswiger 0:d2425a595807 284 //PRINTF("PS2_Data = %d\n", PS2);
cswiger 0:d2425a595807 285 //PRINTF("PS3_Data = %d\n", PS3);
cswiger 0:d2425a595807 286 //OBJECT PRESENT?
cswiger 0:d2425a595807 287 #if (0)
cswiger 0:d2425a595807 288 if(PS1 < 22000){
cswiger 0:d2425a595807 289 //PRINTF("Object Far\n");
cswiger 0:d2425a595807 290 sprintf(SENSOR_DATA.Proximity, "Object Far\0");
cswiger 0:d2425a595807 291 }
cswiger 0:d2425a595807 292 else if(PS1 < 24000)
cswiger 0:d2425a595807 293 {
cswiger 0:d2425a595807 294 //PRINTF("Object in Vicinity\n");
cswiger 0:d2425a595807 295 sprintf(SENSOR_DATA.Proximity, "Object in Vicinity\0");
cswiger 0:d2425a595807 296 }
cswiger 0:d2425a595807 297 else if (PS1 < 30000)
cswiger 0:d2425a595807 298 {
cswiger 0:d2425a595807 299 //PRINTF("Object Near\n");
cswiger 0:d2425a595807 300 sprintf(SENSOR_DATA.Proximity, "Object Near\0");
cswiger 0:d2425a595807 301 }
cswiger 0:d2425a595807 302 else
cswiger 0:d2425a595807 303 {
cswiger 0:d2425a595807 304 //PRINTF("Object Very Near\n");
cswiger 0:d2425a595807 305 sprintf(SENSOR_DATA.Proximity, "Object Very Near\0");
cswiger 0:d2425a595807 306 }
cswiger 0:d2425a595807 307 #else
cswiger 0:d2425a595807 308 sprintf(SENSOR_DATA.Proximity, "%d\0", PS1);
cswiger 0:d2425a595807 309 #endif
cswiger 0:d2425a595807 310
cswiger 0:d2425a595807 311 //Force ALS read:
cswiger 0:d2425a595807 312 //WriteTo_Si1145_Register(REG_COMMAND, 0x06);
cswiger 0:d2425a595807 313 //wait (0.1);
cswiger 0:d2425a595807 314 int ALS_VIS = ReadFrom_Si1145_Register(REG_ALS_VIS_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_VIS_DATA1);
cswiger 0:d2425a595807 315 int ALS_IR = ReadFrom_Si1145_Register(REG_ALS_IR_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_IR_DATA1);
cswiger 0:d2425a595807 316 int UV_INDEX = ReadFrom_Si1145_Register(REG_UVINDEX0) + 256 * ReadFrom_Si1145_Register(REG_UVINDEX1);
cswiger 0:d2425a595807 317 //PRINTF("ALS_VIS_Data = %d\n", ALS_VIS);
cswiger 0:d2425a595807 318 //PRINTF("ALS_IR_Data = %d\n", ALS_IR);
cswiger 0:d2425a595807 319 //PRINTF("UV_INDEX_Data = %d\n", UV_INDEX);
cswiger 0:d2425a595807 320
cswiger 0:d2425a595807 321 //PRINTF("Ambient Light Visible Sensor = %d\n", ALS_VIS);
cswiger 0:d2425a595807 322 sprintf(SENSOR_DATA.AmbientLightVis, "%d", ALS_VIS);
cswiger 0:d2425a595807 323 //PRINTF("Ambient Light Infrared Sensor = %d\n", ALS_IR);
cswiger 0:d2425a595807 324 sprintf(SENSOR_DATA.AmbientLightIr, "%d", ALS_IR);
cswiger 0:d2425a595807 325 //float fUV_value = (UV_INDEX -50.0)/10000.0;
cswiger 0:d2425a595807 326 float fUV_value = (UV_INDEX)/100.0; //this is the aux reading
cswiger 0:d2425a595807 327 //PRINTF("UV_Data = %0.2f\n", fUV_value);
cswiger 0:d2425a595807 328 sprintf(SENSOR_DATA.UVindex, "%0.2f", fUV_value);
cswiger 0:d2425a595807 329 } //bSi1145_present = true
cswiger 0:d2425a595807 330 } //Read_Si1145()
cswiger 0:d2425a595807 331
cswiger 0:d2425a595807 332 //********************************************************************************************************************************************
cswiger 0:d2425a595807 333 //* Read the FXOS8700CQ - 6-axis combo Sensor Accelerometer and Magnetometer
cswiger 0:d2425a595807 334 //********************************************************************************************************************************************
cswiger 0:d2425a595807 335 bool bMotionSensor_present = false;
cswiger 0:d2425a595807 336 void Init_motion_sensor()
cswiger 0:d2425a595807 337 {
cswiger 0:d2425a595807 338 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
cswiger 0:d2425a595807 339 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
cswiger 0:d2425a595807 340 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
cswiger 0:d2425a595807 341 int iWhoAmI = fxos.get_whoami();
cswiger 0:d2425a595807 342
cswiger 0:d2425a595807 343 PRINTF("FXOS8700CQ WhoAmI = %X\r\n", iWhoAmI);
cswiger 0:d2425a595807 344 // Iterrupt for active-low interrupt line from FXOS
cswiger 0:d2425a595807 345 // Configured with only one interrupt on INT2 signaling Data-Ready
cswiger 0:d2425a595807 346 //fxos_int2.fall(&trigger_fxos_int2);
cswiger 0:d2425a595807 347 if (iWhoAmI != 0xC7)
cswiger 0:d2425a595807 348 {
cswiger 0:d2425a595807 349 bMotionSensor_present = false;
cswiger 0:d2425a595807 350 PRINTF("FXOS8700CQ motion sensor not found\r\n");
cswiger 0:d2425a595807 351 }
cswiger 0:d2425a595807 352 else
cswiger 0:d2425a595807 353 {
cswiger 0:d2425a595807 354 bMotionSensor_present = true;
cswiger 0:d2425a595807 355 fxos.enable();
cswiger 0:d2425a595807 356 }
cswiger 0:d2425a595807 357 } //Init_motion_sensor()
cswiger 0:d2425a595807 358
cswiger 0:d2425a595807 359 void Read_motion_sensor()
cswiger 0:d2425a595807 360 {
cswiger 0:d2425a595807 361 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
cswiger 0:d2425a595807 362 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
cswiger 0:d2425a595807 363 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
cswiger 0:d2425a595807 364 if (bMotionSensor_present)
cswiger 0:d2425a595807 365 {
cswiger 0:d2425a595807 366 fxos.enable();
cswiger 0:d2425a595807 367 fxos.get_data(&accel_data, &magn_data);
cswiger 0:d2425a595807 368 //PRINTF("Roll=%5d, Pitch=%5d, Yaw=%5d;\r\n", magn_data.x, magn_data.y, magn_data.z);
cswiger 0:d2425a595807 369 sprintf(SENSOR_DATA.MagnetometerX, "%5d", magn_data.x);
cswiger 0:d2425a595807 370 sprintf(SENSOR_DATA.MagnetometerY, "%5d", magn_data.y);
cswiger 0:d2425a595807 371 sprintf(SENSOR_DATA.MagnetometerZ, "%5d", magn_data.z);
cswiger 0:d2425a595807 372
cswiger 0:d2425a595807 373 //Try to normalize (/2048) the values so they will match the eCompass output:
cswiger 0:d2425a595807 374 float fAccelScaled_x, fAccelScaled_y, fAccelScaled_z;
cswiger 0:d2425a595807 375 fAccelScaled_x = (accel_data.x/2048.0);
cswiger 0:d2425a595807 376 fAccelScaled_y = (accel_data.y/2048.0);
cswiger 0:d2425a595807 377 fAccelScaled_z = (accel_data.z/2048.0);
cswiger 0:d2425a595807 378 //PRINTF("Acc: X=%2.3f Y=%2.3f Z=%2.3f;\r\n", fAccelScaled_x, fAccelScaled_y, fAccelScaled_z);
cswiger 0:d2425a595807 379 sprintf(SENSOR_DATA.AccelX, "%2.3f", fAccelScaled_x);
cswiger 0:d2425a595807 380 sprintf(SENSOR_DATA.AccelY, "%2.3f", fAccelScaled_y);
cswiger 0:d2425a595807 381 sprintf(SENSOR_DATA.AccelZ, "%2.3f", fAccelScaled_z);
cswiger 0:d2425a595807 382 } //bMotionSensor_present
cswiger 0:d2425a595807 383 } //Read_motion_sensor()
cswiger 0:d2425a595807 384
cswiger 0:d2425a595807 385
cswiger 0:d2425a595807 386 //********************************************************************************************************************************************
cswiger 0:d2425a595807 387 //* Read the HTS221 temperature & humidity sensor on the Cellular Shield
cswiger 0:d2425a595807 388 //********************************************************************************************************************************************
cswiger 0:d2425a595807 389 // These are to be built on the fly
cswiger 0:d2425a595807 390 string my_temp;
cswiger 0:d2425a595807 391 string my_humidity;
cswiger 0:d2425a595807 392 HTS221 hts221;
cswiger 0:d2425a595807 393
cswiger 0:d2425a595807 394 #define CTOF(x) ((x)*1.8+32)
cswiger 0:d2425a595807 395 bool bHTS221_present = false;
cswiger 0:d2425a595807 396 void Init_HTS221()
cswiger 0:d2425a595807 397 {
cswiger 0:d2425a595807 398 int i;
cswiger 0:d2425a595807 399 void hts221_init(void);
cswiger 0:d2425a595807 400 i = hts221.begin();
cswiger 0:d2425a595807 401 if (i)
cswiger 0:d2425a595807 402 {
cswiger 0:d2425a595807 403 bHTS221_present = true;
cswiger 0:d2425a595807 404 PRINTF(BLU "HTS221 Detected (0x%02X)\n\r",i);
cswiger 0:d2425a595807 405 PRINTF(" Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
cswiger 0:d2425a595807 406 PRINTF(" Humid is: %02d %%\n\r",hts221.readHumidity());
cswiger 0:d2425a595807 407 }
cswiger 0:d2425a595807 408 else
cswiger 0:d2425a595807 409 {
cswiger 0:d2425a595807 410 bHTS221_present = false;
cswiger 0:d2425a595807 411 PRINTF(RED "HTS221 NOT DETECTED!\n\r");
cswiger 0:d2425a595807 412 }
cswiger 0:d2425a595807 413 } //Init_HTS221()
cswiger 0:d2425a595807 414
cswiger 0:d2425a595807 415 void Read_HTS221()
cswiger 0:d2425a595807 416 {
cswiger 0:d2425a595807 417 if (bHTS221_present)
cswiger 0:d2425a595807 418 {
cswiger 0:d2425a595807 419 sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
cswiger 0:d2425a595807 420 sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
cswiger 0:d2425a595807 421 } //bHTS221_present
cswiger 0:d2425a595807 422 } //Read_HTS221()
cswiger 0:d2425a595807 423
cswiger 0:d2425a595807 424 bool bGPS_present = false;
cswiger 0:d2425a595807 425 void Init_GPS(void)
cswiger 0:d2425a595807 426 {
cswiger 0:d2425a595807 427 char scan_id[GPS_SCAN_SIZE+2]; //The first two bytes are the response length (0x00, 0x04)
cswiger 0:d2425a595807 428 I2C_WriteSingleByte(GPS_DEVICE_ADDR, GPS_SCAN_ID, true); //no hold, must do read
cswiger 0:d2425a595807 429
cswiger 0:d2425a595807 430 unsigned char i;
cswiger 0:d2425a595807 431 for(i=0;i<(GPS_SCAN_SIZE+2);i++)
cswiger 0:d2425a595807 432 {
cswiger 0:d2425a595807 433 scan_id[i] = I2C_ReadSingleByte(GPS_DEVICE_ADDR);
cswiger 0:d2425a595807 434 }
cswiger 0:d2425a595807 435
cswiger 0:d2425a595807 436 if(scan_id[5] != GPS_DEVICE_ID)
cswiger 0:d2425a595807 437 {
cswiger 0:d2425a595807 438 bGPS_present = false;
cswiger 0:d2425a595807 439 PRINTF("Xadow GPS not found\n\r");
cswiger 0:d2425a595807 440 }
cswiger 0:d2425a595807 441 else
cswiger 0:d2425a595807 442 {
cswiger 0:d2425a595807 443 bGPS_present = true;
cswiger 0:d2425a595807 444 PRINTF("Xadow GPS Scan ID response = 0x%02X%02X (length), 0x%02X%02X%02X%02X\r\n", scan_id[0], scan_id[1], scan_id[2], scan_id[3], scan_id[4], scan_id[5]);
cswiger 0:d2425a595807 445 char status = gps_get_status();
cswiger 0:d2425a595807 446 if ((status != 'A') && (iSensorsToReport == TEMP_HUMIDITY_ACCELEROMETER_GPS))
cswiger 0:d2425a595807 447 { //we must wait for GPS to initialize
cswiger 0:d2425a595807 448 PRINTF("Waiting for GPS to become ready... ");
cswiger 0:d2425a595807 449 while (status != 'A')
cswiger 0:d2425a595807 450 {
cswiger 0:d2425a595807 451 wait (5.0);
cswiger 0:d2425a595807 452 status = gps_get_status();
cswiger 0:d2425a595807 453 unsigned char num_satellites = gps_get_sate_in_veiw();
cswiger 0:d2425a595807 454 PRINTF("%c%d", status, num_satellites);
cswiger 0:d2425a595807 455 }
cswiger 0:d2425a595807 456 PRINTF("\r\n");
cswiger 0:d2425a595807 457 } //we must wait for GPS to initialize
cswiger 0:d2425a595807 458 PRINTF("gps_check_online is %d\r\n", gps_check_online());
cswiger 0:d2425a595807 459 unsigned char *data;
cswiger 0:d2425a595807 460 data = gps_get_utc_date_time();
cswiger 0:d2425a595807 461 PRINTF("gps_get_utc_date_time : %d-%d-%d,%d:%d:%d\r\n", data[0], data[1], data[2], data[3], data[4], data[5]);
cswiger 0:d2425a595807 462 PRINTF("gps_get_status : %c ('A' = Valid, 'V' = Invalid)\r\n", gps_get_status());
cswiger 0:d2425a595807 463 PRINTF("gps_get_latitude : %c:%f\r\n", gps_get_ns(), gps_get_latitude());
cswiger 0:d2425a595807 464 PRINTF("gps_get_longitude : %c:%f\r\n", gps_get_ew(), gps_get_longitude());
cswiger 0:d2425a595807 465 PRINTF("gps_get_altitude : %f meters\r\n", gps_get_altitude());
cswiger 0:d2425a595807 466 PRINTF("gps_get_speed : %f knots\r\n", gps_get_speed());
cswiger 0:d2425a595807 467 PRINTF("gps_get_course : %f degrees\r\n", gps_get_course());
cswiger 0:d2425a595807 468 PRINTF("gps_get_position_fix : %c\r\n", gps_get_position_fix());
cswiger 0:d2425a595807 469 PRINTF("gps_get_sate_in_view : %d satellites\r\n", gps_get_sate_in_veiw());
cswiger 0:d2425a595807 470 PRINTF("gps_get_sate_used : %d\r\n", gps_get_sate_used());
cswiger 0:d2425a595807 471 PRINTF("gps_get_mode : %c ('A' = Automatic, 'M' = Manual)\r\n", gps_get_mode());
cswiger 0:d2425a595807 472 PRINTF("gps_get_mode2 : %c ('1' = no fix, '1' = 2D fix, '3' = 3D fix)\r\n", gps_get_mode2());
cswiger 0:d2425a595807 473 } //bool bGPS_present = true
cswiger 0:d2425a595807 474 } //Init_GPS()
cswiger 0:d2425a595807 475
cswiger 0:d2425a595807 476 void Read_GPS()
cswiger 0:d2425a595807 477 {
cswiger 0:d2425a595807 478 unsigned char gps_satellites = 0; //default
cswiger 0:d2425a595807 479 int lat_sign;
cswiger 0:d2425a595807 480 int long_sign;
cswiger 0:d2425a595807 481 if (bGPS_present)
cswiger 0:d2425a595807 482 {
cswiger 0:d2425a595807 483 if ((gps_get_status() == 'A') && (gps_get_mode2() != '1'))
cswiger 0:d2425a595807 484 {
cswiger 0:d2425a595807 485 gps_satellites = gps_get_sate_in_veiw(); //show the number of satellites
cswiger 0:d2425a595807 486 }
cswiger 0:d2425a595807 487 if (gps_get_ns() == 'S')
cswiger 0:d2425a595807 488 {
cswiger 0:d2425a595807 489 lat_sign = -1; //negative number
cswiger 0:d2425a595807 490 }
cswiger 0:d2425a595807 491 else
cswiger 0:d2425a595807 492 {
cswiger 0:d2425a595807 493 lat_sign = 1;
cswiger 0:d2425a595807 494 }
cswiger 0:d2425a595807 495 if (gps_get_ew() == 'W')
cswiger 0:d2425a595807 496 {
cswiger 0:d2425a595807 497 long_sign = -1; //negative number
cswiger 0:d2425a595807 498 }
cswiger 0:d2425a595807 499 else
cswiger 0:d2425a595807 500 {
cswiger 0:d2425a595807 501 long_sign = 1;
cswiger 0:d2425a595807 502 }
cswiger 0:d2425a595807 503 #if (0)
cswiger 0:d2425a595807 504 PRINTF("gps_satellites : %d\r\n", gps_satellites);
cswiger 0:d2425a595807 505 PRINTF("gps_get_latitude : %f\r\n", (lat_sign * gps_get_latitude()));
cswiger 0:d2425a595807 506 PRINTF("gps_get_longitude : %f\r\n", (long_sign * gps_get_longitude()));
cswiger 0:d2425a595807 507 PRINTF("gps_get_altitude : %f meters\r\n", gps_get_altitude());
cswiger 0:d2425a595807 508 PRINTF("gps_get_speed : %f knots\r\n", gps_get_speed());
cswiger 0:d2425a595807 509 PRINTF("gps_get_course : %f degrees\r\n", gps_get_course());
cswiger 0:d2425a595807 510 #endif
cswiger 0:d2425a595807 511 sprintf(SENSOR_DATA.GPS_Satellites, "%d", gps_satellites);
cswiger 0:d2425a595807 512 sprintf(SENSOR_DATA.GPS_Latitude, "%f", (lat_sign * gps_get_latitude()));
cswiger 0:d2425a595807 513 sprintf(SENSOR_DATA.GPS_Longitude, "%f", (long_sign * gps_get_longitude()));
cswiger 0:d2425a595807 514 sprintf(SENSOR_DATA.GPS_Altitude, "%f", gps_get_altitude());
cswiger 0:d2425a595807 515 sprintf(SENSOR_DATA.GPS_Speed, "%f", gps_get_speed());
cswiger 0:d2425a595807 516 sprintf(SENSOR_DATA.GPS_Course, "%f", gps_get_course());
cswiger 0:d2425a595807 517 } //bGPS_present
cswiger 0:d2425a595807 518 } //Read_GPS()
cswiger 0:d2425a595807 519
cswiger 0:d2425a595807 520 #ifdef USE_VIRTUAL_SENSORS
cswiger 0:d2425a595807 521 bool bUsbConnected = false;
cswiger 0:d2425a595807 522 volatile uint8_t usb_uart_rx_buff[256];
cswiger 0:d2425a595807 523 //volatile uint8_t usb_uart_tx_buff[256];
cswiger 0:d2425a595807 524 volatile unsigned char usb_uart_rx_buff_putptr = 0;
cswiger 0:d2425a595807 525 volatile unsigned char usb_uart_rx_buff_getptr = 0;
cswiger 0:d2425a595807 526 //volatile unsigned char usb_uart_tx_buff_putptr = 0;
cswiger 0:d2425a595807 527 //volatile unsigned char usb_uart_tx_buff_getptr = 0;
cswiger 0:d2425a595807 528 char usbhost_rx_string[256];
cswiger 0:d2425a595807 529 unsigned char usbhost_rxstring_index;
cswiger 0:d2425a595807 530 char usbhost_tx_string[256];
cswiger 0:d2425a595807 531
cswiger 0:d2425a595807 532
cswiger 0:d2425a595807 533 float f_sensor1_value = 12.3;
cswiger 0:d2425a595807 534 float f_sensor2_value = 45.6;
cswiger 0:d2425a595807 535 float f_sensor3_value = 78.9;
cswiger 0:d2425a595807 536 float f_sensor4_value = 78.9;
cswiger 0:d2425a595807 537 float f_sensor5_value = 78.9;
cswiger 0:d2425a595807 538 float f_sensor6_value = 78.9;
cswiger 0:d2425a595807 539 float f_sensor7_value = 78.9;
cswiger 0:d2425a595807 540 float f_sensor8_value = 78.9;
cswiger 0:d2425a595807 541 char usb_sensor_string[110];
cswiger 0:d2425a595807 542
cswiger 0:d2425a595807 543
cswiger 0:d2425a595807 544 //********************************************************************************************************************************************
cswiger 0:d2425a595807 545 //* Parse the input sensor data from the USB host
cswiger 0:d2425a595807 546 //********************************************************************************************************************************************
cswiger 0:d2425a595807 547 int parse_usbhost_message()
cswiger 0:d2425a595807 548 {
cswiger 0:d2425a595807 549 //PRINTF("String = %s\n", usbhost_rx_string); //test
cswiger 0:d2425a595807 550 uint8_t length;
cswiger 0:d2425a595807 551 uint8_t x ;
cswiger 0:d2425a595807 552 //It seems that sscanf needs 11 characters to store a 7-character number. There must be some formatting and termination values...
cswiger 0:d2425a595807 553 char Record[8][11]; //There are 8 sensors with up to 7 characters each
cswiger 0:d2425a595807 554 char StringRecord[110]; //There are is a sensor "string" with up to 100 characters in it
cswiger 0:d2425a595807 555
cswiger 0:d2425a595807 556 // Data format is: "S1:1234,S2:5678,S3:1234,S4:5678,S5:1234,S6:5678,S7:5678,S8:5678,S9:abcde...\n"
cswiger 0:d2425a595807 557 int args_assigned = sscanf(usbhost_rx_string, "%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^\n]", Record[0], Record[1], Record[2], Record[3], Record[4], Record[5], Record[6], Record[7], StringRecord);
cswiger 0:d2425a595807 558
cswiger 0:d2425a595807 559 //StringRecord[109] = '\0';
cswiger 0:d2425a595807 560 //PRINTF("Last = %s\n", StringRecord); //test
cswiger 0:d2425a595807 561
cswiger 0:d2425a595807 562 if (args_assigned == 9)
cswiger 0:d2425a595807 563 { //sscanf was able to assign all 9 values
cswiger 0:d2425a595807 564 for (x=0; x < 8; x++) // loop through the 8 sensors
cswiger 0:d2425a595807 565 {
cswiger 0:d2425a595807 566 // Strip the "Sx:" label characters from the field value
cswiger 0:d2425a595807 567 length = strlen(Record[x]); // max of 7 characters but could be less
cswiger 0:d2425a595807 568 strncpy(Record[x], Record[x] + 3, length);
cswiger 0:d2425a595807 569 Record[x][length] = '\0'; // null termination character manually added
cswiger 0:d2425a595807 570 }
cswiger 0:d2425a595807 571 length = strlen(StringRecord);
cswiger 0:d2425a595807 572 strncpy(StringRecord, StringRecord + 3, length);
cswiger 0:d2425a595807 573 StringRecord[length] = '\0'; // null termination character manually added
cswiger 0:d2425a595807 574
cswiger 0:d2425a595807 575 if ((usbhost_rx_string[0] == 'S') && (usbhost_rx_string[1] == '1')) //The message starts with "S1"
cswiger 0:d2425a595807 576 {
cswiger 0:d2425a595807 577 f_sensor1_value = atof(Record[0]);
cswiger 0:d2425a595807 578 f_sensor2_value = atof(Record[1]);
cswiger 0:d2425a595807 579 f_sensor3_value = atof(Record[2]);
cswiger 0:d2425a595807 580 f_sensor4_value = atof(Record[3]);
cswiger 0:d2425a595807 581 f_sensor5_value = atof(Record[4]);
cswiger 0:d2425a595807 582 f_sensor6_value = atof(Record[5]);
cswiger 0:d2425a595807 583 f_sensor7_value = atof(Record[6]);
cswiger 0:d2425a595807 584 f_sensor8_value = atof(Record[7]);
cswiger 0:d2425a595807 585 sprintf(usb_sensor_string,StringRecord);
cswiger 0:d2425a595807 586 //PRINTF("Received = %s, %s, %s, %s, %s, %s, %s, %s, %s\n", Record[0], Record[1], Record[2], Record[3], Record[4], Record[5], Record[6], Record[7], usb_sensor_string); //test
cswiger 0:d2425a595807 587 sprintf(SENSOR_DATA.Virtual_Sensor1, "%s", Record[0]);
cswiger 0:d2425a595807 588 sprintf(SENSOR_DATA.Virtual_Sensor2, "%s", Record[1]);
cswiger 0:d2425a595807 589 sprintf(SENSOR_DATA.Virtual_Sensor3, "%s", Record[2]);
cswiger 0:d2425a595807 590 sprintf(SENSOR_DATA.Virtual_Sensor4, "%s", Record[3]);
cswiger 0:d2425a595807 591 sprintf(SENSOR_DATA.Virtual_Sensor5, "%s", Record[4]);
cswiger 0:d2425a595807 592 sprintf(SENSOR_DATA.Virtual_Sensor6, "%s", Record[5]);
cswiger 0:d2425a595807 593 sprintf(SENSOR_DATA.Virtual_Sensor7, "%s", Record[6]);
cswiger 0:d2425a595807 594 sprintf(SENSOR_DATA.Virtual_Sensor8, "%s", Record[7]);
cswiger 0:d2425a595807 595 }
cswiger 0:d2425a595807 596 } //sscanf was able to assign all values
cswiger 0:d2425a595807 597 return args_assigned;
cswiger 0:d2425a595807 598 } //parse_usbhost_message()
cswiger 0:d2425a595807 599
cswiger 0:d2425a595807 600 //********************************************************************************************************************************************
cswiger 0:d2425a595807 601 //* Process any received message from the USB host
cswiger 0:d2425a595807 602 //********************************************************************************************************************************************
cswiger 0:d2425a595807 603 void process_usb_rx(unsigned char ucNewRxByte)
cswiger 0:d2425a595807 604 {
cswiger 0:d2425a595807 605 if (ucNewRxByte == '?')
cswiger 0:d2425a595807 606 { //just pinging
cswiger 0:d2425a595807 607 usbhost_rxstring_index = 0;
cswiger 0:d2425a595807 608 return;
cswiger 0:d2425a595807 609 } //just pinging
cswiger 0:d2425a595807 610 usbhost_rx_string[usbhost_rxstring_index++] = ucNewRxByte;
cswiger 0:d2425a595807 611 if (ucNewRxByte == '\n')
cswiger 0:d2425a595807 612 { //end of message
cswiger 0:d2425a595807 613 usbhost_rx_string[usbhost_rxstring_index] = 0; //null terminate string
cswiger 0:d2425a595807 614 usbhost_rxstring_index = 0;
cswiger 0:d2425a595807 615 parse_usbhost_message();
cswiger 0:d2425a595807 616 } //end of message
cswiger 0:d2425a595807 617 } //process_usb_rx()
cswiger 0:d2425a595807 618
cswiger 0:d2425a595807 619 void ProcessUsbInterface(void)
cswiger 0:d2425a595807 620 {
cswiger 0:d2425a595807 621 //Process the USB host UART receive commands:
cswiger 0:d2425a595807 622 if (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
cswiger 0:d2425a595807 623 {
cswiger 0:d2425a595807 624 bUsbConnected = true;
cswiger 0:d2425a595807 625 while (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
cswiger 0:d2425a595807 626 {
cswiger 0:d2425a595807 627 unsigned char ucByteFromHost = usb_uart_rx_buff[usb_uart_rx_buff_getptr++]; //Copy latest received byte
cswiger 0:d2425a595807 628 process_usb_rx(ucByteFromHost);
cswiger 0:d2425a595807 629 } //while (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
cswiger 0:d2425a595807 630 } // if there are USB UART bytes to receive
cswiger 0:d2425a595807 631 //USB host UART transmit:
cswiger 0:d2425a595807 632 //while (usb_uart_tx_buff_getptr != usb_uart_tx_buff_putptr)
cswiger 0:d2425a595807 633 //{
cswiger 0:d2425a595807 634 //pc.putc(usb_uart_tx_buff[usb_uart_tx_buff_getptr++]);
cswiger 0:d2425a595807 635 //}
cswiger 0:d2425a595807 636 } //ProcessUsbInterface()
cswiger 0:d2425a595807 637
cswiger 0:d2425a595807 638 // This function is called when a character goes into the RX buffer.
cswiger 0:d2425a595807 639 void UsbUartRxCallback(MODSERIAL_IRQ_INFO *info)
cswiger 0:d2425a595807 640 {
cswiger 0:d2425a595807 641 // Get the pointer to our MODSERIAL object that invoked this callback.
cswiger 0:d2425a595807 642 MODSERIAL *pc = info->serial;
cswiger 0:d2425a595807 643 while (pc->readable())
cswiger 0:d2425a595807 644 {
cswiger 0:d2425a595807 645 usb_uart_rx_buff[usb_uart_rx_buff_putptr++] = pc->getcNb();
cswiger 0:d2425a595807 646 }
cswiger 0:d2425a595807 647 }
cswiger 0:d2425a595807 648 #endif
cswiger 0:d2425a595807 649
cswiger 0:d2425a595807 650 void sensors_init(void)
cswiger 0:d2425a595807 651 {
cswiger 0:d2425a595807 652 #ifdef USE_VIRTUAL_SENSORS
cswiger 0:d2425a595807 653 pc.attach(&UsbUartRxCallback, MODSERIAL::RxIrq);
cswiger 0:d2425a595807 654 #endif
cswiger 0:d2425a595807 655 Init_HTS221();
cswiger 0:d2425a595807 656 Init_Si7020();
cswiger 0:d2425a595807 657 Init_Si1145();
cswiger 0:d2425a595807 658 Init_motion_sensor();
cswiger 0:d2425a595807 659 Init_GPS();
cswiger 0:d2425a595807 660 } //sensors_init
cswiger 0:d2425a595807 661
cswiger 0:d2425a595807 662 void read_sensors(void)
cswiger 0:d2425a595807 663 {
cswiger 0:d2425a595807 664 Read_HTS221();
cswiger 0:d2425a595807 665 Read_Si7020();
cswiger 0:d2425a595807 666 Read_Si1145();
cswiger 0:d2425a595807 667 Read_motion_sensor();
cswiger 0:d2425a595807 668 Read_GPS();
cswiger 0:d2425a595807 669 } //read_sensors