Skip to content

Commit a9abe6d

Browse files
authored
Added 128x32 pixel OLED support
1 parent 7290060 commit a9abe6d

File tree

1 file changed

+127
-37
lines changed

1 file changed

+127
-37
lines changed

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

Lines changed: 127 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@
3535
* advertising payloads, with fully parsed data displayed in the
3636
* Serial Monitor.
3737
*
38-
* ENABLE_TFT
39-
* ----------
40-
* An ILI9341 based TFT can optionally be used to display proximity
41-
* alerts. The Adafruit TFT Feather Wing is recommended and is the only
42-
* device tested with this functionality.
43-
*
4438
* ARRAY_SIZE
4539
* ----------
4640
* The numbers of peripherals tracked and sorted can be set via the
@@ -51,20 +45,36 @@
5145
* This value determines the number of milliseconds before a tracked
5246
* peripheral has it's last sample 'invalidated', such as when a device
5347
* is tracked and then goes out of range.
48+
*
49+
* ENABLE_TFT
50+
* ----------
51+
* An ILI9341 based TFT can optionally be used to display proximity
52+
* alerts. The Adafruit TFT Feather Wing is recommended and is the only
53+
* device tested with this functionality.
54+
*
55+
* ENABLE_OLED
56+
* -----------
57+
* An SSD1306 128x32 based OLED can optionally be used to display
58+
* proximity alerts. The Adafruit OLED Feather Wing is recommended and
59+
* is the only device tested with this functionality.
5460
*/
5561

5662
#include <string.h>
5763
#include <bluefruit.h>
5864
#include <SPI.h>
5965

6066
#define VERBOSE_OUTPUT (0) // Set this to 1 for verbose adv packet output to the serial monitor
61-
#define ENABLE_TFT (0) // Set this to 1 to enable ILI9341 TFT display support
6267
#define ARRAY_SIZE (4) // The number of RSSI values to store and compare
6368
#define TIMEOUT_MS (2500) // Number of milliseconds before a record is invalidated in the list
69+
#define ENABLE_TFT (0) // Set this to 1 to enable ILI9341 TFT display support
70+
#define ENABLE_OLED (0) // Set this to 1 to enable SSD1306 128x32 OLED display support
6471

6572
#if (ARRAY_SIZE <= 0)
6673
#error "ARRAY_SIZE must be a non-zero value"
6774
#endif
75+
#if (ENABLE_TFT) && (ENABLE_OLED)
76+
#error "ENABLE_TFT and ENABLE_OLED can not both be set at the same time"
77+
#endif
6878

6979
// Custom UUID used to differentiate this device.
7080
// Use any online UUID generator to generate a valid UUID.
@@ -91,12 +101,30 @@ BLEUuid uuid = BLEUuid(CUSTOM_UUID);
91101
#define STMPE_CS 30
92102
#define SD_CS 27
93103
#else
94-
#error "Unknown target. Please make sure you are using an nRF52 Feather and BSP 0.6.5 or higher!"
104+
#error "Unknown target. Please make sure you are using an nRF52 Feather and BSP 0.6.5 or higher!"
95105
#endif
96106

97107
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
98108
#endif
99109

