Step3

Dependencies:   BLE_API BMP085 BMP180 I2C_Driver mbed nRF51822

Fork of nRF51822_SimpleControls by Kath VARGAS

Revision:
6:90c63672a17c
Parent:
5:55d6789683d5
--- a/main.cpp	Sun Jan 01 18:27:03 2017 +0000
+++ b/main.cpp	Tue Jan 10 23:06:12 2017 +0000
@@ -1,89 +1,66 @@
 /*
-
 Copyright (c) 2012-2014 RedBearLab
-
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 and associated documentation files (the "Software"), to deal in the Software without restriction, 
 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
 */
-
 #include "mbed.h"
 #include "ble/BLE.h"
 #include "Servo.h"
 #include "GattCallbackParamTypes.h"
-#include "BMP180.h"
-//#include "BMP"
-I2C i2c(D14, D15);
-BMP180 bmp180(&i2c);
-
+#include "BMP180.h" //
 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
-
 #define TXRX_BUF_LEN                     20
-
 #define DIGITAL_OUT_PIN                  P0_17  //D7
 #define DIGITAL_IN_PIN                   P0_5   //A4
 #define PWM_PIN                          P0_16  //D6
 #define SERVO_PIN                        P0_14  //D10
-#define ANALOG_IN_PIN                    P0_6   //A5
-
+#define ANALOG_IN_PIN                    P0_1   //A0
+//
+I2C i2c(D14, D15);
+BMP180 bmp180(&i2c);
 BLE             ble;
-
 DigitalOut      LED_SET(DIGITAL_OUT_PIN);
 DigitalIn       BUTTON(DIGITAL_IN_PIN);
 PwmOut          PWM(PWM_PIN);
 AnalogIn        ANALOG(ANALOG_IN_PIN);
 Servo           MYSERVO(SERVO_PIN);
-
 Serial pc(USBTX, USBRX);
-
 static uint8_t analog_enabled = 0;
-static uint8_t temperature_enabled=0;
-static uint8_t pressure_enabled=0;
+static uint8_t temp_enabled = 0;
+static uint8_t press_enabled = 0;
 static uint8_t old_state = 0;
-
 // The Nordic UART Service
 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
-
-
 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
-
 //static uint8_t rx_buf[TXRX_BUF_LEN];
 //static uint8_t rx_len=0;
-
-
 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
                                       
 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
                                       
 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
-
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
-
-
-
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     pc.printf("Disconnected \r\n");
     pc.printf("Restart advertising \r\n");
     ble.startAdvertising();
 }
-
 void WrittenHandler(const GattWriteCallbackParams *Handler)
 {   
     uint8_t buf[TXRX_BUF_LEN];
@@ -98,20 +75,33 @@
         for(index=0; index<bytesRead; index++)
             pc.putc(buf[index]);
             
-        if(buf[0] == 0x01)
+        if(buf[0] == 0x01) //Activer LED
         {
             if(buf[1] == 0x01)
                 LED_SET = 1;
             else
                 LED_SET = 0;    
         }
-        else if(buf[0] == 0xA0)
+        else if(buf[0] == 0xA0) //Activer potentiometre
         {
             if(buf[1] == 0x01)
                 analog_enabled = 1;
             else
-                analog_enabled = 0;   
-            
+                analog_enabled = 0;
+        }
+        else if(buf[0] == 0xB0) //Activer capteur temp
+        {
+            if(buf[1] == 0x01)
+                temp_enabled = 1;
+            else
+                temp_enabled = 0;
+        }
+        else if(buf[0] == 0xB1)  //Activer capteur press
+        {
+            if(buf[1] == 0x01)
+                press_enabled = 1;
+            else
+                press_enabled = 0;
         }
         else if(buf[0] == 0x02)
         {
@@ -125,29 +115,13 @@
         else if(buf[0] == 0x04)
         {
             analog_enabled = 0;
+            temp_enabled = 0;
+            press_enabled = 0;
             PWM = 0;
             MYSERVO.write(0);
             LED_SET = 0;
             old_state = 0;    
         }
-        
-        else if(buf[0]== 0xA1)
-        {
-            if(buf[1]==0x01)
-            
-                temperature_enabled = 1;
-            else
-                temperature_enabled = 0;
-        }
-        
-        else if(buf[0]== 0xA2)
-        {
-            if(buf[1]==0x01)
-                pressure_enabled = 1;
-            else
-                pressure_enabled = 0;
-        }   
-
     }
 }
 /*
@@ -182,34 +156,42 @@
         buf[2] = (value);
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
     }
-    if(temperature_enabled)
+    
+    if (temp_enabled)  // if I2C temp reading enabled
     {
         bmp180.startTemperature();
-        wait_ms(5);
-        
+        wait_ms(5);     // Wait for conversion to complete
         float temp;
-        bmp180.getTemperature(&temp);    
-        uint16_t value=temp*1024;
-        buf[0]=(0x0B);
-        buf[1]=(value >> 8);
-        buf[2]=(value);
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
-       // printf("Tempreature: %f \r\n", temp);
-        
+        if(bmp180.getTemperature(&temp) != 0) {
+            printf("Error getting temperature\r\n");
+            //continue;
+        }
+        // Read and send out
+        float s = temp;
+        uint16_t value = s*1024; 
+        buf[0] = (0x0C);
+        buf[1] = (value >> 8);
+        buf[2] = (value);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
     }
-    if(pressure_enabled)        
+    
+    
+    if (press_enabled)  // if I2C press reading enabled
     {
-       bmp180.startPressure(BMP180::ULTRA_LOW_POWER);
-       wait_ms(10);
-       
-        int pres;
-        bmp180.getPressure(&pres);
-        uint16_t value=pres*1024;
-        buf[0]=(0x0B);
-        buf[1]=(value>>8);
-        buf[2]=(value);
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
-        //printf("Pressure: %d \r\n", pres);
+        bmp180.startPressure(BMP180::ULTRA_LOW_POWER);
+        wait_ms(10);    // Wait for conversion to complete
+        int pressure;
+        if(bmp180.getPressure(&pressure) != 0) {
+            printf("Error getting pressure\r\n");
+            //continue;
+        }
+        // Read and send out
+        float s = pressure;
+        uint16_t value = s*1024; 
+        buf[0] = (0x0D);
+        buf[1] = (value >> 8);
+        buf[2] = (value);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
     }
     
     // If digital in changes, report the state
@@ -246,32 +228,40 @@
     
     pc.baud(9600);
     pc.printf("SimpleChat Init \r\n");
-
     //pc.attach( uartCB , pc.RxIrq);
     
     // setup advertising 
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"FaKa123", sizeof("FaKa123") - 1);
+                                    (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
     // 100ms; in multiples of 0.625ms. 
     ble.setAdvertisingInterval(160);
-
     ble.addService(uartService);
     
     ble.startAdvertising(); 
     
     pc.printf("Advertising Start \r\n");
     
-    bmp180.init();
+    while(1) 
+    {
+            if (bmp180.init() != 0) {
+                printf("Error communicating with BMP180\r\n");
+                } else {
+                    printf("Initialized BMP180\r\n");
+                    break;
+                    }
+                    wait(1);
+    }
     
     while(1)
     {
-        //bmp180.init();
         ble.waitForEvent(); 
     }
+    
+    //*************************************************************************************
 }
 
 
@@ -304,4 +294,3 @@
 
 
 
-