BLE GATT Button on Board Example Nucleo IDB0XA1

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_GAP_Example by Bluetooth Low Energy

Revision:
14:4af58166825c
Parent:
13:827dd2b32bb8
--- a/main.cpp	Thu Apr 02 21:16:43 2015 +0000
+++ b/main.cpp	Fri Dec 11 13:15:46 2015 +0000
@@ -1,48 +1,93 @@
-// Headers necessary for mbed and BLE device mode
 #include "mbed.h"
-#include "BLEDevice.h"
+#include "ble/BLE.h"
+#include "ble/Gap.h"
+#include "ble/services/BatteryService.h"
+#include "ble/services/DeviceInformationService.h"
 
-// BLE object
-BLEDevice ble;
+#define BUTTON_SERVICE_UUID_0              0x0000    // Custom SERVICE_UUID 
+#define BUTTON_SERVICE_UUID_1              0x0000    // Custom SERVICE_UUID 
+#define BUTTON_SERVICE_UUID_2              0x0000    // Custom SERVICE_UUID 
+#define BUTTON_SERVICE_UUID_3              0x0000    // Custom SERVICE_UUID 
+#define BUTTON_SERVICE_UUID_4              0x0000    // Custom SERVICE_UUID 
+#define BUTTON_SERVICE_UUID_5              0x0000    // Custom SERVICE_UUID 
+#define BUTTON_SERVICE_UUID_6              0x0000    // Custom SERVICE_UUID 
+#define BUTTON_SERVICE_UUID_7              0xFFF0    // Custom SERVICE_UUID 
+
+#define BUTTON_STATE_CHARACTERISTIC_UUID_0 0x0000    // Custom CHARACTERISTIC_UUID
+#define BUTTON_STATE_CHARACTERISTIC_UUID_1 0x0000    // Custom CHARACTERISTIC_UUID
+#define BUTTON_STATE_CHARACTERISTIC_UUID_2 0x0000    // Custom CHARACTERISTIC_UUID
+#define BUTTON_STATE_CHARACTERISTIC_UUID_3 0x0000    // Custom CHARACTERISTIC_UUID
+#define BUTTON_STATE_CHARACTERISTIC_UUID_4 0x0000    // Custom CHARACTERISTIC_UUID
+#define BUTTON_STATE_CHARACTERISTIC_UUID_5 0x0000    // Custom CHARACTERISTIC_UUID
+#define BUTTON_STATE_CHARACTERISTIC_UUID_6 0x0000    // Custom CHARACTERISTIC_UUID
+#define BUTTON_STATE_CHARACTERISTIC_UUID_7 0xFFF1    // Custom CHARACTERISTIC_UUID
+
+BLE  ble;
 
-// Optional: Device Name, add for human read-ability
-//const static char     DEVICE_NAME[]        = "ChangeMe!!"; // Optional: device name
+DigitalOut  led1(LED1);
+InterruptIn int_external(PC_13);
+DigitalIn  Push_Button(PC_13,PullNone);
+
+static const  char    DEVICE_NAME[] = "CESA BLE 4.0 Button";
+//static const uint16_t service_uuid16_list[] = {BUTTON_SERVICE_UUID_7};
+static const uint16_t service_uuid128_list[] = {BUTTON_SERVICE_UUID_7};
+static const uint16_t characteristic_uuid128_list[] = {BUTTON_STATE_CHARACTERISTIC_UUID_7};
+                                               
+bool buttonPressed = false;
+
+//buttonState is accessible to button callbacks(CHARACTERISTIC_UUID)
+ReadOnlyGattCharacteristic<bool> buttonState((uint8_t *)characteristic_uuid128_list,&buttonPressed,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 
-// You have up to 26 bytes of advertising data to use.
-const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05};   // example of hex data
-//const static uint8_t AdvData[] = {"ChangeThisData"};         // example of character data
+
+void buttonPressedCallback(void)
+{
+    buttonPressed = true;
+    ble.updateCharacteristicValue(buttonState.getValueHandle(),(uint8_t *)&buttonPressed, sizeof(bool));
+}
 
-// Optional: Restart advertising when phone app disconnects
-void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+void buttonReleasedCallback(void)
+{
+    buttonPressed = false;
+    ble.updateCharacteristicValue(buttonState.getValueHandle(),(uint8_t *)&buttonPressed, sizeof(bool));
+}
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     ble.startAdvertising();
 }
 
-// main program
+void periodicCallback(void)
+{
+    //led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
+}
+
 int main(void)
 {
-    // Initialize BLE baselayer, always do this first!
+    led1 = 1;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+    int_external.fall(&buttonPressedCallback);
+    int_external.rise(&buttonReleasedCallback);
+
     ble.init();
+    ble.onDisconnection(disconnectionCallback);
 
-    // Optional: add callback for disconnection
-    // ble.onDisconnection(disconnectionCallback);
+    GattCharacteristic *charTable[] = {&buttonState};
+    GattService  buttonService((uint8_t *)service_uuid128_list, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+    ble.addService(buttonService);
 
-    // Sacrifice 3B of 31B to Advertising Flags
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
+    /* setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,(uint8_t *)service_uuid16_list, sizeof(service_uuid16_list));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(uint8_t *)service_uuid128_list, sizeof(service_uuid128_list));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,(uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-
-    // Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
-
-    // Optional: Add name to device
-    //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-
-    // Set advertising interval. Longer interval = longer battery life
-    ble.setAdvertisingInterval(100); // 100ms, set as percentage of a second
+    
+    ble.setAdvertisingInterval(100); /* 100 ms. */
     ble.startAdvertising();
 
-    // Infinite loop waiting for BLE events
-    for (;;) {
-        ble.waitForEvent(); // this saves battery while waiting for callback events
+    while (true) 
+    {
+        ble.waitForEvent();
     }
-}
+}
\ No newline at end of file