microbit_BLE_RCBController_mbed

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Revision:
23:53883bb55dc3
Parent:
22:406127954d1f
--- a/main.cpp	Mon Nov 09 17:08:47 2015 +0000
+++ b/main.cpp	Tue Oct 10 12:46:14 2017 +0000
@@ -1,31 +1,37 @@
 #include "mbed.h"
 #include "ble/BLE.h"
+#include "RCBController.h"
 
-DigitalOut led(LED1, 1);
-uint16_t customServiceUUID  = 0xA000;
-uint16_t readCharUUID       = 0xA001;
-uint16_t writeCharUUID      = 0xA002;
+Serial pc(USBTX, USBRX);
+
+uint16_t RCBController_service_uuid        = 0xFFF0;
+uint16_t RCBController_Characteristic_uuid = 0xFFF1;
 
-const static char     DEVICE_NAME[]        = "ChangeMe!!"; // change this
+RCBController controller;
+
+const static char     DEVICE_NAME[]        = "micro:bit"; // change this
 static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development
+//static const uint16_t uuid16_list[]        = {0xFFF0}; //Custom UUID, FFFF is reserved for development
 
 /* Set Up custom Characteristics */
-static uint8_t readValue[10] = {0};
-ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
-
 static uint8_t writeValue[10] = {0};
-WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> Controller(RCBController_Characteristic_uuid, writeValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
 
 /* Set up custom service */
-GattCharacteristic *characteristics[] = {&readChar, &writeChar};
-GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+GattCharacteristic *characteristics[] = {&Controller};
+GattService        customService(RCBController_service_uuid, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
 
+void connectionCallback(const Gap::ConnectionCallbackParams_t *)
+{
+    printf("connectionCallback\n\r");
+}
 
 /*
  *  Restart advertising when phone app disconnects
 */
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
 {
+    printf("disconnectionCallback\n\r");
     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
 }
 
@@ -34,23 +40,17 @@
 */
 void writeCharCallback(const GattWriteCallbackParams *params)
 {
+    printf("writeCharCallback\n\r");
     /* Check to see what characteristic was written, by handle */
-    if(params->handle == writeChar.getValueHandle()) {
+    if(params->handle == Controller.getValueHandle()) {
         /* toggle LED if only 1 byte is written */
-        if(params->len == 1) {
-            led = params->data[0];
-            (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle
-        }
-        /* Print the data if more than 1 byte is written */
-        else {
+        if(params->len == 10) {
             printf("Data received: length = %d, data = 0x",params->len);
             for(int x=0; x < params->len; x++) {
                 printf("%x", params->data[x]);
             }
             printf("\n\r");
         }
-        /* Update the readChar with the value of writeChar */
-        BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
     }
 }
 /*
@@ -65,6 +65,7 @@
         return;
     }
 
+    ble.gap().onConnection(connectionCallback);
     ble.gap().onDisconnection(disconnectionCallback);
     ble.gattServer().onDataWritten(writeCharCallback);
 
@@ -87,6 +88,7 @@
 */
 int main(void)
 {
+    pc.baud(115200);
     /* initialize stuff */
     printf("\n\r********* Starting Main Loop *********\n\r");