forked from 9and3r/mopidy-ttsgpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_input_tester.py
114 lines (94 loc) · 3.84 KB
/
gpio_input_tester.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import time
longpress_time = 0.3
try:
import RPi.GPIO as GPIO
except ImportError:
print "Could not import GPIO"
class GPIOManager():
def __init__(self):
# Set the pins you are using
pins = {}
pins['pin_button_next'] = 22
pins['pin_button_previous'] = 23
pins['pin_button_vol_up'] = 24
pins['pin_button_vol_down'] = 25
pins['pin_button_main'] = 17
# Variables to control if it is a longpress
self.down_time_previous = 0
self.down_time_next = 0
self.down_time_main = 0
self.down_time_vol_up = 0
self.down_time_vol_down = 0
try:
# GPIO Mode
GPIO.setmode(GPIO.BCM)
# Next Button
GPIO.setup(pins['pin_button_next'], GPIO.IN,
pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pins['pin_button_next'],
GPIO.BOTH, callback=self.next, bouncetime=30)
# Previous Button
GPIO.setup(pins['pin_button_previous'], GPIO.IN,
pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pins['pin_button_previous'], GPIO.BOTH,
callback=self.previous, bouncetime=30)
# Volume Up Button
GPIO.setup(pins['pin_button_vol_up'], GPIO.IN,
pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pins['pin_button_vol_up'], GPIO.BOTH,
callback=self.vol_up, bouncetime=30)
# Volume Down Button
GPIO.setup(pins['pin_button_vol_down'], GPIO.IN,
pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pins['pin_button_vol_down'],
GPIO.BOTH, callback=self.vol_down,
bouncetime=30)
# Main Button
GPIO.setup(pins['pin_button_main'], GPIO.IN,
pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pins['pin_button_main'],
GPIO.BOTH, callback=self.main, bouncetime=30)
self.correctlyLoaded = True
except RuntimeError:
print "TTSGPIO: Not enough permission "
def previous(self, channel):
if GPIO.input(channel) == 1:
if self.down_time_previous + longpress_time > time.time():
print "Previous button released: Short press"
else:
print "Previous button released: Long press"
else:
print "Previous button pressed"
def next(self, channel):
if GPIO.input(channel) == 1:
if self.down_time_next + longpress_time > time.time():
print "Next button released: Short press"
else:
print "Next button released: Long press"
else:
print "Next button pressed"
def main(self, channel):
if GPIO.input(channel) == 1:
if self.down_time_main + longpress_time > time.time():
print "Main button released: Short press"
else:
print "Main button released: Long press"
else:
print "Main button pressed"
def vol_up(self, channel):
if GPIO.input(channel) == 1:
if self.down_time_vol_up + longpress_time > time.time():
print "Vol+ button released: Short press"
else:
print "Vol+ button released: Long press"
else:
print "Vol+ button pressed"
def vol_down(self, channel):
if GPIO.input(channel) == 1:
if self.down_time_vol_down + longpress_time > time.time():
print "Vol- button released: Short press"
else:
print "Vol+ button pressed"
else:
self.down_time_vol_down = time.time()
GPIOManager()