Exercise 4 Calzana Iandiorio

Revision:
2:59e9845ddbb6
Parent:
1:7b8b65fd1d74
--- a/source/main.cpp	Fri Nov 30 12:26:58 2018 +0000
+++ b/source/main.cpp	Fri Nov 30 16:29:55 2018 +0000
@@ -20,11 +20,12 @@
 #include "ble/DiscoveredCharacteristic.h"
 #include "ble/DiscoveredService.h"
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut alivenessLED = led1;
-bool blinking = false;
+static DigitalOut led1(LED1);
+static DigitalOut led2(LED2);
+static DigitalOut led3(LED3);
+static bool blinkLed1 = false;
+static bool blinkLed2 = false;
+static bool blinkLed3 = false;
 static DiscoveredCharacteristic ledCharacteristic;
 static bool triggerLedCharacteristic;
 static const char PEER_NAME[] = "LED666";
@@ -34,8 +35,18 @@
 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
 
 void periodicCallback(void) {
-    if(blinking){
-        alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
+    if(blinkLed1){
+        led1 = !led1;
+        led2 = 0;
+        led3 = 0;
+    }else if(blinkLed2){
+        led1 = 0;
+        led2 = !led2;
+        led3 = 0;
+    }else if(blinkLed3){
+        led1 = 0;
+        led2 = 0;
+        led3 = !led3;
     }
 }
 
@@ -45,6 +56,7 @@
     // byte 0: length of the record excluding this byte
     // byte 1: The key, it is the type of the data
     // byte [2..N] The value. N is equal to byte0 - 1
+
     for (uint8_t i = 0; i < params->advertisingDataLen; ++i) {
 
         const uint8_t record_length = params->advertisingData[i];
@@ -54,38 +66,30 @@
         const uint8_t type = params->advertisingData[i + 1];
         const uint8_t* value = params->advertisingData + i + 2;
         const uint8_t value_length = record_length - 1;
-        printf("device %d %s : distance: %d\n", i, (char*)*value, params->rssi);
         if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
             if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) {
-               uint8_t rssi = params->rssi;
-               
-               if(rssi > -50){
+               printf("connected. %d \n", params->rssi);
+               if(params->rssi > -60){
                     printf("NEAR\n");
-                    alivenessLED = led1;
-               }else if(rssi > -120){
+                    blinkLed1 = true;
+                    blinkLed2 = false;
+                    blinkLed3 = false;
+               }else if(params->rssi > -80){
                      printf("MEDIUM\n");
-                    alivenessLED = led2;
+                    blinkLed1 = false;
+                    blinkLed2 = true;
+                    blinkLed3 = false;
                 }else{
                     printf("FAR\n");
-                    alivenessLED = led3;
-                }
-                if(!blinking){
-                    blinking = true;
+                    blinkLed1 = false;
+                    blinkLed2 = false;
+                    blinkLed3 = true;
                 }
-               
-               // printf(
-//                    "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
-//                    params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2],
-//                    params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type
-//                );
-//                BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
                 break;
             }
         }
         i += record_length;
     }
-    printf("END\n\n");
-
 }
 
 void serviceDiscoveryCallback(const DiscoveredService *service) {