From b12b077b16a3b1fefcbfd4c30110c330fd9b6b0a Mon Sep 17 00:00:00 2001 From: Thomas Lake Date: Sun, 2 Sep 2018 14:58:57 +0100 Subject: [PATCH 1/3] Basic Bluetooth scan app --- btscan/main.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 btscan/main.py diff --git a/btscan/main.py b/btscan/main.py new file mode 100644 index 0000000..08b52cb --- /dev/null +++ b/btscan/main.py @@ -0,0 +1,65 @@ +"""Scan for and display nearby bluetooth devices""" + +___name___ = "Bluetooth Scan" +___license___ = "MIT" +___dependencies___ = ["sleep", "app", "sim800"] +___categories___ = ["Other", "System"] + +import ugfx, app +from machine import Neopix +np = Neopix() + +import sim800 +from tilda import Buttons +from time import sleep + +btrestore = False + +status_height = 20 + +ugfx.init() +ugfx.clear() +ugfx.set_default_font(ugfx.FONT_FIXED) + +ugfx.Label(5, 180, 240, 15, "Press A to scan, MENU to exit") +ugfx.Label(5, 200, 240, 15, "Scan requires ~10 seconds") +if not sim800.btison(): + sim800.btpoweron() + btrestore = True + +# while (not Buttons.is_pressed(Buttons.BTN_A)) and (not Buttons.is_pressed(Buttons.BTN_B)) and (not Buttons.is_pressed(Buttons.BTN_Menu)): +while not Buttons.is_pressed(Buttons.BTN_Menu): + if not Buttons.is_pressed(Buttons.BTN_A): + ugfx.poll() + continue + + ugfx.clear() + + np.display([0,0]) + np.display([0x000099, 0x000099]) + devs = sim800.btscan(15000) + + if len(devs) == 0: + ugfx.Label(0, 0, 240, 25, "No devices found") + np.display([0x110000,0x110000]) + sleep(1) + np.display([0,0]) + else: + if type(devs[0]) == int: + devs = [devs] + print(devs) + + y = 0 + for dev in devs[:20]: + print(dev) + ugfx.Label(0, y, 240, 25, "{3}dB {1}".format(*dev)) + y += status_height + + + np.display([0x00, 0x00]) + +if btrestore: + sim800.btpoweroff() + +ugfx.clear() +app.restart_to_default() From aee5d6b2b607eb51058e8873e49deb0a9a49e839 Mon Sep 17 00:00:00 2001 From: Thomas Lake Date: Sun, 2 Sep 2018 15:11:03 +0100 Subject: [PATCH 2/3] Append to BTScan results, don't replace --- lib/sim800.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sim800.py b/lib/sim800.py index ced8e4a..ebd3da5 100644 --- a/lib/sim800.py +++ b/lib/sim800.py @@ -558,7 +558,7 @@ def btscan(timeout=30000): response = command("AT+BTSCAN=1," + str(int(timeout/1000)), timeout+8000, "+BTSCAN: 1") for entry in extractvals("+BTSCAN: 0,", response): splitentry = entry.split(",") - result = [int(splitentry[0]), splitentry[1].strip("\""), splitentry[2], int(splitentry[3])] + result.append([int(splitentry[0]), splitentry[1].strip("\""), splitentry[2], int(splitentry[3])]) return result # Get the requesting paring device name From 9f78ea99c85711b84bbc356e8c543f80677ab6d6 Mon Sep 17 00:00:00 2001 From: Thomas Lake Date: Sun, 2 Sep 2018 15:13:50 +0100 Subject: [PATCH 3/3] Improve scan UI, slightly --- btscan/main.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/btscan/main.py b/btscan/main.py index 08b52cb..787e227 100644 --- a/btscan/main.py +++ b/btscan/main.py @@ -14,30 +14,44 @@ from tilda import Buttons from time import sleep btrestore = False - +duration = 10 status_height = 20 ugfx.init() ugfx.clear() ugfx.set_default_font(ugfx.FONT_FIXED) -ugfx.Label(5, 180, 240, 15, "Press A to scan, MENU to exit") -ugfx.Label(5, 200, 240, 15, "Scan requires ~10 seconds") +def instructions(duration): + ugfx.Label(5, 180, 240, 30, "Press A to start, B to change scan length or MENU to exit") + ugfx.Label(5, 210, 240, 15, "Scan requires ~{0} seconds".format(duration)) + if not sim800.btison(): sim800.btpoweron() btrestore = True +instructions(duration) # while (not Buttons.is_pressed(Buttons.BTN_A)) and (not Buttons.is_pressed(Buttons.BTN_B)) and (not Buttons.is_pressed(Buttons.BTN_Menu)): while not Buttons.is_pressed(Buttons.BTN_Menu): - if not Buttons.is_pressed(Buttons.BTN_A): + a = Buttons.is_pressed(Buttons.BTN_A) + b = Buttons.is_pressed(Buttons.BTN_B) + if not a and not b: ugfx.poll() continue + if b: + duration = duration + 5 + if duration > 60: + duration = 5 + ugfx.clear() + instructions(duration) + continue + ugfx.clear() np.display([0,0]) np.display([0x000099, 0x000099]) - devs = sim800.btscan(15000) + devs = sim800.btscan(duration*1000) + np.display([0x00, 0x00]) if len(devs) == 0: ugfx.Label(0, 0, 240, 25, "No devices found") @@ -47,17 +61,14 @@ while not Buttons.is_pressed(Buttons.BTN_Menu): else: if type(devs[0]) == int: devs = [devs] - print(devs) y = 0 for dev in devs[:20]: - print(dev) ugfx.Label(0, y, 240, 25, "{3}dB {1}".format(*dev)) y += status_height - - - np.display([0x00, 0x00]) - + instructions(duration) + +## App quitting... if btrestore: sim800.btpoweroff()