Add wifi select UI to bootstrap

philcrump-phil-add-ntp
Marek Ventur 2018-08-27 10:02:16 +01:00
parent 69c7cd39b5
commit 6564ac3681
5 changed files with 129 additions and 37 deletions

View File

@ -61,10 +61,13 @@ def clean(storage):
print("Cleaning:", end=" ", flush=True) print("Cleaning:", end=" ", flush=True)
files = glob.glob(os.path.join(storage, "*")) files = glob.glob(os.path.join(storage, "*"))
for f in files: for f in files:
if os.path.isfile(f): try:
os.remove(f) if os.path.isfile(f):
else: os.remove(f)
shutil.rmtree(f) else:
shutil.rmtree(f)
except:
pass
print("DONE") print("DONE")
def ensure_dir(path, storage): def ensure_dir(path, storage):

View File

@ -6,7 +6,7 @@ To publish apps use https://badge.emfcamp.org"""
___license___ = "MIT" ___license___ = "MIT"
___title___ = "Badge Store" ___title___ = "Badge Store"
___dependencies___ = ["app", "badge_store", "dialogs", "ugfx_helper", "app"] ___dependencies___ = ["badge_store", "dialogs", "ugfx_helper", "app"]
___categories___ = ["System"] ___categories___ = ["System"]
___bootstrapped___ = True ___bootstrapped___ = True

View File

@ -1,8 +1,11 @@
"""Bootstraps the badge by downloading the base software""" """Bootstraps the badge by downloading the base software"""
import ugfx, machine, network, json, time, usocket, os import ugfx, machine, network, json, time, usocket, os, gc
from tilda import Buttons
HOST = "badgeserver.emfcamp.org" HOST = "badgeserver.emfcamp.org"
wifi = network.WLAN()
wifi.active(True)
# Helpers # Helpers
def msg(text): def msg(text):
@ -14,22 +17,45 @@ def msg(text):
for i, line in enumerate(lines): for i, line in enumerate(lines):
ugfx.text(5, 65 + i * 20, line, ugfx.BLACK) ugfx.text(5, 65 + i * 20, line, ugfx.BLACK)
def wifi_details(final_try = False): def wifi_select():
if not "wifi.json" in os.listdir(): msg("Please select your wifi\nConfirm with button A")
with open("wifi.json", "wt") as f: sl = ugfx.List(5, 110, 228, 204)
f.write(json.dumps({"ssid":"emfcamp","pw":"emfemf"})) aps = {}
f.flush() while not Buttons.is_pressed(Buttons.BTN_A):
os.sync() for s in (wifi.scan() or []):
with open("wifi.json") as f: if s[0] not in aps:
try: sl.add_item(s[0])
return json.loads(f.read()) aps[s[0]] = s
except Exception as e: time.sleep(0.01)
if final_try: ugfx.poll()
raise e ssid = sl.selected_text()
os.remove("wifi.json") sl.destroy()
return wifi_details()
def connect(wifi): msg("Wifi: %s\nPlease enter your password\nConfirm with button A" % ssid)
kb = ugfx.Keyboard(0, 160, 240, 170)
e = ugfx.Textbox(5, 130, 228, 25, text="")
while not Buttons.is_pressed(Buttons.BTN_A):
time.sleep(0.01)
ugfx.poll()
pw = e.text()
e.destroy()
kb.destroy()
result = {"ssid":ssid,"pw":pw}
with open("wifi.json", "wt") as file:
file.write(json.dumps(result))
file.flush()
os.sync()
return result
def wifi_details():
try:
with open("wifi.json") as f:
return json.loads(f.read())
except Exception as e:
print(str(e))
return wifi_select()
def connect():
details = wifi_details() details = wifi_details()
if 'pw' in details: if 'pw' in details:
wifi.connect(details['ssid'], details['pw']) wifi.connect(details['ssid'], details['pw'])
@ -39,7 +65,8 @@ def connect(wifi):
wait_until = time.ticks_ms() + 10000 wait_until = time.ticks_ms() + 10000
while not wifi.isconnected(): while not wifi.isconnected():
if (time.ticks_ms() > wait_until): if (time.ticks_ms() > wait_until):
raise OSError("Timeout while trying to\nconnect to wifi.\n\nPlease connect your\nbadge to your computer\nand edit wifi.json with\nyour wifi details"); os.remove("wifi.json")
raise OSError("Wifi timeout");
time.sleep(0.1) time.sleep(0.1)
def addrinfo(host, port, retries_left = 20): def addrinfo(host, port, retries_left = 20):
@ -134,11 +161,14 @@ def makedirs(path):
# Steps # Steps
def step_wifi(): def step_wifi():
msg("Connecting to wifi..."); while not wifi.isconnected():
wifi = network.WLAN() msg("Connecting to wifi...");
wifi.active(True) try:
if not wifi.isconnected(): connect()
connect(wifi) except Exception as e:
print(str(e))
msg("Couldn't connect\nPlease check wifi details")
time.sleep(1)
def step_download(): def step_download():
msg("Connecting to server...") msg("Connecting to server...")

View File

@ -6,7 +6,7 @@ newly activated or reset.
""" """
___name___ = "Homescreen (Default)" ___name___ = "Homescreen (Default)"
___license___ = "GPL" ___license___ = "MIT"
___categories___ = ["homescreen"] ___categories___ = ["homescreen"]
___dependencies___ = ["homescreen"] ___dependencies___ = ["homescreen"]
___launchable___ = False ___launchable___ = False

View File

@ -1,17 +1,76 @@
"""Generic setting used by different apps""" """Settings app for common or shared settings
Currently supports
* Setting name
* Setting wifi
* Pick default app
* Change badgestore repo/branch
"""
___name___ = "Settings" ___name___ = "Settings"
___license___ = "GPL" ___license___ = "MIT"
___dependencies___ = ["dialogs", "ugfx_helper", "database"]
___categories___ = ["System"] ___categories___ = ["System"]
___launchable___ = True
___bootstrapped___ = True ___bootstrapped___ = True
___dependencies___ = ["app", "dialogs"]
import app import ugfx_helper, os, wifi, app, database
from dialogs import *
ugfx.init() ### VIEWS ###
option = prompt_option(["tbd"], none_text="Exit", title="Settings") ugfx_helper.init()
app.restart_to_default() title = "Settings"
def clear():
ugfx.clear(ugfx.html_color(ugfx.WHITE))
def settings_name(state):
pass
def settings_startup_app(state):
pass
def settings_wifi(state):
pass
def settings_badge_store(state):
pass
def settings_main(state):
menu_items = [
{"title": "Change Name", "function": settings_name},
{"title": "Wifi", "function": settings_wifi},
{"title": "Set startup app", "function": settings_startup_app},
{"title": "Change Badge Store", "function": settings_badge_store}
]
return prompt_option(menu_items, none_text="Exit", text="What do you want to do?", title=title)
# Todo: this might be useful in a lib
# A stack-based naviation system.
NEXT_EXIT = "exit" # Leave navigation
NEXT_INIT = "init" # Go to top of stack
def nav(init_fn, init_state={}):
stack = [(init_fn, init_state)]
while len(stack):
(fn, state) = stack[-1] # peek
next_state = state.clone()
print(next_state)
result = fn(next_state)
if callable(result):
stack.append((result, next_state))
elif result == NEXT_INIT:
stack = [(init_fn, init_state)]
elif result == NEXT_EXIT:
break
else:
stack.pop()
print("bye")
# Entry point
nav(settings_main)
#show_app("launcher")