From a64fd6460a6dbe7b316bc5e0be33bd83666dd706 Mon Sep 17 00:00:00 2001 From: jpunkt Date: Sun, 16 Jan 2022 22:10:13 +0100 Subject: [PATCH] Implemented new command structure. TODO: implement parallel execution in storyboard. --- pizzactrl/hal_serial.py | 62 ++++++++++++++++++++++++--------------- pizzactrl/statemachine.py | 14 ++++----- pizzactrl/storyboard.py | 17 +++++++++++ 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/pizzactrl/hal_serial.py b/pizzactrl/hal_serial.py index 0affc18..0234e4b 100644 --- a/pizzactrl/hal_serial.py +++ b/pizzactrl/hal_serial.py @@ -32,21 +32,30 @@ SERIAL_BAUDRATE = 115200 # Serial connection baud rate SERIAL_CONN_TIMEOUT = 60 # Serial connection read timeout +class Lights(Enum): + BACKLIGHT = 0 + FRONTLIGHT = 1 + + +class Scrolls(Enum): + HORIZONTAL = 0 + VERTICAL = 1 + + class SerialCommands(Enum): HELLO = b'\x00' ALREADY_CONNECTED = b'\x01' ERROR = b'\x02' RECEIVED = b'\x03' - MOTOR_H = b'H' - MOTOR_V = b'V' + SET_MOVEMENT = b'M' + SET_LIGHT = b'L' - BACKLIGHT = b'B' - FRONTLIGHT = b'F' + DO_IT = b'D' USER_INTERACT = b'U' - RECORD = b'C' + REWIND = b'R' DEBUG_SCROLL = b'S' @@ -157,15 +166,17 @@ class PizzaHAL: return resp -def move(hal: PizzaHAL, +def set_movement(hal: PizzaHAL, + scroll: Scrolls, steps: int, - horizontal: bool, **kwargs): """ Move the motor controlling the vertical scroll a given distance. """ - hal.send_cmd(SerialCommands.MOTOR_H if horizontal else SerialCommands.MOTOR_V, + scroll = int(scroll.value) + hal.send_cmd(SerialCommands.SET_MOVEMENT, + scroll.to_bytes(1, 'little', signed=False), steps.to_bytes(2, 'little', signed=True)) @@ -181,7 +192,10 @@ def turn_off(hal: PizzaHAL, **kwargs): """ Turn off the lights. """ - light(hal, [SerialCommands.BACKLIGHT, SerialCommands.FRONTLIGHT], 0, 0, 0, 0, 0) + + set_light(hal, Lights.BACKLIGHT, 0, 0, 0, 0, 0) + set_light(hal, Lights.FRONTLIGHT, 0, 0, 0, 0, 0) + do_it(hal) def wait_for_input(hal: PizzaHAL, @@ -241,8 +255,8 @@ def wait_for_input(hal: PizzaHAL, timeout_cb(**kwargs) -def light(hal: PizzaHAL, - light: Iterable, +def set_light(hal: PizzaHAL, + light: Lights, r: float, g: float, b: float, @@ -255,19 +269,21 @@ def light(hal: PizzaHAL, :param hal: The hardware abstraction object :param fade: float Default 0, time in seconds to fade in or out - :param intensity: float - Intensity of the light in percent - :param steps: int - How many steps for the fade (default: 100) """ - # 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')) + # convert color to 32bit number + color = (int(w * 255) << 24) | (int(b * 255) << 16) | (int(g * 255) << 8) | (int(r * 255)) + + hal.send_cmd(SerialCommands.SET_LIGHT, + int(light.value).to_bytes(1, 'little'), + int(color).to_bytes(4, 'little'), + int(fade * 1000).to_bytes(4, 'little')) + + +def do_it(hal: PizzaHAL): + """ + Execute set commands + """ + hal.send_cmd(SerialCommands.DO_IT) def play_sound(hal: PizzaHAL, sound: Any, **kwargs): diff --git a/pizzactrl/statemachine.py b/pizzactrl/statemachine.py index 76dec7a..d8dacc0 100644 --- a/pizzactrl/statemachine.py +++ b/pizzactrl/statemachine.py @@ -12,7 +12,7 @@ from pizzactrl import fs_names, sb_dummy from pizzactrl.hal import ScrollSensor from .storyboard import Activity, Select, Option from .hal_serial import SerialCommunicationError, PizzaHAL, play_sound, take_photo, record_video, record_sound, turn_off, wait_for_input, \ - light, move, rewind + set_light, set_movement, rewind from pizzactrl import storyboard logger = logging.getLogger(__name__) @@ -57,10 +57,10 @@ ACTIVITY_SELECTOR = { Activity.RECORD_SOUND: record_sound, Activity.RECORD_VIDEO: record_video, Activity.TAKE_PHOTO: take_photo, - Activity.LIGHT_LAYER: light, - Activity.LIGHT_BACK: light, - Activity.ADVANCE_UP: move, - Activity.ADVANCE_LEFT: move + Activity.LIGHT_LAYER: set_light, + Activity.LIGHT_BACK: set_light, + Activity.ADVANCE_UP: set_movement, + Activity.ADVANCE_LEFT: set_movement } @@ -320,8 +320,8 @@ class Statemachine: v_steps = steps['v_steps'] if self.move: - move(self.hal, h_steps, True) - move(self.hal, v_steps, False) + set_movement(self.hal, h_steps, True) + set_movement(self.hal, v_steps, False) logger.debug(f'Setting chapter (cur: {self.chapter}) to {self.next_chapter}.') self.chapter = self.next_chapter diff --git a/pizzactrl/storyboard.py b/pizzactrl/storyboard.py index f9dc16f..2d206e0 100644 --- a/pizzactrl/storyboard.py +++ b/pizzactrl/storyboard.py @@ -1,4 +1,5 @@ from enum import Enum, auto +from typing import List from pizzactrl.hal_serial import SerialCommands @@ -73,6 +74,22 @@ class Do: for key, value in self.activity.value.items(): self.values[key] = kwargs.get(key, value) + def execute(): + # TODO implement + pass + + +class Do_Parallel: + """ + A list of activities which should be done at the same time + """ + def __init__(self, *activities: List[Do]) -> None: + self.act_list = activities + + def execute(): + for act in self.act_list: + act.execute() + class Chapter: """