Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import display
import utime
import buttons
CONFIG_NAME = "ble.txt"
ACTIVE_STRING = "active=true"
INACTIVE_STRING = "active=false"
def init():
if CONFIG_NAME not in os.listdir("."):
with open(CONFIG_NAME, "w") as f:
f.write(INACTIVE_STRING)
def triangle(disp, x, y, left):
yf = 1 if left else -1
scale = 6
disp.line(x - scale * yf, int(y + scale / 2), x, y)
disp.line(x, y, x, y + scale)
disp.line(x, y + scale, x - scale * yf, y + int(scale / 2))
def toggle():
content = INACTIVE_STRING if is_active() else ACTIVE_STRING
with open(CONFIG_NAME, "w") as f:
f.write(content)
disp.clear()
disp.print("resetting", posy=0, fg=[0, 255, 255])
disp.print("to toggle", posy=20, fg=[0, 255, 255])
disp.print("BLE state", posy=40, fg=[0, 255, 255])
disp.update()
os.reset()
def is_active():
with open(CONFIG_NAME, "r") as f:
state = f.readlines()[0]
if len(state) < len(ACTIVE_STRING):
return False
state = state[0 : len(ACTIVE_STRING)]
return state == ACTIVE_STRING
def headline():
disp.print("BLE", posy=0, fg=[0, 255, 255])
if is_active():
disp.print("active", posy=20, fg=[0, 255, 255])
else:
disp.print("inactive", posy=20, fg=[0, 255, 255])
def selector():
triangle(disp, 148, 46, False)
disp.print("toggle", posx=25, posy=40, fg=[0, 255, 0])
disp = display.open()
button_pressed = True
init()
while True:
disp.clear()
headline()
v = buttons.read(buttons.TOP_RIGHT)
if v == 0:
button_pressed = False
if not button_pressed and v & buttons.TOP_RIGHT != 0:
button_pressed = True
toggle()
selector()
disp.update()
utime.sleep(0.1)