publish final code

Dependencies:   FXOS8700CQ Pubnub_mbed2_sync2 SoftSerial TinyGPSplus WNCInterface2 mbed-rtos mbed

Committer:
cswiger
Date:
Tue Feb 21 22:28:26 2017 +0000
Revision:
0:1092494506a3
publish final code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cswiger 0:1092494506a3 1 /* ===================================================================
cswiger 0:1092494506a3 2 Copyright © 2016, AVNET Inc.
cswiger 0:1092494506a3 3
cswiger 0:1092494506a3 4 Licensed under the Apache License, Version 2.0 (the "License");
cswiger 0:1092494506a3 5 you may not use this file except in compliance with the License.
cswiger 0:1092494506a3 6 You may obtain a copy of the License at
cswiger 0:1092494506a3 7
cswiger 0:1092494506a3 8 http://www.apache.org/licenses/LICENSE-2.0
cswiger 0:1092494506a3 9
cswiger 0:1092494506a3 10 Unless required by applicable law or agreed to in writing,
cswiger 0:1092494506a3 11 software distributed under the License is distributed on an
cswiger 0:1092494506a3 12 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
cswiger 0:1092494506a3 13 either express or implied. See the License for the specific
cswiger 0:1092494506a3 14 language governing permissions and limitations under the License.
cswiger 0:1092494506a3 15
cswiger 0:1092494506a3 16 ======================================================================== */
cswiger 0:1092494506a3 17
cswiger 0:1092494506a3 18 #include "mbed.h"
cswiger 0:1092494506a3 19 #include "sensors.h" // has anything needed from config_me.h
cswiger 0:1092494506a3 20 #include "hardware.h"
cswiger 0:1092494506a3 21 //#include "config_me.h"
cswiger 0:1092494506a3 22 #include "FXOS8700CQ.h"
cswiger 0:1092494506a3 23 #include "HTS221.h"
cswiger 0:1092494506a3 24 #include <string>
cswiger 0:1092494506a3 25
cswiger 0:1092494506a3 26 //I2C for pmod sensors:
cswiger 0:1092494506a3 27 #define Si1145_PMOD_I2C_ADDR 0xC0 //this is for 7-bit addr 0x60 for the Si7020
cswiger 0:1092494506a3 28 #define Si7020_PMOD_I2C_ADDR 0x80 //this is for 7-bit addr 0x4 for the Si7020
cswiger 0:1092494506a3 29
cswiger 0:1092494506a3 30 // Storage for the data from the motion sensor
cswiger 0:1092494506a3 31 SRAWDATA accel_data;
cswiger 0:1092494506a3 32 SRAWDATA magn_data;
cswiger 0:1092494506a3 33 //InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
cswiger 0:1092494506a3 34 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
cswiger 0:1092494506a3 35 bool fxos_int2_triggered = false;
cswiger 0:1092494506a3 36 void trigger_fxos_int2(void)
cswiger 0:1092494506a3 37 {
cswiger 0:1092494506a3 38 fxos_int2_triggered = true;
cswiger 0:1092494506a3 39
cswiger 0:1092494506a3 40 }
cswiger 0:1092494506a3 41
cswiger 0:1092494506a3 42 /*------------------------------------------------------------------------------
cswiger 0:1092494506a3 43 * Perform I2C single read.
cswiger 0:1092494506a3 44 *------------------------------------------------------------------------------*/
cswiger 0:1092494506a3 45 unsigned char I2C_ReadSingleByte(unsigned char ucDeviceAddress)
cswiger 0:1092494506a3 46 {
cswiger 0:1092494506a3 47 char rxbuffer [1];
cswiger 0:1092494506a3 48 i2c.read(ucDeviceAddress, rxbuffer, 1 );
cswiger 0:1092494506a3 49 return (unsigned char)rxbuffer[0];
cswiger 0:1092494506a3 50 } //I2C_ReadSingleByte()
cswiger 0:1092494506a3 51
cswiger 0:1092494506a3 52 /*------------------------------------------------------------------------------
cswiger 0:1092494506a3 53 * Perform I2C single read from address.
cswiger 0:1092494506a3 54 *------------------------------------------------------------------------------*/
cswiger 0:1092494506a3 55 unsigned char I2C_ReadSingleByteFromAddr(unsigned char ucDeviceAddress, unsigned char Addr)
cswiger 0:1092494506a3 56 {
cswiger 0:1092494506a3 57 char txbuffer [1];
cswiger 0:1092494506a3 58 char rxbuffer [1];
cswiger 0:1092494506a3 59 txbuffer[0] = (char)Addr;
cswiger 0:1092494506a3 60 i2c.write(ucDeviceAddress, txbuffer, 1 );
cswiger 0:1092494506a3 61 i2c.read(ucDeviceAddress, rxbuffer, 1 );
cswiger 0:1092494506a3 62 return (unsigned char)rxbuffer[0];
cswiger 0:1092494506a3 63 } //I2C_ReadSingleByteFromAddr()
cswiger 0:1092494506a3 64
cswiger 0:1092494506a3 65 /*------------------------------------------------------------------------------
cswiger 0:1092494506a3 66 * Perform I2C read of more than 1 byte.
cswiger 0:1092494506a3 67 *------------------------------------------------------------------------------*/
cswiger 0:1092494506a3 68 int I2C_ReadMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength)
cswiger 0:1092494506a3 69 {
cswiger 0:1092494506a3 70 int status;
cswiger 0:1092494506a3 71 status = i2c.read(ucDeviceAddress, ucData, ucLength);
cswiger 0:1092494506a3 72 return status;
cswiger 0:1092494506a3 73 } //I2C_ReadMultipleBytes()
cswiger 0:1092494506a3 74
cswiger 0:1092494506a3 75 /*------------------------------------------------------------------------------
cswiger 0:1092494506a3 76 * Perform I2C write of a single byte.
cswiger 0:1092494506a3 77 *------------------------------------------------------------------------------*/
cswiger 0:1092494506a3 78 int I2C_WriteSingleByte(unsigned char ucDeviceAddress, unsigned char Data, bool bSendStop)
cswiger 0:1092494506a3 79 {
cswiger 0:1092494506a3 80 int status;
cswiger 0:1092494506a3 81 char txbuffer [1];
cswiger 0:1092494506a3 82 txbuffer[0] = (char)Data; //data
cswiger 0:1092494506a3 83 status = i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop
cswiger 0:1092494506a3 84 return status;
cswiger 0:1092494506a3 85 } //I2C_WriteSingleByte()
cswiger 0:1092494506a3 86
cswiger 0:1092494506a3 87 /*------------------------------------------------------------------------------
cswiger 0:1092494506a3 88 * Perform I2C write of 1 byte to an address.
cswiger 0:1092494506a3 89 *------------------------------------------------------------------------------*/
cswiger 0:1092494506a3 90 int I2C_WriteSingleByteToAddr(unsigned char ucDeviceAddress, unsigned char Addr, unsigned char Data, bool bSendStop)
cswiger 0:1092494506a3 91 {
cswiger 0:1092494506a3 92 int status;
cswiger 0:1092494506a3 93 char txbuffer [2];
cswiger 0:1092494506a3 94 txbuffer[0] = (char)Addr; //address
cswiger 0:1092494506a3 95 txbuffer[1] = (char)Data; //data
cswiger 0:1092494506a3 96 //status = i2c.write(ucDeviceAddress, txbuffer, 2, false); //stop at end
cswiger 0:1092494506a3 97 status = i2c.write(ucDeviceAddress, txbuffer, 2, !bSendStop); //true: do not send stop
cswiger 0:1092494506a3 98 return status;
cswiger 0:1092494506a3 99 } //I2C_WriteSingleByteToAddr()
cswiger 0:1092494506a3 100
cswiger 0:1092494506a3 101 /*------------------------------------------------------------------------------
cswiger 0:1092494506a3 102 * Perform I2C write of more than 1 byte.
cswiger 0:1092494506a3 103 *------------------------------------------------------------------------------*/
cswiger 0:1092494506a3 104 int I2C_WriteMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength, bool bSendStop)
cswiger 0:1092494506a3 105 {
cswiger 0:1092494506a3 106 int status;
cswiger 0:1092494506a3 107 status = i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop
cswiger 0:1092494506a3 108 return status;
cswiger 0:1092494506a3 109 } //I2C_WriteMultipleBytes()
cswiger 0:1092494506a3 110
cswiger 0:1092494506a3 111 bool bSi7020_present = false;
cswiger 0:1092494506a3 112 void Init_Si7020(void)
cswiger 0:1092494506a3 113 {
cswiger 0:1092494506a3 114 char SN_7020 [8];
cswiger 0:1092494506a3 115 //SN part 1:
cswiger 0:1092494506a3 116 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFA, 0x0F, false);
cswiger 0:1092494506a3 117 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[0], 4);
cswiger 0:1092494506a3 118
cswiger 0:1092494506a3 119 //SN part 1:
cswiger 0:1092494506a3 120 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFC, 0xC9, false);
cswiger 0:1092494506a3 121 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[4], 4);
cswiger 0:1092494506a3 122
cswiger 0:1092494506a3 123 char Ver_7020 [2];
cswiger 0:1092494506a3 124 //FW version:
cswiger 0:1092494506a3 125 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0x84, 0xB8, false);
cswiger 0:1092494506a3 126 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Ver_7020[0], 2);
cswiger 0:1092494506a3 127
cswiger 0:1092494506a3 128 if (SN_7020[4] != 0x14)
cswiger 0:1092494506a3 129 {
cswiger 0:1092494506a3 130 bSi7020_present = false;
cswiger 0:1092494506a3 131 PRINTF("Si7020 sensor not found\r\n");
cswiger 0:1092494506a3 132 }
cswiger 0:1092494506a3 133 else
cswiger 0:1092494506a3 134 {
cswiger 0:1092494506a3 135 bSi7020_present = true;
cswiger 0:1092494506a3 136 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:1092494506a3 137 PRINTF("Si7020 Version# = 0x%02X\n", Ver_7020[0]);
cswiger 0:1092494506a3 138 } //bool bSi7020_present = true
cswiger 0:1092494506a3 139
cswiger 0:1092494506a3 140 } //Init_Si7020()
cswiger 0:1092494506a3 141
cswiger 0:1092494506a3 142 void Read_Si7020(void)
cswiger 0:1092494506a3 143 {
cswiger 0:1092494506a3 144 if (bSi7020_present)
cswiger 0:1092494506a3 145 {
cswiger 0:1092494506a3 146 char Humidity [2];
cswiger 0:1092494506a3 147 char Temperature [2];
cswiger 0:1092494506a3 148 //Command to measure humidity (temperature also gets measured):
cswiger 0:1092494506a3 149 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xF5, false); //no hold, must do dummy read
cswiger 0:1092494506a3 150 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 1); //dummy read, should get an nack until it is done
cswiger 0:1092494506a3 151 wait (0.05); //wait for measurement. Can also keep reading until no NACK is received
cswiger 0:1092494506a3 152 //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:1092494506a3 153 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 2); //read humidity
cswiger 0:1092494506a3 154 //PRINTF("Read Si7020 Humidity = 0x%02X%02X\n", Humidity[0], Humidity[1]);
cswiger 0:1092494506a3 155 int rh_code = (Humidity[0] << 8) + Humidity[1];
cswiger 0:1092494506a3 156 float fRh = (125.0*rh_code/65536.0) - 6.0; //from datasheet
cswiger 0:1092494506a3 157 //PRINTF("Si7020 Humidity = %*.*f %%\n", 4, 2, fRh); //double % sign for escape //PRINTF("%*.*f\n", myFieldWidth, myPrecision, myFloatValue);
cswiger 0:1092494506a3 158 sprintf(SENSOR_DATA.Humidity_Si7020, "%0.2f", fRh);
cswiger 0:1092494506a3 159
cswiger 0:1092494506a3 160 //Command to read temperature when humidity is already done:
cswiger 0:1092494506a3 161 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE0, false);
cswiger 0:1092494506a3 162 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Temperature[0], 2); //read temperature
cswiger 0:1092494506a3 163 //PRINTF("Read Si7020 Temperature = 0x%02X%02X\n", Temperature[0], Temperature[1]);
cswiger 0:1092494506a3 164 int temp_code = (Temperature[0] << 8) + Temperature[1];
cswiger 0:1092494506a3 165 float fTemp = (175.72*temp_code/65536.0) - 46.85; //from datasheet in Celcius
cswiger 0:1092494506a3 166 //PRINTF("Si7020 Temperature = %*.*f deg C\n", 4, 2, fTemp);
cswiger 0:1092494506a3 167 sprintf(SENSOR_DATA.Temperature_Si7020, "%0.2f", fTemp);
cswiger 0:1092494506a3 168 } //bool bSi7020_present = true
cswiger 0:1092494506a3 169
cswiger 0:1092494506a3 170 } //Read_Si7020()
cswiger 0:1092494506a3 171
cswiger 0:1092494506a3 172 /*------------------------------------------------------------------------------
cswiger 0:1092494506a3 173 * The following are aliases so that the Si1145 coding examples can be used as-is.
cswiger 0:1092494506a3 174 *------------------------------------------------------------------------------*/
cswiger 0:1092494506a3 175 unsigned char ReadFrom_Si1145_Register(unsigned char reg) //returns byte from I2C Register 'reg'
cswiger 0:1092494506a3 176 {
cswiger 0:1092494506a3 177 unsigned char result = I2C_ReadSingleByteFromAddr(Si1145_PMOD_I2C_ADDR, reg);
cswiger 0:1092494506a3 178 return (result);
cswiger 0:1092494506a3 179 } //ReadFrom_Si1145_Register()
cswiger 0:1092494506a3 180
cswiger 0:1092494506a3 181 void WriteTo_Si1145_Register(unsigned char reg, unsigned char value) //writes 'value' into I2C Register reg'
cswiger 0:1092494506a3 182 {
cswiger 0:1092494506a3 183 I2C_WriteSingleByteToAddr(Si1145_PMOD_I2C_ADDR, reg, value, true);
cswiger 0:1092494506a3 184 } //WriteTo_Si1145_Register()
cswiger 0:1092494506a3 185
cswiger 0:1092494506a3 186 #define REG_PARAM_WR 0x17
cswiger 0:1092494506a3 187 #define REG_PARAM_RD 0x2E
cswiger 0:1092494506a3 188 #define REG_COMMAND 0x18
cswiger 0:1092494506a3 189 #define REG_RESPONSE 0x20
cswiger 0:1092494506a3 190 #define REG_HW_KEY 0x07
cswiger 0:1092494506a3 191 #define HW_KEY_VAL0 0x17
cswiger 0:1092494506a3 192 #define REG_MEAS_RATE_LSB 0x08
cswiger 0:1092494506a3 193 #define REG_MEAS_RATE_MSB 0x09
cswiger 0:1092494506a3 194 #define REG_PS_LED21 0x0F
cswiger 0:1092494506a3 195 #define REG_PS_LED3 0x10
cswiger 0:1092494506a3 196 #define MAX_LED_CURRENT 0xF
cswiger 0:1092494506a3 197 #define PARAM_CH_LIST 0x01
cswiger 0:1092494506a3 198 #define REG_ALS_VIS_DATA0 0x22
cswiger 0:1092494506a3 199 #define REG_ALS_VIS_DATA1 0x23
cswiger 0:1092494506a3 200 #define REG_ALS_IR_DATA0 0x24
cswiger 0:1092494506a3 201 #define REG_ALS_IR_DATA1 0x25
cswiger 0:1092494506a3 202 #define REG_PS1_DATA0 0x26
cswiger 0:1092494506a3 203 #define REG_PS1_DATA1 0x27
cswiger 0:1092494506a3 204 #define REG_PS2_DATA0 0x28
cswiger 0:1092494506a3 205 #define REG_PS2_DATA1 0x29
cswiger 0:1092494506a3 206 #define REG_PS3_DATA0 0x2A
cswiger 0:1092494506a3 207 #define REG_PS3_DATA1 0x2B
cswiger 0:1092494506a3 208 #define REG_UVINDEX0 0x2C
cswiger 0:1092494506a3 209 #define REG_UVINDEX1 0x2D
cswiger 0:1092494506a3 210 int Si1145_ParamSet(unsigned char address, unsigned char value) //writes 'value' into Parameter 'address'
cswiger 0:1092494506a3 211 {
cswiger 0:1092494506a3 212 char txbuffer [3];
cswiger 0:1092494506a3 213 txbuffer[0] = (char)REG_PARAM_WR; //destination
cswiger 0:1092494506a3 214 txbuffer[1] = (char)value;
cswiger 0:1092494506a3 215 txbuffer[2] = (char)(0xA0 + (address & 0x1F));
cswiger 0:1092494506a3 216 int retval;
cswiger 0:1092494506a3 217 //if((retval = _waitUntilSleep(si114x_handle))!=0) return retval;
cswiger 0:1092494506a3 218 retval = I2C_WriteMultipleBytes(Si1145_PMOD_I2C_ADDR, &txbuffer[0], 3, true);
cswiger 0:1092494506a3 219 if(retval!=0) return retval;
cswiger 0:1092494506a3 220 while(1)
cswiger 0:1092494506a3 221 {
cswiger 0:1092494506a3 222 retval=ReadFrom_Si1145_Register(REG_PARAM_RD);
cswiger 0:1092494506a3 223 if (retval==value) break;
cswiger 0:1092494506a3 224 }
cswiger 0:1092494506a3 225 return (0);
cswiger 0:1092494506a3 226 } //Si1145_ParamSet()
cswiger 0:1092494506a3 227
cswiger 0:1092494506a3 228 void PsAlsForce(void) //equivalent to WriteTo_Si1145_Register(REG_COMMAND,0x07). This forces PS and ALS measurements
cswiger 0:1092494506a3 229 {
cswiger 0:1092494506a3 230 WriteTo_Si1145_Register(REG_COMMAND,0x07);
cswiger 0:1092494506a3 231 } //PsAlsForce()
cswiger 0:1092494506a3 232
cswiger 0:1092494506a3 233 bool bSi1145_present = false;
cswiger 0:1092494506a3 234 void Init_Si1145(void)
cswiger 0:1092494506a3 235 {
cswiger 0:1092494506a3 236 unsigned char readbyte;
cswiger 0:1092494506a3 237 //Read Si1145 part ID:
cswiger 0:1092494506a3 238 readbyte = ReadFrom_Si1145_Register(0x00);
cswiger 0:1092494506a3 239 if (readbyte != 0x45)
cswiger 0:1092494506a3 240 {
cswiger 0:1092494506a3 241 bSi1145_present = false;
cswiger 0:1092494506a3 242 PRINTF("Si1145 sensor not found\r\n");
cswiger 0:1092494506a3 243 }
cswiger 0:1092494506a3 244 else
cswiger 0:1092494506a3 245 {
cswiger 0:1092494506a3 246 bSi1145_present = true;
cswiger 0:1092494506a3 247 PRINTF("Si1145 Part ID : 0x%02X\n", readbyte);
cswiger 0:1092494506a3 248 //Initialize Si1145 by writing to HW_KEY (I2C Register 0x07 = 0x17)
cswiger 0:1092494506a3 249 WriteTo_Si1145_Register(REG_HW_KEY, HW_KEY_VAL0);
cswiger 0:1092494506a3 250
cswiger 0:1092494506a3 251 // Initialize LED Current
cswiger 0:1092494506a3 252 // I2C Register 0x0F = 0xFF
cswiger 0:1092494506a3 253 // I2C Register 0x10 = 0x0F
cswiger 0:1092494506a3 254 WriteTo_Si1145_Register(REG_PS_LED21,(MAX_LED_CURRENT<<4) + MAX_LED_CURRENT);
cswiger 0:1092494506a3 255 WriteTo_Si1145_Register(REG_PS_LED3, MAX_LED_CURRENT);
cswiger 0:1092494506a3 256
cswiger 0:1092494506a3 257 // Parameter 0x01 = 0x37
cswiger 0:1092494506a3 258 //Si1145_ParamSet(PARAM_CH_LIST, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
cswiger 0:1092494506a3 259 //Si1145_ParamSet(0x01, 0x37); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, some)
cswiger 0:1092494506a3 260 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:1092494506a3 261 // 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:1092494506a3 262 PsAlsForce(); // can also be written as WriteTo_Si1145_Register(REG_COMMAND,0x07);
cswiger 0:1092494506a3 263 WriteTo_Si1145_Register(REG_COMMAND, 0x0F);//command to put it into auto mode
cswiger 0:1092494506a3 264 //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:1092494506a3 265 WriteTo_Si1145_Register(REG_MEAS_RATE_LSB, 0x00);
cswiger 0:1092494506a3 266 WriteTo_Si1145_Register(REG_MEAS_RATE_MSB, 0x10);
cswiger 0:1092494506a3 267 } //bSi1145_present = true
cswiger 0:1092494506a3 268 } //Init_Si1145()
cswiger 0:1092494506a3 269
cswiger 0:1092494506a3 270 void Read_Si1145(void)
cswiger 0:1092494506a3 271 {
cswiger 0:1092494506a3 272 if (bSi1145_present)
cswiger 0:1092494506a3 273 {
cswiger 0:1092494506a3 274 // Once the measurements are completed, here is how to reconstruct them
cswiger 0:1092494506a3 275 // Note very carefully that 16-bit registers are in the 'Little Endian' byte order
cswiger 0:1092494506a3 276 // It may be more efficient to perform block I2C Reads, but this example shows
cswiger 0:1092494506a3 277 // individual reads of registers
cswiger 0:1092494506a3 278
cswiger 0:1092494506a3 279 int PS1 = ReadFrom_Si1145_Register(REG_PS1_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS1_DATA1);
cswiger 0:1092494506a3 280 int PS2 = ReadFrom_Si1145_Register(REG_PS2_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS2_DATA1);
cswiger 0:1092494506a3 281 int PS3 = ReadFrom_Si1145_Register(REG_PS3_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS3_DATA1);
cswiger 0:1092494506a3 282 //PRINTF("PS1_Data = %d\n", PS1);
cswiger 0:1092494506a3 283 //PRINTF("PS2_Data = %d\n", PS2);
cswiger 0:1092494506a3 284 //PRINTF("PS3_Data = %d\n", PS3);
cswiger 0:1092494506a3 285 //OBJECT PRESENT?
cswiger 0:1092494506a3 286 #if (0)
cswiger 0:1092494506a3 287 if(PS1 < 22000){
cswiger 0:1092494506a3 288 //PRINTF("Object Far\n");
cswiger 0:1092494506a3 289 sprintf(SENSOR_DATA.Proximity, "Object Far\0");
cswiger 0:1092494506a3 290 }
cswiger 0:1092494506a3 291 else if(PS1 < 24000)
cswiger 0:1092494506a3 292 {
cswiger 0:1092494506a3 293 //PRINTF("Object in Vicinity\n");
cswiger 0:1092494506a3 294 sprintf(SENSOR_DATA.Proximity, "Object in Vicinity\0");
cswiger 0:1092494506a3 295 }
cswiger 0:1092494506a3 296 else if (PS1 < 30000)
cswiger 0:1092494506a3 297 {
cswiger 0:1092494506a3 298 //PRINTF("Object Near\n");
cswiger 0:1092494506a3 299 sprintf(SENSOR_DATA.Proximity, "Object Near\0");
cswiger 0:1092494506a3 300 }
cswiger 0:1092494506a3 301 else
cswiger 0:1092494506a3 302 {
cswiger 0:1092494506a3 303 //PRINTF("Object Very Near\n");
cswiger 0:1092494506a3 304 sprintf(SENSOR_DATA.Proximity, "Object Very Near\0");
cswiger 0:1092494506a3 305 }
cswiger 0:1092494506a3 306 #else
cswiger 0:1092494506a3 307 sprintf(SENSOR_DATA.Proximity, "%d\0", PS1);
cswiger 0:1092494506a3 308 #endif
cswiger 0:1092494506a3 309
cswiger 0:1092494506a3 310 //Force ALS read:
cswiger 0:1092494506a3 311 //WriteTo_Si1145_Register(REG_COMMAND, 0x06);
cswiger 0:1092494506a3 312 //wait (0.1);
cswiger 0:1092494506a3 313 int ALS_VIS = ReadFrom_Si1145_Register(REG_ALS_VIS_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_VIS_DATA1);
cswiger 0:1092494506a3 314 int ALS_IR = ReadFrom_Si1145_Register(REG_ALS_IR_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_IR_DATA1);
cswiger 0:1092494506a3 315 int UV_INDEX = ReadFrom_Si1145_Register(REG_UVINDEX0) + 256 * ReadFrom_Si1145_Register(REG_UVINDEX1);
cswiger 0:1092494506a3 316 //PRINTF("ALS_VIS_Data = %d\n", ALS_VIS);
cswiger 0:1092494506a3 317 //PRINTF("ALS_IR_Data = %d\n", ALS_IR);
cswiger 0:1092494506a3 318 //PRINTF("UV_INDEX_Data = %d\n", UV_INDEX);
cswiger 0:1092494506a3 319
cswiger 0:1092494506a3 320 //PRINTF("Ambient Light Visible Sensor = %d\n", ALS_VIS);
cswiger 0:1092494506a3 321 sprintf(SENSOR_DATA.AmbientLightVis, "%d", ALS_VIS);
cswiger 0:1092494506a3 322 //PRINTF("Ambient Light Infrared Sensor = %d\n", ALS_IR);
cswiger 0:1092494506a3 323 sprintf(SENSOR_DATA.AmbientLightIr, "%d", ALS_IR);
cswiger 0:1092494506a3 324 //float fUV_value = (UV_INDEX -50.0)/10000.0;
cswiger 0:1092494506a3 325 float fUV_value = (UV_INDEX)/100.0; //this is the aux reading
cswiger 0:1092494506a3 326 //PRINTF("UV_Data = %0.2f\n", fUV_value);
cswiger 0:1092494506a3 327 sprintf(SENSOR_DATA.UVindex, "%0.2f", fUV_value);
cswiger 0:1092494506a3 328 } //bSi1145_present = true
cswiger 0:1092494506a3 329 } //Read_Si1145()
cswiger 0:1092494506a3 330
cswiger 0:1092494506a3 331 //********************************************************************************************************************************************
cswiger 0:1092494506a3 332 //* Read the FXOS8700CQ - 6-axis combo Sensor Accelerometer and Magnetometer
cswiger 0:1092494506a3 333 //********************************************************************************************************************************************
cswiger 0:1092494506a3 334 bool bMotionSensor_present = false;
cswiger 0:1092494506a3 335 void Init_motion_sensor()
cswiger 0:1092494506a3 336 {
cswiger 0:1092494506a3 337 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
cswiger 0:1092494506a3 338 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
cswiger 0:1092494506a3 339 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
cswiger 0:1092494506a3 340 int iWhoAmI = fxos.get_whoami();
cswiger 0:1092494506a3 341
cswiger 0:1092494506a3 342 PRINTF("FXOS8700CQ WhoAmI = %X\r\n", iWhoAmI);
cswiger 0:1092494506a3 343 // Iterrupt for active-low interrupt line from FXOS
cswiger 0:1092494506a3 344 // Configured with only one interrupt on INT2 signaling Data-Ready
cswiger 0:1092494506a3 345 //fxos_int2.fall(&trigger_fxos_int2);
cswiger 0:1092494506a3 346 if (iWhoAmI != 0xC7)
cswiger 0:1092494506a3 347 {
cswiger 0:1092494506a3 348 bMotionSensor_present = false;
cswiger 0:1092494506a3 349 PRINTF("FXOS8700CQ motion sensor not found\r\n");
cswiger 0:1092494506a3 350 }
cswiger 0:1092494506a3 351 else
cswiger 0:1092494506a3 352 {
cswiger 0:1092494506a3 353 bMotionSensor_present = true;
cswiger 0:1092494506a3 354 fxos.enable();
cswiger 0:1092494506a3 355 }
cswiger 0:1092494506a3 356 } //Init_motion_sensor()
cswiger 0:1092494506a3 357
cswiger 0:1092494506a3 358 void Read_motion_sensor()
cswiger 0:1092494506a3 359 {
cswiger 0:1092494506a3 360 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
cswiger 0:1092494506a3 361 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
cswiger 0:1092494506a3 362 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
cswiger 0:1092494506a3 363 if (bMotionSensor_present)
cswiger 0:1092494506a3 364 {
cswiger 0:1092494506a3 365 fxos.enable();
cswiger 0:1092494506a3 366 fxos.get_data(&accel_data, &magn_data);
cswiger 0:1092494506a3 367 //PRINTF("Roll=%5d, Pitch=%5d, Yaw=%5d;\r\n", magn_data.x, magn_data.y, magn_data.z);
cswiger 0:1092494506a3 368 sprintf(SENSOR_DATA.MagnetometerX, "%5d", magn_data.x);
cswiger 0:1092494506a3 369 sprintf(SENSOR_DATA.MagnetometerY, "%5d", magn_data.y);
cswiger 0:1092494506a3 370 sprintf(SENSOR_DATA.MagnetometerZ, "%5d", magn_data.z);
cswiger 0:1092494506a3 371
cswiger 0:1092494506a3 372 //Try to normalize (/2048) the values so they will match the eCompass output:
cswiger 0:1092494506a3 373 float fAccelScaled_x, fAccelScaled_y, fAccelScaled_z;
cswiger 0:1092494506a3 374 fAccelScaled_x = (accel_data.x/2048.0);
cswiger 0:1092494506a3 375 fAccelScaled_y = (accel_data.y/2048.0);
cswiger 0:1092494506a3 376 fAccelScaled_z = (accel_data.z/2048.0);
cswiger 0:1092494506a3 377 //PRINTF("Acc: X=%2.3f Y=%2.3f Z=%2.3f;\r\n", fAccelScaled_x, fAccelScaled_y, fAccelScaled_z);
cswiger 0:1092494506a3 378 sprintf(SENSOR_DATA.AccelX, "%2.3f", fAccelScaled_x);
cswiger 0:1092494506a3 379 sprintf(SENSOR_DATA.AccelY, "%2.3f", fAccelScaled_y);
cswiger 0:1092494506a3 380 sprintf(SENSOR_DATA.AccelZ, "%2.3f", fAccelScaled_z);
cswiger 0:1092494506a3 381 } //bMotionSensor_present
cswiger 0:1092494506a3 382 } //Read_motion_sensor()
cswiger 0:1092494506a3 383
cswiger 0:1092494506a3 384
cswiger 0:1092494506a3 385 //********************************************************************************************************************************************
cswiger 0:1092494506a3 386 //* Read the HTS221 temperature & humidity sensor on the Cellular Shield
cswiger 0:1092494506a3 387 //********************************************************************************************************************************************
cswiger 0:1092494506a3 388 // These are to be built on the fly
cswiger 0:1092494506a3 389 string my_temp;
cswiger 0:1092494506a3 390 string my_humidity;
cswiger 0:1092494506a3 391 HTS221 hts221;
cswiger 0:1092494506a3 392
cswiger 0:1092494506a3 393 #define CTOF(x) ((x)*1.8+32)
cswiger 0:1092494506a3 394 bool bHTS221_present = false;
cswiger 0:1092494506a3 395 void Init_HTS221()
cswiger 0:1092494506a3 396 {
cswiger 0:1092494506a3 397 int i;
cswiger 0:1092494506a3 398 void hts221_init(void);
cswiger 0:1092494506a3 399 i = hts221.begin();
cswiger 0:1092494506a3 400 if (i)
cswiger 0:1092494506a3 401 {
cswiger 0:1092494506a3 402 bHTS221_present = true;
cswiger 0:1092494506a3 403 PRINTF(BLU "HTS221 Detected (0x%02X)\n\r",i);
cswiger 0:1092494506a3 404 PRINTF(" Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
cswiger 0:1092494506a3 405 PRINTF(" Humid is: %02d %%\n\r",hts221.readHumidity());
cswiger 0:1092494506a3 406 }
cswiger 0:1092494506a3 407 else
cswiger 0:1092494506a3 408 {
cswiger 0:1092494506a3 409 bHTS221_present = false;
cswiger 0:1092494506a3 410 PRINTF(RED "HTS221 NOT DETECTED!\n\r");
cswiger 0:1092494506a3 411 }
cswiger 0:1092494506a3 412 } //Init_HTS221()
cswiger 0:1092494506a3 413
cswiger 0:1092494506a3 414 void Read_HTS221()
cswiger 0:1092494506a3 415 {
cswiger 0:1092494506a3 416 if (bHTS221_present)
cswiger 0:1092494506a3 417 {
cswiger 0:1092494506a3 418 sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
cswiger 0:1092494506a3 419 sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
cswiger 0:1092494506a3 420 } //bHTS221_present
cswiger 0:1092494506a3 421 } //Read_HTS221()
cswiger 0:1092494506a3 422
cswiger 0:1092494506a3 423 void sensors_init(void)
cswiger 0:1092494506a3 424 {
cswiger 0:1092494506a3 425 Init_HTS221();
cswiger 0:1092494506a3 426 Init_Si7020();
cswiger 0:1092494506a3 427 Init_Si1145();
cswiger 0:1092494506a3 428 Init_motion_sensor();
cswiger 0:1092494506a3 429 } //sensors_init
cswiger 0:1092494506a3 430
cswiger 0:1092494506a3 431 void read_sensors(void)
cswiger 0:1092494506a3 432 {
cswiger 0:1092494506a3 433 Read_HTS221();
cswiger 0:1092494506a3 434 Read_Si7020();
cswiger 0:1092494506a3 435 Read_Si1145();
cswiger 0:1092494506a3 436 Read_motion_sensor();
cswiger 0:1092494506a3 437 } //read_sensors