110+
/* 128x32 OLED setup if the OLED display is available */
111+
#if (ENABLE_OLED)
112+
#include <Wire.h>
113+
#include <Adafruit_GFX.h>
114+
#include <Adafruit_SSD1306.h>
115+
116+
/* Pin setup for the OLED display */
117+
#ifdef ARDUINO_NRF52_FEATHER
118+
#define BUTTON_A 31
119+
#define BUTTON_B 30
120+
#define BUTTON_C 27
121+
#else
122+
#error "Unknown target. Please make sure you are using an nRF52 Feather and BSP 0.6.5 or higher!"
123+
#endif
124+
125+
Adafruit_SSD1306 oled = Adafruit_SSD1306();
126+
#endif
127+
100128
/* This struct is used to track detected nodes */
101129
typedef struct node_record_s
102130
{
@@ -124,6 +152,12 @@ void setup()
124152
tft.println("DUNKIN");
125153
#endif
126154

155+
/* Enable the OLED display if available */
156+
#if ENABLE_OLED
157+
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
158+
oled.display();
159+
#endif
160+
127161
/* Clear the results list */
128162
memset(records, 0, sizeof(records));
129163
for (uint8_t i = 0; i<ARRAY_SIZE; i++)
@@ -169,7 +203,7 @@ void setup()
169203
Bluefruit.Scanner.setRxCallback(scan_callback);
170204
Bluefruit.Scanner.restartOnDisconnect(true);
171205
Bluefruit.Scanner.filterRssi(-80); // Only invoke callback for devices with RSSI >= -80 dBm
172-
Bluefruit.Scanner.filterUuid(uuid); // Only invoke callback if the target UUID was found
206+
//Bluefruit.Scanner.filterUuid(uuid); // Only invoke callback if the target UUID was found
173207
//Bluefruit.Scanner.filterMSD(0xFFFF); // Only invoke callback when MSD is present with the specified Company ID
174208
Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms
175209
Bluefruit.Scanner.useActiveScan(true); // Request scan response data
@@ -180,43 +214,32 @@ void setup()
180214
/* This callback handler is fired every time a valid advertising packet is detected */
181215
void scan_callback(ble_gap_evt_adv_report_t* report)
182216
{
183-
/* Try to insert this packet into the existing record list */
184217
node_record_t record;
218+
219+
/* Prepare the record to try to insert it into the existing record list */
185220
memcpy(record.addr, report->peer_addr.addr, 6); /* Copy the 6-byte device ADDR */
186221
record.rssi = report->rssi; /* Copy the RSSI value */
187222
record.timestamp = millis(); /* Set the timestamp (approximate) */
223+
224+
/* Attempt to insert the record into the list */
188225
if (insertRecord(&record) == 1) /* Returns 1 if the list was updated */
189226
{
190-
/* The list was updated, print the new values */
191-
printRecordList();
227+
printRecordList(); /* The list was updated, print the new values */
192228
Serial.println("");
229+
230+
/* Display the device list on the TFT if available */
231+
#if ENABLE_TFT
232+
renderResultsToTFT();
233+
#endif
234+
235+
/* Display the device list on the OLED if available */
236+
#if ENABLE_OLED
237+
renderResultsToOLED();
238+
#endif
193239
}
194240

195-
/* Display the device on the TFT if available */
196-
#if ENABLE_TFT
197-
tft.fillRect(0, 0, 240, 30, ILI9341_BLACK);
198-
tft.setTextSize(1);
199-
tft.setCursor(0, 0);
200-
tft.print("[");
201-
tft.print(millis());
202-
tft.print("] ");
203-
tft.print(report->peer_addr.addr[0]);
204-
tft.print(":");
205-
tft.print(report->peer_addr.addr[1]);
206-
tft.print(":");
207-
tft.print(report->peer_addr.addr[2]);
208-
tft.print(":");
209-
tft.print(report->peer_addr.addr[3]);
210-
tft.print(":");
211-
tft.print(report->peer_addr.addr[4]);
212-
tft.print(":");
213-
tft.println(report->peer_addr.addr[5]);
214-
tft.print("RSSI: ");
215-
tft.print(report->rssi);
216-
tft.println(" dBm");
217-
#endif
218-
219-
/* Fully parse and display the advertising packet if verbose/debug output is requested */
241+
/* Fully parse and display the advertising packet to the Serial Monitor
242+
* if verbose/debug output is requested */
220243
#if VERBOSE_OUTPUT
221244
uint8_t len = 0;
222245
uint8_t buffer[32];
@@ -339,6 +362,73 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
339362
#endif
340363
}
341364

365+
#if ENABLE_TFT
366+
void renderResultsToTFT(void)
367+
{
368+
tft.fillRect(0, 0, 240, ARRAY_SIZE*8, ILI9341_BLACK);
369+
tft.setTextSize(1);
370+
tft.setCursor(0, 0);
371+
372+
for (uint8_t i=0; i<ARRAY_SIZE; i++)
373+
{
374+
tft.print(records[i].addr[0], HEX);
375+
tft.print(":");
376+
tft.print(records[i].addr[1], HEX);
377+
tft.print(":");
378+
tft.print(records[i].addr[2], HEX);
379+
tft.print(":");
380+
tft.print(records[i].addr[3], HEX);
381+
tft.print(":");
382+
tft.print(records[i].addr[4], HEX);
383+
tft.print(":");
384+
tft.print(records[i].addr[5], HEX);
385+
tft.print(" ");
386+
tft.print(records[i].rssi);
387+
tft.print(" [");
388+
tft.print(records[i].timestamp);
389+
tft.println("] ");
390+
}
391+
}
392+
#endif
393+
394+
#if ENABLE_OLED
395+
void renderResultsToOLED(void)
396+
{
397+
oled.clearDisplay();
398+
oled.setTextSize(1);
399+
oled.setTextColor(WHITE);
400+
401+
for (uint8_t i=0; i<ARRAY_SIZE; i++)
402+
{
403+
if (i>=4)
404+
{
405+
/* We can only display four records on a 128x32 pixel display */
406+
oled.display();
407+
return;
408+
}
409+
if (records[i].rssi != -128)
410+
{
411+
oled.setCursor(0, i*8);
412+
oled.print(records[i].addr[0], HEX);
413+
oled.print(":");
414+
oled.print(records[i].addr[1], HEX);
415+
oled.print(":");
416+
oled.print(records[i].addr[2], HEX);
417+
oled.print(":");
418+
oled.print(records[i].addr[3], HEX);
419+
oled.print(":");
420+
oled.print(records[i].addr[4], HEX);
421+
oled.print(":");
422+
oled.print(records[i].addr[5], HEX);
423+
oled.print(" ");
424+
oled.println(records[i].rssi);
425+
}
426+
}
427+
428+
oled.display();
429+
}
430+
#endif
431+
342432
/* Prints a UUID16 list to the Serial Monitor */
343433
void printUuid16List(uint8_t* buffer, uint8_t len)
344434
{

0 commit comments

Comments
 (0)