This is my first post.
I have started a project with a rpi2 that currently allows me to bluetooth my rpi2 to a bluetooth speaker. On that bluetooth speaker there are three buttons. One that plays and stops the music and the other two that changes the tracks back and forth, but NOT currently. My ultimate goal is to "map" these buttons to correspond with the key presses that allow mpg123 (command-line mp3 player) to move tracks, back and forward.
When you run mpg123 with the command-line option "-C" you can press a key like "d" (on the keyboard) to move track forward. The key "f" moves the track back.
Now, this rpi2 has no physical keyboard hooked to it. I do this through an ssh session. That could be a problem, I guess? I've been messing around with some python code that I've found, that allows me to view these button "events" when pressed. Here is the code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Reading Events """ from subprocess import call, Popen, PIPE from evdev import eventio, uinput, InputEvent, InputDevice, categorize, ecodes # Get the device dev = InputDevice('/dev/input/event0') # Start event loop for event in dev.read_loop(): # On Bluetooth Device Event if event.code == ecodes.KEY_NEXTSONG: # If the pressed key is 'KEY_NEXTSONG' var1 = (categorize(event)) if var1.keystate == 1: print("Next Track ->") elif event.code == ecodes.KEY_PREVIOUSSONG: # If the pressed key is 'KEY_PREVIOUSSONG' var2 = (categorize(event)) if var2.keystate == 1: print("<- Previous Track") elif event.code == ecodes.KEY_PLAYCD: # Stops script once 'KEY_PLAYCD' is pressed print("Stopping Script") break;
I get this output:
Next Track -> <- Previous Track Stopping Script
I can print the variables by removing the print "string" and replacing it with variable itself:
and I get:
key event at 1465481383.228349, 163 (KEY_NEXTSONG), down
I would like to do more than just print these events. What I would like to know, is it possible (I'm sure it is) to "convert" these key events into keyboard strokes (like "d" and "f") and then inject these into "mpg123" so I can move the tracks "back" and "forward"? Remember, there is no physical keyboard connected to the rpi2.
I'm not a seasoned Python programmer, but I would appreciate someone whom might have done something like this before, whom might could set me on the right path.
I'm thinking I would need to run mpg123 in the script. I have done that before:
import subprocess import evdev from evdev import InputDevice, categorize, ecodes dev = InputDevice('/dev/input/event0') from subprocess import call call(["mpg123", "-C", "--list", "/home/pi/bluemusic/playlist.txt" --random", "--buffer", "1024"])
Any help would be much appreciated.