Переделал под свой датчик bme280 и под свою платку ble400

Dependencies:   mbed BLE_API nRF51822

Revision:
7:5acbc9a636d3
Parent:
6:25ed8e2df02e
--- a/main.cpp	Wed Jan 10 16:42:44 2018 +0000
+++ b/main.cpp	Fri Nov 29 15:11:07 2019 +0000
@@ -1,34 +1,50 @@
-#define I2C_SDA p29
-#define I2C_SCL p28
-
 #include "mbed.h"
 #include "BLE.h"
 #include "WeatherService.h"
-#include "BMP180.h"
+#include "bme280.h"
+#define I2C_SDA P0_0
+#define I2C_SCL P0_1
 
-float temperature1;
-float pressure;
+float t;
+float pr;
+float h;
+
+uint8_t address = 0x78;
+
+I2C i2c(I2C_SDA, I2C_SCL);
 
 BLE ble;
 
 DigitalOut okLED(LED1);
 DigitalOut errLED(LED2);
-DigitalOut instrumentsPower(p30);
+DigitalOut instrumentsPower(P0_22);
 
-BMP180 bmp180;
-
+BME280 bme280;
 
 const static char DEVICE_NAME[] = "Weather Station";
 
 static const uint16_t serviceList[] = {
     GattService::UUID_ENVIRONMENTAL_SERVICE
-    };
+};
 
 static volatile bool triggerSensorPolling = false;
 
+//bme280
+void i2cWrite(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size, uint8_t repeated_start)
+{
+// mbed uses 8-bit addresses, always confusing.
+    i2c.write(i2c_address<<1,(const char *)p_data,data_size,repeated_start);
+}
+
+void i2cRead(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size)
+{
+// mbed uses 8-bit addresses, always confusing.
+    i2c.read(i2c_address<<1,(char *)p_data,data_size);
+}
+
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    /* Restart Advertising on disconnection*/
+/* Restart Advertising on disconnection*/
     ble.gap().startAdvertising();
 }
 
@@ -38,41 +54,33 @@
     triggerSensorPolling = true;
 }
 
-void updateFromBMP180() {
-    uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I);   
-    
-    printf("BMP-180 is 0x%x\n\r", c);
-    printf("BMP-180 should be 0x55\n");
-    
-    if(c == 0x55) {
-        printf("BMP-180 online\n");
-       
-        printf("Calibrating BMP-180...");
-        bmp180.BMP180Calibration();
-        printf("done\n");
-    }
-    else 
-    {
-        printf("BMP-180 unreachable\n");
-        return;
-    }    
-    
-    temperature1 = (float)bmp180.BMP180GetTemperature()/10.0f;
-    pressure = (float)bmp180.BMP180GetPressure();
+void updateFromBME280()
+{
+    bme280.read();
+    t = bme280.temperature();
+//    float p = bme280.pressure()/100;
+//    pr = p*0.750062;
+    pr = bme280.pressure();
+    h = bme280.humidity();
 }
 
 
 int main(void)
 {
-    printf("Start\n");
-    
-    okLED = 1;
-    errLED = 1;
+    okLED = 0;
+    errLED = 0;
 
     Ticker ticker;
     ticker.attach(blink, 5);
 
     ble.init();
+
+    bme280.begin(BME280_I2C_ADDRESS1);
+    // Configure for test purposes.
+    bme280.writeConfigRegister(BME280_STANDBY_500_US,BME280_FILTER_OFF,0);
+    bme280.writeControlRegisters(BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_OVERSAMPLING_1X,BME280_MODE_NORMAL);
+
+
     ble.gap().onDisconnection(disconnectionCallback);
 
     /* Setup weather service. */
@@ -94,20 +102,15 @@
 
             instrumentsPower = 1;
             wait(1);
-            updateFromBMP180();
+            updateFromBME280();
             instrumentsPower = 0;
-            
-            float temperature = (temperature1)/ 2;
-            
-            printf("Temp1: %.1f ºC \n", temperature1);
-            printf("Temp Avg.: %.1f ºC \n", temperature);
-            printf("Pressure: %.3f Pa \n", pressure);
-            printf("---\n");
- 
-            weatherService.updateTemperature(temperature);
-            weatherService.updatePressure(pressure);
+
+            weatherService.updateTemperature(t);
+            weatherService.updatePressure(pr);
+            weatherService.updateHumidity(h);
 
         } else {
+            okLED = 0;
             ble.waitForEvent();
         }
     }