iotio

Fork of Nucleo_BLE_DemoApp by Cortex Challenge Team

Files at this revision

API Documentation at this revision

Comitter:
berlingeradam
Date:
Wed May 27 18:39:23 2015 +0000
Parent:
0:866f2c528c01
Child:
2:510cac0a0250
Commit message:
Factory function for easy initialization

Changed in this revision

DemoAppService.cpp Show annotated file Show diff for this revision Revisions of this file
DemoAppService.h Show annotated file Show diff for this revision Revisions of this file
--- a/DemoAppService.cpp	Sun May 10 15:41:38 2015 +0000
+++ b/DemoAppService.cpp	Wed May 27 18:39:23 2015 +0000
@@ -1,5 +1,12 @@
 #include "DemoAppService.h"
 
+
+#include "mbed.h"
+#include "BLEDevice.h"
+#include "DeviceInformationService.h"
+#include "UARTService.h"
+#include "Utils.h"
+
 const uint8_t DemoAppService::ServiceUUID[LENGTH_OF_LONG_UUID] = {
     0x6E, 0x40, 0x00, 0x10, 0xB5, 0xA3, 0xF3, 0x93,
     0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
@@ -18,4 +25,48 @@
 const uint8_t DemoAppService::slider2CharacteristicUUID[LENGTH_OF_LONG_UUID] = {
     0x6E, 0x40, 0x00, 0x13, 0xB5, 0xA3, 0xF3, 0x93,
     0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
-};
\ No newline at end of file
+};
+
+static BLEDevice  *ble;
+
+/* Callback called when the device is disconnected */
+static void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+  DEBUG("Disconnected!\n\r");
+  DEBUG("Restarting the advertising process\n\r");
+
+  ble->startAdvertising();
+  //connected = false;
+}
+
+/* Callback called when the device is connected */
+static void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason)
+{
+  DEBUG("Connected\r\n");
+
+  //connected = true;
+}
+
+const static char     DEVICE_NAME[]        = "BlueNRG_UART";
+DemoAppService *startDemoBLE(const char* name){
+    ble = new BLEDevice();
+    ble->init();
+/* Set callback functions */
+  ble->onDisconnection(disconnectionCallback);
+  ble->onConnection(connectionCallback);
+ 
+  DeviceInformationService deviceInfo(*ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+  /* setup advertising */
+  ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+  ble->setAdvertisingType          (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    
+  //ble->accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME            , (const uint8_t *)"BlueNRG_UART"          , sizeof("BlueNRG_UART") - 1);
+  //ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+  ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME             , (uint8_t *)name                   , strlen(name)+1);
+
+  /* Start advertising */
+  ble->setAdvertisingInterval(160);
+  ble->startAdvertising();
+ 
+  return new DemoAppService(*ble);
+}
\ No newline at end of file
--- a/DemoAppService.h	Sun May 10 15:41:38 2015 +0000
+++ b/DemoAppService.h	Wed May 27 18:39:23 2015 +0000
@@ -7,8 +7,14 @@
 #include "BLEDevice.h"
 #include "Utils.h"
 
+typedef void (*DemoAppCallback)(uint8_t event);
+
 class DemoAppService {
     public:
+        static const uint8_t EVENT_SLIDER1_CHANGED = 0x01;
+        static const uint8_t EVENT_SLIDER2_CHANGED = 0x02;
+        static const uint8_t EVENT_BUTTON_CHANGED = 0x04;
+    
         static const uint8_t ServiceUUID[LENGTH_OF_LONG_UUID];
         static const uint8_t slider1CharacteristicUUID[LENGTH_OF_LONG_UUID];
         static const uint8_t slider2CharacteristicUUID[LENGTH_OF_LONG_UUID];
@@ -16,6 +22,7 @@
     protected:
         BLEDevice &ble;
         
+        DemoAppCallback eventCallback;
         GattCharacteristic slider1Characteristic;
         GattCharacteristic slider2Characteristic;
         GattCharacteristic buttonCharacteristic;
@@ -26,27 +33,33 @@
     public:
         DemoAppService(BLEDevice &_ble) :
             ble(_ble),
-            slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 1, 2,
+            eventCallback(NULL),
+            slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 2, 2,
                          GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
-            slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 1, 2,
+            slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 2, 2,
                          GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
-            buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 1, 2,
+            buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 2, 2,
                          GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){
                              
             GattCharacteristic *charTable[] = {&slider1Characteristic, &slider2Characteristic, &buttonCharacteristic};
             GattService         demoService(ServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
-    
             ble.addService(demoService);
             ble.onDataWritten(this, &DemoAppService::onDataWritten);
         }
         
+        void setCallback(DemoAppCallback callback){eventCallback = callback;}
+        
+        void waitForEvent(void){ble.waitForEvent(); }
+        
         virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
+            uint8_t event = 0;
             DEBUG("Demo service onWrite\n\r");
             if (params->charHandle == slider1Characteristic.getValueAttribute().getHandle()) {
                 uint16_t bytesRead = params->len;
                 if (bytesRead == 2) {
                     memcpy(&slider1Value, params->data, 2);
                     DEBUG("Slider1: %d\n\r", slider1Value);
+                    event |= EVENT_SLIDER1_CHANGED;
                 }
             }
             else if (params->charHandle == slider2Characteristic.getValueAttribute().getHandle()) {
@@ -54,18 +67,25 @@
                 if (bytesRead == 2) {
                     memcpy(&slider2Value, params->data, 2);
                     DEBUG("Slider2: %d\n\r", slider2Value);
+                    event |= EVENT_SLIDER2_CHANGED;
                 }
             }
             else if (params->charHandle == buttonCharacteristic.getValueAttribute().getHandle()) {
                 uint16_t bytesRead = params->len;
                 if (bytesRead == 2) {
                     memcpy(&lastButtonPressed, params->data, 2);
+                    event |= EVENT_BUTTON_CHANGED;
                 }
             }
+            if(event && eventCallback){
+                eventCallback(event);
+            }
         }
         
         uint16_t getSlider1Value()const{return slider1Value;}
         uint16_t getSlider2Value()const{return slider2Value;}
 };
 
+DemoAppService *startDemoBLE(const char* name);
+
 #endif
\ No newline at end of file