Skip to content

Commit d33956c

Browse files
committed
Add blinking led callbacks
1 parent 0e4513f commit d33956c

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

upython/backend/board.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def __init__(self):
2525
self.pinout_config = json.load(pinout_config_file)
2626
print(self.pinout_config)
2727
self.status_led = StatusLed(self.__load_output(self.pinout_config['status_led']))
28+
self.wifi_manager.on_connecting = lambda: self.status_led.wifi_connecting()
29+
self.wifi_manager.on_station_connected = lambda _: self.status_led.status_ok()
30+
self.wifi_manager.on_ap_started = lambda _: self.status_led.wifi_failed()
2831

2932

3033
def __load_output(self, config: dict):

upython/backend/boards/esp32/wifi_manager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import network
33
import binascii
44
import typing
5+
import protocols.wifi_manager
6+
57
if typing.TYPE_CHECKING:
68
from config import Config
79

@@ -39,8 +41,9 @@ def __str__(self):
3941
def __repr__(self):
4042
return self.__str__()
4143

42-
class ESPWiFiManager:
44+
class ESPWiFiManager(protocols.wifi_manager.WiFiManager):
4345
def __init__(self, config: Config):
46+
super().__init__(config)
4447
self.config = config
4548
self.station_wlan = network.WLAN(network.STA_IF)
4649
self.ap_wlan = network.WLAN(network.AP_IF)
@@ -51,6 +54,7 @@ def scan(self) -> list[ScanResult]:
5154
return [ScanResult(*result) for result in raw_results]
5255

5356
async def connect_stations(self, connect_ap_on_failure: bool = True):
57+
self.on_connecting()
5458
self.set_hostname()
5559
self.ap_wlan.active(False) # Disable AP mode while trying to connect to stations
5660
print('Connecting to WiFi stations...')
@@ -83,6 +87,7 @@ async def connect_stations(self, connect_ap_on_failure: bool = True):
8387
for _ in range(10):
8488
if wlan.isconnected():
8589
print(f'Connected to {wifi.ssid}')
90+
self.on_station_connected(wifi.ssid)
8691
return
8792
await asyncio.sleep(1) # Non-blocking wait
8893
print(f'Failed to connect to {wifi.ssid}')
@@ -98,6 +103,7 @@ async def start_ap(self):
98103
self.ap_wlan.active(True)
99104
self.ap_wlan.config(ssid=ap.ssid, key=ap.psk, security=3)
100105
print(f'AP started with SSID: {ap.ssid}')
106+
self.on_ap_started(ap.ssid)
101107

102108
def set_hostname(self):
103109
network.hostname(self.config.ap.ssid)

upython/backend/boards/simulator/wifi_manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import asyncio
22
from protocols.config import Config
3+
import protocols.wifi_manager
34
from boards.cpython.json_config_storage import JsonConfigStorage
45
import json
56

67

7-
class SimulatorWiFiManager:
8+
class SimulatorWiFiManager(protocols.wifi_manager.WiFiManager):
89
def __init__(self, config: Config):
10+
super().__init__(config)
911
self.config = config
1012
with open(JsonConfigStorage.CONFIG_FILE_PATH, 'r') as config_file:
1113
self.stations = json.load(config_file)['stations']
@@ -20,6 +22,7 @@ def _sorted_available_stations(self) -> list[dict]:
2022

2123

2224
async def connect_stations(self, connect_ap_on_failure: bool = True):
25+
self.on_connecting()
2326
print('Connecting to WiFi stations...')
2427
print(f'Configured stations: {self.config.stations}')
2528
available_stations = self._sorted_available_stations()
@@ -34,6 +37,7 @@ async def connect_stations(self, connect_ap_on_failure: bool = True):
3437
await asyncio.sleep(station.get('sim_delay', 1)) # Simulate connection attempt
3538
if station.get('sim_connection_allowed', True):
3639
print(f'Connected to station: {station['ssid']}')
40+
self.on_station_connected(station['ssid'])
3741
return
3842
else:
3943
print('Skipping station')
@@ -47,6 +51,7 @@ async def start_ap(self):
4751
print(f'Starting AP with SSID: {self.config.ap.ssid}')
4852
await asyncio.sleep(1) # Simulate AP startup
4953
print('AP started')
54+
self.on_ap_started(self.config.ap.ssid)
5055

5156
def set_hostname(self):
5257
print(f'Setting hostname to {self.config.ap.ssid}')

upython/backend/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ async def get_config(request):
5353
async def main():
5454
await board.start()
5555
await board.wifi_manager.connect_stations()
56-
57-
board.status_led.wifi_connecting()
58-
await app.start_server(port=int(os.environ.get('PORT', '80')))
56+
await app.start_server(port=int(os.environ.get('PORT', '80')), debug=os.environ.get('DEBUG', 0) in ['1', 'true'])
5957

6058
asyncio.run(main())
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from protocols.config import Config
22
from typing import Protocol
3+
from typing import Callable
34

45

56
class WiFiManager(Protocol):
6-
def __init__(self, config: Config):
7-
pass
7+
_on_ap_started: Callable[[str], None]
8+
_on_station_connected: Callable[[str], None]
9+
_on_connecting: Callable[[], None]
810

9-
def scan(self) -> list[str]:
10-
raise NotImplementedError()
11+
def __init__(self, config: Config):
12+
self._on_ap_started = lambda ssid: None
13+
self._on_station_connected = lambda ssid: None
14+
self._on_connecting = lambda: None
1115

1216
async def connect_stations(self, connect_ap_on_failure: bool = True):
1317
raise NotImplementedError()
@@ -18,4 +22,27 @@ async def start_ap(self):
1822
def set_hostname(self):
1923
raise NotImplementedError()
2024

21-
25+
@property
26+
def on_station_connected(self) -> Callable[[str], None]:
27+
return self._on_station_connected
28+
29+
@on_station_connected.setter
30+
def on_station_connected(self, callback: Callable[[str], None]) -> None:
31+
self._on_station_connected = callback
32+
33+
@property
34+
def on_ap_started(self) -> Callable[[str], None]:
35+
return self._on_ap_started
36+
37+
@on_ap_started.setter
38+
def on_ap_started(self, callback: Callable[[str], None]) -> None:
39+
self._on_ap_started = callback
40+
41+
@property
42+
def on_connecting(self) -> Callable[[], None]:
43+
return self._on_connecting
44+
45+
@on_connecting.setter
46+
def on_connecting(self, callback: Callable[[], None]) -> None:
47+
self._on_connecting = callback
48+

upython/backend/simulator

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
SIMULATOR=1 PORT=8080 DEBUG=1 python3 main.py

upython/backend/status_led.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ def _set_led(self, state):
2323

2424

2525
def wifi_connecting(self):
26-
self.pattern = [(True, 0.5), (False, 0.5)]
26+
self.pattern = [(True, 0.2), (False, 0.2)]
2727

2828
def status_ok(self):
29-
pass
29+
self.pattern = [(True, 2), (False, 0.4)]
3030

3131
def wifi_failed(self):
32-
pass
32+
self.pattern = [(True, 0.4), (False, 0.4)] * 3 + [(False, 2)]

0 commit comments

Comments
 (0)