babyfut.py 1.74 KB
Newer Older
Antoine Lima's avatar
Antoine Lima committed
1 2 3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Antoine Lima's avatar
Antoine Lima committed
4
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
Antoine Lima's avatar
Antoine Lima committed
5 6
"""

7
import os
Antoine Lima's avatar
Antoine Lima committed
8 9
from os.path import dirname, abspath, join, exists
import glob
Antoine Lima's avatar
Antoine Lima committed
10 11
import sys
import logging
Antoine Lima's avatar
Antoine Lima committed
12

Antoine Lima's avatar
Antoine Lima committed
13
from PyQt5 import QtCore
Antoine Lima's avatar
Antoine Lima committed
14
from PyQt5.QtWidgets import QMainWindow, QApplication
Antoine Lima's avatar
Antoine Lima committed
15

Antoine Lima's avatar
Antoine Lima committed
16 17 18
def getContent(path):
	contentFolder = join(dirname(dirname(abspath(__file__))), 'content')
	return join(contentFolder, path)
19

Antoine Lima's avatar
Antoine Lima committed
20
def getMainWin():
21 22
	from Babyfut.ui.mainwin import MainWin

Antoine Lima's avatar
Antoine Lima committed
23 24 25 26 27 28
	# Global function to find the (open) QMainWindow in application
	for widget in QApplication.instance().topLevelWidgets():
		if isinstance(widget, QMainWindow):
			return widget
	return None

Antoine Lima's avatar
Antoine Lima committed
29 30 31
ON_RASP = os.uname()[1] == 'raspberrypi'
IMG_PATH = getContent('img')

Antoine Lima's avatar
Antoine Lima committed
32
if __name__=='__main__':
Antoine Lima's avatar
Antoine Lima committed
33
	__package__ = 'Babyfut'
34 35 36 37 38 39 40
	from Babyfut.ui.mainwin import MainWin
	from Babyfut.modules import GameModule
	from Babyfut.core.player import Side
	from Babyfut.core.input import GPIOThread
	from Babyfut.core.database import Database
	from Babyfut.core.replay import Replay as ReplayThread

41 42 43
	try:
		#logging.basicConfig(filename='babyfoot.log', level=logging.DEBUG)
		logging.basicConfig(level=logging.DEBUG)
44

Antoine Lima's avatar
Antoine Lima committed
45
		app = QApplication(sys.argv)
46
		myapp = MainWin()
47

Antoine Lima's avatar
Antoine Lima committed
48 49 50
		if not exists(IMG_PATH):
			 os.makedirs(IMG_PATH)

51 52 53 54
		if ReplayThread.isCamAvailable():
			threadReplay = ReplayThread(Side.Left)
			threadReplay.start()
			myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
55

56 57
		threadGPIO = GPIOThread(myapp)
		threadGPIO.start()
58

59 60
		myapp.show()
		app.exec_()
61

62
		threadGPIO.stop()
63

64 65 66
		if ReplayThread.isCamAvailable():
			threadReplay.stop()
			threadReplay.join()
67

68
		threadGPIO.join()
69

70 71
	finally:
		GPIOThread.clean()
Antoine Lima's avatar
Antoine Lima committed
72
		Database.instance().close()
Antoine Lima's avatar
Antoine Lima committed
73 74
		for f in glob.glob(join(IMG_PATH, '*')):
			os.remove(f)