Skip to content
Snippets Groups Projects
Commit 4ec0a6de authored by Woazboat's avatar Woazboat
Browse files

Allow configuration of wifi credentials + hostname via settings.json

parent bcd57529
Branches
Tags
No related merge requests found
......@@ -19,6 +19,7 @@ import captouch, audio, leds, gc
import os
import machine
import network
log = logging.Log(__name__, level=logging.INFO)
......@@ -36,8 +37,8 @@ def _make_reactor() -> Reactor:
settings.onoff_button_swap.subscribe(_onoff_button_swap_update)
_onoff_button_swap_update()
settings.onoff_camp_wifi.subscribe(wifi._onoff_camp_wifi_update)
wifi._onoff_camp_wifi_update()
settings.onoff_wifi.subscribe(wifi._onoff_wifi_update)
wifi._onoff_wifi_update()
return reactor
......@@ -142,6 +143,14 @@ def run_main() -> None:
bundles.update()
settings.load_all()
try:
network.hostname(
settings.str_hostname.value if settings.str_hostname.value else "flow3r"
)
except Exception as e:
log.error(f"Failed to set hostname {e}")
menu_settings = settings.build_menu()
menu_system = SimpleMenu(
[
......
......@@ -242,6 +242,42 @@ class OnOffWidget(TunableWidget):
self._tunable.set_value(not self._state)
class StringTunable(UnaryTunable):
"""
StringTunable is a UnaryTunable that has a string value
"""
def __init__(self, name: str, key: str, default: Optional[str]) -> None:
super().__init__(name, key, default)
def get_widget(self) -> TunableWidget:
return StringWidget(self)
def press(self, vm: Optional[ViewManager]) -> None:
# Text input not supported at the moment
pass
class StringWidget(TunableWidget):
"""
StringWidget is a TunableWidget for StringTunables. It renders a string.
"""
def __init__(self, tunable: StringTunable) -> None:
self._tunable = tunable
def think(self, ins: InputState, delta_ms: int) -> None:
pass
def draw(self, ctx: Context) -> None:
ctx.text_align = ctx.LEFT
ctx.text(self._tunable.value if self._tunable.value else "")
def press(self, vm: Optional[ViewManager]) -> None:
# Text input not supported at the moment
pass
class SettingsMenuItem(MenuItem):
"""
A MenuItem which draws its label offset to the left, and a Tunable's widget
......@@ -293,17 +329,23 @@ class SettingsMenu(SimpleMenu):
# Actual tunables / settings.
onoff_camp_wifi = OnOffTunable("Connect Camp WiFi", "system.camp_wifi_enabled", False)
onoff_button_swap = OnOffTunable("Swap Buttons", "system.swap_buttons", False)
onoff_debug = OnOffTunable("Debug Overlay", "system.debug", False)
onoff_debug_touch = OnOffTunable("Touch Overlay", "system.debug_touch", False)
onoff_show_tray = OnOffTunable("Show Icons", "system.show_icons", True)
onoff_wifi = OnOffTunable("Enable WiFi", "system.wifi.enabled", False)
str_wifi_ssid = StringTunable("WiFi SSID", "system.wifi.ssid", "Camp2023-open")
str_wifi_psk = StringTunable("WiFi Password", "system.wifi.psk", None)
str_hostname = StringTunable("Hostname", "system.hostname", "flow3r")
all_settings: List[UnaryTunable] = [
onoff_camp_wifi,
onoff_show_tray,
onoff_button_swap,
onoff_debug,
onoff_debug_touch,
onoff_wifi,
str_wifi_ssid,
str_wifi_psk,
str_hostname,
]
......
import network
from network import WLAN
from st3m import settings
from st3m.goose import Optional
from st3m.logging import Log
log = Log(__name__)
iface = None
iface: Optional[WLAN] = None
def setup_camp_wifi() -> None:
def setup_wifi() -> None:
global iface
iface = network.WLAN(network.STA_IF)
iface.active(True)
enable()
assert iface
try:
iface.connect(b"Camp2023-open")
if settings.str_wifi_ssid.value:
iface.connect(settings.str_wifi_ssid.value, settings.str_wifi_psk.value)
except OSError as e:
log.error(f"Could not connect to camp wifi: {e}")
log.error(f"Could not connect to wifi: {e}")
def enable() -> None:
global iface
iface = network.WLAN(network.STA_IF)
iface.active(True)
def disable() -> None:
......@@ -35,9 +44,9 @@ def is_connected() -> bool:
return False
def _onoff_camp_wifi_update() -> None:
if settings.onoff_camp_wifi.value:
setup_camp_wifi()
def _onoff_wifi_update() -> None:
if settings.onoff_wifi.value:
setup_wifi()
else:
disable()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment