Introduction

Flick HAT - 3D Tracking & Gesture HAT for Raspberry Pi

Flick is a range of add-on boards for the Raspberry Pi, bringing 3D tracking and gesture control in various form factors. These gestures can be associated with specific commands, offering an entire new way of controlling devices.

In this guide, we’ll cover the control of the JustBoom Player using gestures with Flick and REST.

Prerequisites

For this project, you will need:

  • One Raspberry Pi with JustBoom pHAT or HAT, and the official JustBoom Player installed
  • One Raspberry Pi with Flick board, and Flick software installed


Both Raspberry Pi should be connected to the same (wireless) network, and will interface via REST.

For more information on setting up the JustBoom Player or getting started with Flick, please refer to following guides:

Remote Control

The JustBoom Player, based on Volumio, can be controlled via its REST API. This API makes it possible for any device to control the player using specific URLs.

The full REST API is documented on the Volumio website.

Using Python, it is possible to combine the Flick library with this REST API, associating gestures with commands such as “play”, “pause”, etc …

REST in Python

Below is an example function of how to tackle REST calls in Python, with some checks for connection issues or unexpected responses:


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

With the REST calls available in a simple function, it’s a small task to combine it with the existing Flick demo code.

Full Code

Below, is the full code for this project. The inline comments should help you understand the code.


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

Upload the code to your Pi using scp and give it the necessary permissions:

scp flick-justboom-rest.py [email protected]:Flick/bin/
sudo chmod +x /home/pi/Flick/bin/flick-justboom-rest.py

Run the script, you are ready to try it out!

/home/pi/Flick/bin/flick-justboom.py

Demo

First published at 12:01pm on July 3, 2017
Last updated at 1:13pm on July 3, 2017