2021-10-19 10:10:04 +00:00
|
|
|
import logging
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
from enum import Enum, auto
|
|
|
|
|
2022-01-18 20:35:05 +00:00
|
|
|
from pizzactrl import fs_names
|
|
|
|
from .storyboard import Language, Storyboard
|
2022-01-17 19:39:36 +00:00
|
|
|
from .hal_serial import SerialCommunicationError, PizzaHAL, wait_for_input, play_sound, turn_off
|
2021-10-19 10:10:04 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class State(Enum):
|
|
|
|
POWER_ON = auto()
|
|
|
|
POST = auto()
|
|
|
|
IDLE_START = auto()
|
2022-01-17 19:39:36 +00:00
|
|
|
LANGUAGE_SELECT = auto()
|
2021-10-19 10:10:04 +00:00
|
|
|
PLAY = auto()
|
|
|
|
PAUSE = auto()
|
2022-01-17 19:39:36 +00:00
|
|
|
POST_PROCESS = auto()
|
2021-11-01 16:00:40 +00:00
|
|
|
REWIND = auto()
|
2021-10-19 10:10:04 +00:00
|
|
|
IDLE_END = auto()
|
|
|
|
SHUTDOWN = auto()
|
|
|
|
ERROR = -1
|
|
|
|
|
|
|
|
|
|
|
|
class Statemachine:
|
|
|
|
def __init__(self,
|
2022-01-17 19:39:36 +00:00
|
|
|
hal: PizzaHAL,
|
|
|
|
story: Storyboard,
|
|
|
|
default_lang=Language.NOT_SET,
|
2021-11-01 16:00:40 +00:00
|
|
|
move: bool = False,
|
2022-01-14 21:47:19 +00:00
|
|
|
loop: bool = True,
|
|
|
|
test: bool = False):
|
2022-01-17 19:39:36 +00:00
|
|
|
self.hal = hal
|
2022-01-14 21:47:19 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
self.LANG = default_lang
|
|
|
|
self.lang = None
|
|
|
|
|
|
|
|
self.story = story
|
|
|
|
self.story.MOVE = move
|
2022-01-14 21:47:19 +00:00
|
|
|
|
|
|
|
self.test = test
|
2021-11-01 16:00:40 +00:00
|
|
|
self.loop = loop
|
2022-01-17 19:39:36 +00:00
|
|
|
|
|
|
|
self.state = State.POWER_ON
|
2021-10-19 10:10:04 +00:00
|
|
|
|
|
|
|
def run(self):
|
2022-01-17 19:39:36 +00:00
|
|
|
logger.debug(f'Starting Statemachine...')
|
2022-01-14 21:47:19 +00:00
|
|
|
|
2021-10-19 10:10:04 +00:00
|
|
|
choice = {
|
|
|
|
State.POWER_ON: self._power_on,
|
|
|
|
State.POST: self._post,
|
|
|
|
State.IDLE_START: self._idle_start,
|
2022-01-17 19:39:36 +00:00
|
|
|
State.LANGUAGE_SELECT: self._lang_select,
|
2021-10-19 10:10:04 +00:00
|
|
|
State.PLAY: self._play,
|
2022-01-17 19:39:36 +00:00
|
|
|
State.POST_PROCESS: self._post_process,
|
2021-11-19 15:09:49 +00:00
|
|
|
State.REWIND: self._rewind,
|
2021-10-19 10:10:04 +00:00
|
|
|
State.IDLE_END: self._idle_end
|
|
|
|
}
|
2022-01-14 21:47:19 +00:00
|
|
|
|
2021-10-19 10:10:04 +00:00
|
|
|
while (self.state is not State.ERROR) and \
|
|
|
|
(self.state is not State.SHUTDOWN):
|
2022-01-17 19:39:36 +00:00
|
|
|
logger.debug(f'Run(state={self.state})')
|
2021-10-19 10:10:04 +00:00
|
|
|
choice[self.state]()
|
|
|
|
|
|
|
|
if self.state is State.ERROR:
|
|
|
|
logger.debug('An error occurred. Trying to notify user...')
|
|
|
|
if self.lang is Language.DE:
|
|
|
|
play_sound(self.hal, fs_names.SFX_ERROR_DE)
|
|
|
|
elif self.lang is Language.EN:
|
|
|
|
play_sound(self.hal, fs_names.SFX_ERROR_EN)
|
|
|
|
else:
|
|
|
|
play_sound(self.hal, fs_names.SFX_ERROR)
|
|
|
|
|
|
|
|
self._shutdown()
|
|
|
|
|
2022-01-18 20:35:05 +00:00
|
|
|
def _lid_open(self):
|
|
|
|
# TODO implement
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _lid_closed(self):
|
|
|
|
# TODO implement
|
|
|
|
pass
|
|
|
|
|
2021-10-19 10:10:04 +00:00
|
|
|
def _power_on(self):
|
|
|
|
"""
|
|
|
|
Initialize hal callbacks, load sounds
|
|
|
|
"""
|
2022-01-14 21:47:19 +00:00
|
|
|
# TODO enable lid sensor
|
2022-01-18 20:35:05 +00:00
|
|
|
self.hal.lid_switch.when_pressed = self._lid_open
|
|
|
|
self.hal.lid_switch.when_released = self._lid_closed
|
2022-01-14 21:47:19 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
self.hal.init_sounds()
|
2022-01-14 21:47:19 +00:00
|
|
|
self.hal.init_camera()
|
|
|
|
|
2021-10-19 10:10:04 +00:00
|
|
|
self.state = State.POST
|
|
|
|
|
|
|
|
def _post(self):
|
|
|
|
"""
|
|
|
|
Power on self test.
|
|
|
|
"""
|
2022-01-14 21:47:19 +00:00
|
|
|
if (not self.test) and (not os.path.exists(fs_names.USB_STICK)):
|
2021-10-19 10:10:04 +00:00
|
|
|
logger.warning('USB-Stick not found.')
|
|
|
|
self.state = State.ERROR
|
|
|
|
return
|
|
|
|
|
2022-01-14 21:47:19 +00:00
|
|
|
# TODO set RPi_HELO pins, wait for response
|
|
|
|
|
2021-11-01 16:00:40 +00:00
|
|
|
# Callback for start when blue button is held
|
2022-01-14 21:47:19 +00:00
|
|
|
# self.hal.btn_start.when_activated = self._start_or_rewind
|
|
|
|
# logger.debug('start button callback activated')
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.hal.init_connection()
|
|
|
|
except SerialCommunicationError as e:
|
|
|
|
self.state = State.ERROR
|
|
|
|
logger.exception(e)
|
|
|
|
return
|
2021-11-01 16:00:40 +00:00
|
|
|
|
2021-10-19 10:10:04 +00:00
|
|
|
# play a sound if everything is alright
|
|
|
|
play_sound(self.hal, fs_names.SFX_POST_OK)
|
|
|
|
|
2022-01-14 21:47:19 +00:00
|
|
|
if self.test:
|
2022-01-17 19:39:36 +00:00
|
|
|
self.state = State.LANGUAGE_SELECT
|
2022-01-14 21:47:19 +00:00
|
|
|
else:
|
|
|
|
self.state = State.IDLE_START
|
|
|
|
|
2021-10-19 10:10:04 +00:00
|
|
|
|
|
|
|
def _idle_start(self):
|
|
|
|
"""
|
2021-11-01 16:00:40 +00:00
|
|
|
Device is armed. Wait for user to press start button
|
2021-10-19 10:10:04 +00:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
def _lang_select(self):
|
2021-10-19 10:10:04 +00:00
|
|
|
"""
|
2022-01-17 19:39:36 +00:00
|
|
|
Select language
|
2021-10-19 10:10:04 +00:00
|
|
|
"""
|
2022-01-17 19:39:36 +00:00
|
|
|
def _select_de():
|
|
|
|
self.lang = Language.DE
|
|
|
|
|
|
|
|
def _select_en():
|
|
|
|
self.lang = Language.EN
|
2021-10-19 10:10:04 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
def _select_default():
|
|
|
|
self.lang = self.LANG
|
2022-01-14 21:47:19 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
#sound = self.hal.soundcache.get(fs_names.SND_SELECT_LANG)
|
|
|
|
#logger.debug(f'got sound {sound} from soundcache.')
|
2022-01-16 14:30:30 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
wait_for_input(self.hal,
|
|
|
|
blue_cb=_select_de,
|
|
|
|
red_cb=_select_en,
|
|
|
|
sound=fs_names.SND_SELECT_LANG,
|
|
|
|
timeout_cb=_select_default)
|
|
|
|
|
|
|
|
self.story.language = self.lang
|
|
|
|
|
|
|
|
logger.debug(f'User selected language={self.lang}')
|
|
|
|
self.state = State.PLAY
|
|
|
|
|
|
|
|
def _play(self):
|
2022-01-14 21:47:19 +00:00
|
|
|
"""
|
2022-01-17 19:39:36 +00:00
|
|
|
Select language, then run the storyboard
|
2022-01-14 21:47:19 +00:00
|
|
|
"""
|
2022-01-17 19:39:36 +00:00
|
|
|
self.story.hal = self.hal
|
2022-01-14 21:47:19 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
while self.story.hasnext():
|
|
|
|
self.story.play_chapter()
|
|
|
|
self.story.advance_chapter()
|
|
|
|
|
|
|
|
self.state = State.POST_PROCESS
|
2021-10-19 10:10:04 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
def _post_process(self):
|
2021-10-19 10:10:04 +00:00
|
|
|
"""
|
2022-01-17 19:39:36 +00:00
|
|
|
Post-processing
|
2021-10-19 10:10:04 +00:00
|
|
|
"""
|
2022-01-14 21:47:19 +00:00
|
|
|
# TODO postprocessing - add sound
|
2022-01-16 14:30:30 +00:00
|
|
|
# logger.debug('Converting video...')
|
|
|
|
# cmdstring = f'MP4Box -add {fs_names.REC_DRAW_CITY} {fs_names.REC_MERGED_VIDEO}'
|
|
|
|
# call([cmdstring], shell=True)
|
2022-01-14 21:47:19 +00:00
|
|
|
|
2022-01-17 19:39:36 +00:00
|
|
|
self.state = State.REWIND
|
|
|
|
|
|
|
|
def _rewind(self):
|
|
|
|
"""
|
|
|
|
Rewind all scrolls, post-process videos
|
|
|
|
"""
|
|
|
|
turn_off(self.hal)
|
|
|
|
self.story.rewind()
|
2021-10-19 10:10:04 +00:00
|
|
|
|
2021-11-01 16:00:40 +00:00
|
|
|
if self.loop:
|
|
|
|
self.state = State.IDLE_START
|
|
|
|
else:
|
|
|
|
self.state = State.IDLE_END
|
|
|
|
|
|
|
|
def _idle_end(self):
|
|
|
|
"""
|
|
|
|
Initialize shutdown
|
|
|
|
"""
|
2021-10-19 10:10:04 +00:00
|
|
|
self.state = State.SHUTDOWN
|
|
|
|
|
|
|
|
def _shutdown(self):
|
|
|
|
"""
|
|
|
|
Clean up, end execution
|
|
|
|
"""
|
|
|
|
del self.hal
|