Commit f82fda56 authored by Antoine Lima's avatar Antoine Lima

Button control directly on the PI

parent d2dfdd6f
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""
import logging
import serial
from os.path import isfile as exists
from autopy.key import tap as PressKey, Code as KeyCode
from threading import Thread
from player import Side
class InputThread(Thread):
keyButtonBindings = [KeyCode.ESCAPE, KeyCode.UP_ARROW, KeyCode.LEFT_ARROW, KeyCode.RIGHT_ARROW, KeyCode.DOWN_ARROW, KeyCode.RETURN]
def __init__(self, dispatcher, side):
Thread.__init__(self)
self.side = side
self.dispatcher = dispatcher
self.continueRunning = True
self.path = '/dev/ttyUSB0' if self.side==Side.Left else '/dev/ttyUSB1'
if exists(self.path):
self.arduino = serial.Serial(self.path, 9600, timeout=1)
else:
raise RuntimeError('No arduino connected on the {} side'.format(self.side.name.lower()))
def run(self):
while self.arduino.isOpen():
msg = self.arduino.readline()[:-1]
if msg:
parsedMessage = self.parseMsg(msg)
if 'butn' in parsedMessage:
self.sendKeyStroke(parsedMessage)
else:
self.dispatcher.dispatchMessage(parsedMessage)
else:
logging.warn('No message read on Arduino {}'.format(self.side.name))
def stop(self):
self.continueRunning = False
self.arduino.close()
def sendKeyStroke(self, msg):
if 'butn' in msg:
button = int(msg['butn'])
print({button: ['ESCAPE', 'UP_ARROW', 'LEFT_ARROW', 'RIGHT_ARROW', 'DOWN_ARROW', 'RETURN'][button]})
key = InputThread.keyButtonBindings[button]
PressKey(key, [])
def parseMsg(self, msg):
parts = msg.split(':')
return {parts[0]: parts[1], 'source': self.side}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""
import logging
import pyautogui # PyPi library
from threading import Thread
import RPi.GPIO as GPIO
from player import Side
class GPIOThread(Thread):
_keyButtonBindings = {
26: 'up',
22: 'left',
27: 'right',
23: 'down',
17: 'return',
18: 'escape'
}
def __init__(self, dispatcher):
Thread.__init__(self)
self.dispatcher = dispatcher
self.continueRunning = True
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
for pin in GPIOThread._keyButtonBindings.keys():
print(pin)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.RISING, callback=self.handleButtonPress)
def run(self):
try:
while self.continueRunning:
pass
finally:
GPIOThread.clean()
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
@staticmethod
def clean():
GPIO.cleanup()
......@@ -18,7 +18,7 @@ from PyQt5.QtCore import QTime, Qt
from ui.main_ui import Ui_MainWindow
from modules import *
from player import Side
from com import InputThread
from input import GPIOThread
class MainWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
......@@ -94,37 +94,31 @@ if __name__=='__main__':
from settings import Settings
from replay import Replay as ReplayThread
#logging.basicConfig(filename='babyfoot.log', level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
app = QtWidgets.QApplication(sys.argv)
myapp = MainWin()
threadReplay = ReplayThread(Side.Left)
threadReplay.start()
myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
if Settings['app.mode']!='dev':
threadArduinoLeft = InputThread(myapp, Side.Left)
#threadArduinoRight = InputThread(myapp, Side.Right)
threadArduinoLeft.start()
#threadArduinoRight.start()
myapp.show()
app.exec_()
threadReplay.stop()
threadReplay.join()
if Settings['app.mode']!='dev':
threadArduinoLeft.stop()
#threadArduinoRight.stop()
threadArduinoLeft.join()
#threadArduinoRight.join()
try:
#logging.basicConfig(filename='babyfoot.log', level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
app = QtWidgets.QApplication(sys.argv)
myapp = MainWin()
if ReplayThread.isCamAvailable():
threadReplay = ReplayThread(Side.Left)
threadReplay.start()
myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
threadGPIO = GPIOThread(myapp)
threadGPIO.start()
myapp.show()
app.exec_()
threadGPIO.stop()
if ReplayThread.isCamAvailable():
threadReplay.stop()
threadReplay.join()
threadGPIO.join()
finally:
GPIOThread.clean()
......@@ -12,8 +12,9 @@ from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from module import Module
import modules
from module import Module
from settings import Settings
from ui.menu_ui import Ui_Form as MenuWidget
from player import Side
......@@ -42,7 +43,7 @@ class MenuModule(Module):
self.ui.btnStartQuick.animateClick()
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
if e.key() == Qt.Key_Escape and Settings['app.mode']=='dev':
self.handleExit()
elif e.key() == Qt.Key_Up:
......
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