Skip to content
Snippets Groups Projects

BLE: Show name of device which is connected / is trying to pair

Merged schneider requested to merge schneider/ble-peer-name into master
+ 16
13
@@ -9,6 +9,10 @@ import config
ble_event = None
is_active = False
STATE_IDLE = 1
STATE_NUMERIC_COMPARISON = 2
STATE_WAIT_FOR_COMPLETION = 3
STATE_FAIL = 4
def ble_callback(_):
global ble_event
@@ -74,7 +78,7 @@ def selector():
try:
active = config.get_string("ble_enable")
if active == "true" or active == "1":
if active.lower() == "true" or active == "1":
is_active = True
except OSError:
pass
@@ -82,7 +86,7 @@ except OSError:
init()
disp = display.open()
state = 1
state = STATE_IDLE
v_old = buttons.read()
while True:
@@ -90,7 +94,7 @@ while True:
v = ~v_old & v_new
v_old = v_new
if state == 1:
if state == STATE_IDLE:
# print config screen
disp.clear()
headline()
@@ -100,12 +104,11 @@ while True:
# wait for button press or ble_event
if ble_event == sys_ble.EVENT_HANDLE_NUMERIC_COMPARISON:
ble_event = None
state = 3
state = STATE_NUMERIC_COMPARISON
if v & buttons.TOP_RIGHT:
toggle()
# state = 1
elif state == 3:
elif state == STATE_NUMERIC_COMPARISON:
# print confirmation value
compare_value = sys_ble.get_compare_value()
disp.clear()
@@ -125,23 +128,23 @@ while True:
# wait for button press or ble_event
if ble_event == sys_ble.EVENT_PAIRING_FAILED:
ble_event = None
state = 6
state = STATE_FAIL
if v & buttons.BOTTOM_LEFT:
sys_ble.confirm_compare_value(True)
disp.clear()
disp.print("BLE Pairing", posy=0, fg=[0, 0, 255])
disp.print("Please Wait", posy=40, fg=[255, 255, 255])
disp.update()
state = 5
state = STATE_WAIT_FOR_COMPLETION
elif v & (buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT):
sys_ble.confirm_compare_value(False)
state = 6
state = STATE_FAIL
elif state == 5:
elif state == STATE_WAIT_FOR_COMPLETION:
# Wait for pairing to complete
if ble_event == sys_ble.EVENT_PAIRING_FAILED:
ble_event = None
state = 6
state = STATE_FAIL
elif ble_event == sys_ble.EVENT_PAIRING_COMPLETE:
ble_event = None
pairing_name = sys_ble.get_last_pairing_name().split("/")[-1].split(".")[0]
@@ -154,13 +157,13 @@ while True:
time.sleep(5)
os.exec("main.py")
elif state == 6:
elif state == STATE_FAIL:
# display fail screen and wait 5 seconds
disp.clear()
disp.print("BLE Pairing", posy=0, fg=[0, 0, 255])
disp.print(" Fail", posy=40, fg=[255, 0, 0])
disp.update()
time.sleep(5)
state = 1
state = STATE_IDLE
time.sleep(0.1)
Loading