Skip to content
Snippets Groups Projects

Add w1f1 app & replace wifi in settings menu

Merged ave requested to merge ave/flow3r-firmware:add-wifi-app into main
3 files
+ 41
21
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -13,6 +13,7 @@ from .k3yboard import TextInputModel, KeyboardView
class WifiApp(Application):
WIFI_CONFIG_FILE = "/flash/w1f1_config.json"
SETTINGS_JSON_FILE = "/flash/settings.json"
def __init__(self, app_ctx: ApplicationContext) -> None:
super().__init__(app_ctx)
@@ -42,7 +43,10 @@ class WifiApp(Application):
def on_enter(self, vm: Optional[ViewManager]) -> None:
super().on_enter(vm)
self._connection_timer = 10
self._scan_timer = 0
self._iface = network.WLAN(network.STA_IF)
self._current_ssid = None
self._current_psk = None
self.input._ignore_pressed()
# TODO: big error display
@@ -137,8 +141,12 @@ class WifiApp(Application):
print(self._nearby_wlans)
def update_settings_json(self, ssid: str, psk: str) -> None:
with open("/flash/settings.json") as f:
settings_json = json.load(f)
# weirdo case
if os.path.exists(self.SETTINGS_JSON_FILE):
with open(self.SETTINGS_JSON_FILE) as f:
settings_json = json.load(f)
else:
settings_json = {"system": {}}
if "wifi" not in settings_json["system"]:
settings_json["system"]["wifi"] = {
@@ -153,7 +161,7 @@ class WifiApp(Application):
settings_json["system"]["wifi"]["ssid"] = ssid
settings_json["system"]["wifi"]["psk"] = psk
with open("/flash/settings.json", "w") as f:
with open(self.SETTINGS_JSON_FILE, "w") as f:
json.dump(settings_json, f)
def add_to_config_json(self, ssid: str, psk: str) -> None:
@@ -166,19 +174,19 @@ class WifiApp(Application):
if ssid in self._wifi_config["networks"]:
psk = self._wifi_config["networks"][ssid]["psk"]
self._current_ssid = ssid
self._current_psk = psk
try:
self._is_connecting = ssid
self._iface.connect(
ssid,
psk,
)
self.update_settings_json(ssid, psk)
if ssid not in self._wifi_config["networks"]:
self.add_to_config_json(ssid, psk)
self._status_text = "connecting"
except OSError as e:
self._status_text = str(e)
self._is_connecting = False
self._status_text = "connecting"
def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms)
@@ -192,11 +200,18 @@ class WifiApp(Application):
):
self._wlan_offset += 1
if not self._nearby_wlans and self._iface.active():
if not self._nearby_wlans and self._iface.active() and self._scan_timer <= 0:
self._status_text = "scanning"
self.scan_wifi()
self._wlan_offset = 0
self._status_text = "ready"
self._scan_timer = 1
if not self._nearby_wlans:
self._iface.disconnect()
if not self._nearby_wlans:
self._scan_timer -= delta_ms / 1000
if ins.captouch.petals[0].pressed:
if not self._petal_pressed.get(0, False):
@@ -246,12 +261,19 @@ class WifiApp(Application):
if self._iface.isconnected():
self._connection_timer = 10
self._is_connecting = False
self._status_text = "connected"
if self._current_ssid:
self.update_settings_json(self._current_ssid, self._current_psk)
if self._current_ssid not in self._wifi_config["networks"]:
self.add_to_config_json(self._current_ssid, self._current_psk)
elif self._connection_timer <= 0:
self._iface.disconnect()
self._status_text = "conn timed out"
self._is_connecting = False
if self._iface.isconnected():
self._status_text = "connected"
leds.update()
Loading