Francesco Vannini
Published © LGPL

Control JustBoom Player with Flick (REST API)

Flick adds tracking and gesture control in various form factors.

IntermediateFull instructions provided827
Control JustBoom Player with Flick (REST API)

Things used in this project

Story

Read more

Code

Full code

Python
#!/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)

REST example

Python
#!/usr/bin/env python

import requests

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 teh response
        response = requests.get(url)

        if(response.ok):
            # do something if the response is ok
            print("response ok")
        else:
            # do something if the response is not ok
            print("response not ok")

    # do something if a connection error occurs
    except requests.ConnectionError as e:
        print("connection error")
        
restcmd("play") # play, stop, pause, previous, next, volume

Credits

Francesco Vannini
9 projects • 19 followers
Contact
Thanks to Frederick Vandenbosch.

Comments

Please log in or sign up to comment.