Release for tas lab

Dependencies:   BLE_API mbed

Fork of BLE_RCBController by Junichi Katsu

Revision:
1:48f6e08a3ac2
Parent:
0:8c643bfe55b7
Child:
2:dd85fdc18224
diff -r 8c643bfe55b7 -r 48f6e08a3ac2 main.cpp
--- a/main.cpp	Thu Jul 10 14:21:52 2014 +0000
+++ b/main.cpp	Wed Aug 20 13:24:20 2014 +0000
@@ -1,10 +1,10 @@
 #include "mbed.h"
-#include "nRF51822n.h"
+#include "BLEDevice.h"
 #include "RCBController.h"
 
-#define DBG 0
+#define DBG 1
 
-nRF51822n   nrf;
+BLEDevice  ble;
 Serial  pc(USBTX, USBRX);
 /* LEDs for indication: */
 DigitalOut  ConnectStateLed(LED1);
@@ -14,59 +14,50 @@
 /* RCBController Service */
 static const uint16_t RCBController_service_uuid = 0xFFF0;
 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
-GattService         RCBControllerService (RCBController_service_uuid);
-GattCharacteristic  Controller (RCBController_Characteristic_uuid,10, 10,
+uint8_t RCBControllerPayload[10] = {0,};
+
+GattCharacteristic  ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10,
 								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
 								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
-
-/* Advertising data and parameters */
-GapAdvertisingData   advData;
-GapAdvertisingData   scanResponse;
-GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
-
-uint16_t	uuid16_list[] = { RCBController_service_uuid };
+GattCharacteristic *ControllerChars[] = {&ControllerChar};
+GattService         RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
 
 RCBController controller;
 
-// GapEvent
-class GapEventHandler : public GapEvents
+void onConnected(uint16_t h)
 {
-     
-    virtual void onConnected(void)
-    {
-        ConnectStateLed = 0;
+    ConnectStateLed = 0;
 #if DBG
-		pc.printf("Connected\n\r");
+	pc.printf("Connected\n\r");
 #endif
-    }
+}
 
-    virtual void onDisconnected(void)
-    {
-        nrf.getGap().startAdvertising(advParams);
-		ConnectStateLed = 1;
+void onDisconnected(uint16_t h)
+{
+    ble.startAdvertising();
+	ConnectStateLed = 1;
 #if DBG
-		pc.printf("Disconnected\n\r");
+	pc.printf("Disconnected\n\r");
 #endif
-    }
-};
+}
+
 
 // GattEvent
-class GattServerEventHandler : public GattServerEvents
+void onDataWritten(uint16_t charHandle)
 {
-	virtual void onDataWritten(uint16_t charHandle)
-	{
-   		if (charHandle == Controller.handle) {
-	        nrf.getGattServer().readValue(Controller.handle, &controller.data[0], sizeof(controller));
+	if (charHandle == ControllerChar.getHandle()) {
+		uint16_t bytesRead;
+	 	ble.readCharacteristicValue(ControllerChar.getHandle(),RCBControllerPayload, &bytesRead);
+        memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
 #if DBG
-			pc.printf("DATA:%d %d %d %d %d %d %d %d %d %d\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4],
+
+		pc.printf("DATA:%02X %02X %d %d %d %d %d %d %d %02X\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4],
 															   controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
 #endif
-			ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;;
-			
-		}
+		ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;		
+	}
 		 
-	}
-};
+}
 
 /**************************************************************************/
 /*!
@@ -78,35 +69,27 @@
 #if DBG
 		pc.printf("Start\n\r");
 #endif
-    /* Setup an event handler for GAP events i.e. Client/Server connection events. */
-    nrf.getGap().setEventHandler(new GapEventHandler());
-    
-    /* Initialise the nRF51822 */
-    nrf.init();
-    
-    nrf.getGattServer().setEventHandler(new GattServerEventHandler());
-
-    /* Make sure we get a clean start */
-    nrf.reset();    
+	
+    ble.init();
+    ble.onConnection(onConnected);
+    ble.onDisconnection(onDisconnected);
+    ble.onDataWritten(onDataWritten);
+	
+    /* setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                    (const uint8_t *)"mbed HRM1017", sizeof("mbed HRM1017") - 1);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
+                                    (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid));
 
-    /* Add BLE-Only flag and complete service list to the advertising data */
-    advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 
-                   (uint8_t*)uuid16_list, sizeof(uuid16_list));
-    nrf.getGap().setAdvertisingData(advData, scanResponse);
-	
-	/* RCBController Service */
-	RCBControllerService.addCharacteristic(Controller);
-    nrf.getGattServer().addService(RCBControllerService);
+    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+    ble.startAdvertising();
 
-    /* Start advertising (make sure you've added all your data first) */
-    nrf.getGap().startAdvertising(advParams);
-    ConnectStateLed = 1;
-    ControllerStateLed = 1;
+    ble.addService(RCBControllerService);
 
-    for (;;)
-    {
-        wait(1);
+    while (true) {
+        ble.waitForEvent();
     }
 }