input.py 1.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#!/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
12
13
14
15
16

from main import OnRasp

if OnRasp:
	import RPi.GPIO as GPIO
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

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
34
35
36
37
		
		if OnRasp:
			GPIO.setwarnings(False)
			GPIO.setmode(GPIO.BCM)
38

39
40
41
42
			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)
43
44

	def run(self):
45
46
47
48
49
50
		if OnRasp:
			try:
				while self.continueRunning:
					pass
			finally:
				GPIOThread.clean()
51
52
53
54
55
56
57
58
59
60
61
62
63
64

	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():
65
66
		if OnRasp:
			GPIO.cleanup()