Temp and Pressure get on BMP180

Dependencies:   BLE_API BMP180 mbed nRF51822

Fork of nRF51822_SimpleControls by RedBearLab

Revision:
4:b97fd2ecd539
Parent:
3:823f105078c7
--- a/main.cpp	Thu Jan 07 02:49:37 2016 +0000
+++ b/main.cpp	Fri Jun 10 18:25:44 2016 +0000
@@ -18,6 +18,7 @@
 */
 
 #include "mbed.h"
+#include "BMP180.h"
 #include "ble/BLE.h"
 #include "Servo.h"
 #include "GattCallbackParamTypes.h"
@@ -34,6 +35,9 @@
 #define SERVO_PIN                        P0_14  //D10
 #define ANALOG_IN_PIN                    P0_6   //A5
 
+I2C i2c(D14, D15);
+BMP180 bmp180(&i2c);
+
 BLE             ble;
 
 DigitalOut      LED_SET(DIGITAL_OUT_PIN);
@@ -101,10 +105,31 @@
         }
         else if(buf[0] == 0xA0)
         {
-            if(buf[1] == 0x01)
-                analog_enabled = 1;
-            else
-                analog_enabled = 0;
+            int pres = 0;
+            bmp180.startPressure(BMP180::ULTRA_LOW_POWER);
+            wait_ms(5);
+            if(bmp180.getPressure(&pres) != 0)
+                printf("Error getting temperature\r\n");
+            uint8_t buf[3];
+            uint16_t value = pres/100; 
+            buf[0] = (0x0B);
+            buf[1] = (value >> 8);
+            buf[2] = (value);
+            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);    
+        }
+        else if(buf[0] == 0xA1)
+        {    
+            float temp = 0;
+            bmp180.startTemperature();
+            wait_ms(5);
+            if(bmp180.getTemperature(&temp) != 0)
+                printf("Error getting temperature\r\n");
+            uint8_t buf[3];
+            uint16_t value = temp; 
+            buf[0] = (0x0B);
+            buf[1] = (value >> 8);
+            buf[2] = (value);
+            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);     
         }
         else if(buf[0] == 0x02)
         {
@@ -123,69 +148,13 @@
             LED_SET = 0;
             old_state = 0;    
         }
-
-    }
-}
-/*
-void uartCB(void)
-{   
-    while(pc.readable())    
-    {
-        rx_buf[rx_len++] = pc.getc();    
-        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
-        {
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
-            pc.putc(rx_len);
-            pc.printf("\r\n");
-            rx_len = 0;
-            break;
-        }
-    }
-}
-*/
-void m_status_check_handle(void)
-{   
-    uint8_t buf[3];
-    if (analog_enabled)  // if analog reading enabled
-    {
-        // Read and send out
-        float s = ANALOG;
-        uint16_t value = s*1024; 
-        buf[0] = (0x0B);
-        buf[1] = (value >> 8);
-        buf[2] = (value);
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
-    }
-    
-    // If digital in changes, report the state
-    if (BUTTON != old_state)
-    {
-        old_state = BUTTON;
-        
-        if (BUTTON == 1)
-        {
-            buf[0] = (0x0A);
-            buf[1] = (0x01);
-            buf[2] = (0x00);    
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
-        }
-        else
-        {
-            buf[0] = (0x0A);
-            buf[1] = (0x00);
-            buf[2] = (0x00);
-           ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
-        }
     }
 }
 
-
 int main(void)
 {   
-    Ticker ticker;
-    ticker.attach_us(m_status_check_handle, 200000);
+    //Ticker ticker;
+    //ticker.attach_us(m_status_check_handle, 200000);
     
     ble.init();
     ble.onDisconnection(disconnectionCallback);
@@ -200,7 +169,7 @@
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
+                                    (const uint8_t *)"Pepito", sizeof("Pepito") - 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. 
@@ -212,40 +181,20 @@
     
     pc.printf("Advertising Start \r\n");
     
+    
+    while(1) {
+        if (bmp180.init() != 0) {
+            printf("Error communicating with BMP180\r\n");
+        } else {
+            printf("Initialized BMP180\r\n");
+            break;
+        }
+        wait(1);
+    }
+    
+    
     while(1)
-    {
+    {        
         ble.waitForEvent(); 
     }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+}
\ No newline at end of file