Test code. Blinks by default at 2 seconds. Receives single characters from PC (sensor) and transmit to "nRF Connect" on droid. Droid can send ascii to Nano. When droid sends "A", blinking is twice per second. "B" returns to slow.

Dependencies:   BLE_API mbed nRF51822

Fork of IofT_comm by Eric MacDonald

Files at this revision

API Documentation at this revision

Comitter:
emacdonald
Date:
Sun Jan 15 14:25:30 2017 +0000
Parent:
4:378d43fa5d20
Child:
6:42bb0cde20f0
Commit message:
Test code. Blinks by default at 2 seconds. Receives single characters from PC (sensor) and transmit to "nRF Connect" on droid. Droid can send ascii to Nano. When droid sends "A", blinking is twice per second. "B" returns to slow.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Jan 14 18:06:41 2017 +0000
+++ b/main.cpp	Sun Jan 15 14:25:30 2017 +0000
@@ -14,7 +14,7 @@
 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
 
-#define TXRX_BUF_LEN                     20
+#define TXRX_BUF_LEN                     4
 
 BLE  ble;
 DigitalOut led1(LED1);
@@ -31,8 +31,13 @@
 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
 
-static uint8_t rx_buf[TXRX_BUF_LEN];
-static uint8_t rx_len=0;
+//static uint8_t rx_buf[TXRX_BUF_LEN];
+static uint8_t rx_buf[4] = "a";
+static uint8_t rx_len=2;
+uint8_t blinklow;
+uint8_t blinkperiod;
+uint8_t blinkrate;
+uint8_t blinkcount;
 
 
 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
@@ -63,12 +68,18 @@
         led1 = !led1;    
         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
         memset(txPayload, 0, TXRX_BUF_LEN);
-        memcpy(txPayload, buf, TXRX_BUF_LEN);       
-        pc.printf("WriteHandler \r\n");
-        pc.printf("Length: ");
-        pc.putc(bytesRead);
-        pc.printf("\r\n");
-        pc.printf("Data: ");
+        memcpy(txPayload, buf, TXRX_BUF_LEN);
+        if(bytesRead==0x01) {
+            if(txPayload[0] == 0x41){
+                pc.printf("Fast \r\n");
+                blinkperiod = 5;
+            }
+            if(txPayload[0] == 0x42){
+                pc.printf("Slow \r\n");
+                  blinkperiod = 20;
+            }
+        }       
+        pc.printf("BLE received = ");
         for(index=0; index<bytesRead; index++)
         {
             pc.putc(txPayload[index]);        
@@ -77,51 +88,78 @@
     }
 }
 
+// BLE Write from Serial read
 void uartCB(void)
 {   
     while(pc.readable())    
-    {
-        rx_buf[rx_len++] = pc.getc();
-        led1 = !led1;    
-        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
         {
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
-            pc.putc(rx_len);
-            pc.printf("\r\n");
-            rx_len = 0;
-            break;
+        rx_buf[0] = pc.getc();
+        led1 = !led1;  
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
+        pc.printf("Received sensor data transmitted on BLE ");
+        pc.putc(rx_buf[0]);
+        pc.printf(" \r\n");
         }
-    }
 }
 
+//void u//artCB(void)
+//    {   
+//    while(pc.readable())    
+//        {
+//        rx_buf[rx_len++] = pc.getc();
+//        led1 = !led1;    
+//        if(rx_len>=4 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
+//            {
+//            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
+//            pc.printf("Received sensor data transmitted on BLE ");
+//            pc.putc(rx_len);
+//            pc.printf("\r\n");
+//            rx_len = 0;
+//            break;
+//            }
+//    }
+//}
+
 void periodicCallback(void)
 {
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-
-    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
-     * heavy-weight sensor polling from the main thread. */
+    blinkcount = blinkcount + 1;
+    if(blinkcount >= blinkperiod)
+    {
+        led1 = 0;
+        blinkcount = 0;
+        pc.printf("Blinked\r\n");
+    }
+    else if(blinkcount > 1)
+    {
+        led1 = 1;
+    }
+    else
+    {
+        led1 = 0;
+    }
+    
 }
 
 int main(void)
 {
-    led1 = 1;
+    led1 = 0;
+    blinkcount = 0;
+    blinkperiod = 20;
     Ticker ticker;
-    ticker.attach(periodicCallback, 5.0); // blink LED every second
+    ticker.attach(periodicCallback, 0.1); // blink LED every second
     ble.init();
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(WrittenHandler);  
     
     pc.baud(9600);
-    pc.printf("SimpleChat Init \r\n");
+    pc.printf("IoT Init \r\n");
     
     pc.attach( uartCB , pc.RxIrq);
    // setup advertising 
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"Bennett", sizeof("Bennett") - 1);
+                                    (const uint8_t *)"IoT_bluetooth", sizeof("IoT_bluetooth") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
     // 100ms; in multiples of 0.625ms. 
@@ -130,26 +168,10 @@
     ble.addService(uartService);
     
     ble.startAdvertising(); 
-    pc.printf("Advertising Start \r\n");
+    pc.printf("Advertising Started \r\n");
     
     while(1)
     {
         ble.waitForEvent(); 
     }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+}
\ No newline at end of file