diff --git a/buttons.py b/buttons.py new file mode 100644 index 0000000..16faf69 --- /dev/null +++ b/buttons.py @@ -0,0 +1,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', '')) \ No newline at end of file diff --git a/mpd_client.py b/mpd_client.py index 419cd06..b9f193e 100644 --- a/mpd_client.py +++ b/mpd_client.py @@ -115,7 +115,42 @@ def toUpper(self, data): # Return joined list return " ".join(lst) + + # This function is called by buttons to give commands to MPD + def commands(self, command): + if (command == 'PLAY'): + if (self.data['state'] == 1): + self.cmd_client.pause(1) + else: + self.cmd_client.play() + + elif (command == 'STOP'): + self.cmd_client.stop() + + elif (command == 'NEXT'): + self.cmd_client.next() + + elif (command == 'PREV'): + self.cmd_client.previous() + elif (command == 'VDN'): + # Get volume value + vol = self.data['volume'] - 5 + + if (vol < 0): + vol = 0 + + self.cmd_client.setvol(vol) + + elif (command == 'VUP'): + # Get volume value + vol = self.data['volume'] + 5 + + if (vol > 100): + vol = 100 + + self.cmd_client.setvol(vol) + # Function for updating data # Returns which option has been changed (shuffle - 0, repeat all - 1, repeat single - 2, nothing changed - -1) def updateData(self): diff --git a/start.py b/start.py index 8c1ec6d..5a0e4b5 100644 --- a/start.py +++ b/start.py @@ -1,4 +1,4 @@ -import i2c_display, mpd_client, ir_remote, time +import i2c_display, mpd_client, ir_remote, time, buttons ######### MPD PARAMETERS ############## # Only if you know what you're doing! # @@ -8,7 +8,17 @@ CON_ID = {'host':HOST, 'port':PORT} # ######################################### -display = i2c_display.i2c_display(0x27, 2, 16, 5, 0.1); +# Buttons - put to False if don't want to use it +button_pins = { + 'PLAY_BUTTON': 8, + 'NEXT_BUTTON': 10, + 'PREV_BUTTON': 11, + 'VDN_BUTTON': 12, + 'VUP_BUTTON': 13, + 'STOP_BUTTON': 15 +} + +display = i2c_display.i2c_display(0x27, 4, 20, 5, 0.1); mpdcl = mpd_client.mpd_client(CON_ID, PASSWORD) remote = ir_remote.remote("/tmp/irpipe") @@ -17,10 +27,10 @@ remote.register_display(display) display.start() remote.start() +btn = buttons.buttons(button_pins, 200) +btn.register(mpdcl) mpdcl.mpdMain() -print "test" - '''display.start() time.sleep(5)