nRF51822_OBS + mocro:bit_ADV

Dependencies:   mbed BLE_API Adafruit_GFX nRF51822

Тесты по созданию связи между micro:bit и nRF51822 & BLE400 эта часть под nRF51822

Revision:
14:985bfbee997b
Parent:
13:ae46f7a91beb
Child:
15:23ce464fa0b5
--- a/main.cpp	Sun Dec 15 14:37:04 2019 +0000
+++ b/main.cpp	Mon Dec 16 13:25:59 2019 +0000
@@ -16,8 +16,8 @@
 
 #include "mbed.h"
 #include "BLE.h"
-//#include "TMP_nrf51/TMP_nrf51.h"
 #include "Adafruit_SSD1306.h"
+//#include "bme280.h"
 
 #define SDA P0_0
 #define SCL P0_1
@@ -35,12 +35,29 @@
 struct ApplicationData_t {
     uint16_t applicationSpecificId;             /* An ID used to identify temperature value
                                                    in the manufacture specific AD data field */
-    uint8_t tmpSensorValue; /* User defined application data */
+    uint8_t ExtT_Value; /* User defined application data */
 //    uint8_t btnA_Value;
 //    uint8_t btnB_Value;
 };
 #pragma pack()
 
+//Функции обработки событий кнопок
+void triggerfall_1();   //Button1 falling interrupt function
+void triggerrise_1();   //Button1 rising interrupt function
+void triggerfall_2();   //Button2 falling interrupt function
+void triggerrise_2();   //Button2 rising interrupt function
+
+DigitalIn  sw1(BUTTON1);
+DigitalIn  sw2(BUTTON2);
+
+//Initiate input interrupts
+InterruptIn sw1Press(BUTTON1);
+InterruptIn sw2Press(BUTTON2);
+int flag_button;
+//float t;
+//float pr;
+//float h;
+
 class I2C2 : public I2C
 {
 public:
@@ -55,10 +72,23 @@
 Adafruit_SSD1306_I2c gOled2(gI2C, NC, 0x78, 64, 128);
 
 BLE        ble;
+//BME280 bme280;
 DigitalOut led1(LED1);
 
 Serial pc(USBTX, USBRX);
+/*//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.
+    gI2C.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.
+    gI2C.read(i2c_address<<1,(char *)p_data,data_size);
+}
+*/
 void periodicCallback(void)
 {
     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
@@ -75,18 +105,31 @@
         if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
             ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
             if(pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
-                pc.printf("From [%02x %02x %02x %02x %02x %02x], ", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
-                if(params->peerAddr[0] == 0x38) {
-                    pc.printf("Temp is %.02f\r", (float)pAppData->tmpSensorValue);
-                    gOled2.setTextCursor(4, 16);
+//                pc.printf("From [%02x %02x %02x %02x %02x %02x], ", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
+                if(params->peerAddr[0] == 0x38 && flag_button == 1) {
+                    pc.printf("Temp is %.02f\r", (float)pAppData->ExtT_Value);
+//                    gOled2.clearDisplay();
+                    gOled2.drawRoundRect(0, 0, 127, 15, 3, 1);
+                    gOled2.drawRoundRect(1, 1, 125, 13, 2, 1);
+                    gOled2.drawRoundRect(0, 16, 127, 47, 3, 1);
+                    gOled2.drawRoundRect(1, 17, 125, 45, 2, 1);
+//    gOled2.drawRect(0, 0, 127, 63, 1);
+//    gOled2.drawRect(1, 1, 125, 61, 1);
+                    gOled2.display();
+                    gOled2.setTextCursor(4, 4);
+                    gOled2.setTextSize(1);
+                    gOled2.printf("Observer Init \r");
+                    gOled2.display();
+
+                    gOled2.setTextCursor(4, 22);
                     gOled2.setTextSize(2);
-                    gOled2.printf("Temp: %.01f", (float)pAppData->tmpSensorValue);
+                    gOled2.printf("ExtT: %.01f", (float)pAppData->ExtT_Value);
                     gOled2.display();
-                } else {
-                    pc.printf("XZ is %.02f\r", (float)pAppData->tmpSensorValue);
-                    gOled2.setTextCursor(4, 32);
+                } else if (params->peerAddr[0] != 0x38 && flag_button == 2) {
+                    pc.printf("XZ is %.02f\r", (float)pAppData->ExtT_Value);
+                    gOled2.setTextCursor(4, 22);
                     gOled2.setTextSize(2);
-                    gOled2.printf("XZ: 0x%02xh", pAppData->tmpSensorValue);
+                    gOled2.printf("IntT: 0x%02x", pAppData->ExtT_Value);
                     gOled2.display();
                 }
 //                pc.printf(" ButtonAB is %02x", pAppData->btnA_Value);
@@ -97,13 +140,35 @@
         len += (pAdvData->length + 1);
     }
 }
-
+/*
+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)
 {
+//Set falling and rising edge to apppropriate interrup function
+    sw1Press.fall(&triggerfall_1);
+    sw1Press.rise(&triggerrise_1);
+    sw2Press.fall(&triggerfall_2);
+    sw2Press.rise(&triggerrise_2);
+    flag_button = 0;
+
     led1 = 1;
     Ticker ticker;
     ticker.attach(periodicCallback, 1);
-
+/*
+    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.init();
     ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
     ble.gap().startScan(advertisementCallback);
@@ -112,15 +177,44 @@
     pc.printf("Observer Init \r\n");
 
     gOled2.clearDisplay();
-    gOled2.drawRect(0, 0, 127, 63, 1);
-    gOled2.drawRect(1, 1, 125, 61, 1);
-    gOled2.display();
-    gOled2.setTextCursor(4, 4);
-    gOled2.setTextSize(1);
-    gOled2.printf("Observer Init \r");
     gOled2.display();
 
     while (true) {
+/*        if (flag_button == 2) {
+            updateFromBME280();
+            gOled2.setTextCursor(4, 22);
+            gOled2.setTextSize(2);
+            gOled2.printf("IntT: %.01f", t);
+            gOled2.setTextCursor(4, 42);
+            gOled2.printf("IntP: %.01f", pr);
+            gOled2.display();
+        }
+*/
         ble.waitForEvent();
     }
 }
+
+//Button1 falling interrupt function
+void triggerfall_1()
+{
+    flag_button = flag_button++;
+    pc.printf("\r\nflag: %d\r\n", flag_button);
+    if (flag_button == 3) {
+        flag_button = 0;
+        gOled2.clearDisplay();
+        gOled2.display();
+    }
+}
+//Button1 rising interrupt function
+void triggerrise_1()
+{
+}
+
+//Button1 falling interrupt function
+void triggerfall_2()
+{
+}
+//Button1 rising interrupt function
+void triggerrise_2()
+{
+}