input.py 1.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#!/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()