mDot processor with ROHM sensor board on UDK2.

Dependencies:   MbedJSONValue libmDot mbed-rtos mbed

Fork of VVV_MultiTech_Dragonfly_ATT_Dallas by Paul Jaeger

Committer:
BlueShadow
Date:
Sun Feb 28 23:17:46 2016 +0000
Revision:
9:10082fc85d18
Parent:
8:720595aa7bfd
Cleaned up ROHM board for Training classes.  Removed some extra remarks and unused variables.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:a44e71488e1f 1 /*************************************************************************
BlueShadow 9:10082fc85d18 2 * Originally this was a
mfiore 0:a44e71488e1f 3 * Dragonfly Example program for 2015 AT&T Government Solutions Hackathon
BlueShadow 6:7946b5c2376a 4 *
BlueShadow 8:720595aa7bfd 5 * This is in process of being convertered to a mDot processor. mDot has a
BlueShadow 8:720595aa7bfd 6 * limited set of IO that are available to the ROHM board. Most of the
BlueShadow 8:720595aa7bfd 7 * Sensors will be used but the ones that can't have been commented out.
BlueShadow 8:720595aa7bfd 8 *
mfiore 0:a44e71488e1f 9 * The following hardware is required to successfully run this program:
mfiore 0:a44e71488e1f 10 * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna
mfiore 0:a44e71488e1f 11 * connector, micro USB ports, and 40-pin connector for Dragonfly)
BlueShadow 8:720595aa7bfd 12 * - MultiTech mDot with a LoRa radio
BlueShadow 8:720595aa7bfd 13 * - Seeed Studio Base Shield to elevate the ROHM board connectors away from mDOt
mfiore 0:a44e71488e1f 14 * - MEMs Inertial and Environmental Nucleo Expansion board (LSM6DS0
mfiore 0:a44e71488e1f 15 * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis
mfiore 0:a44e71488e1f 16 * magnetometer, HTS221 humidity and temperature sensor and LPS25HB
mfiore 0:a44e71488e1f 17 * pressure sensor)
mfiore 0:a44e71488e1f 18 *
mfiore 0:a44e71488e1f 19 * What this program does:
BlueShadow 9:10082fc85d18 20 * - reads data from all sensors on MEMs board on a periodic basis
mfiore 0:a44e71488e1f 21 * - prints all sensor data to debug port on a periodic basis
BlueShadow 8:720595aa7bfd 22 * - optionally sends LoRa sensor data when the timer expires
BlueShadow 9:10082fc85d18 23 *
BlueShadow 9:10082fc85d18 24 * This needs to be written yet.
mfiore 4:730b61258422 25 * - optionally sends sensor data to AT&T M2X cloud platform (user must
mfiore 4:730b61258422 26 * create own M2X account and configure a device)
mfiore 5:a946ef74a8c4 27 * - you need to set the "m2x_api_key" field and the "m2x_device_id"
mfiore 5:a946ef74a8c4 28 * field based on your M2X account for this to work
mfiore 5:a946ef74a8c4 29 * - you need to set the "do_cloud_post" flag to true for this to
mfiore 5:a946ef74a8c4 30 * work
mfiore 0:a44e71488e1f 31 *
mfiore 0:a44e71488e1f 32 * Setup:
BlueShadow 8:720595aa7bfd 33 * - Seat the mDot on the UDK2 board
mfiore 0:a44e71488e1f 34 * - Stack the Base Shield on the UDK2 Arduino headers
mfiore 3:f6bceb9e5e1a 35 * - Make sure the reference voltage selector switch (next to the A0
BlueShadow 8:720595aa7bfd 36 * socket) is switched to 3.3V so you don't blow the mDot analog converter
BlueShadow 8:720595aa7bfd 37 * accuracy will suffer as a result when compared to 5V.
mfiore 0:a44e71488e1f 38 * - Stack the MEMs board on top of the Base Shield
mfiore 0:a44e71488e1f 39 * - Plug in the power cable
BlueShadow 8:720595aa7bfd 40 * - Plug a micro USB cable away from the multiple LED String
mfiore 0:a44e71488e1f 41 *
mfiore 0:a44e71488e1f 42 * Go have fun and make something cool!
mfiore 0:a44e71488e1f 43 *
mfiore 0:a44e71488e1f 44 ************************************************************************/
BlueShadow 6:7946b5c2376a 45 /*
BlueShadow 6:7946b5c2376a 46 Sample Program Description:
BlueShadow 8:720595aa7bfd 47 This Program will enable to Multi-Tech mDot platform to utilize ROHM's Multi-sensor Shield Board.
BlueShadow 8:720595aa7bfd 48 This program will initialize most of the sensors on the shield and then read back the sensor data.
BlueShadow 6:7946b5c2376a 49 Data will then be output to the UART Debug Terminal every 1 second.
BlueShadow 6:7946b5c2376a 50
BlueShadow 6:7946b5c2376a 51 Sample Program Author:
BlueShadow 6:7946b5c2376a 52 ROHM USDC
BlueShadow 6:7946b5c2376a 53
BlueShadow 6:7946b5c2376a 54 Additional Resources:
BlueShadow 6:7946b5c2376a 55 ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield
BlueShadow 6:7946b5c2376a 56 */
BlueShadow 6:7946b5c2376a 57
BlueShadow 6:7946b5c2376a 58
mfiore 0:a44e71488e1f 59 #include "mbed.h"
mfiore 0:a44e71488e1f 60 #include "MbedJSONValue.h"
mfiore 0:a44e71488e1f 61 #include <string>
mfiore 0:a44e71488e1f 62
BlueShadow 8:720595aa7bfd 63 // added the following help files for a mDot not required for Dragonfly.
BlueShadow 8:720595aa7bfd 64 #include "mDot.h"
BlueShadow 8:720595aa7bfd 65 #include "MTSLog.h"
BlueShadow 8:720595aa7bfd 66 #include <vector>
BlueShadow 8:720595aa7bfd 67 #include <algorithm>
BlueShadow 8:720595aa7bfd 68 #include "rtos.h"
BlueShadow 8:720595aa7bfd 69
BlueShadow 8:720595aa7bfd 70
mfiore 0:a44e71488e1f 71 // Debug serial port
mfiore 0:a44e71488e1f 72 static Serial debug(USBTX, USBRX);
mfiore 0:a44e71488e1f 73
mfiore 0:a44e71488e1f 74 // variables for sensor data
mfiore 0:a44e71488e1f 75 float temp_celsius;
mfiore 0:a44e71488e1f 76 float humidity_percent;
mfiore 0:a44e71488e1f 77 float pressure_mbar;
mfiore 0:a44e71488e1f 78 float moisture_percent;
mfiore 0:a44e71488e1f 79 int32_t mag_mgauss[3];
mfiore 0:a44e71488e1f 80 int32_t acc_mg[3];
mfiore 0:a44e71488e1f 81 int32_t gyro_mdps[3];
mfiore 0:a44e71488e1f 82
mfiore 0:a44e71488e1f 83 // misc variables
mfiore 0:a44e71488e1f 84 static char wall_of_dash[] = "--------------------------------------------------";
BlueShadow 6:7946b5c2376a 85 static int thpm_interval_ms = 5000;
BlueShadow 6:7946b5c2376a 86 static int motion_interval_ms = 5000;
BlueShadow 6:7946b5c2376a 87 static int print_interval_ms = 5000;
BlueShadow 6:7946b5c2376a 88 static int sms_interval_ms = 60000;
mfiore 0:a44e71488e1f 89 int debug_baud = 115200;
mfiore 0:a44e71488e1f 90
BlueShadow 6:7946b5c2376a 91
BlueShadow 6:7946b5c2376a 92 /****************************************************************************************************
BlueShadow 6:7946b5c2376a 93
BlueShadow 6:7946b5c2376a 94 ****************************************************************************************************/
BlueShadow 6:7946b5c2376a 95
BlueShadow 6:7946b5c2376a 96 //Macros for checking each of the different Sensor Devices
BlueShadow 6:7946b5c2376a 97 #define AnalogTemp //BDE0600
BlueShadow 8:720595aa7bfd 98 // #define AnalogUV //ML8511 // analog pin A4 on Arduino connector is not connected to the mDot on the UDK.
BlueShadow 6:7946b5c2376a 99 #define HallSensor //BU52011
BlueShadow 6:7946b5c2376a 100 #define RPR0521 //RPR0521
BlueShadow 6:7946b5c2376a 101 #define KMX62 //KMX61, Accel/Mag
BlueShadow 6:7946b5c2376a 102 #define COLOR //BH1745
BlueShadow 6:7946b5c2376a 103 #define KX022 //KX022, Accel Only
BlueShadow 6:7946b5c2376a 104 #define Pressure //BM1383
BlueShadow 9:10082fc85d18 105
BlueShadow 9:10082fc85d18 106 #define SMS //allow SMS messaging now sending LORA!!!!
BlueShadow 7:dd550a829ece 107 //#define Web //allow M2X communication
BlueShadow 6:7946b5c2376a 108
BlueShadow 6:7946b5c2376a 109
BlueShadow 6:7946b5c2376a 110 //Define Pins for I2C Interface
BlueShadow 6:7946b5c2376a 111 I2C i2c(I2C_SDA, I2C_SCL);
BlueShadow 6:7946b5c2376a 112 bool RepStart = true;
BlueShadow 6:7946b5c2376a 113 bool NoRepStart = false;
BlueShadow 6:7946b5c2376a 114
BlueShadow 6:7946b5c2376a 115 //Define Sensor Variables
BlueShadow 6:7946b5c2376a 116 #ifdef AnalogTemp
BlueShadow 8:720595aa7bfd 117 AnalogIn BDE0600_Temp(PC_1); //Mapped to A2 pin 15 on the mDot
BlueShadow 6:7946b5c2376a 118 uint16_t BDE0600_Temp_value;
BlueShadow 6:7946b5c2376a 119 float BDE0600_output;
BlueShadow 6:7946b5c2376a 120 #endif
BlueShadow 6:7946b5c2376a 121
BlueShadow 8:720595aa7bfd 122 //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK.
BlueShadow 8:720595aa7bfd 123 //AnalogIn ML8511_UV(PA_7); //Mapped to A4 not a pin routed on the UDK to the mDot
BlueShadow 8:720595aa7bfd 124 //uint16_t ML8511_UV_value;
BlueShadow 8:720595aa7bfd 125 //float ML8511_output;
BlueShadow 8:720595aa7bfd 126 //#endif
BlueShadow 6:7946b5c2376a 127
BlueShadow 6:7946b5c2376a 128 #ifdef HallSensor
BlueShadow 8:720595aa7bfd 129 DigitalIn Hall_GPIO0(PA_4); // assigned to D10 on Arduino, mapped to pin 17 on mDot
BlueShadow 8:720595aa7bfd 130 DigitalIn Hall_GPIO1(PA_7); // assigned to D11 on Arduino, mapped to pin 11 on mDot
BlueShadow 6:7946b5c2376a 131 int Hall_Return1;
BlueShadow 6:7946b5c2376a 132 int Hall_Return0;
BlueShadow 6:7946b5c2376a 133 int32_t Hall_Return[2];
BlueShadow 6:7946b5c2376a 134 #endif
BlueShadow 6:7946b5c2376a 135
BlueShadow 6:7946b5c2376a 136 #ifdef RPR0521
BlueShadow 6:7946b5c2376a 137 int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0
BlueShadow 6:7946b5c2376a 138 int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1
BlueShadow 6:7946b5c2376a 139 char RPR0521_ModeControl[2] = {0x41, 0xE6};
BlueShadow 6:7946b5c2376a 140 char RPR0521_ALSPSControl[2] = {0x42, 0x03};
BlueShadow 6:7946b5c2376a 141 char RPR0521_Persist[2] = {0x43, 0x20};
BlueShadow 6:7946b5c2376a 142 char RPR0521_Addr_ReadData = 0x44;
BlueShadow 6:7946b5c2376a 143 char RPR0521_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 144 int RPR0521_PS_RAWOUT = 0; //this is an output
BlueShadow 6:7946b5c2376a 145 float RPR0521_PS_OUT = 0;
BlueShadow 6:7946b5c2376a 146 int RPR0521_ALS_D0_RAWOUT = 0;
BlueShadow 6:7946b5c2376a 147 int RPR0521_ALS_D1_RAWOUT = 0;
BlueShadow 6:7946b5c2376a 148 float RPR0521_ALS_DataRatio = 0;
BlueShadow 6:7946b5c2376a 149 float RPR0521_ALS_OUT = 0; //this is an output
BlueShadow 6:7946b5c2376a 150 float RPR0521_ALS[2]; // is this ok taking an int to the [0] value and float to [1]???????????
BlueShadow 6:7946b5c2376a 151 #endif
BlueShadow 6:7946b5c2376a 152
BlueShadow 6:7946b5c2376a 153 #ifdef KMX62
BlueShadow 6:7946b5c2376a 154 int KMX62_addr_w = 0x1C; //7bit addr = 0x38, with write bit 0
BlueShadow 6:7946b5c2376a 155 int KMX62_addr_r = 0x1D; //7bit addr = 0x38, with read bit 1
BlueShadow 6:7946b5c2376a 156 char KMX62_CNTL2[2] = {0x3A, 0x5F};
BlueShadow 6:7946b5c2376a 157 char KMX62_Addr_Accel_ReadData = 0x0A;
BlueShadow 6:7946b5c2376a 158 char KMX62_Content_Accel_ReadData[6];
BlueShadow 6:7946b5c2376a 159 char KMX62_Addr_Mag_ReadData = 0x10;
BlueShadow 6:7946b5c2376a 160 char KMX62_Content_Mag_ReadData[6];
BlueShadow 6:7946b5c2376a 161 short int MEMS_Accel_Xout = 0;
BlueShadow 6:7946b5c2376a 162 short int MEMS_Accel_Yout = 0;
BlueShadow 6:7946b5c2376a 163 short int MEMS_Accel_Zout = 0;
BlueShadow 6:7946b5c2376a 164 double MEMS_Accel_Conv_Xout = 0;
BlueShadow 6:7946b5c2376a 165 double MEMS_Accel_Conv_Yout = 0;
BlueShadow 6:7946b5c2376a 166 double MEMS_Accel_Conv_Zout = 0;
BlueShadow 6:7946b5c2376a 167
BlueShadow 6:7946b5c2376a 168 short int MEMS_Mag_Xout = 0;
BlueShadow 6:7946b5c2376a 169 short int MEMS_Mag_Yout = 0;
BlueShadow 6:7946b5c2376a 170 short int MEMS_Mag_Zout = 0;
BlueShadow 6:7946b5c2376a 171 float MEMS_Mag_Conv_Xout = 0;
BlueShadow 6:7946b5c2376a 172 float MEMS_Mag_Conv_Yout = 0;
BlueShadow 6:7946b5c2376a 173 float MEMS_Mag_Conv_Zout = 0;
BlueShadow 6:7946b5c2376a 174
BlueShadow 6:7946b5c2376a 175 double MEMS_Accel[3];
BlueShadow 6:7946b5c2376a 176 float MEMS_Mag[3];
BlueShadow 6:7946b5c2376a 177 #endif
BlueShadow 6:7946b5c2376a 178
BlueShadow 6:7946b5c2376a 179 #ifdef COLOR
BlueShadow 6:7946b5c2376a 180 int BH1745_addr_w = 0x72; //write
BlueShadow 6:7946b5c2376a 181 int BH1745_addr_r = 0x73; //read
BlueShadow 6:7946b5c2376a 182 char BH1745_persistence[2] = {0x61, 0x03};
BlueShadow 6:7946b5c2376a 183 char BH1745_mode1[2] = {0x41, 0x00};
BlueShadow 6:7946b5c2376a 184 char BH1745_mode2[2] = {0x42, 0x92};
BlueShadow 6:7946b5c2376a 185 char BH1745_mode3[2] = {0x43, 0x02};
BlueShadow 6:7946b5c2376a 186 char BH1745_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 187 char BH1745_Addr_color_ReadData = 0x50;
BlueShadow 6:7946b5c2376a 188 int BH1745_Red;
BlueShadow 6:7946b5c2376a 189 int BH1745_Blue;
BlueShadow 6:7946b5c2376a 190 int BH1745_Green;
BlueShadow 6:7946b5c2376a 191 int32_t BH1745[3]; //Red, Blue Green matrix
BlueShadow 6:7946b5c2376a 192 #endif
BlueShadow 6:7946b5c2376a 193
BlueShadow 6:7946b5c2376a 194 #ifdef KX022
BlueShadow 6:7946b5c2376a 195 int KX022_addr_w = 0x3C; //write
BlueShadow 6:7946b5c2376a 196 int KX022_addr_r = 0x3D; //read
BlueShadow 6:7946b5c2376a 197 char KX022_Accel_CNTL1[2] = {0x18, 0x41};
BlueShadow 6:7946b5c2376a 198 char KX022_Accel_ODCNTL[2] = {0x1B, 0x02};
BlueShadow 6:7946b5c2376a 199 char KX022_Accel_CNTL3[2] = {0x1A, 0xD8};
BlueShadow 6:7946b5c2376a 200 char KX022_Accel_TILT_TIMER[2] = {0x22, 0x01};
BlueShadow 6:7946b5c2376a 201 char KX022_Accel_CNTL2[2] = {0x18, 0xC1};
BlueShadow 6:7946b5c2376a 202 char KX022_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 203 char KX022_Addr_Accel_ReadData = 0x06;
BlueShadow 6:7946b5c2376a 204 float KX022_Accel_X;
BlueShadow 6:7946b5c2376a 205 float KX022_Accel_Y;
BlueShadow 6:7946b5c2376a 206 float KX022_Accel_Z;
BlueShadow 6:7946b5c2376a 207 short int KX022_Accel_X_RawOUT = 0;
BlueShadow 6:7946b5c2376a 208 short int KX022_Accel_Y_RawOUT = 0;
BlueShadow 6:7946b5c2376a 209 short int KX022_Accel_Z_RawOUT = 0;
BlueShadow 6:7946b5c2376a 210 int KX022_Accel_X_LB = 0;
BlueShadow 6:7946b5c2376a 211 int KX022_Accel_X_HB = 0;
BlueShadow 6:7946b5c2376a 212 int KX022_Accel_Y_LB = 0;
BlueShadow 6:7946b5c2376a 213 int KX022_Accel_Y_HB = 0;
BlueShadow 6:7946b5c2376a 214 int KX022_Accel_Z_LB = 0;
BlueShadow 6:7946b5c2376a 215 int KX022_Accel_Z_HB = 0;
BlueShadow 6:7946b5c2376a 216 float KX022_Accel[3];
BlueShadow 6:7946b5c2376a 217 #endif
BlueShadow 6:7946b5c2376a 218
BlueShadow 6:7946b5c2376a 219 #ifdef Pressure
BlueShadow 6:7946b5c2376a 220 int Press_addr_w = 0xBA; //write
BlueShadow 6:7946b5c2376a 221 int Press_addr_r = 0xBB; //read
BlueShadow 6:7946b5c2376a 222 char PWR_DOWN[2] = {0x12, 0x01};
BlueShadow 6:7946b5c2376a 223 char SLEEP[2] = {0x13, 0x01};
BlueShadow 6:7946b5c2376a 224 char Mode_Control[2] = {0x14, 0xC4};
BlueShadow 6:7946b5c2376a 225 char Press_Content_ReadData[6];
BlueShadow 6:7946b5c2376a 226 char Press_Addr_ReadData =0x1A;
BlueShadow 6:7946b5c2376a 227 int BM1383_Temp_highByte;
BlueShadow 6:7946b5c2376a 228 int BM1383_Temp_lowByte;
BlueShadow 6:7946b5c2376a 229 int BM1383_Pres_highByte;
BlueShadow 6:7946b5c2376a 230 int BM1383_Pres_lowByte;
BlueShadow 6:7946b5c2376a 231 int BM1383_Pres_leastByte;
BlueShadow 6:7946b5c2376a 232 short int BM1383_Temp_Out;
BlueShadow 6:7946b5c2376a 233 float BM1383_Temp_Conv_Out;
BlueShadow 6:7946b5c2376a 234 float BM1383_Pres_Conv_Out;
BlueShadow 6:7946b5c2376a 235 float_t BM1383[2]; // Temp is 0 and Pressure is 1
BlueShadow 6:7946b5c2376a 236 float BM1383_Var;
BlueShadow 6:7946b5c2376a 237 float BM1383_Deci;
BlueShadow 6:7946b5c2376a 238 #endif
BlueShadow 6:7946b5c2376a 239
BlueShadow 6:7946b5c2376a 240 /****************************************************************************************************
mfiore 0:a44e71488e1f 241 // function prototypes
BlueShadow 6:7946b5c2376a 242 ****************************************************************************************************/
mfiore 0:a44e71488e1f 243 bool init_mtsas();
BlueShadow 6:7946b5c2376a 244 void ReadAnalogTemp();
BlueShadow 8:720595aa7bfd 245 // void ReadAnalogUV (); // analog pin A4 on Arduino connector is not connected to the mDot on the UDK.
BlueShadow 6:7946b5c2376a 246 void ReadHallSensor ();
BlueShadow 6:7946b5c2376a 247 void ReadCOLOR ();
BlueShadow 6:7946b5c2376a 248 void ReadRPR0521_ALS ();
BlueShadow 6:7946b5c2376a 249 void ReadKMX62_Accel ();
BlueShadow 6:7946b5c2376a 250 void ReadKMX62_Mag ();
BlueShadow 6:7946b5c2376a 251 void ReadPressure ();
BlueShadow 6:7946b5c2376a 252 void ReadKX022();
mfiore 0:a44e71488e1f 253
BlueShadow 8:720595aa7bfd 254 // these options must match the settings on your Conduit
BlueShadow 8:720595aa7bfd 255 // uncomment the following lines and edit their values to match your configuration
BlueShadow 8:720595aa7bfd 256 static std::string config_network_name = "Arrow123";
BlueShadow 8:720595aa7bfd 257 static std::string config_network_pass = "Arrow123";
BlueShadow 8:720595aa7bfd 258 static uint8_t config_frequency_sub_band = 1;
BlueShadow 8:720595aa7bfd 259
BlueShadow 6:7946b5c2376a 260 /****************************************************************************************************
mfiore 0:a44e71488e1f 261 // main
BlueShadow 6:7946b5c2376a 262 ****************************************************************************************************/
BlueShadow 6:7946b5c2376a 263 int main()
BlueShadow 6:7946b5c2376a 264 {
BlueShadow 8:720595aa7bfd 265 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); //NONE_, FATAL_, ERROR_, WARNING_, INFO_, DEBUG_, TRACE_
mfiore 0:a44e71488e1f 266 debug.baud(debug_baud);
mfiore 0:a44e71488e1f 267 logInfo("starting...");
BlueShadow 6:7946b5c2376a 268
BlueShadow 6:7946b5c2376a 269
BlueShadow 6:7946b5c2376a 270 /****************************************************************************************************
BlueShadow 6:7946b5c2376a 271 Initialize I2C Devices ************
BlueShadow 6:7946b5c2376a 272 ****************************************************************************************************/
BlueShadow 6:7946b5c2376a 273
BlueShadow 6:7946b5c2376a 274 #ifdef RPR0521
BlueShadow 6:7946b5c2376a 275 i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false);
BlueShadow 6:7946b5c2376a 276 i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false);
BlueShadow 6:7946b5c2376a 277 i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false);
BlueShadow 6:7946b5c2376a 278 #endif
BlueShadow 6:7946b5c2376a 279
BlueShadow 6:7946b5c2376a 280 #ifdef KMX62
BlueShadow 6:7946b5c2376a 281 i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false);
BlueShadow 6:7946b5c2376a 282 #endif
BlueShadow 6:7946b5c2376a 283
BlueShadow 6:7946b5c2376a 284 #ifdef COLOR
BlueShadow 6:7946b5c2376a 285 i2c.write(BH1745_addr_w, &BH1745_persistence[0], 2, false);
BlueShadow 6:7946b5c2376a 286 i2c.write(BH1745_addr_w, &BH1745_mode1[0], 2, false);
BlueShadow 6:7946b5c2376a 287 i2c.write(BH1745_addr_w, &BH1745_mode2[0], 2, false);
BlueShadow 6:7946b5c2376a 288 i2c.write(BH1745_addr_w, &BH1745_mode3[0], 2, false);
BlueShadow 6:7946b5c2376a 289 #endif
BlueShadow 6:7946b5c2376a 290
BlueShadow 6:7946b5c2376a 291 #ifdef KX022
BlueShadow 6:7946b5c2376a 292 i2c.write(KX022_addr_w, &KX022_Accel_CNTL1[0], 2, false);
BlueShadow 6:7946b5c2376a 293 i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false);
BlueShadow 6:7946b5c2376a 294 i2c.write(KX022_addr_w, &KX022_Accel_CNTL3[0], 2, false);
BlueShadow 6:7946b5c2376a 295 i2c.write(KX022_addr_w, &KX022_Accel_TILT_TIMER[0], 2, false);
BlueShadow 6:7946b5c2376a 296 i2c.write(KX022_addr_w, &KX022_Accel_CNTL2[0], 2, false);
BlueShadow 6:7946b5c2376a 297 #endif
BlueShadow 6:7946b5c2376a 298
BlueShadow 6:7946b5c2376a 299 #ifdef Pressure
BlueShadow 6:7946b5c2376a 300 i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false);
BlueShadow 6:7946b5c2376a 301 i2c.write(Press_addr_w, &SLEEP[0], 2, false);
BlueShadow 6:7946b5c2376a 302 i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
BlueShadow 6:7946b5c2376a 303 #endif
BlueShadow 6:7946b5c2376a 304 //End I2C Initialization Section **********************************************************
BlueShadow 6:7946b5c2376a 305
BlueShadow 8:720595aa7bfd 306 //Initialize the mDot ********************************************************************
BlueShadow 8:720595aa7bfd 307 int32_t ret;
BlueShadow 8:720595aa7bfd 308 mDot* dot;
BlueShadow 8:720595aa7bfd 309 std::vector<uint8_t> data;
BlueShadow 8:720595aa7bfd 310 std::string data_str = "hello! Jeff";
BlueShadow 9:10082fc85d18 311
BlueShadow 8:720595aa7bfd 312 // get a mDot handle
BlueShadow 8:720595aa7bfd 313 dot = mDot::getInstance();
BlueShadow 9:10082fc85d18 314
BlueShadow 8:720595aa7bfd 315 // print library version information
BlueShadow 8:720595aa7bfd 316 logInfo("version: %s", dot->getId().c_str());
BlueShadow 6:7946b5c2376a 317
BlueShadow 8:720595aa7bfd 318 //*******************************************
BlueShadow 8:720595aa7bfd 319 // configuration
BlueShadow 8:720595aa7bfd 320 //*******************************************
BlueShadow 8:720595aa7bfd 321 // reset to default config so we know what state we're in
BlueShadow 8:720595aa7bfd 322 dot->resetConfig();
BlueShadow 9:10082fc85d18 323
BlueShadow 8:720595aa7bfd 324 dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); //INFO_LEVEL
BlueShadow 6:7946b5c2376a 325
BlueShadow 8:720595aa7bfd 326 // set up the mDot with our network information: frequency sub band, network name, and network password
BlueShadow 8:720595aa7bfd 327 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
BlueShadow 9:10082fc85d18 328
BlueShadow 8:720595aa7bfd 329 // frequency sub band is only applicable in the 915 (US) frequency band
BlueShadow 8:720595aa7bfd 330 // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
BlueShadow 8:720595aa7bfd 331 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
BlueShadow 8:720595aa7bfd 332 logInfo("setting frequency sub band");
BlueShadow 8:720595aa7bfd 333 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
BlueShadow 8:720595aa7bfd 334 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
BlueShadow 8:720595aa7bfd 335 }
BlueShadow 9:10082fc85d18 336
BlueShadow 8:720595aa7bfd 337 logInfo("setting network name");
BlueShadow 8:720595aa7bfd 338 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
BlueShadow 8:720595aa7bfd 339 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
BlueShadow 8:720595aa7bfd 340 }
BlueShadow 9:10082fc85d18 341
BlueShadow 8:720595aa7bfd 342 logInfo("setting network password");
BlueShadow 8:720595aa7bfd 343 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
BlueShadow 8:720595aa7bfd 344 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
BlueShadow 8:720595aa7bfd 345 }
BlueShadow 9:10082fc85d18 346
BlueShadow 8:720595aa7bfd 347 // a higher spreading factor allows for longer range but lower throughput
BlueShadow 8:720595aa7bfd 348 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
BlueShadow 8:720595aa7bfd 349 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
BlueShadow 8:720595aa7bfd 350 logInfo("setting TX spreading factor");
BlueShadow 9:10082fc85d18 351 if ((ret = dot->setTxDataRate(mDot::SF_7)) != mDot::MDOT_OK) {
BlueShadow 8:720595aa7bfd 352 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
BlueShadow 8:720595aa7bfd 353 }
BlueShadow 9:10082fc85d18 354
BlueShadow 8:720595aa7bfd 355 // request receive confirmation of packets from the gateway
BlueShadow 8:720595aa7bfd 356 logInfo("enabling ACKs");
BlueShadow 8:720595aa7bfd 357 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
BlueShadow 8:720595aa7bfd 358 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
BlueShadow 8:720595aa7bfd 359 }
BlueShadow 9:10082fc85d18 360
BlueShadow 8:720595aa7bfd 361 // save this configuration to the mDot's NVM
BlueShadow 8:720595aa7bfd 362 logInfo("saving config");
BlueShadow 8:720595aa7bfd 363 if (! dot->saveConfig()) {
BlueShadow 8:720595aa7bfd 364 logError("failed to save configuration");
BlueShadow 8:720595aa7bfd 365 }
BlueShadow 8:720595aa7bfd 366 //*******************************************
BlueShadow 8:720595aa7bfd 367 // end of configuration
BlueShadow 8:720595aa7bfd 368 //*******************************************
BlueShadow 6:7946b5c2376a 369
BlueShadow 8:720595aa7bfd 370 // attempt to join the network
BlueShadow 8:720595aa7bfd 371 logInfo("joining network");
BlueShadow 8:720595aa7bfd 372 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
BlueShadow 8:720595aa7bfd 373 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
BlueShadow 8:720595aa7bfd 374 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
BlueShadow 8:720595aa7bfd 375 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
BlueShadow 8:720595aa7bfd 376 }
BlueShadow 6:7946b5c2376a 377
BlueShadow 8:720595aa7bfd 378 // format data for sending to the gateway
BlueShadow 8:720595aa7bfd 379 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
BlueShadow 8:720595aa7bfd 380 data.push_back((uint8_t) *it);
BlueShadow 6:7946b5c2376a 381
BlueShadow 8:720595aa7bfd 382 // END Initialization of mDot ******************************************************************
BlueShadow 6:7946b5c2376a 383
mfiore 3:f6bceb9e5e1a 384 Timer thpm_timer;
BlueShadow 6:7946b5c2376a 385 thpm_timer.start(); // Timer data is set in the Variable seciton see misc variables Timer motion_timer;
mfiore 1:a049d113e250 386 Timer print_timer;
BlueShadow 6:7946b5c2376a 387 print_timer.start();
BlueShadow 6:7946b5c2376a 388 Timer motion_timer;
BlueShadow 6:7946b5c2376a 389 motion_timer.start();
BlueShadow 6:7946b5c2376a 390
BlueShadow 6:7946b5c2376a 391 #ifdef SMS
BlueShadow 6:7946b5c2376a 392 Timer sms_timer;
BlueShadow 6:7946b5c2376a 393 sms_timer.start();
BlueShadow 6:7946b5c2376a 394 #endif
BlueShadow 8:720595aa7bfd 395
mfiore 0:a44e71488e1f 396 while (true) {
BlueShadow 6:7946b5c2376a 397 if (thpm_timer.read_ms() > thpm_interval_ms) {
BlueShadow 6:7946b5c2376a 398 #ifdef AnalogTemp
BlueShadow 6:7946b5c2376a 399 ReadAnalogTemp ();
BlueShadow 6:7946b5c2376a 400 #endif
BlueShadow 6:7946b5c2376a 401
BlueShadow 8:720595aa7bfd 402 //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK.
BlueShadow 8:720595aa7bfd 403 // ReadAnalogUV ();
BlueShadow 8:720595aa7bfd 404 //#endif
BlueShadow 6:7946b5c2376a 405
BlueShadow 6:7946b5c2376a 406 #ifdef HallSensor
BlueShadow 6:7946b5c2376a 407 ReadHallSensor ();
BlueShadow 6:7946b5c2376a 408 #endif
BlueShadow 6:7946b5c2376a 409
BlueShadow 6:7946b5c2376a 410 #ifdef COLOR
BlueShadow 6:7946b5c2376a 411 ReadCOLOR ();
BlueShadow 6:7946b5c2376a 412 #endif
BlueShadow 6:7946b5c2376a 413
BlueShadow 6:7946b5c2376a 414 #ifdef RPR0521 //als digital
BlueShadow 6:7946b5c2376a 415 ReadRPR0521_ALS ();
BlueShadow 6:7946b5c2376a 416 #endif
BlueShadow 6:7946b5c2376a 417
BlueShadow 6:7946b5c2376a 418 #ifdef Pressure
BlueShadow 6:7946b5c2376a 419 ReadPressure();
BlueShadow 6:7946b5c2376a 420 #endif
BlueShadow 6:7946b5c2376a 421 thpm_timer.reset();
BlueShadow 6:7946b5c2376a 422 }
BlueShadow 6:7946b5c2376a 423
mfiore 0:a44e71488e1f 424 if (motion_timer.read_ms() > motion_interval_ms) {
BlueShadow 6:7946b5c2376a 425 #ifdef KMX62
BlueShadow 6:7946b5c2376a 426 ReadKMX62_Accel ();
BlueShadow 6:7946b5c2376a 427 ReadKMX62_Mag ();
BlueShadow 6:7946b5c2376a 428 #endif
BlueShadow 6:7946b5c2376a 429
BlueShadow 6:7946b5c2376a 430 #ifdef KX022
BlueShadow 6:7946b5c2376a 431 ReadKX022 ();
BlueShadow 6:7946b5c2376a 432 #endif
mfiore 0:a44e71488e1f 433 motion_timer.reset();
mfiore 0:a44e71488e1f 434 }
BlueShadow 6:7946b5c2376a 435
mfiore 0:a44e71488e1f 436 if (print_timer.read_ms() > print_interval_ms) {
mfiore 0:a44e71488e1f 437 logDebug("%s", wall_of_dash);
mfiore 0:a44e71488e1f 438 logDebug("SENSOR DATA");
BlueShadow 6:7946b5c2376a 439 logDebug("temperature: %0.2f C", BM1383[0]);
BlueShadow 8:720595aa7bfd 440 // logDebug("analog uv: %.1f mW/cm2", ML8511_output); // analog pin A4 on Arduino connector is not connected to the mDot on the UDK.
BlueShadow 6:7946b5c2376a 441 logDebug("ambient Light %0.3f", RPR0521_ALS[0]);
BlueShadow 6:7946b5c2376a 442 logDebug("proximity count %0.3f", RPR0521_ALS[1]);
BlueShadow 6:7946b5c2376a 443 logDebug("hall effect: South %d\t North %d", Hall_Return[0],Hall_Return[1]);
BlueShadow 6:7946b5c2376a 444 logDebug("pressure: %0.2f hPa", BM1383[1]);
BlueShadow 6:7946b5c2376a 445 logDebug("magnetometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tuT", MEMS_Mag[0], MEMS_Mag[1], MEMS_Mag[2]);
BlueShadow 6:7946b5c2376a 446 logDebug("accelerometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tg", MEMS_Accel[0], MEMS_Accel[1], MEMS_Accel[2]);
BlueShadow 6:7946b5c2376a 447 logDebug("color:\r\n\tred: %ld\tgrn: %ld\tblu: %ld\t", BH1745[0], BH1745[1], BH1745[2]);
mfiore 0:a44e71488e1f 448 logDebug("%s", wall_of_dash);
mfiore 0:a44e71488e1f 449 print_timer.reset();
mfiore 0:a44e71488e1f 450 }
BlueShadow 6:7946b5c2376a 451
BlueShadow 6:7946b5c2376a 452
BlueShadow 6:7946b5c2376a 453
BlueShadow 6:7946b5c2376a 454 #ifdef SMS
BlueShadow 6:7946b5c2376a 455 if (sms_timer.read_ms() > sms_interval_ms) {
BlueShadow 6:7946b5c2376a 456 sms_timer.reset();
BlueShadow 6:7946b5c2376a 457 logInfo("SMS Send Routine");
BlueShadow 9:10082fc85d18 458 printf(" In sms routine \r\n");
BlueShadow 9:10082fc85d18 459
mfiore 0:a44e71488e1f 460 }
BlueShadow 6:7946b5c2376a 461 #endif
BlueShadow 8:720595aa7bfd 462
BlueShadow 8:720595aa7bfd 463
mfiore 1:a049d113e250 464 wait_ms(10);
mfiore 0:a44e71488e1f 465 }
mfiore 0:a44e71488e1f 466 }
mfiore 0:a44e71488e1f 467
mfiore 1:a049d113e250 468
BlueShadow 9:10082fc85d18 469 /************************************************************************************************/
mfiore 0:a44e71488e1f 470 // Sensor data acquisition functions
BlueShadow 6:7946b5c2376a 471 /************************************************************************************************/
BlueShadow 6:7946b5c2376a 472 #ifdef AnalogTemp
BlueShadow 6:7946b5c2376a 473 void ReadAnalogTemp ()
BlueShadow 6:7946b5c2376a 474 {
BlueShadow 6:7946b5c2376a 475 BDE0600_Temp_value = BDE0600_Temp.read_u16();
BlueShadow 6:7946b5c2376a 476
BlueShadow 6:7946b5c2376a 477 BDE0600_output = (float)BDE0600_Temp_value * (float)0.000050354; //(value * (3.3V/65535))
BlueShadow 6:7946b5c2376a 478 BDE0600_output = (BDE0600_output-(float)1.753)/((float)-0.01068) + (float)30;
BlueShadow 6:7946b5c2376a 479
BlueShadow 6:7946b5c2376a 480 // printf("BDE0600 Analog Temp Sensor Data:\r\n");
BlueShadow 6:7946b5c2376a 481 // printf(" Temp = %.2f C\r\n", BDE0600_output);
BlueShadow 6:7946b5c2376a 482 }
BlueShadow 6:7946b5c2376a 483 #endif
BlueShadow 6:7946b5c2376a 484
BlueShadow 8:720595aa7bfd 485 //#ifdef AnalogUV // analog pin A4 on Arduino connector is not connected to the mDot on the UDK.
BlueShadow 8:720595aa7bfd 486 //void ReadAnalogUV ()
BlueShadow 8:720595aa7bfd 487 //{
BlueShadow 8:720595aa7bfd 488 // ML8511_UV_value = ML8511_UV.read_u16();
BlueShadow 8:720595aa7bfd 489 // ML8511_output = (float)ML8511_UV_value * (float)0.000050354; //(value * (3.3V/65535)) //Note to self: when playing with this, a negative value is seen... Honestly, I think this has to do with my ADC converstion...
BlueShadow 8:720595aa7bfd 490 // ML8511_output = (ML8511_output-(float)2.2)/((float)0.129) + 10; // Added +5 to the offset so when inside (aka, no UV, readings show 0)... this is the wrong approach... and the readings don't make sense... Fix this.
BlueShadow 6:7946b5c2376a 491
BlueShadow 6:7946b5c2376a 492 // printf("ML8511 Analog UV Sensor Data:\r\n");
BlueShadow 6:7946b5c2376a 493 // printf(" UV = %.1f mW/cm2\r\n", ML8511_output);
BlueShadow 6:7946b5c2376a 494
BlueShadow 8:720595aa7bfd 495 //}
BlueShadow 8:720595aa7bfd 496 //#endif
BlueShadow 6:7946b5c2376a 497
BlueShadow 6:7946b5c2376a 498
BlueShadow 6:7946b5c2376a 499 #ifdef HallSensor
BlueShadow 6:7946b5c2376a 500 void ReadHallSensor ()
BlueShadow 6:7946b5c2376a 501 {
BlueShadow 6:7946b5c2376a 502
BlueShadow 6:7946b5c2376a 503 Hall_Return[0] = Hall_GPIO0;
BlueShadow 6:7946b5c2376a 504 Hall_Return[1] = Hall_GPIO1;
BlueShadow 6:7946b5c2376a 505
BlueShadow 6:7946b5c2376a 506 // printf("BU52011 Hall Switch Sensor Data:\r\n");
BlueShadow 6:7946b5c2376a 507 // printf(" South Detect = %d\r\n", Hall_Return[0]);
BlueShadow 6:7946b5c2376a 508 // printf(" North Detect = %d\r\n", Hall_Return[1]);
BlueShadow 6:7946b5c2376a 509
BlueShadow 9:10082fc85d18 510
mfiore 0:a44e71488e1f 511 }
BlueShadow 6:7946b5c2376a 512 #endif
BlueShadow 6:7946b5c2376a 513
BlueShadow 6:7946b5c2376a 514 #ifdef COLOR
BlueShadow 6:7946b5c2376a 515 void ReadCOLOR ()
BlueShadow 6:7946b5c2376a 516 {
BlueShadow 6:7946b5c2376a 517
BlueShadow 6:7946b5c2376a 518 //Read color data from the IC
BlueShadow 6:7946b5c2376a 519 i2c.write(BH1745_addr_w, &BH1745_Addr_color_ReadData, 1, RepStart);
BlueShadow 6:7946b5c2376a 520 i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 6, NoRepStart);
BlueShadow 6:7946b5c2376a 521
BlueShadow 6:7946b5c2376a 522 //separate all data read into colors
BlueShadow 6:7946b5c2376a 523 BH1745[0] = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]);
BlueShadow 6:7946b5c2376a 524 BH1745[1] = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]);
BlueShadow 6:7946b5c2376a 525 BH1745[2] = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]);
mfiore 0:a44e71488e1f 526
BlueShadow 6:7946b5c2376a 527 //Output Data into UART
BlueShadow 6:7946b5c2376a 528 // printf("BH1745 COLOR Sensor Data:\r\n");
BlueShadow 6:7946b5c2376a 529 // printf(" Red = %d ADC Counts\r\n",BH1745[0]);
BlueShadow 6:7946b5c2376a 530 // printf(" Green = %d ADC Counts\r\n",BH1745[1]);
BlueShadow 6:7946b5c2376a 531 // printf(" Blue = %d ADC Counts\r\n",BH1745[2]);
BlueShadow 6:7946b5c2376a 532
mfiore 0:a44e71488e1f 533 }
BlueShadow 6:7946b5c2376a 534 #endif
mfiore 0:a44e71488e1f 535
BlueShadow 6:7946b5c2376a 536 #ifdef RPR0521 //als digital
BlueShadow 6:7946b5c2376a 537 void ReadRPR0521_ALS ()
BlueShadow 6:7946b5c2376a 538 {
BlueShadow 6:7946b5c2376a 539 i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart);
BlueShadow 6:7946b5c2376a 540 i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart);
BlueShadow 6:7946b5c2376a 541
BlueShadow 6:7946b5c2376a 542 RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]);
BlueShadow 6:7946b5c2376a 543 RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]);
BlueShadow 6:7946b5c2376a 544 RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]);
BlueShadow 6:7946b5c2376a 545 RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT;
BlueShadow 6:7946b5c2376a 546
BlueShadow 6:7946b5c2376a 547 if(RPR0521_ALS_DataRatio < (float)0.595) {
BlueShadow 6:7946b5c2376a 548 RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 6:7946b5c2376a 549 } else if(RPR0521_ALS_DataRatio < (float)1.015) {
BlueShadow 6:7946b5c2376a 550 RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 6:7946b5c2376a 551 } else if(RPR0521_ALS_DataRatio < (float)1.352) {
BlueShadow 6:7946b5c2376a 552 RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 6:7946b5c2376a 553 } else if(RPR0521_ALS_DataRatio < (float)3.053) {
BlueShadow 6:7946b5c2376a 554 RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT);
BlueShadow 6:7946b5c2376a 555 } else {
BlueShadow 6:7946b5c2376a 556 RPR0521_ALS[0] = 0;
BlueShadow 6:7946b5c2376a 557 }
BlueShadow 6:7946b5c2376a 558 // printf("RPR-0521 ALS/PROX Sensor Data:\r\n");
BlueShadow 6:7946b5c2376a 559 // printf(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]);
BlueShadow 6:7946b5c2376a 560 // printf(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]); //defined as a float but is an unsigned.
BlueShadow 6:7946b5c2376a 561
BlueShadow 6:7946b5c2376a 562 }
BlueShadow 6:7946b5c2376a 563 #endif
BlueShadow 6:7946b5c2376a 564
BlueShadow 6:7946b5c2376a 565 #ifdef KMX62
BlueShadow 6:7946b5c2376a 566 void ReadKMX62_Accel ()
BlueShadow 6:7946b5c2376a 567 {
BlueShadow 6:7946b5c2376a 568 //Read Accel Portion from the IC
BlueShadow 6:7946b5c2376a 569 i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart);
BlueShadow 6:7946b5c2376a 570 i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart);
BlueShadow 6:7946b5c2376a 571
BlueShadow 6:7946b5c2376a 572 //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
BlueShadow 6:7946b5c2376a 573 // However, because we need the signed value, we will adjust the value when converting to "g"
BlueShadow 6:7946b5c2376a 574 MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]);
BlueShadow 6:7946b5c2376a 575 MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]);
BlueShadow 6:7946b5c2376a 576 MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]);
BlueShadow 6:7946b5c2376a 577
BlueShadow 6:7946b5c2376a 578 //Note: Conversion to G is as follows:
BlueShadow 6:7946b5c2376a 579 // Axis_ValueInG = MEMS_Accel_axis / 1024
BlueShadow 6:7946b5c2376a 580 // However, since we did not remove the LSB previously, we need to divide by 4 again
BlueShadow 6:7946b5c2376a 581 // Thus, we will divide the output by 4096 (1024*4) to convert and cancel out the LSB
BlueShadow 6:7946b5c2376a 582 MEMS_Accel[0] = ((float)MEMS_Accel_Xout/4096/2);
BlueShadow 6:7946b5c2376a 583 MEMS_Accel[1] = ((float)MEMS_Accel_Yout/4096/2);
BlueShadow 6:7946b5c2376a 584 MEMS_Accel[2] = ((float)MEMS_Accel_Zout/4096/2);
BlueShadow 6:7946b5c2376a 585
BlueShadow 6:7946b5c2376a 586 // Return Data to UART
BlueShadow 6:7946b5c2376a 587 // printf("KMX62 Accel+Mag Sensor Data:\r\n");
BlueShadow 6:7946b5c2376a 588 // printf(" AccX= %0.2f g\r\n", MEMS_Accel[0]);
BlueShadow 6:7946b5c2376a 589 // printf(" AccY= %0.2f g\r\n", MEMS_Accel[1]);
BlueShadow 6:7946b5c2376a 590 // printf(" AccZ= %0.2f g\r\n", MEMS_Accel[2]);
BlueShadow 6:7946b5c2376a 591
mfiore 0:a44e71488e1f 592 }
mfiore 0:a44e71488e1f 593
BlueShadow 6:7946b5c2376a 594 void ReadKMX62_Mag ()
BlueShadow 6:7946b5c2376a 595 {
BlueShadow 6:7946b5c2376a 596
BlueShadow 6:7946b5c2376a 597 //Read Mag portion from the IC
BlueShadow 6:7946b5c2376a 598 i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart);
BlueShadow 6:7946b5c2376a 599 i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart);
BlueShadow 6:7946b5c2376a 600
BlueShadow 6:7946b5c2376a 601 //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
BlueShadow 6:7946b5c2376a 602 // However, because we need the signed value, we will adjust the value when converting to "g"
BlueShadow 6:7946b5c2376a 603 MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]);
BlueShadow 6:7946b5c2376a 604 MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]);
BlueShadow 6:7946b5c2376a 605 MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]);
mfiore 0:a44e71488e1f 606
BlueShadow 6:7946b5c2376a 607 //Note: Conversion to G is as follows:
BlueShadow 6:7946b5c2376a 608 // Axis_ValueInG = MEMS_Accel_axis / 1024
BlueShadow 6:7946b5c2376a 609 // However, since we did not remove the LSB previously, we need to divide by 4 again
BlueShadow 6:7946b5c2376a 610 // Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB
BlueShadow 6:7946b5c2376a 611 MEMS_Mag[0] = (float)MEMS_Mag_Xout/4096*(float)0.146;
BlueShadow 6:7946b5c2376a 612 MEMS_Mag[1] = (float)MEMS_Mag_Yout/4096*(float)0.146;
BlueShadow 6:7946b5c2376a 613 MEMS_Mag[2] = (float)MEMS_Mag_Zout/4096*(float)0.146;
BlueShadow 6:7946b5c2376a 614
BlueShadow 6:7946b5c2376a 615 // Return Data to UART
BlueShadow 6:7946b5c2376a 616 // printf(" MagX= %0.2f uT\r\n", MEMS_Mag[0]);
BlueShadow 6:7946b5c2376a 617 // printf(" MagY= %0.2f uT\r\n", MEMS_Mag[1]);
BlueShadow 6:7946b5c2376a 618 // printf(" MagZ= %0.2f uT\r\n", MEMS_Mag[2]);
BlueShadow 6:7946b5c2376a 619
mfiore 0:a44e71488e1f 620 }
BlueShadow 6:7946b5c2376a 621 #endif
mfiore 0:a44e71488e1f 622
BlueShadow 6:7946b5c2376a 623 #ifdef KX022
BlueShadow 6:7946b5c2376a 624 void ReadKX022 ()
BlueShadow 6:7946b5c2376a 625 {
BlueShadow 9:10082fc85d18 626
BlueShadow 6:7946b5c2376a 627 //Read KX022 Portion from the IC
BlueShadow 6:7946b5c2376a 628 i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart);
BlueShadow 6:7946b5c2376a 629 i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart);
BlueShadow 6:7946b5c2376a 630
BlueShadow 6:7946b5c2376a 631 //Format Data
BlueShadow 6:7946b5c2376a 632 KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]);
BlueShadow 6:7946b5c2376a 633 KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]);
BlueShadow 6:7946b5c2376a 634 KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]);
BlueShadow 6:7946b5c2376a 635
BlueShadow 6:7946b5c2376a 636 //Scale Data
BlueShadow 6:7946b5c2376a 637 KX022_Accel[0] = (float)KX022_Accel_X_RawOUT / 16384;
BlueShadow 6:7946b5c2376a 638 KX022_Accel[1] = (float)KX022_Accel_Y_RawOUT / 16384;
BlueShadow 6:7946b5c2376a 639 KX022_Accel[2] = (float)KX022_Accel_Z_RawOUT / 16384;
BlueShadow 6:7946b5c2376a 640
BlueShadow 6:7946b5c2376a 641 //Return Data through UART
BlueShadow 6:7946b5c2376a 642 // printf("KX022 Accelerometer Sensor Data: \r\n");
BlueShadow 6:7946b5c2376a 643 // printf(" AccX= %0.2f g\r\n", KX022_Accel[0]);
BlueShadow 6:7946b5c2376a 644 // printf(" AccY= %0.2f g\r\n", KX022_Accel[1]);
BlueShadow 6:7946b5c2376a 645 // printf(" AccZ= %0.2f g\r\n", KX022_Accel[2]);
BlueShadow 6:7946b5c2376a 646
mfiore 0:a44e71488e1f 647 }
BlueShadow 6:7946b5c2376a 648 #endif
BlueShadow 6:7946b5c2376a 649
BlueShadow 6:7946b5c2376a 650
BlueShadow 6:7946b5c2376a 651 #ifdef Pressure
BlueShadow 6:7946b5c2376a 652 void ReadPressure ()
BlueShadow 6:7946b5c2376a 653 {
BlueShadow 6:7946b5c2376a 654
BlueShadow 6:7946b5c2376a 655 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
BlueShadow 6:7946b5c2376a 656 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 6, NoRepStart);
BlueShadow 6:7946b5c2376a 657
BlueShadow 6:7946b5c2376a 658 BM1383_Temp_Out = (Press_Content_ReadData[0]<<8) | (Press_Content_ReadData[1]);
BlueShadow 6:7946b5c2376a 659 BM1383[0] = (float)BM1383_Temp_Out/32;
BlueShadow 6:7946b5c2376a 660
BlueShadow 6:7946b5c2376a 661 BM1383_Var = (Press_Content_ReadData[2]<<3) | (Press_Content_ReadData[3] >> 5);
BlueShadow 6:7946b5c2376a 662 BM1383_Deci = ((Press_Content_ReadData[3] & 0x1f) << 6 | ((Press_Content_ReadData[4] >> 2)));
BlueShadow 6:7946b5c2376a 663 BM1383_Deci = (float)BM1383_Deci* (float)0.00048828125; //0.00048828125 = 2^-11
BlueShadow 6:7946b5c2376a 664 BM1383[1] = (BM1383_Var + BM1383_Deci); //question pending here...
BlueShadow 6:7946b5c2376a 665
BlueShadow 6:7946b5c2376a 666 // printf("BM1383 Pressure Sensor Data:\r\n");
BlueShadow 6:7946b5c2376a 667 // printf(" Temperature= %0.2f C\r\n", BM1383[0]);
BlueShadow 6:7946b5c2376a 668 // printf(" Pressure = %0.2f hPa\r\n", BM1383[1]);
mfiore 0:a44e71488e1f 669
mfiore 0:a44e71488e1f 670 }
BlueShadow 6:7946b5c2376a 671 #endif