Exercise 4 Calzana Iandiorio

Revision:
1:7b8b65fd1d74
Parent:
0:c544827b6b11
Child:
2:59e9845ddbb6
--- a/source/main.cpp	Thu Nov 29 18:13:44 2018 +0000
+++ b/source/main.cpp	Fri Nov 30 12:26:58 2018 +0000
@@ -20,7 +20,11 @@
 #include "ble/DiscoveredCharacteristic.h"
 #include "ble/DiscoveredService.h"
 
-DigitalOut alivenessLED(LED1, 1);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut alivenessLED = led1;
+bool blinking = false;
 static DiscoveredCharacteristic ledCharacteristic;
 static bool triggerLedCharacteristic;
 static const char PEER_NAME[] = "LED666";
@@ -30,7 +34,9 @@
 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
 
 void periodicCallback(void) {
-    alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
+    if(blinking){
+        alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
+    }
 }
 
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
@@ -48,20 +54,38 @@
         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)) {
-                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);
+               uint8_t rssi = params->rssi;
+               
+               if(rssi > -50){
+                    printf("NEAR\n");
+                    alivenessLED = led1;
+               }else if(rssi > -120){
+                     printf("MEDIUM\n");
+                    alivenessLED = led2;
+                }else{
+                    printf("FAR\n");
+                    alivenessLED = led3;
+                }
+                if(!blinking){
+                    blinking = 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) {