NRF51822 as bluetooth to UART

Dependencies:   BLE_API BLE_LoopbackUART mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Revision:
12:fc9171411cf5
Parent:
10:633cec718bf4
--- a/main.cpp	Tue Dec 09 08:55:59 2014 +0000
+++ b/main.cpp	Tue Feb 24 01:46:20 2015 +0000
@@ -30,7 +30,10 @@
 
 BLEDevice  ble;
 DigitalOut led1(LED1);
+Serial pc(USBTX, USBRX);
 
+static uint8_t rx_buf[20];
+static uint8_t rx_len=0;
 UARTService *uartServicePtr;
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
@@ -44,11 +47,26 @@
 {
     if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
-        DEBUG("received %u bytes\n\r", bytesRead);
-        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+        for(uint16_t index=0; index<bytesRead; index++)
+        {
+            pc.putc(params->data[index]);        
+        }
     }
 }
 
+void uartCB(void)
+{   
+    while(pc.readable())    
+    {
+        rx_buf[rx_len++] = pc.getc();    
+        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
+        {
+            ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len);
+            rx_len = 0;
+            break;
+        }
+    }
+}
 void periodicCallback(void)
 {
     led1 = !led1;
@@ -57,6 +75,9 @@
 int main(void)
 {
     led1 = 1;
+    pc.baud(115200);
+    
+    pc.attach( uartCB , pc.RxIrq);
     Ticker ticker;
     ticker.attach(periodicCallback, 1);