BLE GATT LED on Board Nucleo with IDB0XA1

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_GAP_Example by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
anoney180133
Date:
Fri Dec 11 13:24:16 2015 +0000
Parent:
13:827dd2b32bb8
Commit message:
asdasd

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
X_NUCLEO_IDB0XA1.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show diff for this revision Revisions of this file
diff -r 827dd2b32bb8 -r 414e9bbb2701 BLE_API.lib
--- a/BLE_API.lib	Thu Apr 02 21:16:43 2015 +0000
+++ b/BLE_API.lib	Fri Dec 11 13:24:16 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#8c645f5694b2
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#a097e1be76f4
diff -r 827dd2b32bb8 -r 414e9bbb2701 X_NUCLEO_IDB0XA1.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_IDB0XA1.lib	Fri Dec 11 13:24:16 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1/#d22cc4715b0c
diff -r 827dd2b32bb8 -r 414e9bbb2701 main.cpp
--- a/main.cpp	Thu Apr 02 21:16:43 2015 +0000
+++ b/main.cpp	Fri Dec 11 13:24:16 2015 +0000
@@ -1,48 +1,100 @@
-// 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 LED_SERVICE_UUID_0              0x0000    // Custom SERVICE_UUID 
+#define LED_SERVICE_UUID_1              0x0000    // Custom SERVICE_UUID 
+#define LED_SERVICE_UUID_2              0x0000    // Custom SERVICE_UUID 
+#define LED_SERVICE_UUID_3              0x0000    // Custom SERVICE_UUID 
+#define LED_SERVICE_UUID_4              0x0000    // Custom SERVICE_UUID 
+#define LED_SERVICE_UUID_5              0x0000    // Custom SERVICE_UUID 
+#define LED_SERVICE_UUID_6              0x0000    // Custom SERVICE_UUID 
+#define LED_SERVICE_UUID_7              0xFFE0    // Custom SERVICE_UUID 
+
+#define LED_STATE_CHARACTERISTIC_UUID_0 0x0000    // Custom CHARACTERISTIC_UUID
+#define LED_STATE_CHARACTERISTIC_UUID_1 0x0000    // Custom CHARACTERISTIC_UUID
+#define LED_STATE_CHARACTERISTIC_UUID_2 0x0000    // Custom CHARACTERISTIC_UUID
+#define LED_STATE_CHARACTERISTIC_UUID_3 0x0000    // Custom CHARACTERISTIC_UUID
+#define LED_STATE_CHARACTERISTIC_UUID_4 0x0000    // Custom CHARACTERISTIC_UUID
+#define LED_STATE_CHARACTERISTIC_UUID_5 0x0000    // Custom CHARACTERISTIC_UUID
+#define LED_STATE_CHARACTERISTIC_UUID_6 0x0000    // Custom CHARACTERISTIC_UUID
+#define LED_STATE_CHARACTERISTIC_UUID_7 0xFFE1    // Custom CHARACTERISTIC_UUID
 
-// Optional: Device Name, add for human read-ability
-//const static char     DEVICE_NAME[]        = "ChangeMe!!"; // Optional: device name
+BLE  ble;
+
+DigitalOut  led1(LED1);
+
+//------------------------------------
+// Hyperterminal configuration
+// 115200 bauds(Default 9600), 8-bit data, no parity
+//------------------------------------
+/* No need Serial to UART just usb debugger. */
+
+Serial UART(SERIAL_TX, SERIAL_RX); // TX PA_2 , RX PA_3
 
-// 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
+static const  char    DEVICE_NAME[] = "CESA BLE 4.0 LED";
+//static const uint16_t service_uuid16_list[] = {LED_SERVICE_UUID_7};
+static const uint16_t service_uuid128_list[] = {LED_SERVICE_UUID_7};
+static const uint16_t characteristic_uuid128_list[] = {LED_STATE_CHARACTERISTIC_UUID_7};
 
-// Optional: Restart advertising when phone app disconnects
-void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+bool LEDCharacteristic_Value = false;                                               
+
+//LEDState is accessible to LED callbacks(CHARACTERISTIC_UUID)
+ReadWriteGattCharacteristic<bool> ledState((uint8_t *)characteristic_uuid128_list,&LEDCharacteristic_Value,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
+
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     ble.startAdvertising();
 }
 
-// main program
+void periodicCallback(void)
+{
+    //led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
+}
+
+
+void onDataWrittenCallback(const GattWriteCallbackParams *params)
+{
+    if ((params->handle == ledState.getValueHandle()) && (params->len == 1)) 
+    {
+        led1 = params->data[0]; 
+        
+        printf("data = %d\r\n",params->data[0]);
+        
+    }
+}
+
 int main(void)
 {
-    // Initialize BLE baselayer, always do this first!
+    led1 = 0;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+    
+    UART.baud(115200); // Set BuadRate
+
     ble.init();
+    ble.onDisconnection(disconnectionCallback);
+    ble.onDataWritten(onDataWrittenCallback);
 
-    // Optional: add callback for disconnection
-    // ble.onDisconnection(disconnectionCallback);
+    GattCharacteristic *charTable[] = {&ledState};
+    GattService  LEDService((uint8_t *)service_uuid128_list, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+    ble.addService(LEDService);
 
-    // 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
diff -r 827dd2b32bb8 -r 414e9bbb2701 nRF51822.lib
--- a/nRF51822.lib	Thu Apr 02 21:16:43 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#bdc690669431