Code for the nRF51822. The BLE advertises, connects to a master, sends information about the status of the LDRs and receives inputs from the Master

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_UART1 by StepSense

Revision:
14:a8c2ad841fc6
Parent:
13:4e4ec6fe8123
--- a/main.cpp	Mon Nov 10 03:54:49 2014 +0000
+++ b/main.cpp	Mon Nov 10 11:29:20 2014 +0000
@@ -46,6 +46,7 @@
 
 int threshold = 0;
 
+//Output string to indicate the status of the LDRs
 char output[50] = "LDR0 = w x y z";
 
 UARTService *uartServicePtr;
@@ -64,27 +65,29 @@
     ble.startAdvertising();
 }
 
+//Thresholding the values of the LDRs
 void getValuesofLDR()
 {
-    if (ldr0.read() < 0.7f)
+    if (ldr0.read() < 0.52f)
         output[7] = '0';
     else
         output[7] = '1';
-    if (ldr0.read() < 0.7f)
+    if (ldr0.read() < 0.52f)
         output[9] = '0';
     else
         output[9] = '1';
-    if (ldr0.read() < 0.7f)
+    if (ldr0.read() < 0.52f)
         output[11] = '0';
     else
         output[11] = '1';
-    if (ldr0.read() < 0.7f)
+    if (ldr0.read() < 0.52f)
         output[13] = '0';
     else
         output[13] = '1';
  
 }
 
+//Preparation of output string
 void onDataWritten(const GattCharacteristicWriteCBParams *params)
 {
     char *str = 0;
@@ -94,6 +97,7 @@
         uint16_t bytesRead = params->len;
         DEBUG("received %u bytes\n\r", bytesRead);
         
+        //Obtaining user command
         str = (char *)malloc((sizeof(char) * bytesRead) + 1);
         while(count <= bytesRead)
         {
@@ -103,18 +107,20 @@
         count--;
         *(str + count) = '\0';
         DEBUG("payload = %s\n\r",str);
+        
+        //Comparing user input against keywords
         if (strncmp(str,"On",bytesRead) == 0)
         {
-            led2 = 1;
-            pwmOut = 1;
+            led2 = 0;
+            pwmOut = 0;
             bytesRead = 7;
             strncpy((char *)params->data,"Lamp On", bytesRead);
         }    
         else if (strncmp(str,"Off",bytesRead) == 0)
         {
-            led2 = 0;
-            pwmOut = 0;
-            bytesRead = 7;
+            led2 = 1;
+            pwmOut = 1;
+            bytesRead = 8;
             strncpy((char *)params->data,"Lamp Off", bytesRead);
         }
         else if (strncmp(str,"Read",bytesRead) == 0)
@@ -124,7 +130,7 @@
             strncpy((char *)params->data,output,bytesRead);
             
         }
-        else
+        else//None of the commands match. Invalid entry
         {
             bytesRead = 14;
             strncpy((char *)params->data,"Invalid Entry", bytesRead);
@@ -163,13 +169,10 @@
     //ticker.attach(periodicCallback, 0.00001f);
     ble.init();
     
-    DEBUG("Initialising the nRF51822_1\n\r");
     ble.onDisconnection(disconnectionCallback);
+
+    ble.onDataWritten(onDataWritten);
     
-    DEBUG("Initialising the nRF51822_2\n\r");
-    ble.onDataWritten(onDataWritten);
-
-    DEBUG("Initialising the nRF51822_3\n\r");
     /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
@@ -183,8 +186,7 @@
 
     UARTService uartService(ble);
     uartServicePtr = &uartService;
-
-    DEBUG("Initialising the nRF51822_4\n\r");
+    
     while (true) {
         ble.waitForEvent();
     }