Initial Program for MultiTech Dragonfly to communicate to PTC ThingWorx. Sensors are from Rohm version1

Dependencies:   MbedJSONValue mbed mtsas

Fork of UUU_MultiTech_Dragonfly_Sprint by Paul Jaeger

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*************************************************************************
00002  * Dragonfly Example program for 2016 Sprint Exosite Training
00003  *
00004  * The following hardware is required to successfully run this program:
00005  *   - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna
00006  *     connector, micro USB ports, and 40-pin connector for Dragonfly)
00007  *   - MultiTech Dragonfly (1"x2" green PCB with Telit radio)
00008  *   - Rohm Electronics Sensor Board
00009 
00010  *   - Expansion board (LSM6DS0
00011  *     3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis
00012  *     magnetometer, HTS221 humidity and temperature sensor and LPS25HB
00013  *     pressure sensor)
00014  *
00015  * What this program does:
00016  *   - reads data from all sensors on board
00017  *   - prints all sensor data to debug port on a periodic basis
00018  *   - optionally send data to Exosite
00019  *   - All data is sent to a specific location determined by the student login.
00020  *   - Exosite cloud platform (user must create own account and configure a device
00021  *       - you need to set the "VENDOR" and "MODEL"
00022 
00023  *       - you need to set the "do_cloud_post" flag to true for this to
00024  *         work
00025  *
00026  * Setup:
00027  *   - Correctly insert SIM card into Dragonfly
00028  *   - Seat the Dragonfly on the UDK2 board
00029  *   - Connect an antenna to the connector on the Dragonfly labled "M"
00030  *   - Stack the Base Shield on the UDK2 Arduino headers
00031  *   - Stack the MEMs board on top of the Base Shield
00032  *   - Plug in the power cable
00033  *   - Plug a micro USB cable into the port below and slightly to the
00034  *     left of the Dragonfly (NOT the port on the Dragonfly)
00035  *
00036  * Go have fun and make something cool!
00037  *
00038  ************************************************************************/
00039 /*
00040 Sample Program Description:
00041    This Program will enable to Multi-Tech Dragonfly platform to utilize ROHM's Multi-sensor Shield Board.
00042    This program will initialize all sensors on the shield and then read back the sensor data.
00043    Data will then be output to the UART Debug Terminal every 1 second.
00044 
00045 Sample Program Author:
00046    ROHM USDC
00047 
00048 Additional Resources:
00049    ROHM Sensor Shield GitHub Repository: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield
00050 */
00051 
00052 
00053 #include "mbed.h"
00054 #include "mtsas.h"
00055 #include <string>
00056 #include <sstream>
00057 
00058 #define EXOSITE_CIK " YOUR_NUMBER_HERE "
00059 
00060 char EXOSITE_HEADER[] = "X-Exosite-CIK: " EXOSITE_CIK "\r\nAccept: application/x-www-form-urlencoded; charset=utf-8\r\n";
00061 const char EXOSITE_URL[] = "https://m2.exosite.com:443/onep:v1/stack/alias";
00062 
00063 DigitalOut Led1Out(LED1);
00064 
00065 // Debug serial port
00066 static Serial debug(USBTX, USBRX);
00067 
00068 // MTSSerialFlowControl - serial link between processor and radio
00069 static MTSSerialFlowControl* io;
00070 
00071 // Cellular - radio object for cellular operations (SMS, TCP, etc)
00072 Cellular* radio;
00073 
00074 // APN associated with SIM card
00075 // this APN should work for the AT&T SIM that came with your Dragonfly
00076 //static const std::string apn = "";
00077 static const std::string apn = "b2b.tmobile.com";
00078 
00079 // set to true if you want to post to the cloud
00080 //bool do_cloud_post = false;
00081 bool do_cloud_post = true;
00082 
00083 // variables for sensor data
00084 float temp_celsius;
00085 float humidity_percent;
00086 float pressure_mbar;
00087 float moisture_percent;
00088 int32_t mag_mgauss[3];
00089 int32_t acc_mg[3];
00090 int32_t gyro_mdps[3];
00091 
00092 // misc variables
00093 static char wall_of_dash[] = "--------------------------------------------------";
00094 static int post_interval_ms = 30000;
00095 int debug_baud = 115200;
00096 
00097 
00098 
00099 /****************************************************************************************************
00100 
00101  ****************************************************************************************************/
00102 
00103 //Macros for checking each of the different Sensor Devices
00104 #define AnalogTemp  //BDE0600
00105 #define AnalogUV    //ML8511
00106 #define HallSensor  //BU52011
00107 #define RPR0521     //RPR0521
00108 #define KMX62       //KMX61, Accel/Mag         
00109 #define COLOR       //BH1745
00110 #define KX022       //KX022, Accel Only
00111 #define Pressure    //BM1383
00112 
00113 
00114 //Define Pins for I2C Interface
00115 I2C i2c(I2C_SDA, I2C_SCL);
00116 bool        RepStart = true;
00117 bool        NoRepStart = false;
00118 
00119 //Define Sensor Variables
00120 #ifdef AnalogTemp
00121 AnalogIn    BDE0600_Temp(PC_4); //Mapped to A2
00122 uint16_t    BDE0600_Temp_value;
00123 float       BDE0600_output;
00124 #endif
00125 
00126 #ifdef AnalogUV
00127 AnalogIn    ML8511_UV(PC_1);    //Mapped to A4
00128 uint16_t    ML8511_UV_value;
00129 float       ML8511_output;
00130 #endif
00131 
00132 #ifdef HallSensor
00133 DigitalIn   Hall_GPIO0(PC_8);
00134 DigitalIn   Hall_GPIO1(PB_5);
00135 int         Hall_Return1;
00136 int         Hall_Return0;
00137 int32_t     Hall_Return[2];
00138 #endif
00139 
00140 #ifdef RPR0521
00141 int         RPR0521_addr_w = 0x70;          //7bit addr = 0x38, with write bit 0
00142 int         RPR0521_addr_r = 0x71;          //7bit addr = 0x38, with read bit 1
00143 char        RPR0521_ModeControl[2] = {0x41, 0xE6};
00144 char        RPR0521_ALSPSControl[2] = {0x42, 0x03};
00145 char        RPR0521_Persist[2] = {0x43, 0x20};
00146 char        RPR0521_Addr_ReadData = 0x44;
00147 char        RPR0521_Content_ReadData[6];
00148 int         RPR0521_PS_RAWOUT = 0;                  //this is an output
00149 float       RPR0521_PS_OUT = 0;
00150 int         RPR0521_ALS_D0_RAWOUT = 0;
00151 int         RPR0521_ALS_D1_RAWOUT = 0;
00152 float       RPR0521_ALS_DataRatio = 0;
00153 float       RPR0521_ALS_OUT = 0;                    //this is an output
00154 float       RPR0521_ALS[2];                         // is this ok taking an int to the [0] value and float to [1]???????????
00155 #endif
00156 
00157 #ifdef KMX62
00158 int         KMX62_addr_w = 0x1C;          //7bit addr = 0x38, with write bit 0
00159 int         KMX62_addr_r = 0x1D;          //7bit addr = 0x38, with read bit 1
00160 char        KMX62_CNTL2[2] = {0x3A, 0x5F};
00161 char        KMX62_Addr_Accel_ReadData = 0x0A;
00162 char        KMX62_Content_Accel_ReadData[6];
00163 char        KMX62_Addr_Mag_ReadData = 0x10;
00164 char        KMX62_Content_Mag_ReadData[6];
00165 short int   MEMS_Accel_Xout = 0;
00166 short int   MEMS_Accel_Yout = 0;
00167 short int   MEMS_Accel_Zout = 0;
00168 double      MEMS_Accel_Conv_Xout = 0;
00169 double      MEMS_Accel_Conv_Yout = 0;
00170 double      MEMS_Accel_Conv_Zout = 0;
00171 
00172 short int   MEMS_Mag_Xout = 0;
00173 short int   MEMS_Mag_Yout = 0;
00174 short int   MEMS_Mag_Zout = 0;
00175 float       MEMS_Mag_Conv_Xout = 0;
00176 float       MEMS_Mag_Conv_Yout = 0;
00177 float       MEMS_Mag_Conv_Zout = 0;
00178 
00179 double      MEMS_Accel[3];
00180 float       MEMS_Mag[3];
00181 #endif
00182 
00183 #ifdef COLOR
00184 int         BH1745_addr_w = 0x72;   //write
00185 int         BH1745_addr_r = 0x73;   //read
00186 char        BH1745_persistence[2] = {0x61, 0x03};
00187 char        BH1745_mode1[2] = {0x41, 0x00};
00188 char        BH1745_mode2[2] = {0x42, 0x92};
00189 char        BH1745_mode3[2] = {0x43, 0x02};
00190 char        BH1745_Content_ReadData[6];
00191 char        BH1745_Addr_color_ReadData = 0x50;
00192 int         BH1745_Red;
00193 int         BH1745_Blue;
00194 int         BH1745_Green;
00195 int32_t     BH1745[3];  //Red, Blue Green matrix
00196 #endif
00197 
00198 #ifdef KX022
00199 int         KX022_addr_w = 0x3C;   //write
00200 int         KX022_addr_r = 0x3D;   //read
00201 char        KX022_Accel_CNTL1[2] = {0x18, 0x41};
00202 char        KX022_Accel_ODCNTL[2] = {0x1B, 0x02};
00203 char        KX022_Accel_CNTL3[2] = {0x1A, 0xD8};
00204 char        KX022_Accel_TILT_TIMER[2] = {0x22, 0x01};
00205 char        KX022_Accel_CNTL2[2] = {0x18, 0xC1};
00206 char        KX022_Content_ReadData[6];
00207 char        KX022_Addr_Accel_ReadData = 0x06;
00208 float       KX022_Accel_X;
00209 float       KX022_Accel_Y;
00210 float       KX022_Accel_Z;
00211 short int   KX022_Accel_X_RawOUT = 0;
00212 short int   KX022_Accel_Y_RawOUT = 0;
00213 short int   KX022_Accel_Z_RawOUT = 0;
00214 int         KX022_Accel_X_LB = 0;
00215 int         KX022_Accel_X_HB = 0;
00216 int         KX022_Accel_Y_LB = 0;
00217 int         KX022_Accel_Y_HB = 0;
00218 int         KX022_Accel_Z_LB = 0;
00219 int         KX022_Accel_Z_HB = 0;
00220 float       KX022_Accel[3];
00221 #endif
00222 
00223 #ifdef Pressure
00224 int         Press_addr_w = 0xBA;   //write
00225 int         Press_addr_r = 0xBB;   //read
00226 char        PWR_DOWN[2] = {0x12, 0x01};
00227 char        SLEEP[2] = {0x13, 0x01};
00228 char        Mode_Control[2] = {0x14, 0xC4};
00229 char        Press_Content_ReadData[6];
00230 char        Press_Addr_ReadData =0x1A;
00231 int         BM1383_Temp_highByte;
00232 int         BM1383_Temp_lowByte;
00233 int         BM1383_Pres_highByte;
00234 int         BM1383_Pres_lowByte;
00235 int         BM1383_Pres_leastByte;
00236 short int   BM1383_Temp_Out;
00237 float       BM1383_Temp_Conv_Out;
00238 float       BM1383_Pres_Conv_Out;
00239 float_t       BM1383[2];   // Temp is 0 and Pressure is 1
00240 float       BM1383_Var;
00241 float       BM1383_Deci;
00242 #endif
00243 
00244 /****************************************************************************************************
00245 // function prototypes
00246  ****************************************************************************************************/
00247 bool init_mtsas();
00248 void ReadAnalogTemp();
00249 void ReadAnalogUV ();
00250 void ReadHallSensor ();
00251 void ReadCOLOR ();
00252 void ReadRPR0521_ALS ();
00253 void ReadKMX62_Accel ();
00254 void ReadKMX62_Mag ();
00255 void ReadPressure ();
00256 void ReadKX022();
00257 char* httpResToStr(HTTPResult res);
00258 
00259 namespace patch
00260 {
00261 template < typename T > std::string to_string( const T& n )
00262 {
00263     std::ostringstream stm ;
00264     stm << n ;
00265     return stm.str() ;
00266 }
00267 }
00268 
00269 /****************************************************************************************************
00270 // main
00271  ****************************************************************************************************/
00272 int main()
00273 {
00274     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00275     debug.baud(debug_baud);
00276 
00277 // Initialization Radio Section **********************************************************
00278     logInfo("initializing cellular radio");
00279     if (!init_mtsas()) {
00280         while (true) {
00281             logError("failed to initialize cellular radio");
00282             wait(1);
00283         }
00284     }
00285 
00286     logInfo("Configuring http...\r\n");
00287     HTTPClient http;
00288     HTTPResult result;
00289     http.setHeader(EXOSITE_HEADER);
00290 
00291     char http_rx_buf[1024];
00292     // IHTTPDataIn object - will contain data received from server.
00293     HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf));
00294 
00295     // IHTTPDataOut object - contains data to be posted to server.
00296     HTTPMap http_tx;
00297 
00298 
00299 //****************************************************************************************************
00300 //        Initialize I2C Devices ************
00301 //****************************************************************************************************/
00302 
00303 #ifdef RPR0521
00304     i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false);
00305     i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false);
00306     i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false);
00307 #endif
00308 
00309 #ifdef KMX62
00310     i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false);
00311 #endif
00312 
00313 #ifdef COLOR
00314     i2c.write(BH1745_addr_w, &BH1745_persistence[0], 2, false);
00315     i2c.write(BH1745_addr_w, &BH1745_mode1[0], 2, false);
00316     i2c.write(BH1745_addr_w, &BH1745_mode2[0], 2, false);
00317     i2c.write(BH1745_addr_w, &BH1745_mode3[0], 2, false);
00318 #endif
00319 
00320 #ifdef KX022
00321     i2c.write(KX022_addr_w, &KX022_Accel_CNTL1[0], 2, false);
00322     i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false);
00323     i2c.write(KX022_addr_w, &KX022_Accel_CNTL3[0], 2, false);
00324     i2c.write(KX022_addr_w, &KX022_Accel_TILT_TIMER[0], 2, false);
00325     i2c.write(KX022_addr_w, &KX022_Accel_CNTL2[0], 2, false);
00326 #endif
00327 
00328 #ifdef Pressure
00329     i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false);
00330     i2c.write(Press_addr_w, &SLEEP[0], 2, false);
00331     i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
00332 #endif
00333 
00334 
00335 //****************************************************************************************************/
00336 //End I2C Initialization Section **********************************************************
00337 //****************************************************************************************************/
00338 
00339     Timer post_timer;
00340     post_timer.start();
00341     logInfo("Setup complete.");
00342     logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
00343 
00344 //*******************************************************************************************************/
00345 //Beging loop for Main
00346 //*******************************************************************************************************/
00347 
00348     while (true) {
00349         if (post_timer.read_ms() > post_interval_ms && do_cloud_post) {
00350             logInfo("bringing up the link");
00351 
00352 #ifdef AnalogTemp
00353             ReadAnalogTemp ();
00354 #endif
00355 
00356 #ifdef AnalogUV
00357             ReadAnalogUV ();
00358 #endif
00359 
00360 #ifdef HallSensor
00361             ReadHallSensor ();
00362 #endif
00363 
00364 #ifdef COLOR
00365             ReadCOLOR ();
00366 #endif
00367 
00368 #ifdef RPR0521       //als digital
00369             ReadRPR0521_ALS ();
00370 #endif
00371 
00372 #ifdef Pressure
00373             ReadPressure();
00374 #endif
00375          
00376 #ifdef KMX62
00377             ReadKMX62_Accel ();
00378             ReadKMX62_Mag ();
00379 #endif
00380 
00381 #ifdef KX022
00382             ReadKX022 ();
00383 #endif
00384 
00385             logDebug("%s", wall_of_dash);
00386             logDebug("SENSOR DATA");
00387             logDebug("temperature: %0.2f C", BM1383[0]);
00388             logDebug("analog uv: %.1f mW/cm2", ML8511_output);
00389             logDebug("ambient Light  %0.3f", RPR0521_ALS[0]);
00390             logDebug("proximity count  %0.3f", RPR0521_ALS[1]);
00391             logDebug("hall effect: South %d\t North %d",  Hall_Return[0],Hall_Return[1]);
00392             logDebug("pressure: %0.2f hPa", BM1383[1]);
00393             logDebug("magnetometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tuT", MEMS_Mag[0], MEMS_Mag[1], MEMS_Mag[2]);
00394             logDebug("accelerometer:\r\n\tx: %0.3f\ty: %0.3f\tz: %0.3f\tg", MEMS_Accel[0], MEMS_Accel[1], MEMS_Accel[2]);
00395             logDebug("color:\r\n\tred: %ld\tgrn: %ld\tblu: %ld\t", BH1745[0], BH1745[1], BH1745[2]);
00396             logDebug("%s", wall_of_dash);
00397 
00398 
00399 //*******************************************************************************************************/
00400 //Connect to the radio to send data
00401 //*******************************************************************************************************/
00402 
00403             if (radio->connect()) {
00404                 int sensor_data = RPR0521_ALS[0];
00405                 logDebug("posting sensor data");
00406                 logDebug("%d",sensor_data);
00407                 
00408                 std::string sensor_data_str = patch::to_string(sensor_data);
00409                 logDebug("Sensor data string: %s",sensor_data_str.c_str());
00410 
00411                 http_tx.clear();
00412                 http_tx.put(" YOUR_VARIABLE_HERE ", sensor_data_str.c_str());
00413 
00414                 // Make HTTP POST request
00415                 result = http.post(EXOSITE_URL, http_tx, &http_rx);
00416                 if (result != HTTP_OK) {
00417                     logError("HTTP POST failed [%d][%s]", result, httpResToStr(result));
00418                 } else {
00419                     logInfo("HTTP POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
00420                 }
00421 
00422                 radio->disconnect();
00423             } else {
00424                 logError("establishing PPP link failed");
00425             }
00426 
00427             post_timer.reset();
00428 
00429             logInfo("Waiting for %d ms to trigger connect...", post_interval_ms);
00430         }
00431     }
00432 }
00433 
00434 
00435 // init functions
00436 bool init_mtsas()
00437 {
00438     io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
00439     if (! io)
00440         return false;
00441 
00442     io->baud(115200);
00443     radio = CellularFactory::create(io);
00444     if (! radio)
00445         return false;
00446 
00447     Code ret = radio->setApn(apn);
00448     if (ret != MTS_SUCCESS)
00449         return false;
00450 
00451     Transport::setTransport(radio);
00452 
00453     return true;
00454 }
00455 
00456 char* httpResToStr(HTTPResult result)
00457 {
00458     switch(result) {
00459         case HTTP_PROCESSING:
00460             return "HTTP_PROCESSING";
00461         case HTTP_PARSE:
00462             return "HTTP_PARSE";
00463         case HTTP_DNS:
00464             return "HTTP_DNS";
00465         case HTTP_PRTCL:
00466             return "HTTP_PRTCL";
00467         case HTTP_NOTFOUND:
00468             return "HTTP_NOTFOUND";
00469         case HTTP_REFUSED:
00470             return "HTTP_REFUSED";
00471         case HTTP_ERROR:
00472             return "HTTP_ERROR";
00473         case HTTP_TIMEOUT:
00474             return "HTTP_TIMEOUT";
00475         case HTTP_CONN:
00476             return "HTTP_CONN";
00477         case HTTP_CLOSED:
00478             return "HTTP_CLOSED";
00479         case HTTP_REDIRECT:
00480             return "HTTP_REDIRECT";
00481         case HTTP_OK:
00482             return "HTTP_OK";
00483         default:
00484             return "HTTP Result unknown";
00485     }
00486 }
00487 
00488 
00489 //************************************************************************************************/
00490 // Sensor data acquisition functions
00491 //************************************************************************************************/
00492 #ifdef AnalogTemp
00493 void ReadAnalogTemp ()
00494 {
00495     BDE0600_Temp_value = BDE0600_Temp.read_u16();
00496 
00497     BDE0600_output = (float)BDE0600_Temp_value * (float)0.000050354; //(value * (3.3V/65535))
00498     BDE0600_output = (BDE0600_output-(float)1.753)/((float)-0.01068) + (float)30;
00499 
00500 //    printf("BDE0600 Analog Temp Sensor Data:\r\n");
00501 //    printf(" Temp = %.2f C\r\n", BDE0600_output);
00502 }
00503 #endif
00504 
00505 #ifdef AnalogUV
00506 void ReadAnalogUV ()
00507 {
00508     ML8511_UV_value = ML8511_UV.read_u16();
00509     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...
00510     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.
00511 
00512 //    printf("ML8511 Analog UV Sensor Data:\r\n");
00513 //    printf(" UV = %.1f mW/cm2\r\n", ML8511_output);
00514 
00515 }
00516 #endif
00517 
00518 
00519 #ifdef HallSensor
00520 void ReadHallSensor ()
00521 {
00522 
00523     Hall_Return[0] = Hall_GPIO0;
00524     Hall_Return[1] = Hall_GPIO1;
00525 
00526 //    printf("BU52011 Hall Switch Sensor Data:\r\n");
00527 //    printf(" South Detect = %d\r\n", Hall_Return[0]);
00528 //    printf(" North Detect = %d\r\n", Hall_Return[1]);
00529 
00530 
00531 }
00532 #endif
00533 
00534 #ifdef COLOR
00535 void ReadCOLOR ()
00536 {
00537 
00538     //Read color data from the IC
00539     i2c.write(BH1745_addr_w, &BH1745_Addr_color_ReadData, 1, RepStart);
00540     i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 6, NoRepStart);
00541 
00542     //separate all data read into colors
00543     BH1745[0] = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]);
00544     BH1745[1] = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]);
00545     BH1745[2] = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]);
00546 
00547     //Output Data into UART
00548 //    printf("BH1745 COLOR Sensor Data:\r\n");
00549 //    printf(" Red   = %d ADC Counts\r\n",BH1745[0]);
00550 //    printf(" Green = %d ADC Counts\r\n",BH1745[1]);
00551 //    printf(" Blue  = %d ADC Counts\r\n",BH1745[2]);
00552 
00553 }
00554 #endif
00555 
00556 #ifdef RPR0521       //als digital
00557 void ReadRPR0521_ALS ()
00558 {
00559     i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart);
00560     i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart);
00561 
00562     RPR0521_ALS[1] = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]);
00563     RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]);
00564     RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]);
00565     RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT;
00566 
00567     if(RPR0521_ALS_DataRatio < (float)0.595) {
00568         RPR0521_ALS[0] = ((float)1.682*(float)RPR0521_ALS_D0_RAWOUT - (float)1.877*(float)RPR0521_ALS_D1_RAWOUT);
00569     } else if(RPR0521_ALS_DataRatio < (float)1.015) {
00570         RPR0521_ALS[0] = ((float)0.644*(float)RPR0521_ALS_D0_RAWOUT - (float)0.132*(float)RPR0521_ALS_D1_RAWOUT);
00571     } else if(RPR0521_ALS_DataRatio < (float)1.352) {
00572         RPR0521_ALS[0] = ((float)0.756*(float)RPR0521_ALS_D0_RAWOUT - (float)0.243*(float)RPR0521_ALS_D1_RAWOUT);
00573     } else if(RPR0521_ALS_DataRatio < (float)3.053) {
00574         RPR0521_ALS[0] = ((float)0.766*(float)RPR0521_ALS_D0_RAWOUT - (float)0.25*(float)RPR0521_ALS_D1_RAWOUT);
00575     } else {
00576         RPR0521_ALS[0] = 0;
00577     }
00578 //    printf("RPR-0521 ALS/PROX Sensor Data:\r\n");
00579 //    printf(" ALS = %0.2f lx\r\n", RPR0521_ALS[0]);
00580 //    printf(" PROX= %0.2f ADC Counts\r\n", RPR0521_ALS[1]);     //defined as a float but is an unsigned.
00581 
00582 }
00583 #endif
00584 
00585 #ifdef KMX62
00586 void ReadKMX62_Accel ()
00587 {
00588     //Read Accel Portion from the IC
00589     i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart);
00590     i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart);
00591 
00592     //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
00593     //      However, because we need the signed value, we will adjust the value when converting to "g"
00594     MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]);
00595     MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]);
00596     MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]);
00597 
00598     //Note: Conversion to G is as follows:
00599     //      Axis_ValueInG = MEMS_Accel_axis / 1024
00600     //      However, since we did not remove the LSB previously, we need to divide by 4 again
00601     //      Thus, we will divide the output by 4096 (1024*4) to convert and cancel out the LSB
00602     MEMS_Accel[0] = ((float)MEMS_Accel_Xout/4096/2);
00603     MEMS_Accel[1] = ((float)MEMS_Accel_Yout/4096/2);
00604     MEMS_Accel[2] = ((float)MEMS_Accel_Zout/4096/2);
00605 
00606     // Return Data to UART
00607 //    printf("KMX62 Accel+Mag Sensor Data:\r\n");
00608 //    printf(" AccX= %0.2f g\r\n", MEMS_Accel[0]);
00609 //    printf(" AccY= %0.2f g\r\n", MEMS_Accel[1]);
00610 //    printf(" AccZ= %0.2f g\r\n", MEMS_Accel[2]);
00611 
00612 }
00613 
00614 void ReadKMX62_Mag ()
00615 {
00616 
00617     //Read Mag portion from the IC
00618     i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart);
00619     i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart);
00620 
00621     //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte.
00622     //      However, because we need the signed value, we will adjust the value when converting to "g"
00623     MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]);
00624     MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]);
00625     MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]);
00626 
00627     //Note: Conversion to G is as follows:
00628     //      Axis_ValueInG = MEMS_Accel_axis / 1024
00629     //      However, since we did not remove the LSB previously, we need to divide by 4 again
00630     //      Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB
00631     MEMS_Mag[0] = (float)MEMS_Mag_Xout/4096*(float)0.146;
00632     MEMS_Mag[1] = (float)MEMS_Mag_Yout/4096*(float)0.146;
00633     MEMS_Mag[2] = (float)MEMS_Mag_Zout/4096*(float)0.146;
00634 
00635     // Return Data to UART
00636 //    printf(" MagX= %0.2f uT\r\n", MEMS_Mag[0]);
00637 //    printf(" MagY= %0.2f uT\r\n", MEMS_Mag[1]);
00638 //    printf(" MagZ= %0.2f uT\r\n", MEMS_Mag[2]);
00639 
00640 }
00641 #endif
00642 
00643 #ifdef KX022
00644 void ReadKX022 ()
00645 {
00646 
00647     //Read KX022 Portion from the IC
00648     i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart);
00649     i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart);
00650 
00651     //Format Data
00652     KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]);
00653     KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]);
00654     KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]);
00655 
00656     //Scale Data
00657     KX022_Accel[0] = (float)KX022_Accel_X_RawOUT / 16384;
00658     KX022_Accel[1] = (float)KX022_Accel_Y_RawOUT / 16384;
00659     KX022_Accel[2] = (float)KX022_Accel_Z_RawOUT / 16384;
00660 
00661     //Return Data through UART
00662 //    printf("KX022 Accelerometer Sensor Data: \r\n");
00663 //    printf(" AccX= %0.2f g\r\n", KX022_Accel[0]);
00664 //    printf(" AccY= %0.2f g\r\n", KX022_Accel[1]);
00665 //    printf(" AccZ= %0.2f g\r\n", KX022_Accel[2]);
00666 
00667 }
00668 #endif
00669 
00670 
00671 #ifdef Pressure
00672 void ReadPressure ()
00673 {
00674 
00675     i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
00676     i2c.read(Press_addr_r, &Press_Content_ReadData[0], 6, NoRepStart);
00677 
00678     BM1383_Temp_Out = (Press_Content_ReadData[0]<<8) | (Press_Content_ReadData[1]);
00679     BM1383[0] = (float)BM1383_Temp_Out/32;
00680 
00681     BM1383_Var  = (Press_Content_ReadData[2]<<3) | (Press_Content_ReadData[3] >> 5);
00682     BM1383_Deci = ((Press_Content_ReadData[3] & 0x1f) << 6 | ((Press_Content_ReadData[4] >> 2)));
00683     BM1383_Deci = (float)BM1383_Deci* (float)0.00048828125;  //0.00048828125 = 2^-11
00684     BM1383[1] = (BM1383_Var + BM1383_Deci);   //question pending here...
00685 
00686 //    printf("BM1383 Pressure Sensor Data:\r\n");
00687 //    printf(" Temperature= %0.2f C\r\n", BM1383[0]);
00688 //    printf(" Pressure   = %0.2f hPa\r\n", BM1383[1]);
00689 
00690 }
00691 #endif