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