BM1383, BH1745,KX122

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 
00002  * mbed Microcontroller Library
00003  * Copyright (c) 2006-2013 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017  
00018  //BALLOON GAME!
00019  /*
00020  *  Code Example for ROHM Sensor Expo Balloon Game
00021  * 
00022  *  Description: This Application interfaces ROHM's BM1383AGLV with Nordic's nRF51-DK
00023  *  This Code supports the following sensor devices:
00024  *      > BM1383AGLV Pressure Sensor
00025  *
00026  *  This Code allows the user to configure two known pressure distances and save pressure readings
00027  *  onto the application.  Then it will automatically extrapolate these values and allow the user to see
00028  *  the height of the board.  When connected to a balloon, greater heights can be achieved and the board
00029  *  will return the current height of the board.
00030  * 
00031  *  Last Upadtaed: 6/19/2016 
00032  *  Author: ROHM USDC
00033  *  Contact Information: engineering@rohmsemiconductor.com
00034  *  GitHub Link: https://github.com/ROHMUSDC/ROHM-SensorExpo2016-Pressure-Sensor-Demo/
00035  *  MBED Repository Link: https://developer.mbed.org/teams/ROHMUSDC/code/ROHMSensorShield_BALLOONGAME/
00036  */
00037 
00038 //#define nRF52DevKit         //Comment out and change target to Nordic NRf51822 for DemoBoard.  Comment in and change target to Nordic nRF51-DK for Nordic DK Board (and ROHM SHLD0 or SHLD1)
00039 //#define NRFDK               //Comment out and change target to Nordic NRf51822 for DemoBoard.  Comment in and change target to Nordic nRF51-DK for Nordic DK Board (and ROHM SHLD0 or SHLD1)
00040 #define Pressure            //BM1383, Barometric Pressure Sensor
00041 #define BM1383A             //Comment in for SHLD1. Comment out for SHLD0 and DemoBoard.
00042 #define LED_EN
00043 #define BH1745                                 
00044 #define KX022
00045 
00046 #include "mbed.h"
00047 #include "BLEDevice.h"
00048 #include "UARTService.h"
00049 #include "nrf_temp.h"
00050 #include "I2C.h"
00051 #include <string>
00052 
00053 #define MAX_REPLY_LEN           (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN)    //Actually equal to 20
00054 #define SENSOR_READ_INTERVAL_S  (1.0F) 
00055 #define ADV_INTERVAL_MS         (1000UL)
00056 #define UART_BAUD_RATE          (19200UL)
00057 #define DEVICE_NAME             ("ROHM DEMO  ") // This can be read AFTER connecting to the device.
00058 #define SHORT_NAME              ("BALLOON1")    // Keep this short: max 8 chars if a 128bit UUID is also advertised.
00059 #define DEBUG(...)              { m_serial_port.printf(__VA_ARGS__); }
00060 
00061 // Function Prototypes
00062 void PBTrigger();               //Interrupt function for PB4
00063 void BTLE_DataWrittenHandler();
00064 
00065 // Global Variables
00066 BLEDevice   m_ble;
00067 Serial      m_serial_port(p9, p11);  // TX pin, RX pin Original
00068 //Serial      m_serial_port(p8, p10);  // TX pin, RX pin 
00069 DigitalOut  m_cmd_led(LED1);
00070 DigitalOut  m_error_led(LED2);
00071 
00072 #ifdef LED_EN
00073 PwmOut      BlueLED(p14); // BlueLED Out
00074 PwmOut      GreenLED(p15); // GreenLED
00075 PwmOut      RedLED(p16); //Speed Output
00076 #endif
00077 
00078 UARTService *m_uart_service_ptr;
00079 DigitalIn   testButton(p20);
00080 InterruptIn sw4Press(p20);
00081 #ifdef NRFDK
00082 I2C         i2c(p30,p7);
00083 #endif
00084 #ifndef NRFDK
00085 I2C         i2c(p7,p30);
00086 #endif
00087 bool        RepStart = true;
00088 bool        NoRepStart = false;
00089 int         i = 1;
00090 unsigned char   printQue = 0;
00091 string      ReceivedValue;
00092 char        FormattedData[30];
00093 uint8_t     buf[MAX_REPLY_LEN];
00094 uint32_t    len = 0;
00095 
00096 //Sensor Variables
00097 #ifdef Pressure
00098 int         Press_addr_w = 0xBA;
00099 int         Press_addr_r = 0xBB;
00100 
00101 char        Press_Content_ReadData[6];
00102 #ifdef BM1383A
00103 char        Press_Addr_ReadData =0x1A;
00104 char        PWR_ON[2] = {0x12, 0x01};
00105 char        PWR_OFF[2] = {0x12, 0x00};
00106 char        SLEEP_OFF[2] = {0x13, 0x01};
00107 char        SLEEP_ON[2] = {0x13, 0x00};
00108 char        Mode_Control[2] = {0x14, 0xCA};
00109 #endif
00110 #ifndef BM1383A
00111 char        Press_Addr_ReadData =0x1C;
00112 char        PWR_DOWN[2] = {0x12, 0x01};
00113 char        SLEEP[2] = {0x13, 0x01};
00114 char        Mode_Control[2] = {0x14, 0xC4}; 
00115 #endif
00116 
00117 int         BM1383_Temp_highByte;
00118 int         BM1383_Temp_lowByte;
00119 int         BM1383_Pres_highByte;
00120 int         BM1383_Pres_lowByte;
00121 int         BM1383_Pres_leastByte; 
00122 
00123 short int   BM1383_Temp_Out;
00124 float       BM1383_Temp_Conv_Out;
00125 float       BM1383_Pres_Conv_Out;
00126 
00127 float       BM1383_Var;
00128 float       BM1383_Deci;
00129 
00130 uint32_t    BM1383_TempPressure;
00131 
00132 
00133 #endif
00134 #ifdef BH1745
00135 int         BH1745_addr_w = 0x72;
00136 int         BH1745_addr_r = 0x73;
00137 
00138 char        BH1745_CTRL2[2] = {0x42, 0x11};  
00139 
00140 char        BH1745_Content_ReadData[8]; 
00141 char        BH1745_Content_RED_ReadData[2];   
00142 char        BH1745_Content_GREEN_ReadData[2];   
00143 char        BH1745_Content_BLUE_ReadData[2];   
00144 char        BH1745_Content_CLEAR_ReadData[2];          
00145 
00146 float       BH1745_Red;
00147 float       BH1745_Green;
00148 float       BH1745_Blue;
00149 float       BH1745_Clear;
00150 
00151 float       SUM_BH1745_Red[10];
00152 float       SUM_BH1745_Green[10];
00153 float       SUM_BH1745_Blue[10];
00154 float       SUM_BH1745_Clear[10];
00155 
00156 double      colorSum;
00157 int         colorNum;
00158 
00159 unsigned short   BH1745_Raw_R = 0;
00160 unsigned short   BH1745_Raw_G = 0;
00161 unsigned short   BH1745_Raw_B = 0; 
00162 short int   BH1745_Raw_C = 0; 
00163 
00164 char        BH1745_Red_Data_LSB = 0x50;    
00165 char        BH1745_GREEN_Data_LSB = 0x52;    
00166 char        BH1745_BLUE_Data_LSB = 0x54;    
00167 char        BH1745_CLEAR_Data_LSB = 0x56;    
00168 
00169 
00170             int sampleNum=0;
00171             char message[20];
00172             float rValue, gValue, bValue, cValue;
00173             
00174             float colorError; 
00175 #endif
00176 
00177 #ifdef KX022
00178 int         KX022_addr_w = 0x3C;
00179 int         KX022_addr_r = 0x3D;
00180 
00181 char        KX022_Accel_CNTL1_CNTL[2] = {0x18, 0x00};
00182 char        KX022_Accel_ODCNTL[2] = {0x1B, 0x02};
00183 char        KX022_Accel_CNTL1_OPER[2] = {0x18, 0x80};
00184 //char        KX022_Accel_CNTL3[2] = {0x1A, 0xD8};
00185 //char        KX022_Accel_TILT_TIMER[2] = {0x22, 0x01};
00186 //char        KX022_Accel_CNTL2[2] = {0x18, 0xC1};
00187 
00188 char        KX022_Content_ReadData[6];
00189 char        KX022_Addr_Accel_ReadData = 0x06;           
00190 
00191 float       KX022_Accel_X;
00192 float       KX022_Accel_Y;                               
00193 float       KX022_Accel_Z;
00194 
00195 int16_t     KX022_Accel_X_RawOUT = 0;
00196 int16_t     KX022_Accel_Y_RawOUT = 0;
00197 int16_t     KX022_Accel_Z_RawOUT = 0;
00198 
00199 int         KX022_Accel_X_LB = 0;
00200 int         KX022_Accel_X_HB = 0;
00201 int         KX022_Accel_Y_LB = 0;
00202 int         KX022_Accel_Y_HB = 0;
00203 int         KX022_Accel_Z_LB = 0;
00204 int         KX022_Accel_Z_HB = 0;
00205 
00206 int         lockAccelSum,prevLockAccelSum, isAccelDiffCounter;
00207 int8_t      isAlarmTrig;
00208 int8_t      initConnect;
00209 #endif
00210 
00211 //Balloon Game Variables
00212             
00213             bool swapped;
00214             int  n;
00215             float buffer[80];
00216             float tempS;
00217             float initial_P;
00218             float final_P;
00219             float lengthMeter;
00220             unsigned char debugEnable = 0;
00221             unsigned char isCalibrated = 0; 
00222             unsigned char isMessageDisplayed = 0;
00223             unsigned int feet, inches;
00224             unsigned int gainFactor;
00225             float inchConversionCoefficient=300.0;
00226             unsigned int bufferSize = 80;
00227 float       BM1383_Pres_0Level;
00228 float       BM1383_Pres_KnownLevel;
00229 float       HeightBase = 0;
00230 float       HeightKnown_Total = 68;
00231 float       HeightKnown_Foot = 60;
00232 float       HeightKnown_Inches = 8;
00233 float       slope;
00234 float       yInt;
00235 float       pressureCurr;
00236 float       HeightExtrapolated; //Assuming linear pressure curve
00237 float       HeightExtrapolated_Foot = 60;
00238 float       HeightExtrapolated_Inches = 8;
00239 
00240 
00241 /**
00242  * This callback is used whenever a disconnection occurs.
00243  */
00244 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00245 {
00246     isCalibrated = 0; 
00247     switch (reason) {
00248     case Gap::REMOTE_USER_TERMINATED_CONNECTION:
00249         DEBUG("Disconnected (REMOTE_USER_TERMINATED_CONNECTION)\n\r");
00250         break;
00251     case Gap::LOCAL_HOST_TERMINATED_CONNECTION:
00252         DEBUG("Disconnected (LOCAL_HOST_TERMINATED_CONNECTION)\n\r");
00253         break;
00254     case Gap::CONN_INTERVAL_UNACCEPTABLE:
00255         DEBUG("Disconnected (CONN_INTERVAL_UNACCEPTABLE)\n\r");
00256         break;
00257     }
00258 
00259     DEBUG("Restarting the advertising process\n\r");
00260     m_ble.startAdvertising();
00261 }
00262 
00263 /**
00264  * This callback is used whenever the host writes data to one of our GATT characteristics.
00265  */
00266 void dataWrittenCallback(const GattCharacteristicWriteCBParams *params)
00267 {
00268     // Ensure that initialization is finished and the host has written to the TX characteristic.
00269     if ((m_uart_service_ptr != NULL) && (params->charHandle == m_uart_service_ptr->getTXCharacteristicHandle())) {
00270         int i;
00271         ReceivedValue.clear();
00272         for(i = 0; i < params->len; i++)
00273         {
00274             ReceivedValue += params->data[i];
00275         }
00276         printQue = 1; 
00277         //if(ReceivedValue[0] == '1')
00278         //        isCalibrated = 1; 
00279     }
00280 }
00281 
00282 /**
00283  * This callback is used whenever a write to a GATT characteristic causes data to be sent to the host.
00284  */
00285 void dataSentCallback(unsigned count)
00286 {
00287     // NOTE: The count always seems to be 1 regardless of data.
00288     DEBUG("%d bytes sent to host\n\r", count);
00289 }
00290 
00291 
00292 /**
00293  * This callback is scheduled to be called periodically via a low-priority interrupt.
00294  */
00295 void periodicCallback(void)
00296 {
00297     //uint8_t  buf[MAX_REPLY_LEN];
00298     //uint32_t len = 0;
00299 }
00300 
00301 void error(ble_error_t err, uint32_t line)
00302 {
00303     m_error_led = 1;
00304     DEBUG("Error %d on line number %d\n\r", err, line);
00305 }
00306 
00307 void PBTrigger()
00308 {
00309     uint8_t  buf[MAX_REPLY_LEN];
00310     uint32_t len = 0;
00311     
00312     m_cmd_led = !m_cmd_led;
00313     
00314     if (m_ble.getGapState().connected) {
00315         len = snprintf((char*) buf, MAX_REPLY_LEN, "Button Pressed!");
00316         m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00317     }
00318 }
00319 
00320 int main(void)
00321 {
00322     ble_error_t err;
00323     Ticker      ticker;
00324 
00325     m_serial_port.baud(UART_BAUD_RATE);
00326 
00327     DEBUG("Initialising...\n\r");
00328 
00329     m_cmd_led      = 0;
00330     m_error_led    = 0;
00331 
00332     ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S);
00333 
00334     sw4Press.fall(&PBTrigger);
00335 
00336     #ifdef BH1745
00337     i2c.write(BH1745_addr_w, &BH1745_CTRL2[0], 2, false); 
00338     #endif
00339     
00340     #ifdef Pressure  //no Initialization because we keep in low power mode until we need to measure pressure
00341     //i2c.write(Press_addr_w, &PWR_OFF[0], 2, false);
00342     //i2c.write(Press_addr_w, &SLEEP_ON[0], 2, false);
00343     //i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
00344     #endif
00345 
00346     #ifndef BM1383A
00347     i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false);
00348     i2c.write(Press_addr_w, &SLEEP[0], 2, false);
00349     i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
00350     #endif
00351     
00352     #ifdef KX022
00353     i2c.write(KX022_addr_w, &KX022_Accel_CNTL1_CNTL[0], 2, false);
00354     i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false);
00355     i2c.write(KX022_addr_w, &KX022_Accel_CNTL1_OPER[0], 2, false); 
00356     #endif
00357     //Start BTLE Initialization Section
00358     m_ble.init();
00359     m_ble.onDisconnection(disconnectionCallback);
00360     m_ble.onDataWritten(dataWrittenCallback);
00361     m_ble.onDataSent(dataSentCallback);
00362 
00363     // Set the TX power in dBm units.
00364     // Possible values (in decreasing order): 4, 0, -4, -8, -12, -16, -20.
00365     err = m_ble.setTxPower(4);
00366     if (BLE_ERROR_NONE != err) {
00367         error(err, __LINE__);
00368     }
00369 
00370     // Setup advertising (GAP stuff).
00371     err = m_ble.setDeviceName(DEVICE_NAME);
00372     if (BLE_ERROR_NONE != err) {
00373         error(err, __LINE__);
00374     }
00375 
00376     err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00377     if (BLE_ERROR_NONE != err) {
00378         error(err, __LINE__);
00379     }
00380 
00381     m_ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00382 
00383     err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00384                                                 (const uint8_t *)SHORT_NAME,
00385                                                 (sizeof(SHORT_NAME) - 1));
00386     if (BLE_ERROR_NONE != err) {
00387         error(err, __LINE__);
00388     }
00389 
00390     err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00391                                                 (const uint8_t *)UARTServiceUUID_reversed,
00392                                                 sizeof(UARTServiceUUID_reversed));
00393     if (BLE_ERROR_NONE != err) {
00394         error(err, __LINE__);
00395     }
00396 
00397     m_ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADV_INTERVAL_MS));
00398     m_ble.startAdvertising();
00399  
00400     
00401     #ifdef LED_EN 
00402     //Initialize PWM Output
00403     BlueLED.period_us(1000);
00404     BlueLED.pulsewidth_us(1000);
00405     wait_ms(50);
00406     BlueLED.pulsewidth_us(0);
00407     GreenLED.period_us(1000); 
00408     GreenLED.pulsewidth_us(1000);
00409     wait_ms(50);
00410     GreenLED.pulsewidth_us(0);
00411     RedLED.period_us(1000);
00412     RedLED.pulsewidth_us(1000);
00413     wait_ms(50);
00414     RedLED.pulsewidth_us(0);
00415     #endif
00416     // Create a UARTService object (GATT stuff).
00417     UARTService uartService(m_ble);
00418     m_uart_service_ptr = &uartService;
00419     while(!m_ble.getGapState().connected); 
00420     wait(1); 
00421     len = snprintf((char*) buf, MAX_REPLY_LEN, "Rohm Semiconductor");
00422     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00423     len = snprintf((char*) buf, MAX_REPLY_LEN, "Sensors EVK");
00424     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00425     wait(1); 
00426     len = snprintf((char*) buf, MAX_REPLY_LEN, "All outputs are");
00427     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00428     len = snprintf((char*) buf, MAX_REPLY_LEN, "not normalized.");
00429     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00430     wait(1); 
00431     len = snprintf((char*) buf, MAX_REPLY_LEN, "For design resources,");
00432     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00433     len = snprintf((char*) buf, MAX_REPLY_LEN, "Visit GITHUB:");
00434     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00435     wait(1); 
00436     len = snprintf((char*) buf, MAX_REPLY_LEN, "ROHMUSDC/ROHM-");
00437     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00438     len = snprintf((char*) buf, MAX_REPLY_LEN, "SensorExpo2016");
00439     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00440     wait(1); 
00441     len = snprintf((char*) buf, MAX_REPLY_LEN, "       ");
00442     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00443     len = snprintf((char*) buf, MAX_REPLY_LEN, "BM1383: Press '1'");
00444     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00445     len = snprintf((char*) buf, MAX_REPLY_LEN, "BH1745: Press '2'");
00446     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00447     len = snprintf((char*) buf, MAX_REPLY_LEN, "KX122:  Press '3'");
00448     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00449      
00450     #ifdef LED_EN 
00451     //Initialize PWM Output
00452     BlueLED.period_us(1000);
00453     BlueLED.pulsewidth_us(1000);
00454     wait_ms(50);
00455     BlueLED.pulsewidth_us(0);
00456     GreenLED.period_us(1000); 
00457     GreenLED.pulsewidth_us(1000);
00458     wait_ms(50);
00459     GreenLED.pulsewidth_us(0);
00460     RedLED.period_us(1000);
00461     RedLED.pulsewidth_us(1000);
00462     wait_ms(50);
00463     RedLED.pulsewidth_us(0);
00464     #endif
00465     isCalibrated = 0; 
00466     wait(1);
00467     
00468     isCalibrated = 1;
00469     
00470     inchConversionCoefficient = 300.0;
00471     while (true) {
00472         m_ble.waitForEvent();
00473         
00474         if(m_ble.getGapState().connected){ 
00475                    
00476                 //***************************** BM1383 **********************************
00477                     pressureCurr = 0;
00478                     #ifdef BM1383A
00479                     i2c.write(Press_addr_w, &PWR_ON[0], 2, false);
00480                     i2c.write(Press_addr_w, &SLEEP_OFF[0], 2, false);
00481                     i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
00482                     #endif 
00483                     Press_Content_ReadData[0] = 0;
00484                     Press_Content_ReadData[1] = 0;
00485                     Press_Content_ReadData[2] = 0;   
00486                     #ifdef Pressure
00487                         wait_ms(500);
00488                         i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
00489                         i2c.read(Press_addr_r, &Press_Content_ReadData[0], 3, NoRepStart);
00490                         BM1383_TempPressure = (Press_Content_ReadData[0]<<14)|(Press_Content_ReadData[1]<<6)|(Press_Content_ReadData[2]);
00491                         BM1383_Pres_Conv_Out = (float)BM1383_TempPressure / (float)2048;
00492                         wait_ms(500);
00493                     #endif
00494                           
00495                     #ifdef BM1383A
00496                     //i2c.write(Press_addr_w, &PWR_OFF[0], 2, false);
00497                     //i2c.write(Press_addr_w, &SLEEP_ON[0], 2, false);
00498                     #endif  
00499                 //*************************************************************************
00500                  
00501                 
00502                 
00503                 //***************************** BH1745 **********************************
00504                 #ifdef BH1745
00505                 i2c.write(BH1745_addr_w, &BH1745_Red_Data_LSB, 1, RepStart);
00506                 i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 8, NoRepStart);
00507                 //reconfigure the data (taken from arduino code)
00508                 BH1745_Raw_R = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]);
00509                 BH1745_Raw_G = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]);
00510                 BH1745_Raw_B = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]);  
00511                 BH1745_Raw_C = (BH1745_Content_ReadData[7]<<8) | (BH1745_Content_ReadData[6]);       
00512           
00513                 BH1745_Red = (float)BH1745_Raw_R / 16384; 
00514                 BH1745_Green = (float)BH1745_Raw_G / 16384; 
00515                 BH1745_Blue = (float)BH1745_Raw_B / 16384; 
00516                 BH1745_Clear = (float)BH1745_Raw_C / 16384;  
00517                 #endif
00518                 //*************************************************************************
00519                 
00520                 
00521                 
00522                 #ifdef KX022
00523                 //---------------------------- KX022 ----------------------------
00524                 //Read KX022 Portion from the IC
00525                 i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart);
00526                 i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart);
00527                     
00528                 //reconfigure the data (taken from arduino code)
00529                 KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]) ;
00530                 KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]) ;
00531                 KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]) ;  
00532                 //apply needed changes (taken from arduino code)
00533                 KX022_Accel_X = (float)KX022_Accel_X_RawOUT / 16384;
00534                 KX022_Accel_Y = (float)KX022_Accel_Y_RawOUT / 16384;
00535                 KX022_Accel_Z = (float)KX022_Accel_Z_RawOUT / 16384; 
00536                 //----------------------------------------------------------------
00537                 #endif
00538                 /*
00539                 #ifdef LED_EN 
00540                 //Initialize PWM Output
00541                 BlueLED.period_us(1000);
00542                 BlueLED.pulsewidth_us(1000);
00543                 wait_ms(50);
00544                 BlueLED.pulsewidth_us(0);
00545                 GreenLED.period_us(1000); 
00546                 GreenLED.pulsewidth_us(1000);
00547                 wait_ms(50);
00548                 GreenLED.pulsewidth_us(0);
00549                 RedLED.period_us(1000);
00550                 RedLED.pulsewidth_us(1000);
00551                 wait_ms(50);
00552                 RedLED.pulsewidth_us(0);
00553                 #endif
00554                 wait_ms(50);
00555                 */
00556         } 
00557         else{
00558             NVIC_SystemReset();
00559         }
00560         
00561         if(printQue)    //Handle Data Written Interrupt
00562         {
00563             #ifdef LED_EN 
00564             //Initialize PWM Output 
00565             BlueLED.pulsewidth_us(1000);
00566             wait_ms(50);
00567             BlueLED.pulsewidth_us(0); 
00568             GreenLED.pulsewidth_us(1000);
00569             wait_ms(50);
00570             GreenLED.pulsewidth_us(0); 
00571             RedLED.pulsewidth_us(1000);
00572             wait_ms(50);
00573             RedLED.pulsewidth_us(0);
00574             #endif
00575             BTLE_DataWrittenHandler();
00576             printQue = 0;
00577         }
00578     }
00579 }
00580 
00581 void BTLE_DataWrittenHandler(){    
00582     int i;
00583     if (ReceivedValue.length() == 1) {
00584         switch (ReceivedValue[0]) {  
00585             case '1':  
00586                 len = snprintf((char*) buf, MAX_REPLY_LEN, "*****************");
00587                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00588                 len = snprintf((char*) buf, MAX_REPLY_LEN, "> Pres= %0.3f hpa", BM1383_Pres_Conv_Out);
00589                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00590                 len = snprintf((char*) buf, MAX_REPLY_LEN, "*****************");
00591                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00592                 break;
00593             case '2': 
00594                  #ifdef BH1745
00595                 len = snprintf((char*) buf, MAX_REPLY_LEN,"*****************");
00596                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00597                 len = snprintf((char*) buf,MAX_REPLY_LEN, "> Red     = %0.2f", BH1745_Red*1000); 
00598                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00599                 len = snprintf((char*) buf,MAX_REPLY_LEN, "> Green = %0.2f", BH1745_Green*1000); 
00600                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00601                 len = snprintf((char*) buf,MAX_REPLY_LEN, "> Blue    = %0.2f", BH1745_Blue*1000); 
00602                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00603                 len = snprintf((char*) buf,MAX_REPLY_LEN, "> Clear  = %0.2f", BH1745_Clear*1000);
00604                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00605                 len = snprintf((char*) buf, MAX_REPLY_LEN,"*****************");
00606                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00607                 #endif
00608                 break;
00609             case '3':
00610                 #ifdef KX022 
00611                 len = snprintf((char*) buf, MAX_REPLY_LEN,"*****************");
00612                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00613                 len = snprintf((char*) buf,MAX_REPLY_LEN, "> AccelX  = %0.2f", KX022_Accel_X);
00614                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00615                 len = snprintf((char*) buf,MAX_REPLY_LEN, "> AccelY  = %0.2f", KX022_Accel_Y);
00616                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00617                 len = snprintf((char*) buf,MAX_REPLY_LEN, "> AccelZ  = %0.2f", KX022_Accel_Z);
00618                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00619                 len = snprintf((char*) buf, MAX_REPLY_LEN,"*****************");
00620                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00621                 #endif
00622                 break;
00623             case 'x': //System Reset
00624                 NVIC_SystemReset(); //Q. What is this? no break statement?
00625                                     //A. SoftReset... No need for break because this resets the program...
00626                 break;
00627                 
00628             default:
00629                 len = snprintf((char*) buf, MAX_REPLY_LEN, "1b,ERROR");
00630                 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00631                 break;
00632         }
00633     } 
00634     wait_ms(1000);
00635     len = snprintf((char*) buf, MAX_REPLY_LEN, "BM1383: Press '1'");
00636     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00637     len = snprintf((char*) buf, MAX_REPLY_LEN, "BH1745: Press '2'");
00638     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 
00639     len = snprintf((char*) buf, MAX_REPLY_LEN, "KX122:  Press '3'");
00640     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00641   
00642     /*
00643     else if (ReceivedValue.length() > 1) {
00644         
00645         //BTLE Interface Code, Added 5/29/2016
00646         for(i = 0; i < 29; i++)
00647         {
00648             FormattedData[i] = '\0';
00649         }
00650         
00651         if(ReceivedValue.compare(0,4,"CAL0") == 0)
00652         {
00653         }
00654         
00655         else if(ReceivedValue.compare(0,4,"CAL1") == 0)
00656         {               
00657         }
00658         
00659         else if(ReceivedValue.compare(0,4,"HEI?") == 0)
00660         {
00661             HeightKnown_Total = HeightKnown_Foot + HeightKnown_Inches;
00662             slope = (HeightKnown_Total - HeightBase) / (BM1383_Pres_KnownLevel - BM1383_Pres_0Level);
00663             yInt = HeightBase - (slope * BM1383_Pres_0Level);
00664             pressureCurr = 0;
00665             #ifdef BM1383A
00666             i2c.write(Press_addr_w, &PWR_ON[0], 2, false);
00667             i2c.write(Press_addr_w, &SLEEP_OFF[0], 2, false);
00668             i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
00669             #endif
00670             #ifndef BM1383A
00671             //i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false);
00672             //i2c.write(Press_addr_w, &SLEEP[0], 2, false);
00673             //i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
00674             #endif
00675             Press_Content_ReadData[0] = 0;
00676             Press_Content_ReadData[1] = 0;
00677             Press_Content_ReadData[2] = 0;
00678             for(i = 0; i < 10; i++)
00679             {
00680                 #ifdef Pressure
00681                 wait_ms(200);
00682                 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
00683                 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 3, NoRepStart);
00684                 BM1383_TempPressure = (Press_Content_ReadData[0]<<14)|(Press_Content_ReadData[1]<<6)|(Press_Content_ReadData[2]);
00685                 BM1383_Pres_Conv_Out = (float)BM1383_TempPressure / (float)2048;
00686                 #endif
00687                 pressureCurr += BM1383_Pres_Conv_Out;
00688             }
00689             #ifdef BM1383A
00690             i2c.write(Press_addr_w, &PWR_OFF[0], 2, false);
00691             i2c.write(Press_addr_w, &SLEEP_ON[0], 2, false);
00692             #endif
00693             pressureCurr = pressureCurr/10;
00694             
00695             HeightExtrapolated = ((pressureCurr * slope) + yInt)/12;
00696             //len = snprintf((char*) buf, MAX_REPLY_LEN, "Height=%f FT",HeightExtrapolated);
00697             //m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00698             
00699             HeightExtrapolated_Foot = int(HeightExtrapolated);
00700             HeightExtrapolated_Inches = (HeightExtrapolated - HeightExtrapolated_Foot)* 12;
00701             len = snprintf((char*) buf, MAX_REPLY_LEN, "H=%.0fFT, %.0fIN",HeightExtrapolated_Foot,HeightExtrapolated_Inches);
00702         }
00703         
00704         else if(ReceivedValue.compare(0,2,"FT") == 0)
00705         {
00706             HeightKnown_Foot = (ReceivedValue[2]-48) * 12;  //Convert ASCII to INT, then Convert to Inches
00707             sprintf(FormattedData, "%2.0f", HeightKnown_Foot);
00708             len = snprintf((char*) buf, MAX_REPLY_LEN, "FT = %s inchs", FormattedData);
00709             wait_ms(200);
00710         }
00711         
00712         else if(ReceivedValue.compare(0,2,"IN") == 0)
00713         {
00714             if((ReceivedValue[3]>= 48) && (ReceivedValue[3]<=57)){
00715                 HeightKnown_Inches = ((ReceivedValue[2]-48)*10) + (ReceivedValue[3]-48);    //Convert ASCII to INT, then Convert to Inches  
00716                 
00717             }
00718             else{
00719                 HeightKnown_Inches = (ReceivedValue[2]-48); //Convert ASCII to INT
00720             }
00721             sprintf(FormattedData, "%2.0f", HeightKnown_Inches);
00722             len = snprintf((char*) buf, MAX_REPLY_LEN, "IN = %s inchs", FormattedData);
00723             wait_ms(200);
00724         }
00725         
00726         else
00727         {
00728             len = snprintf((char*) buf, MAX_REPLY_LEN, "??? = %s", ReceivedValue.c_str());
00729             //m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00730         } 
00731     }
00732     else
00733     {
00734         len = snprintf((char*) buf, MAX_REPLY_LEN, "ERR:NUL");
00735     }
00736     m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
00737     */
00738 }