2018-09-02 09:58:57 -04:00
|
|
|
"""Scan for and display nearby bluetooth devices"""
|
|
|
|
|
2018-09-03 09:59:37 -04:00
|
|
|
___title___ = "Bluetooth Scan"
|
2018-09-02 09:58:57 -04:00
|
|
|
___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
|
2018-09-02 10:13:50 -04:00
|
|
|
duration = 10
|
2018-09-02 09:58:57 -04:00
|
|
|
status_height = 20
|
|
|
|
|
|
|
|
ugfx.init()
|
|
|
|
ugfx.clear()
|
|
|
|
ugfx.set_default_font(ugfx.FONT_FIXED)
|
|
|
|
|
2018-09-02 10:13:50 -04:00
|
|
|
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))
|
|
|
|
|
2018-09-02 09:58:57 -04:00
|
|
|
if not sim800.btison():
|
|
|
|
sim800.btpoweron()
|
|
|
|
btrestore = True
|
|
|
|
|
2018-09-02 10:13:50 -04:00
|
|
|
instructions(duration)
|
2018-09-02 09:58:57 -04:00
|
|
|
# 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):
|
2018-09-02 10:13:50 -04:00
|
|
|
a = Buttons.is_pressed(Buttons.BTN_A)
|
|
|
|
b = Buttons.is_pressed(Buttons.BTN_B)
|
|
|
|
if not a and not b:
|
2018-09-02 09:58:57 -04:00
|
|
|
ugfx.poll()
|
|
|
|
continue
|
|
|
|
|
2018-09-02 10:13:50 -04:00
|
|
|
if b:
|
|
|
|
duration = duration + 5
|
|
|
|
if duration > 60:
|
|
|
|
duration = 5
|
|
|
|
ugfx.clear()
|
|
|
|
instructions(duration)
|
|
|
|
continue
|
|
|
|
|
2018-09-02 09:58:57 -04:00
|
|
|
ugfx.clear()
|
|
|
|
|
|
|
|
np.display([0,0])
|
|
|
|
np.display([0x000099, 0x000099])
|
2018-09-02 10:13:50 -04:00
|
|
|
devs = sim800.btscan(duration*1000)
|
|
|
|
np.display([0x00, 0x00])
|
2018-09-02 09:58:57 -04:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
y = 0
|
|
|
|
for dev in devs[:20]:
|
|
|
|
ugfx.Label(0, y, 240, 25, "{3}dB {1}".format(*dev))
|
|
|
|
y += status_height
|
2018-09-02 10:13:50 -04:00
|
|
|
instructions(duration)
|
|
|
|
|
|
|
|
## App quitting...
|
2018-09-02 09:58:57 -04:00
|
|
|
if btrestore:
|
|
|
|
sim800.btpoweroff()
|
|
|
|
|
|
|
|
ugfx.clear()
|
|
|
|
app.restart_to_default()
|