Avnet AT&T IoT Starter Kit demo for Pubnub

Dependencies:   FXOS8700CQ mbed

Revision:
11:e6602513730f
Parent:
4:f83bedd9cab4
Child:
29:e6c8bd41caa6
--- a/sensors.cpp	Mon Jul 11 23:54:24 2016 +0000
+++ b/sensors.cpp	Tue Jul 12 03:11:05 2016 +0000
@@ -5,7 +5,8 @@
 #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
 
-I2C pmod_i2c(PTC11, PTC10); //SDA, SCL
+#include "hardware.h"
+//I2C i2c(PTC11, PTC10); //SDA, SCL
 
 #include "FXOS8700CQ.h"
 // Pin names for the motion sensor FRDM-K64F board:
@@ -28,7 +29,7 @@
 unsigned char I2C_ReadSingleByte(unsigned char ucDeviceAddress)
 {
     char rxbuffer [1];
-    pmod_i2c.read(ucDeviceAddress, rxbuffer, 1 );
+    i2c.read(ucDeviceAddress, rxbuffer, 1 );
     return (unsigned char)rxbuffer[0];
 } //I2C_ReadSingleByte()
 
@@ -40,8 +41,8 @@
     char txbuffer [1];
     char rxbuffer [1];
     txbuffer[0] = (char)Addr;
-    pmod_i2c.write(ucDeviceAddress, txbuffer, 1 );
-    pmod_i2c.read(ucDeviceAddress, rxbuffer, 1 );
+    i2c.write(ucDeviceAddress, txbuffer, 1 );
+    i2c.read(ucDeviceAddress, rxbuffer, 1 );
     return (unsigned char)rxbuffer[0];
 } //I2C_ReadSingleByteFromAddr()
 
@@ -51,7 +52,7 @@
 int I2C_ReadMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength)
 {
     int status;
-    status = pmod_i2c.read(ucDeviceAddress, ucData, ucLength);
+    status = i2c.read(ucDeviceAddress, ucData, ucLength);
     return status;
 } //I2C_ReadMultipleBytes()
 
@@ -63,7 +64,7 @@
     int status;
     char txbuffer [1];
     txbuffer[0] = (char)Data; //data
-    status = pmod_i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop
+    status = i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop
     return status;
 } //I2C_WriteSingleByte()
 
@@ -76,8 +77,8 @@
     char txbuffer [2];
     txbuffer[0] = (char)Addr; //address
     txbuffer[1] = (char)Data; //data
-    //status = pmod_i2c.write(ucDeviceAddress, txbuffer, 2, false); //stop at end
-    status = pmod_i2c.write(ucDeviceAddress, txbuffer, 2, !bSendStop); //true: do not send stop
+    //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()
 
@@ -87,7 +88,7 @@
 int I2C_WriteMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength, bool bSendStop)
 {
     int status;
-    status = pmod_i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop
+    status = i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop
     return status;
 } //I2C_WriteMultipleBytes()
 
@@ -310,34 +311,49 @@
 //********************************************************************************************************************************************
 //* Read the FXOS8700CQ - 6-axis combo Sensor Accelerometer and Magnetometer
 //********************************************************************************************************************************************
-void read_motion_sensor()
-{
-    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);
-} //read_motion_sensor
-
+bool bMotionSensor_present = false;
 void init_motion_sensor()
 {
-    printf("FXOS8700CQ WhoAmI = %X\r\n", fxos.get_whoami());
+    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);
-    fxos.enable();
+    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()
+{
+    if (bMotionSensor_present)
+    {
+        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();