This is a simple program to work with RedBearLab BLE Controller App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal.

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_Chat_Softswitch by david bateman

Files at this revision

API Documentation at this revision

Comitter:
speedave
Date:
Sun Nov 12 18:28:00 2017 +0000
Parent:
3:b3f6c612b603
Commit message:
update

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jan 07 02:46:54 2016 +0000
+++ b/main.cpp	Sun Nov 12 18:28:00 2017 +0000
@@ -26,6 +26,8 @@
  
 #include "mbed.h"
 #include "ble/BLE.h"
+#include <stdlib.h>
+#include <stdio.h>
 
 
 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
@@ -61,7 +63,8 @@
 
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
-
+int counter=10;
+int counter2=0;
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
@@ -93,14 +96,21 @@
     }
 }
 
+/* serial interupt (from PC) calls this function */
 void uartCB(void)
 {   
+    rx_len = 0;
+    /* while text in the serial buffer from the PC */
     while(pc.readable())    
     {
-        rx_buf[rx_len++] = pc.getc();    
+        /* load characters from serial buffer into rx_buf array, one by one */
+        rx_buf[rx_len++] = pc.getc();   
+        /*check if rx_buf array is too long, ends in 'space' or ends 'carrage return' */ 
         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
         {
+            /* update the rxCharacteristic with the rx_buf data fromm the PC */
             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
+            /* print debug info and reset */
             pc.printf("RecHandler \r\n");
             pc.printf("Length: ");
             pc.putc(rx_len);
@@ -111,16 +121,114 @@
     }
 }
 
+void dummyticker(void) {
+        
+    rx_len = 0;
+    char counterString[2];
+    
+    counter2++;
+    if (counter2 > 2){
+        counter2 = 1;
+    }
+    
+    /* this could be done more efficiently using sprinf */    
+    if ( counter2 == 1) {
+        pc.printf("press1\n");
+        /* this only seems to work with exactly 20 characters */
+        char rx_message[] = "SWITCH1-901234567-";
+        sprintf(counterString, "%d\n", counter);
+        strncat(rx_message, counterString,20);
+        pc.printf(rx_message);
+            
+        while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
+        
+            rx_buf[rx_len] = (int) rx_message[rx_len++];
+        }            
+
+    }
+    if ( counter2 == 2) {
+        pc.printf("press2\n");
+        char rx_message[] = "SWITCH2-901234567-";
+        sprintf(counterString, "%d\n", counter);
+        strncat(rx_message, counterString,20);
+        pc.printf(rx_message);
+        
+        while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
+                 
+            rx_buf[rx_len] = (int) rx_message[rx_len++];
+        }                   
+    }
+    
+    if ( counter2 == 3) {
+        pc.printf("press2\n");
+        char rx_message[] = "SWITCH2-901234567-";
+        sprintf(counterString, "%d\n", counter);
+        strncat(rx_message, counterString,20);
+        pc.printf(rx_message);
+        
+        while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
+                 
+            rx_buf[rx_len] = (int) rx_message[rx_len++];
+        }                   
+    }
+    
+    if ( counter2 == 4) {
+        pc.printf("press2\n");
+        char rx_message[] = "SWITCH2-901234567-";
+        sprintf(counterString, "%d\n", counter);
+        strncat(rx_message, counterString,20);
+        pc.printf(rx_message);
+        
+        while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
+                 
+            rx_buf[rx_len] = (int) rx_message[rx_len++];
+        }                   
+    }
+    
+    if ( counter2 == 5) {
+        pc.printf("press2\n");
+        char rx_message[] = "SWITCH2-901234567-";
+        sprintf(counterString, "%d\n", counter);
+        strncat(rx_message, counterString,20);
+        pc.printf(rx_message);
+        
+        while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
+                 
+            rx_buf[rx_len] = (int) rx_message[rx_len++];
+        }                   
+    }
+    
+    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
+   
+    pc.printf("button press\n");
+    pc.printf("Length: ");
+    pc.putc(rx_len);
+    pc.printf("\r\n");
+    rx_len = 0;
+    counter++;
+    if (counter > 99) {
+        counter = 0;
+    }
+}
+
 int main(void)
 {
     ble.init();
     ble.onDisconnection(disconnectionCallback);
+    
+    /* bluetooth data interrupt */
+    /* when data sent from client (phone) call 'WrittenHandler' */
     ble.onDataWritten(WrittenHandler);  
     
     pc.baud(9600);
     pc.printf("SimpleChat Init \r\n");
     
+    /* serial interupt*/
+    /* when text typed into PC terminal call 'uartCB' */
     pc.attach( uartCB , pc.RxIrq);
+    
+
+    
    // setup advertising 
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
@@ -136,6 +244,11 @@
     ble.startAdvertising(); 
     pc.printf("Advertising Start \r\n");
     
+    /* ticker interupt */
+    /* timer will call 'dummyticker' every X seconds */
+    Ticker ticker;
+    ticker.attach(dummyticker, 3);
+    
     while(1)
     {
         ble.waitForEvent(); 
@@ -143,6 +256,9 @@
 }
 
 
+        
+
+
 
 
 
@@ -155,5 +271,3 @@
 
 
 
-
-