Skip to content
Snippets Groups Projects
Verified Commit f2d50a71 authored by genofire's avatar genofire
Browse files

py(ble): menu for toggle fileTrans

parent 15dd5318
No related branches found
No related tags found
No related merge requests found
import os import os
import display import display
import utime import simple_menu
import buttons
CONFIG_NAME = "ble.txt" CONFIG_NAME = "ble.txt"
MAC_NAME = "mac.txt" MAC_NAME = "mac.txt"
ACTIVE_STRING = "active=true" BLE="active"
INACTIVE_STRING = "active=false" BLE_FILETRANS="fileTrans"
ENABLE = "true"
DISABLE = "false"
def init():
if CONFIG_NAME not in os.listdir("."): MENU = {
with open(CONFIG_NAME, "w") as f: BLE: {
f.write(INACTIVE_STRING) "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(): def load_mac():
if MAC_NAME in os.listdir("."): if MAC_NAME in os.listdir("."):
with open(MAC_NAME) as f: with open(MAC_NAME) as f:
return f.read().strip() return f.read().strip()
def init():
def triangle(disp, x, y, left): if CONFIG_NAME not in os.listdir("."):
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: with open(CONFIG_NAME, "w") as f:
f.write(content) f.write(BLE+"="+DISABLE)
f.write(BLE_FILETRANS+"="+ENABLE)
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 load():
with open(CONFIG_NAME) as f:
for line in f.readlines():
key, value = line.split("=")
MENU[key].value = value == ENABLE,
def selector():
triangle(disp, 148, 46, False)
disp.print("toggle", posx=25, posy=40, fg=[255, 255, 255])
def toggle(key):
MENU[key].value = not MENU[key].value
disp = display.open() def save():
button_pressed = True with open(CONFIG_NAME, "w") as f:
init() f.write(BLE + "=" + MENU[BLE].value)
f.write(BLE_FILETRANS + "=" + MENU[BLE_FILETRANS].value)
while True:
disp.clear() class BLEMenu(simple_menu.Menu):
headline() color_1 = color.CAMPGREEN
v = buttons.read(buttons.TOP_RIGHT) color_2 = color.CAMPGREEN_DARK
if v == 0:
button_pressed = False def draw_entry(self, item, index, offset):
name = item if type(item) is str else item.name
if not button_pressed and v & buttons.TOP_RIGHT != 0: self.disp.print(
button_pressed = True " " + name + " " * 9,
toggle() posy=offset,
fg=self.color_text,
selector() bg=self.color_1 if index % 2 == 0 else self.color_2,
disp.update() )
utime.sleep(0.1) 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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment