...
 
Commits (2)
......@@ -55,8 +55,8 @@ if __name__=='__main__':
myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
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.rfidReceived.connect(lambda side, rfid: myapp.dispatchMessage({'rfid': rfid, 'source': side}))
input.goalDetected.connect(lambda side : myapp.dispatchMessage({'goal': True, 'source': side}))
input.start()
threadDownloader = Downloader.instance()
......
......@@ -31,17 +31,25 @@ class Input(QObject):
}
_GoalPins = {
'pin_trig': 4,
'pin_echo': 16
'pin_trig': 3,
'pin_echo': 2
}
_keyButtonBindings = {
26: 'up',
22: 'left',
27: 'right',
23: 'down',
17: 'return',
18: 'escape'
# 26: 'up',
# 22: 'left',
# 27: 'right',
# 23: 'down',
# 17: 'return',
# 18: 'escape'
16: 'up',
6: 'left',
12: 'right',
13: 'down',
26: 'return',
20: 'del',
19: 'escape'
}
rfidReceived = pyqtSignal(Side, str)
......@@ -49,6 +57,7 @@ class Input(QObject):
def __init__(self):
QObject.__init__(self)
self.last_input = time.time()
if ON_RASP:
GPIO.setmode(GPIO.BCM)
......@@ -72,14 +81,18 @@ class Input(QObject):
self.goalThread.stop(); self.goalThread.join()
def _handleButtonPress(self, button_pin):
arrival_time = time.time()
if button_pin not in Input._keyButtonBindings.keys():
logging.warn('Unknown button pin: {}'.format(button_pin))
else:
elif arrival_time-self.last_input>0.5:
self.last_input = arrival_time
key = Input._keyButtonBindings[button_pin]
logging.debug('Sending {} as {}'.format(button_pin, key))
pyautogui.press(key)
class GPIOThread(Thread):
CLEANED = False
def __init__(self):
Thread.__init__(self)
self._running = True
......@@ -143,12 +156,12 @@ class GoalThread(GPIOThread):
self.parent = parent
self.pin_trig = pin_trig
self.pin_echo = pin_echo
self.last_goal = time.time()
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:
......@@ -156,18 +169,29 @@ class GoalThread(GPIOThread):
time.sleep(2)
while self.running():
# Trigger a scan
# Trigger a scan with a 10us pulse
GPIO.output(self.pin_trig, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(self.pin_trig, GPIO.LOW)
timeout = False
start_read = time.time()
# Read the echo
while self.running() and GPIO.input(self.pin_echo)==0:
pulse_start_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
while self.running() and GPIO.input(self.pin_echo)==1:
pulse_end_time = time.time()
pulse_end_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
if self.running():
if self.running() and not timeout:
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
self._handle_dist(distance)
......@@ -175,6 +199,11 @@ class GoalThread(GPIOThread):
self.clean()
def _handle_dist(self, dist):
logging.debug('Distance: {}cm'.format(dist))
if dist<3:
self.parent.goalDetected.emit(self.parent.side)
#print('Distance: {}cm'.format(dist))
if dist<10:
if (time.time()-self.last_goal)>1:
print('goal')
self.parent.goalDetected.emit(self.parent.side)
self.last_goal = time.time()
......@@ -64,6 +64,7 @@ class Player(QObject):
__query_victories = 'SELECT COUNT(*) AS victories FROM Players INNER JOIN Teams ON (Players.id==Teams.player1 OR Players.id==Teams.player2) INNER JOIN Matchs ON (Teams.id==Matchs.winningTeam) WHERE Players.id==?'
_placeholder_pic_path = ':ui/img/placeholder_head.jpg'
_imgLocalPath = os.path.join(IMG_PATH, '{}.jpg')
_utcPictureURL = 'https://demeter.utc.fr/portal/pls/portal30/portal30.get_photo_utilisateur?username={}'
def __init__(self, id, rfid, login, fname, lname, stats=None):
......@@ -74,10 +75,13 @@ class Player(QObject):
self.fname = fname
self.lname = lname
self.pic_path = Player._placeholder_pic_path
if self.login:
if os.path.isfile(Player._imgLocalPath.format(self.id)):
self.pic_path = Player._imgLocalPath.format(self.id)
elif self.login:
self.pic_path = Player._utcPictureURL.format(self.login)
else:
self.pic_path = Player._placeholder_pic_path
if stats==None:
self.stats = { 'time_played': 0, 'goals_scored': 0, 'games_played': 0, 'victories': 0 }
else:
......
......@@ -83,7 +83,7 @@ class Replay(Thread):
def detectCam():
if ON_RASP:
camdet = subprocess.check_output(["vcgencmd","get_camera"])
return int(chr(camdet[-2]))
return bool(chr(camdet[-2]))
else:
return False
......
......@@ -133,7 +133,7 @@ class GameModule(Module):
self.players = val
elif key=='replayThread':
self.replayer = val
self.camera = val
def resizeEvent(self, event):
# 40% of the window width to have (5% margin)-(40% circle)-(10% middle)-(40% circle)-(5% margin)
......@@ -177,7 +177,7 @@ class GameModule(Module):
def goal(self, side):
if side not in Side:
logging.error('Wrong goal side: {}'.format(side))
else:
elif not self.video_player:
self.scores[side] += 1
# Show replay
......
import logging
import time
import RPi.GPIO as GPIO
keyButtonBindings = {
16: 'up',
6: 'left',
12: 'right',
13: 'down',
26: 'return',
20: 'del',
19: 'escape'
}
def fun(pin):
global last_input
arrival_time = time.time()
if pin not in keyButtonBindings.keys():
print('Unknown button pin: {}'.format(pin))
elif arrival_time-last_input>0.5:
key = keyButtonBindings[pin]
print('Sending {} as {}'.format(pin, key))
last_input = arrival_time
if __name__=='__main__':
GPIO.setmode(GPIO.BCM)
for pin in keyButtonBindings.keys():
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.RISING, callback=fun)
try:
last_input = time.time()
while True:
pass
finally:
GPIO.cleanup()
import time
import RPi.GPIO as GPIO
from threading import Thread
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 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
self.last_goal = time.time()
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)
def run(self):
try:
# Waiting for sensor to settle
time.sleep(2)
while self.running():
# Trigger a scan with a 10us pulse
GPIO.output(self.pin_trig, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(self.pin_trig, GPIO.LOW)
timeout = False
start_read = time.time()
# Read the echo
while self.running() and GPIO.input(self.pin_echo)==0:
pulse_start_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
while self.running() and GPIO.input(self.pin_echo)==1:
pulse_end_time = time.time()
# Prevent infinite loops, add timeout.
if (time.time() - start_read) > 0.06:
timeout = True
break
if self.running() and not timeout:
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):
print('Distance: {}cm'.format(dist))
if dist<10:
if (time.time()-self.last_goal)>1:
print('goal') #self.parent.goalDetected.emit(self.parent.side)
self.last_goal = time.time()
if __name__=='__main__':
try:
_GoalPins = {
'pin_trig': 3,
'pin_echo': 2
}
goalThread = GoalThread(None, **_GoalPins)
goalThread.start()
while True:
pass
finally:
goalThread.stop()
from pirc522 import RFID
import signal
import time
rf_reader = RFID()
try:
print('Starting...')
while True:
print('Waiting for a tag')
rf_reader.wait_for_tag()
(error, tag_type) = rf_reader.request()
if error:
print('Error: {}'.format(error))
else:
(error, id) = rf_reader.anticoll()
if error:
print('Error: {}'.format(error))
else:
(error, tag_type) = rf_reader.request()
print('RFID: {}'.format([hex(x) for x in id]))
except KeyboardInterrupt:
print('Closing...')
rf_reader.cleanup()
\ No newline at end of file