MPD and Button Shim


#1

Hi all, I’m having trouble with my MPD script, what I’m trying to do is whenever I press a button, it should perform an action in MPD.

What I’m finding is that all of my MPD codes are executed when the python script is run, instead of when a button is pressed.

If I press a button, I’ve found it won’t perform the MPD command, and execute another part of the code i.e print(“Japa”)

Could someone have a look at this and tell me where I’m going wrong?

I’ve also tired this with using subprocess, and the MPD python library and get similar results, below is an example code I used to test if the basic functions are working.

Many thanks,

Kit

================
#!/usr/bin/env python

_ import signal_
_ import buttonshim_
_ import subprocess_
_ import os_

_ @buttonshim.on_press(buttonshim.BUTTON_A)_
_ def button_a(button, pressed):_
_ buttonshim.set_pixel(0x94, 0x00, 0xd3)_
_ _
_ print(“Japa”)_
_ os.system (‘mpc clear’)_

_ os.system(‘mpc load Japa’)_
_ os.system(‘mpc repeat on’)_
_ os.system(‘mpc play’)_

_ signal.pause()_


#2

if you put three ` then your code, followed by another three of those it will put your code in code tags like so
your code here
On my keyboard its the key below the esc key with the ~ on it.That will maintain its structure.
I can’t make a whole lot of sense out of your code but one thing I don’t see is a “while true” loop?
I’m no python expert but I think that is needed for what your trying to do.
Without that, as far as I know, it will just do the one run and stop.

Also what does MPD and MPC stand for?


#3

Hi Alphanumeric,

Thank you for the quick reply, I’m not sure if this looks any better:


_ import signal_
_ import buttonshim_
_ import subprocess_
_ import os_

_ @buttonshim.on_press(buttonshim.BUTTON_A)_
_ def button_a(button, pressed):_
_ buttonshim.set_pixel(0x94, 0x00, 0xd3)_
_ _
_ print(“Japa”)_
os.system (‘mpc clear’)_

os.system(‘mpc load Japa’)_
os.system(‘mpc repeat on’)_
 os.system(‘mpc play’)

 signal.pause()```

-----------------------------
MPC/MPD is a MusicPlayerDaemon and MusicPlayerClient for Linux/RPI, which allow music to be played and controlled by other devices. In this scenario, I want to be able it to load a playlist.

In terms of the loop, the runs and if I press the corresponding button i.e A, it will execute apart of the code i.e Print("Japa"), I based the syntax of my code on some of the examples provided with the Button Shim exmaples, which work without a loop

#4

Looking at the examples I think you may be missing a
try:
statement or two?
But to be honest, I can’t make much sense out of the code you posted. That may just be me though, I’m no Python expert. I’m about average with my Python skills.
If you open your python file for editing in say Idle. And clip and past from there, adding the code tags to just that. It should keep the formatting and get ride of all the _ etc. Keep your comments outside of the code tags though and that part will be normal and easy to read. ;)


#5

Thank you for trying to help,

ok, so the code is


import signal
import buttonshim
import subprocess
import os



@buttonshim.on_press(buttonshim.BUTTON_A)
def button_a(button, pressed):
        buttonshim.set_pixel(0x94, 0x00, 0xd3)
    
print("Japa")
os.system ('mpc clear')

os.system('mpc load Japa')
os.system('mpc repeat on')
os.system('mpc play')


signal.pause()```

#6

and that’s based on this example from https://github.com/pimoroni/button-shim/blob/master/examples/rainbow.py

#!/usr/bin/env python

import signal
import buttonshim

print("""
Button SHIM: rainbow.py
Light up the LED a different colour of the rainbow with each button pressed.
Press Ctrl+C to exit.
""")

@buttonshim.on_press(buttonshim.BUTTON_A)
def button_a(button, pressed):
    buttonshim.set_pixel(0x94, 0x00, 0xd3)

@buttonshim.on_press(buttonshim.BUTTON_B)
def button_b(button, pressed):
    buttonshim.set_pixel(0x00, 0x00, 0xff)

@buttonshim.on_press(buttonshim.BUTTON_C)
def button_c(button, pressed):
    buttonshim.set_pixel(0x00, 0xff, 0x00)

@buttonshim.on_press(buttonshim.BUTTON_D)
def button_d(button, pressed):
    buttonshim.set_pixel(0xff, 0xff, 0x00)

@buttonshim.on_press(buttonshim.BUTTON_E)
def button_e(button, pressed):
    buttonshim.set_pixel(0xff, 0x00, 0x00)

signal.pause()

#7

Try this

import signal
import buttonshim
import subprocess
import os



@buttonshim.on_press(buttonshim.BUTTON_A)
def button_a(button, pressed):
        buttonshim.set_pixel(0x94, 0x00, 0xd3)
        print("Japa")
        os.system ('mpc clear')
        os.system('mpc load Japa')
        os.system('mpc repeat on')
        os.system('mpc play')


signal.pause()```

#8

You’re kidding, is indentation that important? ok I’ll try that tonight and let you know if that resolved it,

Thank you again

Kit


#9

Yes, unfortunately in python indentation is very very important.
It affects how the code is treated and how it is grouped together.


#10

Ok, that is interesting,

hopefully that should resolve it


#11

If you open your file in Idle, and do a Run > check module from the menu. It will flage any issues with indentation. You may get messages saying something like unexpected indent where no indent was expected, lol.

EDIT: those in the know will tell you to use the tab key, not the space key.


#12

brilliant, I’ll try that out


#13

Thank you very much :)

It’s all working now, and could see the importance of indentation,


#14

Thats good to here. Thats something about Python that will give you headaches trying to sort out. Especially progressive indents, tab, tab tab, tab tab tab.
Anyway, have fun. =)


#15

lol

thank you, will keep that in mind