Verified Commit f2d50a71 authored by genofire's avatar genofire
Browse files

py(ble): menu for toggle fileTrans

parent 15dd5318
import os
import display
import utime
import buttons
import simple_menu
CONFIG_NAME = "ble.txt"
MAC_NAME = "mac.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)
BLE="active"
BLE_FILETRANS="fileTrans"
ENABLE = "true"
DISABLE = "false"
MENU = {
BLE: {
"name:":"Active",
"value":False,
"run": lambda: toggle(BLE),
},
BLE_FILETRANS: {
"name":"FileTransfer",
"value": False,
"run": lambda: toggle(BLE),
},
"__save__": {
"name": "Save",
"run": lambda: save() os.reset(),
}
}
def load_mac():
if MAC_NAME in os.listdir("."):
with open(MAC_NAME) as f:
return f.read().strip()
def init():
if CONFIG_NAME not in os.listdir("."):
with open(CONFIG_NAME, "w") as f:
f.write(BLE+"="+DISABLE)
f.write(BLE_FILETRANS+"="+ENABLE)
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, 0])
mac = load_mac()
if mac is not None:
disp.print(mac[9:], posy=60, fg=[0, 0, 255])
else:
disp.print("inactive", posy=20, fg=[255, 0, 0])
def selector():
triangle(disp, 148, 46, False)
disp.print("toggle", posx=25, posy=40, fg=[255, 255, 255])
disp = display.open()
button_pressed = True
init()
while True:
disp.clear()
headline()
v = buttons.read(buttons.TOP_RIGHT)
if v == 0:
button_pressed = False
def load():
with open(CONFIG_NAME) as f:
for line in f.readlines():
key, value = line.split("=")
MENU[key].value = value == ENABLE,
if not button_pressed and v & buttons.TOP_RIGHT != 0:
button_pressed = True
toggle()
def toggle(key):
MENU[key].value = not MENU[key].value
selector()
disp.update()
utime.sleep(0.1)
def save():
with open(CONFIG_NAME, "w") as f:
f.write(BLE + "=" + MENU[BLE].value)
f.write(BLE_FILETRANS + "=" + MENU[BLE_FILETRANS].value)
class BLEMenu(simple_menu.Menu):
color_1 = color.CAMPGREEN
color_2 = color.CAMPGREEN_DARK
def draw_entry(self, item, index, offset):
name = item if type(item) is str else item.name
self.disp.print(
" " + name + " " * 9,
posy=offset,
fg=self.color_text,
bg=self.color_1 if index % 2 == 0 else self.color_2,
)
if value in item:
self.disp.rect(
xs=-20, ys=offset,
xe=-4, ye=offset+18,
filled=True,
col=color.GREEN if item.value else color.RED
)
def on_select(self, item, index):
if type(item) is not str and "run" in item:
item.run()
if __name__ == "__main__":
BLEMenu([load_mac()]+MENU).run()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment