Write a proximity-aware Mbed-based LED scanner!

Files at this revision

API Documentation at this revision

Comitter:
vicara
Date:
Fri Nov 30 18:34:23 2018 +0000
Parent:
4:28c05d426fc1
Commit message:
fix

Changed in this revision

source/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/source/main.cpp	Fri Nov 30 12:32:04 2018 +0000
+++ b/source/main.cpp	Fri Nov 30 18:34:23 2018 +0000
@@ -29,18 +29,38 @@
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 
-static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
+static bool led1_on = false;
+static bool led2_on = false;
+static bool led3_on = false;
 
-void periodicCallback(void) {
-    alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
+void blink_led_check(void) {
+    if(led1_on){
+        led1 = !led1;
+        led2 = 0;
+        led3 = 0;
+    }else if(led2_on){
+        led1 = 0;
+        led2 = !led2;
+        led3 = 0;
+    }else if(led3_on){
+        led1 = 0;
+        led2 = 0;
+        led3 = !led3;
+    }
 }
 
+static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
+
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
     // parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME
     // The advertising payload is a collection of key/value records where
     // 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
+    
+    printf("Device(s) discovered: %d \n", &params->advertisingDataLen);
+
+    
     for (uint8_t i = 0; i < params->advertisingDataLen; ++i) {
         
         const uint8_t record_length = params->advertisingData[i];
@@ -56,20 +76,19 @@
         if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
             if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) {
                 printf("Distance from %d is %d\n", &PEER_NAME, params->rssi);
-                int uint8_t = params->rssi;
-                
-                if (distance > -100 && distance < 20) {
-                    led1 = 1;
-                    led2 = 0;
-                    led3 = 0;
-                } else if (distance >= 20 && distance < 250){
-                    led1 = 0;
-                    led2 = 1;
-                    led3 = 0;
-                } else if (distance >= 250){
-                    led1 = 0;
-                    led2 = 0;
-                    led3 = 1;
+                int distance = params->rssi;
+                if(distance >= -63){
+                    led1_on = true;
+                    led2_on = false;
+                    led3_on = false;
+               }else if(distance >= -85){
+                    led1_on = false;
+                    led2_on = true;
+                    led3_on = false;
+                }else{
+                    led1_on = false;
+                    led2_on = false;
+                    led3_on = true;
                 }
 //                printf(
 //                    "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
@@ -210,7 +229,7 @@
 int main()
 {
     triggerLedCharacteristic = false;
-    eventQueue.call_every(500, periodicCallback);
+    eventQueue.call_every(500, blink_led_check);
     BLE &ble = BLE::Instance();
     ble.onEventsToProcess(scheduleBleEventsProcessing);
     ble.init(bleInitComplete);