This is a simple UART chat that will be used to read sensors in Sand Molds.

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Revision:
4:378d43fa5d20
Parent:
3:b3f6c612b603
--- a/main.cpp	Thu Jan 07 02:46:54 2016 +0000
+++ b/main.cpp	Sat Jan 14 18:06:41 2017 +0000
@@ -1,21 +1,3 @@
-/*
-
-Copyright (c) 2012-2014 RedBearLab
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
-and associated documentation files (the "Software"), to deal in the Software without restriction, 
-including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
-and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
-subject to the following conditions:
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
-FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
-ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-*/
 
 /*
  *    The application works with the BLEController iOS/Android App.
@@ -35,7 +17,7 @@
 #define TXRX_BUF_LEN                     20
 
 BLE  ble;
-
+DigitalOut led1(LED1);
 Serial pc(USBTX, USBRX);
 
 
@@ -70,6 +52,7 @@
     ble.startAdvertising();
 }
 
+// Serial Write from BLE Read
 void WrittenHandler(const GattWriteCallbackParams *Handler)
 {   
     uint8_t buf[TXRX_BUF_LEN];
@@ -77,6 +60,7 @@
     
     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
     {
+        led1 = !led1;    
         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
         memset(txPayload, 0, TXRX_BUF_LEN);
         memcpy(txPayload, buf, TXRX_BUF_LEN);       
@@ -97,7 +81,8 @@
 {   
     while(pc.readable())    
     {
-        rx_buf[rx_len++] = pc.getc();    
+        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); 
@@ -111,8 +96,19 @@
     }
 }
 
+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. */
+}
+
 int main(void)
 {
+    led1 = 1;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 5.0); // blink LED every second
     ble.init();
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(WrittenHandler);  
@@ -125,7 +121,7 @@
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
+                                    (const uint8_t *)"Bennett", sizeof("Bennett") - 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.