Commit 66c779b4 authored by Antoine Lima's avatar Antoine Lima

RFID Integration

parent c2b449fa
......@@ -28,6 +28,7 @@ def getMainWin():
ON_RASP = os.uname()[1] == 'raspberrypi'
IMG_PATH = getContent('img')
SIDE = None
if __name__=='__main__':
__package__ = 'Babyfut'
......@@ -40,6 +41,7 @@ if __name__=='__main__':
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)
......@@ -54,7 +56,8 @@ if __name__=='__main__':
threadReplay.start()
myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
threadGPIO = GPIOThread(myapp)
threadGPIO = GPIOThread()
threadGPIO.rfidReceived.connect(myapp.rfidHandler)
threadGPIO.start()
threadDownloader = Downloader.instance()
......
......@@ -16,7 +16,7 @@ from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
class Downloader(Thread, QObject):
'''
Helper for downloading images with the help of queues
Helper for downloading images using queues
'''
N_ATTEMPS = 5
......
......@@ -5,8 +5,12 @@
"""
import logging
import pyautogui # PyPi library
import time
from threading import Thread
import pyautogui # PyPi library
from pirc522 import RFID # PyPi library
from PyQt5.QtCore import QObject, pyqtSignal
from Babyfut.babyfut import ON_RASP
from Babyfut.core.player import Side
......@@ -14,7 +18,7 @@ from Babyfut.core.player import Side
if ON_RASP:
import RPi.GPIO as GPIO
class GPIOThread(Thread):
class GPIOThread(Thread, QObject):
_keyButtonBindings = {
26: 'up',
22: 'left',
......@@ -23,11 +27,15 @@ class GPIOThread(Thread):
17: 'return',
18: 'escape'
}
rfidReceived = pyqtSignal(str)
def __init__(self, dispatcher):
def __init__(self):
Thread.__init__(self)
self.dispatcher = dispatcher
QObject.__init__(self)
self.rf_reader = RFID(pin_rst=25, pin_ce=8, pin_irq=24, pin_mode=GPIO.BCM)
self.continueRunning = True
self.lastRFIDReception = 0
if ON_RASP:
GPIO.setwarnings(False)
......@@ -42,9 +50,25 @@ class GPIOThread(Thread):
if ON_RASP:
try:
while self.continueRunning:
pass
self.rf_reader.wait_for_tag()
(error, tag_type) = self.rf_reader.request()
if not error:
self.handleRFID()
finally:
GPIOThread.clean()
self.clean()
def handleRFID(self):
(error, id) = self.rf_reader.anticoll()
if not error:
# Prevent RFID "spam" (one second removal delay)
now = time.time()
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)
logging.debug('Received RFID: {}'.format(receivedRFID))
else:
self.lastRFIDReception = now
def handleButtonPress(self, button_pin):
if button_pin not in GPIOThread._keyButtonBindings.keys():
......@@ -56,6 +80,12 @@ class GPIOThread(Thread):
def stop(self):
self.continueRunning = False
# Falsely trigger the rfid reader to stop it waiting
self.rf_reader.irq.set()
def clean(self):
GPIOThread.clean()
self.rf_reader.cleanup()
@staticmethod
def clean():
......
......@@ -59,12 +59,12 @@ case "$1" in
bash ./devtools.sh "clean"
bash ./devtools.sh "all"
;;
"exec")
"run"|"exec")
cd ..
python -m Babyfut.babyfut
;;
*)
echo "Unknown command \"$1\". See content of script for available commands."
echo "Unknown command \"$1\". See script for available commands."
;;
esac
......
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