From dedf1fd00e1951aa9764290c4cc956f20a1f5ad0 Mon Sep 17 00:00:00 2001 From: cyroxx Date: Fri, 12 Dec 2025 06:45:17 +0100 Subject: [PATCH] improved controls, more videos --- __init__.py | 85 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/__init__.py b/__init__.py index ae59e92..87aeb09 100644 --- a/__init__.py +++ b/__init__.py @@ -1,43 +1,104 @@ -import st3m.run, media +import st3m.run +import media +import uos + from st3m.application import Application + +VIDEO_EXTS = (".mpg", ".mpeg", ".mp3", ".mod") + + +def list_media_sorted(path): + files = [] + for name in uos.listdir(path): + if any(name.lower().endswith(ext) for ext in VIDEO_EXTS): + files.append(name) + files.sort() + return files + + class App(Application): def __init__(self, app_ctx): super().__init__(app_ctx) - self.path = "/sd/apps/paloma_blanca/output.mpg" - # Exit per long-press RIGHT + self.video_dir = "videos" + + self.items = [] + self.index = 0 + + # Petal mapping + self.PETAL_NEXT = 3 + self.PETAL_PREV = 7 + self.PETAL_PAUSE = 5 + + # Exit via long-press RIGHT (App button) self._right_hold_ms = 0 + # ---------- Media helpers ---------- + + def load_current(self, autoplay=True): + media.stop() + path = self.video_dir + "/" + self.items[self.index] + print("Loading:", path) + media.load(path) + if autoplay: + media.play() + + def next_video(self): + self.index = (self.index + 1) % len(self.items) + self.load_current(True) + + def prev_video(self): + self.index = (self.index - 1) % len(self.items) + self.load_current(True) + + # ---------- Lifecycle ---------- + def on_enter(self, vm): super().on_enter(vm) - media.stop() - media.load(self.path) - media.play() + + self.items = list_media_sorted(self.video_dir) + if not self.items: + raise RuntimeError("No media files found in " + self.video_dir) + + self.index = 0 + self.load_current(True) def on_exit(self): media.stop() + # ---------- Main loop ---------- + def think(self, ins, delta_ms): super().think(ins, delta_ms) - # Media-Framework laufen lassen + # Media framework tick (REQUIRED) media.think(delta_ms) - # DOWN: Play/Pause toggle - if self.input.captouch.petals[5].whole.pressed: + # ---- Petal controls ---- + + # Next video + if ins.captouch.petals[self.PETAL_NEXT].pressed: + self.next_video() + + # Previous video + if ins.captouch.petals[self.PETAL_PREV].pressed: + self.prev_video() + + # Play / Pause + if ins.captouch.petals[self.PETAL_PAUSE].pressed: if media.is_playing(): media.pause() else: media.play() - # Loop am Ende + # ---- Loop at end ---- dur = media.get_duration() if dur > 0 and media.get_position() >= dur: media.seek(0.0) media.play() - # EXIT: RIGHT long-press (800ms) + # ---- Exit: long-press RIGHT app button ---- if self.input.buttons.app.right.down: self._right_hold_ms += delta_ms if self._right_hold_ms > 800: @@ -48,6 +109,6 @@ class App(Application): def draw(self, ctx): media.draw(ctx) + if __name__ == "__main__": st3m.run.run_app(App) -