HTM Demo of BLE with Microbit

Dependencies:   microbit

Revision:
2:7c86dc4b74a8
Parent:
1:7623f55dd990
--- a/main.cpp	Sat Aug 10 06:53:10 2019 +0000
+++ b/main.cpp	Sat Aug 10 07:46:21 2019 +0000
@@ -32,6 +32,8 @@
 Ticker ticker;
 Serial  pc(USBTX, USBRX);
 
+bool isConnect = false;
+
 /* Health Thermometer Service */ 
 float temperature = 25.0;
 uint8_t             thermTempPayload[5] = { 0, 0, 0, 0, 0 };
@@ -59,9 +61,11 @@
 static Gap::ConnectionParams_t connectionParams;
 
 uint32_t quick_ieee11073_from_float(float temperature);
+void updateServiceValues(void);
 
 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
+    isConnect = true;
     pc.printf("connected. Got handle %u\r\n", params->handle);
 
     connectionParams.slaveLatency = 1;
@@ -72,9 +76,11 @@
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
+    isConnect = false;
     pc.printf("Disconnected handle %u, reason %u\r\n", params->handle, params->reason);
     pc.printf("Restarting the advertising process\r\n");
 
+    pc.printf("Start Advertising\r\n");
     uBit.ble->gap().startAdvertising();
 }
 
@@ -108,27 +114,25 @@
     uBit.ble->gattServer().addService(htmService);
     uBit.ble->gattServer().addService(battService);
     pc.printf("Add Service\r\n");
-
+    
     ticker.attach(periodicCallback, 1);
 
     // Insert your code here!
     while(1)
     {
+        if(isConnect)
+        {
+            uBit.display.printChar('C');
+        }
+        else
+        {
+            uBit.display.printChar('A');            
+        }   
+        
         //uBit.display.scroll("ADV");
         if (triggerSensorPolling) {
             triggerSensorPolling = false;
-            
-            // Decrement the battery level.
-            batt <=50 ? batt=100 : batt--;
-            pc.printf("Batt is %u\r\n", batt);
-            
-          /* Update the temperature. Note that we need to convert to an ieee11073 format float. */
-            temperature >= 30.0 ? temperature = 20.0 : temperature+=0.2;
-            pc.printf("temp:%f\r\n", temperature);
-            uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
-            memcpy(thermTempPayload+1, &temp_ieee11073, 4);
-            uBit.ble->gattServer().write(tempChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload));  //Mod
-            uBit.ble->gattServer().write(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt));             //Mod
+            updateServiceValues();
         } else {
             uBit.ble->waitForEvent();
         }
@@ -140,6 +144,26 @@
     //release_fiber();
 }
 
+/**************************************************************************/
+/*!
+    @brief  Ticker callback to switch advertisingStateLed state
+*/
+/**************************************************************************/
+void updateServiceValues(void)
+{
+    // Decrement the battery level.
+    batt <=50 ? batt=100 : batt--;
+    pc.printf("Batt is %u\r\n", batt);
+    
+    /* Update the temperature. Note that we need to convert to an ieee11073 format float. */
+    temperature >= 30.0 ? temperature = 20.0 : temperature+=0.2;
+    pc.printf("temp:%f\r\n", temperature);
+    uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
+    memcpy(thermTempPayload+1, &temp_ieee11073, 4);
+    uBit.ble->gattServer().write(tempChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload));  //Mod
+    uBit.ble->gattServer().write(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt));             //Mod
+}
+
 /**
  * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
  * @param temperature The temperature as a float.