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
5 unresolved threads
5 files
+ 55
30
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -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
@@ -57,11 +61,7 @@ class WifiApp(Application):
ctx.rgb(0.2, 0.2, 0.2).rectangle(-120, 90, 240, 30).fill()
ctx.font_size = 15
current_ssid = (
self._iface.config("ssid")
if self._iface.active() and self._iface.isconnected()
else ""
)
current_ssid = self._iface.config("ssid")
ctx.save()
ctx.rgb(1, 1, 1)
@@ -83,7 +83,11 @@ class WifiApp(Application):
for wlan in self._nearby_wlans:
ssid = wlan[0].decode()
if ssid == current_ssid:
if (
ssid == current_ssid
and self._iface.active()
and self._iface.isconnected()
):
ctx.rgb(0, 1, 0)
elif ssid == self._is_connecting:
ctx.rgb(0, 0, 1)
@@ -130,13 +134,19 @@ class WifiApp(Application):
def scan_wifi(self):
# skip hidden WLANs
self._nearby_wlans = [wlan for wlan in self._iface.scan() if not wlan[5]]
self._nearby_wlans = [
wlan for wlan in self._iface.scan() if not wlan[5] and wlan[0]
]
# TODO: sort by known, then signal strength
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"] = {
@@ -151,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:
@@ -164,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)
@@ -190,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):
@@ -229,7 +246,9 @@ class WifiApp(Application):
self._waiting_for_password = True
self.vm.push(KeyboardView(self._password_model))
if self._waiting_for_password and not self.vm._history:
if self._waiting_for_password and (
not self.vm._history or not isinstance(self.vm._history[-1], WifiApp)
):
ssid = self._nearby_wlans[self._wlan_offset][0].decode()
psk = self._password_model.text
print(ssid, psk)
@@ -242,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