diff --git a/.development/sync.py b/.development/sync.py index ab6d519..3f78f59 100644 --- a/.development/sync.py +++ b/.development/sync.py @@ -61,10 +61,13 @@ def clean(storage): print("Cleaning:", end=" ", flush=True) files = glob.glob(os.path.join(storage, "*")) for f in files: - if os.path.isfile(f): - os.remove(f) - else: - shutil.rmtree(f) + try: + if os.path.isfile(f): + os.remove(f) + else: + shutil.rmtree(f) + except: + pass print("DONE") def ensure_dir(path, storage): diff --git a/badge_store/main.py b/badge_store/main.py index 0632a26..3c2ed27 100644 --- a/badge_store/main.py +++ b/badge_store/main.py @@ -6,7 +6,7 @@ To publish apps use https://badge.emfcamp.org""" ___license___ = "MIT" ___title___ = "Badge Store" -___dependencies___ = ["app", "badge_store", "dialogs", "ugfx_helper", "app"] +___dependencies___ = ["badge_store", "dialogs", "ugfx_helper", "app"] ___categories___ = ["System"] ___bootstrapped___ = True diff --git a/bootstrap.py b/bootstrap.py index c55bd0b..06d0c63 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -1,8 +1,11 @@ """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" +wifi = network.WLAN() +wifi.active(True) # Helpers def msg(text): @@ -14,22 +17,45 @@ def msg(text): for i, line in enumerate(lines): ugfx.text(5, 65 + i * 20, line, ugfx.BLACK) -def wifi_details(final_try = False): - if not "wifi.json" in os.listdir(): - with open("wifi.json", "wt") as f: - f.write(json.dumps({"ssid":"emfcamp","pw":"emfemf"})) - f.flush() - os.sync() - with open("wifi.json") as f: - try: - return json.loads(f.read()) - except Exception as e: - if final_try: - raise e - os.remove("wifi.json") - return wifi_details() +def wifi_select(): + msg("Please select your wifi\nConfirm with button A") + sl = ugfx.List(5, 110, 228, 204) + aps = {} + while not Buttons.is_pressed(Buttons.BTN_A): + for s in (wifi.scan() or []): + if s[0] not in aps: + sl.add_item(s[0]) + aps[s[0]] = s + time.sleep(0.01) + ugfx.poll() + ssid = sl.selected_text() + sl.destroy() -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() if 'pw' in details: wifi.connect(details['ssid'], details['pw']) @@ -39,7 +65,8 @@ def connect(wifi): wait_until = time.ticks_ms() + 10000 while not wifi.isconnected(): 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) def addrinfo(host, port, retries_left = 20): @@ -134,11 +161,14 @@ def makedirs(path): # Steps def step_wifi(): - msg("Connecting to wifi..."); - wifi = network.WLAN() - wifi.active(True) - if not wifi.isconnected(): - connect(wifi) + while not wifi.isconnected(): + msg("Connecting to wifi..."); + try: + connect() + except Exception as e: + print(str(e)) + msg("Couldn't connect\nPlease check wifi details") + time.sleep(1) def step_download(): msg("Connecting to server...") diff --git a/home_default/main.py b/home_default/main.py index a5b3099..f4131dd 100644 --- a/home_default/main.py +++ b/home_default/main.py @@ -6,7 +6,7 @@ newly activated or reset. """ ___name___ = "Homescreen (Default)" -___license___ = "GPL" +___license___ = "MIT" ___categories___ = ["homescreen"] ___dependencies___ = ["homescreen"] ___launchable___ = False diff --git a/settings/main.py b/settings/main.py index 9edb20e..e5e4b45 100644 --- a/settings/main.py +++ b/settings/main.py @@ -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" -___license___ = "GPL" +___license___ = "MIT" +___dependencies___ = ["dialogs", "ugfx_helper", "database"] ___categories___ = ["System"] -___launchable___ = True ___bootstrapped___ = True -___dependencies___ = ["app", "dialogs"] -import app -from dialogs import * +import ugfx_helper, os, wifi, app, database -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")