-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoypadio.py
59 lines (47 loc) · 1.63 KB
/
Joypadio.py
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
import os
import time
import RPi.GPIO as GPIO
class JoypadioEvent(object):
pass
class Joypadio:
'hardware I/O class for joypad voting system'
#choose your GPIO pins here
GPIOteamA = 16
GPIOteamB = 20
def __init__(self):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setup(self.GPIOteamA, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(self.GPIOteamB, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
self.scoreA = 0
self.scoreB = 0
self.callbacks = []
#Record a vote agains the given object
def registerVote(self,channel):
if (channel == self.GPIOteamA):
self.scoreA = self.scoreA +1;
self.fire(action='vote', team='a');
elif (channel == self.GPIOteamB):
self.scoreB = self.scoreB + 1;
self.fire(action='vote', team='b');
else:
print("unknown channel input detected on GPIO pin:" + channel)
def resetScores(self):
self.scoreA = 0
self.scoreB = 0
#callback handling
def subscribe(self, callback):
self.callbacks.append(callback)
def fire(self, **attrs):
e = JoypadioEvent()
e.source = self;
for k, v in attrs.iteritems():
setattr(e,k,v)
for fn in self.callbacks:
fn(e)
"""
In your code, implement the following
GPIO.add_event_detect(Joypadio.GPIOteamA, GPIO.RISING, callback=JoypadioObject.registerVote, bouncetime=200)
GPIO.add_event_detect(Joypadio.GPIOteamB, GPIO.RISING, callback=JoypadioObject.registerVote, bouncetime=200)
"""