Depart L476 Version 1 Communication UART par BLE avec App Inventor Communication HC05 avec F411

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Revision:
8:60033323cbcb
Parent:
7:8490fe113598
--- a/main.cpp	Tue Apr 25 15:26:50 2017 +0000
+++ b/main.cpp	Tue Apr 25 15:45:05 2017 +0000
@@ -16,77 +16,44 @@
 
 #include "mbed.h"
 #include "ble/BLE.h"
-#include "ble/services/UARTService.h"
-#include "Serial.h"
-
-#define UART_BUFFER (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN*10)
+#include "LEDService.h"
 
-const static char DEVICE_NAME[] = "UART";
-UARTService *uartServicePtr;
-static uint8_t uartBuff[UART_BUFFER];
-static uint8_t uartBuffPos=0;
+DigitalOut actuatedLED(LED1, 0);
+
+const static char     DEVICE_NAME[] = "LED";
+static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
 
-void onBleError(ble_error_t error);
-
-DigitalOut led1(LED1);
-Serial uart1(USBTX,USBRX);
-Ticker ticker;
+LEDService *ledServicePtr;
 
-#define DEBUG uart1.printf
-
-/* Ticker */
-void periodicCallback(void)
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    led1 = !led1;
+    (void)params;
+    BLE::Instance().gap().startAdvertising(); // restart advertising
 }
 
-/* UART */
-void uartRx(void)
-{
-    if(uart1.readable()){
-        uartBuff[uartBuffPos] = uart1.getc();
-        if((uartBuff[uartBuffPos] == '\r') || (uartBuff[uartBuffPos] == '\n') || (uartBuffPos >= UART_BUFFER)) {
-            uartBuff[uartBuffPos] = '\0';
-            /* We are sending the whole string even if less than BLE_UART_SERVICE_MAX_DATA_LEN otherwise we need to wait */
-            uartServicePtr->write(uartBuff, (uartBuffPos/UARTService::BLE_UART_SERVICE_MAX_DATA_LEN +1) * UARTService::BLE_UART_SERVICE_MAX_DATA_LEN);
-            DEBUG("TX : %s\r\n", uartBuff);
-            memset(uartBuff, 0, UART_BUFFER);
-            uartBuffPos = 0;
-        }
-        else
-            uartBuffPos++;
+/**
+ * This callback allows the LEDService to receive updates to the ledState Characteristic.
+ *
+ * @param[in] params
+ *     Information about the characterisitc being updated.
+ */
+void onDataWrittenCallback(const GattWriteCallbackParams *params) {
+    if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
+        actuatedLED = *(params->data);
     }
 }
 
-
-/* BLE */
-void BleConnectionCallback(const Gap::ConnectionCallbackParams_t *params) {
-    DEBUG("BLE Client Connected!\n\r");
-    DEBUG("Please type a string and press return\r\n");
-
-    ticker.attach(periodicCallback, 1);
-}
-
-void BleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    (void)params;
-    DEBUG("BLE Client Disconnected!\r\n");
-    ticker.detach();
-    BLE::Instance().gap().startAdvertising(); // restart advertising
-    led1=0;
-}
-
-void BleOnDataWrittenCallback(const GattWriteCallbackParams *params) {
-    if (params->handle == uartServicePtr->getTXCharacteristicHandle()){
-        DEBUG("RX: %s\r\n", params->data);
-    }
-}
-
-void onBleError(ble_error_t error) { 
-    DEBUG("BLE Error: %d\r\n");
-    /* Handle error now */
+/** 
+ * This function is called when the ble initialization process has failled 
+ */ 
+void onBleInitError(BLE &ble, ble_error_t error) 
+{ 
+    /* Initialization error handling should go here */ 
 } 
 
+/** 
+ * Callback triggered when the ble initialization process has finished 
+ */ 
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 
 {
     BLE&        ble   = params->ble;
@@ -94,7 +61,7 @@
 
     if (error != BLE_ERROR_NONE) {
         /* In case of error, forward the error handling to onBleInitError */
-        onBleError(error);
+        onBleInitError(ble, error);
         return;
     }
 
@@ -103,22 +70,18 @@
         return;
     }
 
-    ble.gap().onConnection(BleConnectionCallback);
-    ble.gap().onDisconnection(BleDisconnectionCallback);
-    ble.gattServer().onDataWritten(BleOnDataWrittenCallback);
+    ble.gap().onDisconnection(disconnectionCallback);
+    ble.gattServer().onDataWritten(onDataWrittenCallback);
 
-    DEBUG("BLE UARTService: ");
-    /* Setup primary service. */
-    UARTService uartService(ble);
-    uartServicePtr = &uartService;
-    DEBUG("Started\r\n");
+    bool initialValueForLEDCharacteristic = true;
+    ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
 
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(500); /* 500ms. */
+    ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
     ble.gap().startAdvertising();
 
     while (true) {
@@ -128,11 +91,8 @@
 
 int main(void)
 {
-    led1=0;
-        
-    uart1.baud(9600);
-    uart1.attach(uartRx,Serial::RxIrq);
     BLE &ble = BLE::Instance();
+    
     ble.init(bleInitComplete);
 }