-
Notifications
You must be signed in to change notification settings - Fork 7
/
buttons.py
43 lines (35 loc) · 1.24 KB
/
buttons.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
import RPi.GPIO as GPIO
import time
# This class will send commands to MPD client from buttons
class buttons():
# Class constructor
# Buttons pins is a dictionary with button_name=>pin_number format
def __init__(self, button_pins, bounce_time):
# Set bounce time
self.bounce_time = bounce_time
# Set buttons
self.buttons = button_pins
# Set GPIO numbering mode
GPIO.setmode(GPIO.BOARD)
# We don't need warnings from GPIO
GPIO.setwarnings(False)
# Set button GPIO pins as inputs and enable interrupts
for button in button_pins:
if (button_pins[button] != False):
GPIO.setup(button_pins[button], GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(button_pins[button], GPIO.FALLING, callback=self.button_pressed, bouncetime=self.bounce_time)
# Initalize MPD
self.mpd = False
# Register MPD client to send it commands
def register(self, mpd):
self.mpd = mpd
def button_pressed(self, channel):
# Debouncing
time.sleep(0.05)
if (GPIO.input(channel) == 0):
# Find out which button was pressed
for button in self.buttons:
if (channel == self.buttons[button]):
# Send command to MPD client
if (self.mpd != False):
self.mpd.commands(button.replace('_BUTTON', ''))