Skip to content

Commit 7290060

Browse files
committed
Merge branch 'develop' of github.com:adafruit/Adafruit_nRF52_Arduino into develop
2 parents d915371 + 69f29a4 commit 7290060

File tree

1 file changed

+66
-47
lines changed

1 file changed

+66
-47
lines changed

libraries/Bluefruit52Lib/examples/Projects/rssi_proximity/rssi_proximity_central/rssi_proximity_central.ino

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
#define VERBOSE_OUTPUT (0) // Set this to 1 for verbose adv packet output to the serial monitor
6161
#define ENABLE_TFT (0) // Set this to 1 to enable ILI9341 TFT display support
62-
#define ARRAY_SIZE (6) // The number of RSSI values to store and compare
62+
#define ARRAY_SIZE (4) // The number of RSSI values to store and compare
6363
#define TIMEOUT_MS (2500) // Number of milliseconds before a record is invalidated in the list
6464

6565
#if (ARRAY_SIZE <= 0)
@@ -132,12 +132,6 @@ void setup()
132132
// since 0 would be higher than any valid RSSI value
133133
records[i].rssi = -128;
134134
}
135-
records[2].rssi = -45;
136-
records[2].timestamp = millis();
137-
records[1].rssi = -65;
138-
records[1].timestamp = millis();
139-
bubbleSort();
140-
printRecordList();
141135

142136
/* Enable both peripheral and central modes */
143137
err_t err = Bluefruit.begin(true, true);
@@ -175,7 +169,7 @@ void setup()
175169
Bluefruit.Scanner.setRxCallback(scan_callback);
176170
Bluefruit.Scanner.restartOnDisconnect(true);
177171
Bluefruit.Scanner.filterRssi(-80); // Only invoke callback for devices with RSSI >= -80 dBm
178-
//Bluefruit.Scanner.filterUuid(uuid); // Only invoke callback if the target UUID was found
172+
Bluefruit.Scanner.filterUuid(uuid); // Only invoke callback if the target UUID was found
179173
//Bluefruit.Scanner.filterMSD(0xFFFF); // Only invoke callback when MSD is present with the specified Company ID
180174
Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms
181175
Bluefruit.Scanner.useActiveScan(true); // Request scan response data
@@ -191,10 +185,11 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
191185
memcpy(record.addr, report->peer_addr.addr, 6); /* Copy the 6-byte device ADDR */
192186
record.rssi = report->rssi; /* Copy the RSSI value */
193187
record.timestamp = millis(); /* Set the timestamp (approximate) */
194-
if (insertRecord(&record) == 1) /* Returns -1 if the list was updated */
188+
if (insertRecord(&record) == 1) /* Returns 1 if the list was updated */
195189
{
196190
/* The list was updated, print the new values */
197191
printRecordList();
192+
Serial.println("");
198193
}
199194

200195
/* Display the device on the TFT if available */
@@ -409,20 +404,44 @@ void bubbleSort(void)
409404
/* This function will check if any records in the list
410405
* have expired and need to be invalidated, such as when
411406
* a device goes out of range.
407+
*
408+
* Returns the number of invalidated records, or 0 if
409+
* nothing was changed.
412410
*/
413411
int invalidateRecords(void)
414412
{
415413
uint8_t i;
414+
int match = 0;
415+
416+
/* Not enough time has elapsed to avoid an underflow error */
417+
if (millis() <= TIMEOUT_MS)
418+
{
419+
return 0;
420+
}
416421

422+
/* Check if any records have expired */
417423
for (i=0; i<ARRAY_SIZE; i++)
418424
{
419-
if (records[i].timestamp <= millis() - TIMEOUT_MS)
425+
if (records[i].timestamp) // Ignore zero"ed records
420426
{
421-
/* Record has expired, zero it out and then re-sort */
422-
memset((void *)&records[i], 0, sizeof(node_record_t));
423-
bubbleSort();
427+
if (records[i].timestamp <= millis() - TIMEOUT_MS)
428+
{
429+
/* Record has expired, zero it out */
430+
memset(&records[i], 0, sizeof(node_record_t));
431+
records[i].rssi = -128;
432+
match++;
433+
}
424434
}
425-
}
435+
}
436+
437+
/* Resort the list if something was zero'ed out */
438+
if (match)
439+
{
440+
// Serial.printf("Invalidated %i records!\n", match);
441+
bubbleSort();
442+
}
443+
444+
return match;
426445
}
427446

