-
-
Save fvdbosch/c42ab17ef84aae641d28f05fbecbc7f7 to your computer and use it in GitHub Desktop.
Controlling the JustBoom Player via its REST API, using Flick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import signal | |
import flicklib | |
from time import sleep | |
from curses import wrapper | |
from os import system | |
import requests | |
import json | |
from gpiozero import LED | |
some_value = 1000 | |
# onboard LEDs for Flick Large | |
greenled = LED(22) | |
redled = LED(4) | |
# flick demo code | |
@flicklib.move() | |
def move(x, y, z): | |
global xyztxt | |
xyztxt = '{:5.3f} {:5.3f} {:5.3f}'.format(x,y,z) | |
@flicklib.flick() | |
def flick(start,finish): | |
global flicktxt | |
flicktxt = start + '-' + finish | |
@flicklib.airwheel() | |
def spinny(delta): | |
global some_value | |
global airwheeltxt | |
some_value += delta | |
if some_value < 0: | |
some_value = 0 | |
if some_value > 10000: | |
some_value = 10000 | |
airwheeltxt = str(some_value/100) | |
@flicklib.double_tap() | |
def doubletap(position): | |
global doubletaptxt | |
doubletaptxt = position | |
@flicklib.tap() | |
def tap(position): | |
global taptxt | |
taptxt = position | |
@flicklib.touch() | |
def touch(position): | |
global touchtxt | |
touchtxt = position | |
def setled(colour): | |
# turn on or off LEDs based on input string | |
if colour == "green": | |
redled.off() | |
greenled.on() | |
elif colour == "red": | |
greenled.off() | |
redled.on() | |
else: | |
redled.off() | |
greenled.off() | |
# Volumio REST API | |
def restcmd(command): | |
# url of the REST API, player command passed via function | |
url = "http://justboom.local/api/v1/commands/?cmd=" + command | |
try: | |
# make the request and get the response | |
response = requests.get(url) | |
if(response.ok): | |
# if the response is ok, set the onboard LED to GREEN | |
setled("green") | |
else: | |
# if the response is not ok, set the onboard LED to RED | |
setled("red") | |
# if a connection error occurs, set the onboard LED to RED | |
except requests.ConnectionError as e: | |
setled("red") | |
# Get player status: playing, paused, stopped | |
def reststatus(): | |
url = "http://justboom.local/api/v1/getstate" | |
try: | |
response = requests.get(url) | |
if(response.ok): | |
data = json.loads(response.content) | |
# toggle player status: if playing pause, if paused or stapped, play | |
if(data["status"] == "stop" or data["status"] == "pause"): | |
cmd = "play" | |
elif(data["status"] == "play"): | |
cmd = "pause" | |
return cmd | |
except requests.ConnectionError as e: | |
return "error" | |
def main(stdscr): | |
global xyztxt | |
global flicktxt | |
global airwheeltxt | |
global touchtxt | |
global taptxt | |
global doubletaptxt | |
xyztxt = '' | |
flicktxt = '' | |
flickcount = 0 | |
airwheeltxt = '' | |
airwheelcount = 0 | |
touchtxt = '' | |
touchcount = 0 | |
taptxt = '' | |
tapcount = 0 | |
doubletaptxt = '' | |
doubletapcount = 0 | |
cmd = "" | |
setled("off") | |
# continuously check for gestures | |
while(True): | |
# flick from left to right and right to left, control which song plays | |
if len(flicktxt) > 0: | |
if flicktxt == "east-west": | |
cmd = "next" | |
elif flicktxt == "west-east": | |
cmd = "prev" | |
flicktxt = '' | |
# airwheel controls volume | |
if len(airwheeltxt) > 0: | |
cmd = "volume&volume=" + airwheeltxt | |
airwheeltxt = '' | |
# double tap in center toggles between play and pause | |
if len(doubletaptxt) > 0: | |
if doubletaptxt == "center": | |
cmd = reststatus() | |
doubletaptxt = '' | |
# pass the command to the REST call | |
if cmd != '' and cmd != "error": | |
restcmd(cmd) | |
cmd = '' | |
sleep(0.25) | |
wrapper(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment