-
Notifications
You must be signed in to change notification settings - Fork 0
/
pins.py
executable file
·90 lines (68 loc) · 2.65 KB
/
pins.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
import logging, sys, time, functions
try:
import RPi.GPIO as GPIO
except RuntimeError:
logging.error("Not a raspberry ! Quitting")
sys.exit(0)
from collections import OrderedDict
class Pins(object):
def __init__(self, pins):
self.pins = pins
self.gpio_setup("DEBUG")
self.event = "wait"
def get_keys(self):
return self.pins.keys()
def get_values(self):
return self.pins.values()
def gpio_setup(self, log_level):
GPIO.setmode(GPIO.BCM)
if log_level == "DEBUG":
GPIO.setwarnings(True)
else:
GPIO.setwarnings(False)
self.gpio_set_inputs()
def cleanup_pins(self):
GPIO.cleanup(self.pins.values())
def get_colors(self):
return self.pins.keys()
def gpio_set_inputs(self):
self.cleanup_pins()
GPIO.setup(self.pins.values(), GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def set_color_and_black(self, color, duration):
self.cleanup_pins()
GPIO.setup(self.pins.values(), GPIO.OUT, initial=GPIO.LOW)
if color in self.pins:
GPIO.output(self.pins[color], GPIO.HIGH)
time.sleep(duration)
GPIO.output(self.pins[color], GPIO.LOW)
def read_inputs(self):
result = []
self.gpio_set_inputs()
logging.debug("Reading GPIO")
for i in range(len(self.pins)):
result.append(GPIO.input(self.pins.values()[i]))
logging.debug("Result " + self.pins.keys()[i] + " = " + str(result[i]))
return result
def wait_for_color(self, color):
self.gpio_set_inputs()
self.event = "wait"
self.gpio_set_inputs()
if GPIO.wait_for_edge(self.pins.get(color), GPIO.RISING):
logging.debug("Button " + str(color) + " pressed")
self.event = self.pins.get(functions.search_key_with_value(color, self.pins))
self.read_inputs()
def get_first_color_event(self, color):
self.event = "wait"
self.gpio_set_inputs()
GPIO.add_event_detect(self.pins.get(color), GPIO.RISING, callback=self.first_color_name, bouncetime=1000)
for i in range(len(self.pins)):
if self.pins.keys()[i] != color:
GPIO.add_event_detect(self.pins.values()[i], GPIO.RISING, callback=self.first_color_name, bouncetime=1000)
def first_color_name(self, channel):
self.event = channel
def delete_event_detections(self):
for i in range(len(self.pins)):
GPIO.remove_event_detect(self.pins.values()[i])
def is_pressed(self, color):
self.gpio_set_inputs()
return GPIO.input(self.pins.get(color)) == 1