diff --git a/__init__.py b/__init__.py index bb70eeb9b98163a1a9f3df0274c599b18496e1bd..fa4af692138aa967eeec2c4a5a491e131416810f 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,4 @@ import gc -import json import math import leds @@ -14,15 +13,37 @@ from st3m.ui.colours import BLACK, GO_GREEN, PUSH_RED AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" +# Set this to true to run without actually attempting WiFi connection. +FAKE = False + class TrainType(Enum): ICE = 0 RAILJET = 1 -# Set this to a train type to run without actually attempting WiFi connection. -# FAKE = TrainType.ICE -FAKE = None +ALL_TRAIN_CONFIGS: Dict[TrainType, Dict[str, Any]] = { + TrainType.ICE: { + "name": "ICE", + "logo": "ICE-Logo.png", + "logo_location": (-35, -80, 70, 39), + "max_speed": 300, # 300km/h in germany, 320 in france + "theme": { + "background": (0.137, 0.161, 0.204), + "text": (1, 1, 1), + }, + }, + TrainType.RAILJET: { + "name": "Railjet", + "logo": "Railjet-Logo.png", + "logo_location": (-58, -80, 116, 30), + "max_speed": 260, + "theme": { + "background": (0.275, 0.275, 0.267), + "text": (0.969, 0.984, 0.961), + }, + }, +} class AppState(Enum): @@ -34,17 +55,24 @@ class AppState(Enum): RUN1 = 5 +TRAIN_TYPE = TrainType.ICE +TRAIN_CONFIG = ALL_TRAIN_CONFIGS[TRAIN_TYPE] + + class App(Application): def __init__(self, app_ctx: ApplicationContext) -> None: super().__init__(app_ctx) self.nic = network.WLAN(network.STA_IF) self.nic.active(True) - if FAKE is None: + + # Skip all the WiFi stuff if we're running in fake mode. + if not FAKE: self.state: AppState = AppState.DETECTING self.train_type: Optional[TrainType] = None else: self.state = AppState.RUN1 self.train_type = FAKE + self.time = 0 self.last_request_time = 0 self.bundle_path = app_ctx.bundle_path @@ -52,7 +80,7 @@ class App(Application): def on_enter(self, vm: Optional[ViewManager]) -> None: super().on_enter(vm) - if FAKE is None: + if not FAKE: self.state = AppState.DETECTING self.train_type = None else: @@ -60,12 +88,9 @@ class App(Application): self.train_type = FAKE def draw(self, ctx: Context) -> None: - if self.train_type == TrainType.ICE: - ctx.rgb(0.137, 0.161, 0.204) - elif self.train_type == TrainType.RAILJET: - ctx.rgb(0.275, 0.275, 0.267) - else: - ctx.rgb(0, 0, 0) + # Draw background + bg_color = TRAIN_CONFIG["theme"]["background"] + ctx.rgb(bg_color[0], bg_color[1], bg_color[2]) ctx.rectangle(-120, -120, 240, 240).fill() if self.state in ( @@ -96,14 +121,10 @@ class App(Application): ctx.font_size = 1 ctx.font = ctx.get_font_name(1) - if self.train_type == TrainType.ICE: - ctx.rgb(1, 1, 1) - text = f"{int(self.train_status['speed'])} km/h" - elif self.train_type == TrainType.RAILJET: - ctx.rgb(0.969, 0.984, 0.961) - text = f"{int(self.train_status['speed'])} km/h" - else: - text = "???" + # Show Speed + speed_color = TRAIN_CONFIG["theme"]["text"] + ctx.rgb(speed_color[0], speed_color[1], speed_color[2]) + text = f"{int(self.train_status['speed'])} km/h" scale = 1 / ctx.text_width(text) * 220 ctx.save() @@ -123,18 +144,21 @@ class App(Application): ctx.text("with Deutsche Bahn!") ctx.restore() - if self.train_type == TrainType.ICE: - ctx.image(self.bundle_path + "/ICE-Logo.png", -35, -80, 70, 39) - elif self.train_type == TrainType.RAILJET: - ctx.image(self.bundle_path + "/Railjet-Logo.png", -58, -80, 116, 30) + # Show Logo + image_name = TRAIN_CONFIG["logo"] + image_location = TRAIN_CONFIG["logo_location"] + ctx.image( + self.bundle_path + "/" + image_name, + image_location[0], + image_location[1], + image_location[2], + image_location[3], + ) # LED Speed Gauge - if self.train_type == TrainType.ICE: - speed_percent = min(self.train_status["speed"] / 260, 1.0) - elif self.train_type == TrainType.RAILJET: - speed_percent = min(self.train_status["speed"] / 260, 1.0) - else: - speed_percent = 0.0 + speed_percent = min( + self.train_status["speed"] / TRAIN_CONFIG["max_speed"], 1.0 + ) for led in range(33): if speed_percent == 0: value = 0.0 @@ -165,7 +189,7 @@ class App(Application): self.train_type = TrainType.RAILJET self.state = AppState.CONNECTING break - elif FAKE is None and not self.nic.isconnected(): + elif not FAKE and not self.nic.isconnected(): self.state = AppState.CONNECTING elif self.state == AppState.CONNECTING and self.nic.isconnected(): print("Connected!") @@ -199,7 +223,7 @@ class App(Application): elif self.state in (AppState.RUN, AppState.RUN1): if (self.time - self.last_request_time) > 100 if FAKE else 2500: self.last_request_time = self.time - if FAKE is not None: + if FAKE: self.train_status = { "speed": ( math.sin(self.time / 1000 * math.pi * 2 / 10) * 0.5 + 0.5 diff --git a/flow3r.toml b/flow3r.toml index 41af2132c0260c3bb23c93c81557877ef2abdb0c..2a7f0449df59a6c494ffd4cdb5802103abb0b0a6 100644 --- a/flow3r.toml +++ b/flow3r.toml @@ -10,4 +10,4 @@ author = "rahix, git-commit, david" license = "LGPL-3.0-only" url = "https://git.flow3r.garden/rahix/FLOW3RonICE" description = "FLOW3RonICE - Show current speed of the ICE train you're riding! (Data from WIFIonICE)" -version = 8 +version = 9