I have installed the software, I can play music using vlc, the example python scripts work and I’ve had a bit of success with mopidy (but not much). I had hoped there would be a way to select music using just the buttons on the hat but it seems the supplied code all relies on connecting from another computer or using a keyboard, mouse and display on the pi.Is this correct or am I missing something?
Four buttons, active low connected to BCM 5, 6, 16, and 20 (A, B, X, Y respectively)
From here.
At the moment it looks like you’ll have to map and or code them on your own.
I have one of the original Pirate Radio setups with the pHat Beat. Its buttons are mapped to functions like volume up down etc. I’m not sure if they are hooked in alsa or VLC, or both depending on the button though?
This may or may not help but its a list of VLC commands that you could map your buttons too.
longhelp
±—[ CLI commands ]
| add XYZ . . . . . . . . . . . . . . . . . . . . add XYZ to playlist
| enqueue XYZ . . . . . . . . . . . . . . . . . queue XYZ to playlist
| playlist . . . . . . . . . . . . . show items currently in playlist
| search [string] . . search for items in playlist (or reset search)
| delete . . . . . . . . . . . . . . . . delete item X in playlist
| move [Y] . . . . . . . . . . . . move item X in playlist after Y
| sort key . . . . . . . . . . . . . . . . . . . . . sort the playlist
| sd [sd] . . . . . . . . . . . . . show services discovery or toggle
| play . . . . . . . . . . . . . . . . . . . . . . . . . . play stream
| stop . . . . . . . . . . . . . . . . . . . . . . . . . . stop stream
| next . . . . . . . . . . . . . . . . . . . . . . next playlist item
| prev . . . . . . . . . . . . . . . . . . . . previous playlist item
| goto, gotoitem . . . . . . . . . . . . . . . . . goto item at index
| repeat [on|off] . . . . . . . . . . . . . . toggle playlist repeat
| loop [on|off] . . . . . . . . . . . . . . . . toggle playlist loop
| random [on|off] . . . . . . . . . . . . . . toggle playlist random
| clear . . . . . . . . . . . . . . . . . . . . . clear the playlist
| status . . . . . . . . . . . . . . . . . . . current playlist status
| title . . . . . . . . . . . . . . set/get title in current item
| title_n . . . . . . . . . . . . . . . . next title in current item
| title_p . . . . . . . . . . . . . . previous title in current item
| chapter . . . . . . . . . . . . set/get chapter in current item
| chapter_n . . . . . . . . . . . . . . next chapter in current item
| chapter_p . . . . . . . . . . . . previous chapter in current item
|
| seek X . . . . . . . . . . . seek in seconds, for instance `seek 12’
| pause . . . . . . . . . . . . . . . . . . . . . . . . toggle pause
| fastforward . . . . . . . . . . . . . . . . . . set to maximum rate
| rewind . . . . . . . . . . . . . . . . . . . . . set to minimum rate
| faster . . . . . . . . . . . . . . . . . . faster playing of stream
| slower . . . . . . . . . . . . . . . . . . slower playing of stream
| normal . . . . . . . . . . . . . . . . . . normal playing of stream
| rate [playback rate] . . . . . . . . . . set playback rate to value
| frame . . . . . . . . . . . . . . . . . . . . . play frame by frame
| fullscreen, f, F [on|off] . . . . . . . . . . . . toggle fullscreen
| info . . . . . . . . . . . . . information about the current stream
| stats . . . . . . . . . . . . . . . . show statistical information
| get_time . . . . . . . . . seconds elapsed since stream’s beginning
| is_playing . . . . . . . . . . . . 1 if a stream plays, 0 otherwise
| get_title . . . . . . . . . . . . . the title of the current stream
| get_length . . . . . . . . . . . . the length of the current stream
|
| volume . . . . . . . . . . . . . . . . . . set/get audio volume
| volup . . . . . . . . . . . . . . . raise audio volume X steps
| voldown . . . . . . . . . . . . . . lower audio volume X steps
| achan . . . . . . . . . . . . set/get stereo audio output mode
| atrack . . . . . . . . . . . . . . . . . . . set/get audio track
| vtrack . . . . . . . . . . . . . . . . . . . set/get video track
| vratio . . . . . . . . . . . . . . . set/get video aspect ratio
| vcrop, crop . . . . . . . . . . . . . . . . set/get video crop
| vzoom, zoom . . . . . . . . . . . . . . . . set/get video zoom
| vdeinterlace . . . . . . . . . . . . . set/get video deinterlace
| vdeinterlace_mode . . . . . . . set/get video deinterlace mode
| snapshot . . . . . . . . . . . . . . . . . . . . take video snapshot
| strack . . . . . . . . . . . . . . . . . set/get subtitle track
| hotkey, key [hotkey name] . . . . . . . . . . simulate hotkey press
|
| vlm . . . . . . . . . . . . . . . . . . . . . . . . . load the VLM
| set [var [value]] . . . . . . . . . . . . . . . . . set/get env var
| save_env . . . . . . . . . . . . save env vars (for future clients)
| alias [cmd] . . . . . . . . . . . . . . . . set/get command aliases
| description . . . . . . . . . . . . . . . . . describe this module
| license . . . . . . . . . . . . . . . . print VLC’s license message
| help, ? [pattern] . . . . . . . . . . . . . . . . . a help message
| longhelp [pattern] . . . . . . . . . . . . . . a longer help message
| lock . . . . . . . . . . . . . . . . . . . . lock the telnet prompt
| logout . . . . . . . . . . . . . . exit (if in a socket connection)
| quit . . . . . . . . quit VLC (or logout if in a socket connection)
| shutdown . . . . . . . . . . . . . . . . . . . . . . . shutdown VLC
±—[ end of help ]
This is a python file I did up to control my Pirate radio with a wireless IR remote control.
It hooks into VLC. It worked, but would just quit working some times and I never sorted out why? My remote would just stop working and do nothing when the buttons were pressed.
import os
import socket
import re
import sys
import phatbeat
VLC_HOST = "127.0.0.1"
VLC_PORT = 9294
class VLC():
def __init__(self, host="127.0.0.1", port=9294):
self.host = host
self.port = port
self.current_stream = None
self.current_state = None
self.connecting = False
self.socket = None
def send(self, command):
if self.connecting:
return False
print("Sending command: {}".format(command))
command_string = command + "\n"
if sys.version_info[0] >= 3:
command_string = command_string.encode("utf-8")
try:
self.socket.send(command_string)
except socket.error:
print("Failed to send command to VLC")
if self.connect():
self.send(command)
def recv(self, length):
value = self.socket.recv(8192)
if sys.version_info[0] >= 3:
value = value.decode("utf-8")
return value
def communicate(self, command, response_length=8192):
self.send(command)
return self.recv(response_length)
def get_current_stream(self):
self.send("status")
status = self.recv(8192)
result = re.search("input:\ (.*)\ ", status)
state = re.search("state\ (.*)\ ", status)
if state is not None:
self.current_state = state.group(1)
if result is not None:
self.current_stream = result.group(1)
else:
self.current_stream = None
return self.current_stream
def connect(self):
if self.connecting:
return
self.connecting = True
if self.socket is not None:
self.socket.close()
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for attempt in range(10):
try:
print("Attempting to connect to VLC on {host}:{port}".format(host=self.host, port=self.port))
self.socket.connect((self.host, self.port))
print("Connection successful!")
self.connecting = False
return True
except socket.error:
time.sleep(1)
print("Connection failed!")
self.connecting = False
return False
vlc = VLC(host=VLC_HOST, port=VLC_PORT)
if vlc.connect():
import usb.core
import usb.util
import time
USB_VENDOR = 0x20a0
USB_PRODUCT = 0x0001
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS
BTN_FASTFWD = 55
BTN_REWIND = 54
BTN_PLAYPAUSE = 205
BTN_VOLUP = 46
BTN_VOLDN = 45
BTN_ONOFF = 52
dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
endpoint = dev[0][(0,0)][0]
if dev.is_kernel_driver_active(USB_IF) is True:
dev.detach_kernel_driver(USB_IF)
usb.util.claim_interface(dev, USB_IF)
while True:
control = None
try:
control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
print(control)
except:
pass
if control != None:
if BTN_FASTFWD in control:
vlc.communicate("next") # Next track
time.sleep(0.1)
if BTN_REWIND in control:
vlc.communicate("prev") # Prev track
time.sleep(0.1)
if BTN_PLAYPAUSE in control:
vlc.communicate("pause") # Pause/Play
time.sleep(0.1)
phatbeat.clear()
phatbeat.show()
if BTN_VOLUP in control:
vlc.communicate("volup") # Turn the volume up one step
time.sleep(0.1)
if BTN_VOLDN in control:
vlc.communicate("voldown") # Turn the volume down one step
time.sleep(0.1)
if BTN_ONOFF in control:
os.system("sudo shutdown now -P")
time.sleep(0.1)
Thank you for that information, alphanumeric, it will be very helpful.
I don’t think mapping buttons to commands will do it. I think the best approach would be to write code that reads the buttons, handles navigation through the directories and then runs vlc or something else to play the flies. I’ll also have to figure out how to use the screen by reading the example code. It should be possible to do it.
Yeah, I don’t play any local files on mine, just an internet stream. What I wanted to be able to do was adjust the volume without having to get up and manually push a button.
My FLIR IR receiver shows up as a USB HID device. Thats why I used evdev.
And the Pirate radio setup used VLC so those where the commands I used.
At some point I’d like to add a display to show the song titles etc. Some day, lol.