Implemented new command structure.

TODO: implement parallel execution in storyboard.
This commit is contained in:
jpunkt 2022-01-16 22:10:13 +01:00
parent 75c1d69850
commit a64fd6460a
3 changed files with 63 additions and 30 deletions

View file

@ -32,21 +32,30 @@ SERIAL_BAUDRATE = 115200 # Serial connection baud rate
SERIAL_CONN_TIMEOUT = 60 # Serial connection read timeout 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): class SerialCommands(Enum):
HELLO = b'\x00' HELLO = b'\x00'
ALREADY_CONNECTED = b'\x01' ALREADY_CONNECTED = b'\x01'
ERROR = b'\x02' ERROR = b'\x02'
RECEIVED = b'\x03' RECEIVED = b'\x03'
MOTOR_H = b'H' SET_MOVEMENT = b'M'
MOTOR_V = b'V' SET_LIGHT = b'L'
BACKLIGHT = b'B' DO_IT = b'D'
FRONTLIGHT = b'F'
USER_INTERACT = b'U' USER_INTERACT = b'U'
RECORD = b'C' RECORD = b'C'
REWIND = b'R' REWIND = b'R'
DEBUG_SCROLL = b'S' DEBUG_SCROLL = b'S'
@ -157,15 +166,17 @@ class PizzaHAL:
return resp return resp
def move(hal: PizzaHAL, def set_movement(hal: PizzaHAL,
scroll: Scrolls,
steps: int, steps: int,
horizontal: bool,
**kwargs): **kwargs):
""" """
Move the motor controlling the vertical scroll a given distance. 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)) steps.to_bytes(2, 'little', signed=True))
@ -181,7 +192,10 @@ def turn_off(hal: PizzaHAL, **kwargs):
""" """
Turn off the lights. 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, def wait_for_input(hal: PizzaHAL,
@ -241,8 +255,8 @@ def wait_for_input(hal: PizzaHAL,
timeout_cb(**kwargs) timeout_cb(**kwargs)
def light(hal: PizzaHAL, def set_light(hal: PizzaHAL,
light: Iterable, light: Lights,
r: float, r: float,
g: float, g: float,
b: float, b: float,
@ -255,21 +269,23 @@ def light(hal: PizzaHAL,
:param hal: The hardware abstraction object :param hal: The hardware abstraction object
:param fade: float :param fade: float
Default 0, time in seconds to fade in or out 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 # convert color to 32bit number
for l in iter(light): color = (int(w * 255) << 24) | (int(b * 255) << 16) | (int(g * 255) << 8) | (int(r * 255))
hal.send_cmd(l,
int(b * 255).to_bytes(1, 'little'), hal.send_cmd(SerialCommands.SET_LIGHT,
int(g * 255).to_bytes(1, 'little'), int(light.value).to_bytes(1, 'little'),
int(r * 255).to_bytes(1, 'little'), int(color).to_bytes(4, 'little'),
int(w * 255).to_bytes(1, 'little'),
int(fade * 1000).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): def play_sound(hal: PizzaHAL, sound: Any, **kwargs):
""" """
Play a sound (blocking). Play a sound (blocking).

View file

@ -12,7 +12,7 @@ from pizzactrl import fs_names, sb_dummy
from pizzactrl.hal import ScrollSensor from pizzactrl.hal import ScrollSensor
from .storyboard import Activity, Select, Option 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, \ 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 from pizzactrl import storyboard
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -57,10 +57,10 @@ ACTIVITY_SELECTOR = {
Activity.RECORD_SOUND: record_sound, Activity.RECORD_SOUND: record_sound,
Activity.RECORD_VIDEO: record_video, Activity.RECORD_VIDEO: record_video,
Activity.TAKE_PHOTO: take_photo, Activity.TAKE_PHOTO: take_photo,
Activity.LIGHT_LAYER: light, Activity.LIGHT_LAYER: set_light,
Activity.LIGHT_BACK: light, Activity.LIGHT_BACK: set_light,
Activity.ADVANCE_UP: move, Activity.ADVANCE_UP: set_movement,
Activity.ADVANCE_LEFT: move Activity.ADVANCE_LEFT: set_movement
} }
@ -320,8 +320,8 @@ class Statemachine:
v_steps = steps['v_steps'] v_steps = steps['v_steps']
if self.move: if self.move:
move(self.hal, h_steps, True) set_movement(self.hal, h_steps, True)
move(self.hal, v_steps, False) set_movement(self.hal, v_steps, False)
logger.debug(f'Setting chapter (cur: {self.chapter}) to {self.next_chapter}.') logger.debug(f'Setting chapter (cur: {self.chapter}) to {self.next_chapter}.')
self.chapter = self.next_chapter self.chapter = self.next_chapter

View file

@ -1,4 +1,5 @@
from enum import Enum, auto from enum import Enum, auto
from typing import List
from pizzactrl.hal_serial import SerialCommands from pizzactrl.hal_serial import SerialCommands
@ -73,6 +74,22 @@ class Do:
for key, value in self.activity.value.items(): for key, value in self.activity.value.items():
self.values[key] = kwargs.get(key, value) 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: class Chapter:
""" """