Commit 7e4f6d67 authored by Antoine Lima's avatar Antoine Lima

Merged upstream

parents 72b509e5 4b5260cb
......@@ -28,20 +28,18 @@ def getMainWin():
ON_RASP = os.uname()[1] == 'raspberrypi'
IMG_PATH = getContent('img')
SIDE = None
if __name__=='__main__':
__package__ = 'Babyfut'
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.input import Input
from Babyfut.core.downloader import Downloader
from Babyfut.core.database import Database
from Babyfut.core.replay import Replay as ReplayThread
try:
SIDE = Side.Left
#logging.basicConfig(filename='babyfoot.log', level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
......@@ -56,9 +54,10 @@ if __name__=='__main__':
threadReplay.start()
myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
threadGPIO = GPIOThread()
threadGPIO.rfidReceived.connect(myapp.rfidHandler)
threadGPIO.start()
input = Input()
input.rfidReceived.connect(lambda side: myapp.dispatchMessage({'rfid': rfid, 'source': side}))
input.goalDetected.connect(lambda side: myapp.dispatchMessage({'goal': True, 'source': side}))
input.start()
threadDownloader = Downloader.instance()
threadDownloader.start()
......@@ -66,18 +65,16 @@ if __name__=='__main__':
myapp.show()
app.exec_()
threadDownloader.stop()
threadGPIO.stop()
if ReplayThread.isCamAvailable():
threadReplay.stop()
threadReplay.join()
threadGPIO.join()
input.stop()
threadDownloader.stop()
threadDownloader.join()
finally:
GPIOThread.clean()
Database.instance().close()
for f in glob.glob(join(IMG_PATH, '*')):
os.remove(f)
......@@ -14,8 +14,8 @@ class Ginger(object):
def __init__(self):
if Ginger._instance!=None:
self.url = Settings['ginger.url']
self.api_key = Settings['ginger.key']
self.url = Ginger.URL
@property
@staticmethod
......@@ -25,7 +25,7 @@ class Ginger(object):
return Ginger._instance
@staticmethod
def call(endpoint, params={}):
def get(endpoint, params={}):
# Add the API key to the parameter list
params['key'] = Ginger.instance.api_key
......
......@@ -7,18 +7,34 @@
import logging
import time
from threading import Thread
import pyautogui # PyPi library
from PyQt5.QtCore import QObject, pyqtSignal
from Babyfut.babyfut import ON_RASP
from Babyfut.core.player import Side
from Babyfut.core.settings import Settings
if ON_RASP:
import RPi.GPIO as GPIO
from pirc522 import RFID # PyPi library
import pyautogui # PyPi library
class Input(QObject):
'''
Defines pins. Uses BCM pin identifiers
'''
_RFIDPins = {
'pin_rst': 25,
'pin_ce' : 8,
'pin_irq': 24
}
_GoalPins = {
'pin_trig': 4,
'pin_echo': 16
}
class GPIOThread(Thread, QObject):
_keyButtonBindings = {
26: 'up',
22: 'left',
......@@ -28,36 +44,77 @@ class GPIOThread(Thread, QObject):
18: 'escape'
}
rfidReceived = pyqtSignal(str)
rfidReceived = pyqtSignal(Side, str)
goalDetected = pyqtSignal(Side)
def __init__(self):
Thread.__init__(self)
QObject.__init__(self)
self.continueRunning = True
self.lastRFIDReception = 0
if ON_RASP:
self.rf_reader = RFID(pin_rst=25, pin_ce=8, pin_irq=24, pin_mode=GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
for pin in GPIOThread._keyButtonBindings.keys():
print(pin)
for pin in Input._keyButtonBindings.keys():
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.RISING, callback=self.handleButtonPress)
GPIO.add_event_detect(pin, GPIO.RISING, callback=self._handleButtonPress)
def run(self):
self.side = Side.Left if Settings['app.side']=='left' else Side.Right
print(self.side)
self.rfidThread = RFIDThread(self, **Input._RFIDPins)
self.goalThread = GoalThread(self, **Input._GoalPins)
def start(self):
if ON_RASP:
self.rfidThread.start()
self.goalThread.start()
def stop(self):
if ON_RASP:
try:
while self.continueRunning:
self.rf_reader.wait_for_tag()
(error, tag_type) = self.rf_reader.request()
if not error:
self.handleRFID()
finally:
self.clean()
def handleRFID(self):
self.rfidThread.stop(); self.rfidThread.join()
self.goalThread.stop(); self.goalThread.join()
def _handleButtonPress(self, button_pin):
if button_pin not in Input._keyButtonBindings.keys():
logging.warn('Unknown button pin: {}'.format(button_pin))
else:
key = Input._keyButtonBindings[button_pin]
logging.debug('Sending {} as {}'.format(button_pin, key))
pyautogui.press(key)
class GPIOThread(Thread):
def __init__(self):
Thread.__init__(self)
self._running = True
def running(self):
return self._running
def start(self):
Thread.start(self)
def stop(self):
self._running = False
def clean(self):
GPIO.cleanup()
class RFIDThread(GPIOThread):
def __init__(self, parent, **pins):
GPIOThread.__init__(self)
self.parent = parent
self.lastRFIDReception = 0
self.rf_reader = RFID(**pins, pin_mode=GPIO.BCM)
print(self.rf_reader.pin_rst, self.rf_reader.pin_ce, self.rf_reader.pin_irq)
def run(self):
try:
while self.running():
self.rf_reader.wait_for_tag()
(error, tag_type) = self.rf_reader.request()
if not error:
self._handleRFID()
finally:
self.clean()
def _handleRFID(self):
(error, id) = self.rf_reader.anticoll()
if not error:
# Prevent RFID "spam" (one second removal delay)
......@@ -65,30 +122,59 @@ class GPIOThread(Thread, QObject):
if self.lastRFIDReception!=0 and abs(self.lastRFIDReception-now)>1:
self.lastRFIDReception = 0
receivedRFID = ':'.join([str(x) for x in id])
self.rfidReceived.emit(receivedRFID)
self.input.emit(self.parent.side, receivedRFID)
logging.debug('Received RFID: {}'.format(receivedRFID))
else:
self.lastRFIDReception = now
def handleButtonPress(self, button_pin):
if button_pin not in GPIOThread._keyButtonBindings.keys():
logging.warn('Unknown button pin: {}'.format(button_pin))
else:
key = GPIOThread._keyButtonBindings[button_pin]
logging.debug('Sending {} as {}'.format(button_pin, key))
pyautogui.press(key)
def stop(self):
self.continueRunning = False
# Falsely trigger the rfid reader to stop it waiting
if ON_RASP:
self.rf_reader.irq.set()
GPIOThread.stop(self)
# Falsely trigger the rfid reader to stop it from waiting
self.rf_reader.irq.set()
def clean(self):
GPIOThread.clean()
GPIOThread.clean(self)
self.rf_reader.cleanup()
@staticmethod
def clean():
if ON_RASP:
GPIO.cleanup()
class GoalThread(GPIOThread):
def __init__(self, parent, pin_trig, pin_echo):
GPIOThread.__init__(self)
self.parent = parent
self.pin_trig = pin_trig
self.pin_echo = pin_echo
GPIO.setmode(GPIO.BCM)
GPIO.setup (self.pin_echo, GPIO.IN)
GPIO.setup (self.pin_trig, GPIO.OUT)
GPIO.output(self.pin_trig, GPIO.LOW)
print(self.pin_trig, pin_echo)
def run(self):
try:
# Waiting for sensor to settle
time.sleep(2)
while self.running():
# Trigger a scan
GPIO.output(self.pin_trig, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(self.pin_trig, GPIO.LOW)
# Read the echo
while self.running() and GPIO.input(self.pin_echo)==0:
pulse_start_time = time.time()
while self.running() and GPIO.input(self.pin_echo)==1:
pulse_end_time = time.time()
if self.running():
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
self._handle_dist(distance)
finally:
self.clean()
def _handle_dist(self, dist):
logging.debug('Distance: {}cm'.format(dist))
if dist<3:
self.parent.goalDetected.emit(self.parent.side)
......@@ -128,7 +128,7 @@ class Player(QObject):
'''
Retrieves a player's informations from the Ginger API
'''
response = Ginger.call('badge/{}'.format(rfid))
response = Ginger.instance.get('badge/{}'.format(rfid))
if isinstance(response, HTTPStatus):
logging.warn('Request to Ginger failed ({}): returning Guest'.format(response.value))
return PlayerGuest
......@@ -138,7 +138,7 @@ class Player(QObject):
return Player._loadFromDB(rfid)
def displayImg(self, container_widget, *args):
def displayImg(self, container_widget):
self.pic_container = container_widget
if self.pic_path.startswith('http'):
......
......@@ -63,6 +63,37 @@ case "$1" in
cd ..
python -m Babyfut.babyfut
;;
"install")
echo "Installing.."
echo "** Assuming debian-like environment. This shouldn't be run more than once"
echo "** Updating the system to make sure everything is up-to-date."
echo ""
sudo apt-get update && sudo apt-get upgrade
echo ""
echo "** Installing python3 and python tools"
# Sometimes the PYTHONPATH wont be set accordingly for some raspbian distributions
# In which case, manually import the right path (/usr/lib/python3/dist-packages) in
# the virtual environment's activation script
sudo apt-get install -y python3 python3-venv python3-pyqt5 python3-pip qtmultimedia5-examples \
pyqt5-dev pyqt5-dev-tools
echo ""
echo "** Setting up the python virtual environment"
python3 -m venv ../PyQt5
source ../PyQt5/bin/activate
echo ""
echo "** Installing libraries used by the software"
pip install pi-rc522 pyautogui Xlib RPi.GPIO request
echo ""
echo "****************************"
echo ""
echo "Installation done successfully! You may have to source the v-env."
echo "Don't forget to download the \"content\" folder from another source."
bash ./devtools.sh "allc"
;;
*)
echo "Unknown command \"$1\". See script for available commands."
;;
......
......@@ -66,12 +66,12 @@
<translation>Stat</translation>
</message>
<message>
<location filename="../ui/endgame_ui.py" line="104"/>
<location filename="../ui/endgame_ui.py" line="92"/>
<source>Player 1</source>
<translation>Joueur 1</translation>
</message>
<message>
<location filename="../ui/endgame_ui.py" line="105"/>
<location filename="../ui/endgame_ui.py" line="93"/>
<source>Player 2</source>
<translation>Joueur 2</translation>
</message>
......@@ -91,7 +91,7 @@
<translation>Joueur 4</translation>
</message>
<message>
<location filename="../ui/endgame_ui.py" line="103"/>
<location filename="../ui/endgame_ui.py" line="91"/>
<source>Congratulations!</source>
<translation>Félicitations !</translation>
</message>
......
......@@ -80,11 +80,6 @@ class MainWin(QtWidgets.QMainWindow):
for modIdx in modulesIdx:
self.modules[modIdx].other(**msg)
@pyqtSlot(str)
def rfidHandler(self, rfid):
side = Side.Left if Settings['app.side']=='left' else Side.Right
self.dispatchMessage({'rfid': rfid, 'source': side})
def _loadSettings(self):
if Settings['ui.fullscreen']:
......
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