From 69f29a4d3c0dbf8df8287d73976ab556bb109086 Mon Sep 17 00:00:00 2001 From: Kevin Townsend Date: Tue, 1 Aug 2017 06:52:09 +0200 Subject: [PATCH] Sketch cleanup --- .../rssi_proximity_central.ino | 115 ++++++++++-------- 1 file changed, 67 insertions(+), 48 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Projects/rssi_proximity/rssi_proximity_central/rssi_proximity_central.ino b/libraries/Bluefruit52Lib/examples/Projects/rssi_proximity/rssi_proximity_central/rssi_proximity_central.ino index 8d8339de..6fb3a2f8 100644 --- a/libraries/Bluefruit52Lib/examples/Projects/rssi_proximity/rssi_proximity_central/rssi_proximity_central.ino +++ b/libraries/Bluefruit52Lib/examples/Projects/rssi_proximity/rssi_proximity_central/rssi_proximity_central.ino @@ -59,7 +59,7 @@ #define VERBOSE_OUTPUT (0) // Set this to 1 for verbose adv packet output to the serial monitor #define ENABLE_TFT (0) // Set this to 1 to enable ILI9341 TFT display support -#define ARRAY_SIZE (6) // The number of RSSI values to store and compare +#define ARRAY_SIZE (4) // The number of RSSI values to store and compare #define TIMEOUT_MS (2500) // Number of milliseconds before a record is invalidated in the list #if (ARRAY_SIZE <= 0) @@ -132,12 +132,6 @@ void setup() // since 0 would be higher than any valid RSSI value records[i].rssi = -128; } - records[2].rssi = -45; - records[2].timestamp = millis(); - records[1].rssi = -65; - records[1].timestamp = millis(); - bubbleSort(); - printRecordList(); /* Enable both peripheral and central modes */ err_t err = Bluefruit.begin(true, true); @@ -175,7 +169,7 @@ void setup() Bluefruit.Scanner.setRxCallback(scan_callback); Bluefruit.Scanner.restartOnDisconnect(true); Bluefruit.Scanner.filterRssi(-80); // Only invoke callback for devices with RSSI >= -80 dBm - //Bluefruit.Scanner.filterUuid(uuid); // Only invoke callback if the target UUID was found + Bluefruit.Scanner.filterUuid(uuid); // Only invoke callback if the target UUID was found //Bluefruit.Scanner.filterMSD(0xFFFF); // Only invoke callback when MSD is present with the specified Company ID Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms Bluefruit.Scanner.useActiveScan(true); // Request scan response data @@ -191,10 +185,11 @@ void scan_callback(ble_gap_evt_adv_report_t* report) memcpy(record.addr, report->peer_addr.addr, 6); /* Copy the 6-byte device ADDR */ record.rssi = report->rssi; /* Copy the RSSI value */ record.timestamp = millis(); /* Set the timestamp (approximate) */ - if (insertRecord(&record) == 1) /* Returns -1 if the list was updated */ + if (insertRecord(&record) == 1) /* Returns 1 if the list was updated */ { /* The list was updated, print the new values */ printRecordList(); + Serial.println(""); } /* Display the device on the TFT if available */ @@ -409,20 +404,44 @@ void bubbleSort(void) /* This function will check if any records in the list * have expired and need to be invalidated, such as when * a device goes out of range. + * + * Returns the number of invalidated records, or 0 if + * nothing was changed. */ int invalidateRecords(void) { uint8_t i; + int match = 0; + /* Not enough time has elapsed to avoid an underflow error */ + if (millis() <= TIMEOUT_MS) + { + return 0; + } + + /* Check if any records have expired */ for (i=0; iaddr, 6) == 0) { - if (memcmp((void *)&records[i].addr, (void *)&record->addr, 6) == 0) - { - match = 1; - } - if (match) - { - memcpy((void *)&records[i], (void *)&record, sizeof(node_record_t)); - goto sort_then_exit; - } + match = 1; } + if (match) + { + memcpy(&records[i], record, sizeof(node_record_t)); + goto sort_then_exit; + } + } - /* 3. Check for zero'ed records */ - /* Insert if a zero record is found, then sort */ - for (i=0; i=, then sort */ - if (records[ARRAY_SIZE-1].rssi <= record->rssi) - { - memcpy((void *)&records[ARRAY_SIZE-1], (void *)&record, sizeof(node_record_t)); - goto sort_then_exit; - } + /* 4. Check RSSI of the lowest record */ + /* Replace if >=, then sort */ + if (records[ARRAY_SIZE-1].rssi <= record->rssi) + { + memcpy(&records[ARRAY_SIZE-1], record, sizeof(node_record_t)); + goto sort_then_exit; + } /* Nothing to do ... RSSI is lower than the last value, exit and ignore */ return 0; @@ -530,4 +550,3 @@ void loop() delay(1000); } -