Skip to content
Snippets Groups Projects
Commit e4ac89ce authored by Maximilian Berger's avatar Maximilian Berger
Browse files

My flow3r goes brrrr on this ice

parent 49bf21b9
No related branches found
No related tags found
No related merge requests found
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)
# 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"
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 = "???"
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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment