From 75c1d6985060c5fc6443a98541a4afbcda00b436 Mon Sep 17 00:00:00 2001 From: jpunkt Date: Sun, 16 Jan 2022 15:30:30 +0100 Subject: [PATCH] Storyboard mechanism tested, works. Improved light command structure. --- pizzactrl/hal_serial.py | 23 ++++++++++++----------- pizzactrl/statemachine.py | 24 +++++++++++++++++------- pizzactrl/statemachine_test.py | 9 +++++++++ pizzactrl/storyboard.py | 6 ++++-- 4 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 pizzactrl/statemachine_test.py diff --git a/pizzactrl/hal_serial.py b/pizzactrl/hal_serial.py index 410f64e..0affc18 100644 --- a/pizzactrl/hal_serial.py +++ b/pizzactrl/hal_serial.py @@ -4,7 +4,7 @@ import threading from time import sleep from enum import Enum -from typing import Any, List +from typing import Any, List, Iterable from scipy.io.wavfile import write as writewav import sounddevice as sd @@ -181,8 +181,7 @@ def turn_off(hal: PizzaHAL, **kwargs): """ Turn off the lights. """ - hal.send_cmd(SerialCommands.BACKLIGHT, 0) - hal.send_cmd(SerialCommands.FRONTLIGHT, 0) + light(hal, [SerialCommands.BACKLIGHT, SerialCommands.FRONTLIGHT], 0, 0, 0, 0, 0) def wait_for_input(hal: PizzaHAL, @@ -243,12 +242,12 @@ def wait_for_input(hal: PizzaHAL, def light(hal: PizzaHAL, + light: Iterable, r: float, g: float, b: float, w: float, - fade: float = 0.0, - backlight: bool = False, + fade: float = 0.0, **kwargs): """ Turn on the light to illuminate the upper scroll @@ -261,12 +260,14 @@ def light(hal: PizzaHAL, :param steps: int How many steps for the fade (default: 100) """ - hal.send_cmd(SerialCommands.BACKLIGHT if backlight else SerialCommands.FRONTLIGHT, - int(b * 255).to_bytes(1, 'little'), - int(g * 255).to_bytes(1, 'little'), - int(r * 255).to_bytes(1, 'little'), - int(w * 255).to_bytes(1, 'little'), - int(fade * 1000).to_bytes(4, 'little')) + # TODO send light as bitmask + for l in iter(light): + hal.send_cmd(l, + int(b * 255).to_bytes(1, 'little'), + int(g * 255).to_bytes(1, 'little'), + int(r * 255).to_bytes(1, 'little'), + int(w * 255).to_bytes(1, 'little'), + int(fade * 1000).to_bytes(4, 'little')) def play_sound(hal: PizzaHAL, sound: Any, **kwargs): diff --git a/pizzactrl/statemachine.py b/pizzactrl/statemachine.py index a7ae7c6..76dec7a 100644 --- a/pizzactrl/statemachine.py +++ b/pizzactrl/statemachine.py @@ -74,8 +74,9 @@ class Statemachine: self.state = State.POWER_ON self.hal = PizzaHAL() - self.chapter = 0 - self.next_chapter = 0 + self.chapter = 0 # The storyboard index of the current chapter to play + self.next_chapter = 0 # The storyboard index of the next chapter to play + self.chapter_set = False # `True` if the next chapter has been set self.story = None self.story_de = story_de @@ -214,6 +215,7 @@ class Statemachine: Continue in the Storyboard. Prepare advancing to the next chapter. """ self.move = self.MOVE + self.chapter_set = True if len(self.story) > (self.chapter + 1): self.next_chapter = self.chapter + 1 else: @@ -223,6 +225,7 @@ class Statemachine: """ Repeat the current chapter. Do not rewind if the selection says so. """ + self.chapter_set = True self.move = rewind self.next_chapter = self.chapter @@ -230,10 +233,12 @@ class Statemachine: """ Jump to a specified chapter. """ + self.chapter_set = True self.move = self.MOVE self.next_chapter = next_chapter def _quit(**kwargs): + self.chapter_set = True self.move = self.MOVE self.loop = not shutdown self.next_chapter = None @@ -273,7 +278,10 @@ class Statemachine: logger.exception('Caught KeyError, ignoring...') pass - self.next_chapter = self.chapter + 1 + if not self.chapter_set: + self.chapter_set = True + self.next_chapter = self.chapter + 1 + else: self.next_chapter = None @@ -282,7 +290,7 @@ class Statemachine: Update chapters and move the scrolls. Update self.chapter to self.next_chapter """ - if self.next_chapter is not None: + if self.chapter_set and (self.next_chapter is not None): diff = self.next_chapter - self.chapter h_steps = 0 v_steps = 0 @@ -315,16 +323,18 @@ class Statemachine: move(self.hal, h_steps, True) move(self.hal, v_steps, False) + logger.debug(f'Setting chapter (cur: {self.chapter}) to {self.next_chapter}.') self.chapter = self.next_chapter + self.chapter_set = False def _rewind(self): """ Rewind all scrolls, post-process videos """ # TODO postprocessing - add sound - logger.debug('Converting video...') - cmdstring = f'MP4Box -add {fs_names.REC_DRAW_CITY} {fs_names.REC_MERGED_VIDEO}' - call([cmdstring], shell=True) + # logger.debug('Converting video...') + # cmdstring = f'MP4Box -add {fs_names.REC_DRAW_CITY} {fs_names.REC_MERGED_VIDEO}' + # call([cmdstring], shell=True) logger.debug('Rewinding...') if self.move: diff --git a/pizzactrl/statemachine_test.py b/pizzactrl/statemachine_test.py new file mode 100644 index 0000000..3304a57 --- /dev/null +++ b/pizzactrl/statemachine_test.py @@ -0,0 +1,9 @@ +import sys +import logging + +logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) + +from pizzactrl.statemachine import Statemachine + +sm = Statemachine(move=True, loop=False, test=True) + diff --git a/pizzactrl/storyboard.py b/pizzactrl/storyboard.py index a028aab..f9dc16f 100644 --- a/pizzactrl/storyboard.py +++ b/pizzactrl/storyboard.py @@ -1,5 +1,7 @@ from enum import Enum, auto +from pizzactrl.hal_serial import SerialCommands + class Option(Enum): """ @@ -52,13 +54,13 @@ class Activity(Enum): 'b': 0, 'w': 1.0, 'fade': 1.0, - 'backlight': False} + 'light': [ SerialCommands.FRONTLIGHT ]} LIGHT_BACK = {'r': 0, 'g': 0, 'b': 0, 'w': 1.0, 'fade': 1.0, - 'backlight': True} + 'light': [ SerialCommands.BACKLIGHT ]} class Do: