Commit 1776ccaa authored by Antoine Lima's avatar Antoine Lima

PICamera replay in another thread

parent 90227f52
......@@ -66,8 +66,12 @@ class MainWin(QtWidgets.QMainWindow):
mod_idx = [i for i, x in enumerate(self.modules) if isinstance(x, type)]
return -1 if len(mod_idx)==0 else mod_idx[0]
def dispatchMessage(self, msg, toAll=False):
modulesIdx = self.modules if toAll else [self.findMod(type(self.ui.panels.currentWidget()))]
def dispatchMessage(self, msg, toType=None, toAll=False):
if toType!=None:
modulesIdx = [self.findMod(toType)]
else:
modulesIdx = self.modules if toAll else [self.findMod(type(self.ui.panels.currentWidget()))]
for modIdx in modulesIdx:
self.modules[modIdx].other(**msg)
......@@ -88,6 +92,7 @@ class MainWin(QtWidgets.QMainWindow):
if __name__=='__main__':
from settings import Settings
from replay import Replay as ReplayThread
#logging.basicConfig(filename='babyfoot.log', level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
......@@ -95,6 +100,10 @@ if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
myapp = MainWin()
threadReplay = ReplayThread(Side.Left)
threadReplay.start()
myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
if Settings['app.mode']!='dev':
threadArduinoLeft = InputThread(myapp, Side.Left)
#threadArduinoRight = InputThread(myapp, Side.Right)
......@@ -104,6 +113,9 @@ if __name__=='__main__':
myapp.show()
app.exec_()
threadReplay.stop()
threadReplay.join()
if Settings['app.mode']!='dev':
threadArduinoLeft.stop()
#threadArduinoRight.stop()
......
......@@ -57,8 +57,6 @@ class GameModule(Module):
self.ui.btnScore1.clicked.connect(lambda: self.goal(Side.Left))
self.ui.btnScore2.clicked.connect(lambda: self.goal(Side.Right))
self.replayer = Replay(Side.Left)
def load(self):
logging.debug('Loading GameModule')
......@@ -93,6 +91,10 @@ class GameModule(Module):
elif key=='players':
self.players = val
elif key=='replayThread':
self.replayer = val
def resizeEvent(self, event):
# 40% of the window width to have (5% margin)-(40% circle)-(10% middle)-(40% circle)-(5% margin)
btnDiameter = self.mainwin.width()*0.4
......@@ -156,6 +158,7 @@ class GameModule(Module):
self.ui.videoWidget.setFullScreen(False);
self.showingReplay = False
self.updateScores()
self.replayer.start_recording()
def handleCancel(self):
self.switchModule(modules.MenuModule)
......
......@@ -7,8 +7,7 @@ Created on Wed Apr 18 18:34:40 2018
"""
import os
from threading import Event
from multiprocessing import Process, Lock
from threading import Thread, Event
from main import MainWin
from settings import Settings
......@@ -18,55 +17,63 @@ onRasp = os.uname()[1] == 'raspberrypi'
if onRasp:
import picamera
class LockableValue():
def __init__(self, value):
self.value = value
self.mutex = Lock()
class Replay():
class Replay(Thread):
def __init__(self, side):
self.recording = LockableValue(False)
Thread.__init__(self)
self.replayPath = MainWin.getContent('Replay {}.mp4'.format(side.name))
self.stopped = Event()
if onRasp:
self.cam = picamera.PiCamera()
self.cam.resolution = Settings['picam.resolution']
self.cam.framerate = Settings['picam.fps']
self.cam.hflip = Settings['picam.hflip']
self.cam.vflip = Settings['picam.vflip']
self.format = Settings['picam.format']
self.stream = picamera.PiCameraCircularIO(self.cam, seconds=Settings['replay.duration'])
self.shutdown = False
self.cam = picamera.PiCamera()
self.cam.resolution = Settings['picam.resolution']
self.cam.framerate = Settings['picam.fps']
self.cam.hflip = Settings['picam.hflip']
self.cam.vflip = Settings['picam.vflip']
self.stream = picamera.PiCameraCircularIO(self.cam, seconds=Settings['replay.duration'])
self.start_flag = Event()
self.stop_flag = Event()
self.stopped_flag = Event()
def start_recording(self):
if not onRasp:
self.stopped.set()
else:
self.recording.value = True
self.stopped.clear()
self.capture_process = Process(target=self.__capture)
self.capture_process.start()
if onRasp:
self.start_flag.set()
def stop_recording(self):
self.recording.val = True
self.stopped.wait(timeout=2.0)
return self.replayPath
def capture(self, fileToSave):
if onRasp:
self.cam.start_recording(self.stream, self.format)
self.stop_flag.set()
self.stopped_flag.wait()
self.stop_flag.clear()
self.start_flag.clear()
self.stopped_flag.clear()
try:
recording = self.recording.val
return self.replayPath
while recording:
self.cam.wait_recording(1)
recording = self.recording.val
finally:
self.cam.stop_recording()
def stop(self):
self.start_flag.set()
self.shutdown = True
def run(self):
while not self.shutdown:
print('1')
self.start_flag.wait()
print('2')
if not self.shutdown:
print('3')
self.cam.start_recording(self.stream, Settings['picam.format'])
try:
while not self.stop_flag.is_set():
self.cam.wait_recording(1)
print('4')
finally :
self.cam.stop_recording()
self.stream.copy_to(self.replayPath)
self.stream.clear()
self.stopped_flag.set()
print('5')
self.stopped.set()
self.stream.copy_to(self.replayPath)
self.cam.close()
self.stream.close()
self.cam.close()
self.stream.close()
print('6')
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment