Skip to content

Commit

Permalink
Added buttons, fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyCupic committed Aug 1, 2015
1 parent fe03ac6 commit 1966142
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
43 changes: 43 additions & 0 deletions 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', ''))
35 changes: 35 additions & 0 deletions mpd_client.py
Expand Up @@ -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):
Expand Down
18 changes: 14 additions & 4 deletions 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! #
Expand All @@ -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")

Expand All @@ -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)
Expand Down

0 comments on commit 1966142

Please sign in to comment.