428447
/* This function attempts to insert the record if it is larger than the smallest valid RSSI entry */
@@ -431,7 +450,8 @@ int insertRecord(node_record_t *record)
431450
{
432451
uint8_t i;
433452

434-
/* ToDo: Invalidate results after n milliseconds! */
453+
/* Invalidate results older than n milliseconds */
454+
invalidateRecords();
435455

436456
/* Record Insertion Workflow:
437457
*
@@ -474,45 +494,45 @@ int insertRecord(node_record_t *record)
474494
* +----------------+
475495
*/
476496

477-
/* 1. Bubble Sort
478-
* This puts the lists in a known state where we can make
479-
* certain assumptions about the last record in the array. */
480-
bubbleSort();
497+
/* 1. Bubble Sort
498+
* This puts the lists in a known state where we can make
499+
* certain assumptions about the last record in the array. */
500+
bubbleSort();
481501

482-
/* 2. Check for a match on existing device address */
483-
/* Replace it if a match is found, then sort */
484-
uint8_t match = 0;
485-
for (i=0; i<ARRAY_SIZE; i++)
502+
/* 2. Check for a match on existing device address */
503+
/* Replace it if a match is found, then sort */
504+
uint8_t match = 0;
505+
for (i=0; i<ARRAY_SIZE; i++)
506+
{
507+
if (memcmp(records[i].addr, record->addr, 6) == 0)
486508
{
487-
if (memcmp((void *)&records[i].addr, (void *)&record->addr, 6) == 0)
488-
{
489-
match = 1;
490-
}
491-
if (match)
492-
{
493-
memcpy((void *)&records[i], (void *)&record, sizeof(node_record_t));
494-
goto sort_then_exit;
495-
}
509+
match = 1;
496510
}
497-
498-
/* 3. Check for zero'ed records */
499-
/* Insert if a zero record is found, then sort */
500-
for (i=0; i<ARRAY_SIZE; i++)
511+
if (match)
501512
{
502-
if (records[i].rssi == -128)
503-
{
504-
memcpy((void *)&records[i], (void *)&record, sizeof(node_record_t));
505-
goto sort_then_exit;
506-
}
513+
memcpy(&records[i], record, sizeof(node_record_t));
514+
goto sort_then_exit;
507515
}
516+
}
508517

509-
/* 4. Check RSSI of the lowest record */
510-
/* Replace if >=, then sort */
511-
if (records[ARRAY_SIZE-1].rssi <= record->rssi)
518+
/* 3. Check for zero'ed records */
519+
/* Insert if a zero record is found, then sort */
520+
for (i=0; i<ARRAY_SIZE; i++)
521+
{
522+
if (records[i].rssi == -128)
512523
{
513-
memcpy((void *)&records[ARRAY_SIZE-1], (void *)&record, sizeof(node_record_t));
514-
goto sort_then_exit;
524+
memcpy(&records[i], record, sizeof(node_record_t));
525+
goto sort_then_exit;
515526
}
527+
}
528+
529+
/* 4. Check RSSI of the lowest record */
530+
/* Replace if >=, then sort */
531+
if (records[ARRAY_SIZE-1].rssi <= record->rssi)
532+
{
533+
memcpy(&records[ARRAY_SIZE-1], record, sizeof(node_record_t));
534+
goto sort_then_exit;
535+
}
516536

517537
/* Nothing to do ... RSSI is lower than the last value, exit and ignore */
518538
return 0;
@@ -530,4 +550,3 @@ void loop()
530550

531551
delay(1000);
532552
}
533-

0 commit comments

Comments
 (0)