I’ve recently received my hover gesture board from justhover.com and I needed a nice project to test it ! In the past, I’ve managed to get a bluetooth wireless speaker to work with my Raspberry PI and now, I’m going to use to output music from my music player controlled with gestures !
Note: Didn’t get to output music from pygame to my bluetooth speaker. Always getting the following error:
Hardware used
Raspberry PI model B+
Hover
Bluetooth Dongle
Bluetooth wireless speaker
Requirements
Having your PI already up and running with some sort of audio output – whether by bluetooth or hdmi or jack .
Hover
Wiring
For wiring the Hover, you can check the hover page. For the B+ version, the first half of the GPIO is the same, so there’s no problem with the different versions.
NOTE: Hover has updated the board, the PINS are almost the same – having the newer board more PINs than the version 1
Here you can check the page from hoverlabs. Here’s the pinout:
Hover Board | Raspberry PI |
HOST_V+ | 3.3v |
RESET | GPIO 24 (default library) |
SCL | SCL |
SDA | SDA |
GND | GND |
3v3 | 3.3v |
TS | GPIO 23 (default library) |
Libraries
Head to http://www.justhover.com/raspberry and download the libraries or get the latest:
wget https://github.com/jonco91/hover_raspberrypi/archive/master.zip
Unzip the files
unzip master.zip Archive: master.zip dd05bf58cec56ba487dad21050eb1791e879370e creating: hover_raspberrypi-master/ inflating: hover_raspberrypi-master/Hover_example.py inflating: hover_raspberrypi-master/Hover_library.py inflating: hover_raspberrypi-master/LICENSE inflating: hover_raspberrypi-master/README.md creating: hover_raspberrypi-master/examples/ inflating: hover_raspberrypi-master/examples/Hover_drum.py inflating: hover_raspberrypi-master/examples/readme.txt creating: hover_raspberrypi-master/examples/samples/ inflating: hover_raspberrypi-master/examples/samples/ahems.wav inflating: hover_raspberrypi-master/examples/samples/champion.wav inflating: hover_raspberrypi-master/examples/samples/clap.wav inflating: hover_raspberrypi-master/examples/samples/closed.wav inflating: hover_raspberrypi-master/examples/samples/cymbal.wav inflating: hover_raspberrypi-master/examples/samples/doit.wav inflating: hover_raspberrypi-master/examples/samples/force.wav inflating: hover_raspberrypi-master/examples/samples/kick.wav inflating: hover_raspberrypi-master/examples/samples/open.wav inflating: hover_raspberrypi-master/examples/samples/snare.wav
Prepare the RaspberryPI for GPIO programming
Following the instructions, we need to make sure to have the latest tools available to work with the GPIO.
Instructions from Adafruit
https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-gpio
https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
sudo apt-get update
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio
Check the I2C modules
sudo vi /etc/modules
and add the following:
i2c-bcm2708
i2c-dev
Save and quit
If you want, you can reboot or PI or just load the modules
sudo modprob i2c-dev
sudo modprob i2c-bcm2708
Install more tools
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
Now, some modules related to i2c and spi are blacklisted, because not everyone uses or needs them. Check if you have /etc/modules.d/raspi-blacklist.conf . If you have, edit it and comment the following lines:
(put a # in front of the lines)
#blacklist spi-bcm2708 #blacklist i2c-bcm2708
Save and quit.
Reboot your PI
After reboot, check if hover is detected by checking any addresses in use:
sudo i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 40: -- -- 42 -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
Testing the libraries
Now, head to the directory unzipped from the hover github
cd hover_raspberrypi-master/
and execute the example (using sudo) and wave yor hand…
sudo python Hover_example.py Initializing Hover...please wait. Hover is ready! To exit the program, hit Ctrl+C 00100100 = Left Swipe 00100010 = Right Swipe 00101000 = Up Swipe 00110000 = Down Swipe
You can get more information about the values of swipe in the raspberry PI page of hover.
Music player
Python should already have all the necessary modules installed and ready to run.
The music player is a python script that uses hover to stop, pause, skip to next track and skip to previous track using gestures.
The script controls are:
- Hover left – previous track
- Hover right – next track
- Hover top – pause
- Hover down – unpause
- touch center – play
- touch down – stop
- touch left – vol down
- touch right – vol up
Get the files from my bitbucket account and put it were you want.
Edit the file playerHover.py and search for the following lines:
# music files location - Change only here
toPath = '<location_of_mp3_files>'
Change toPath variable to where you have your music files (mp3).
Save and quit.
Execute the player with
sudo ./playerHover.py
or
sudo python playerHover.py
Have fun with gestures !
A bit of explaining
Import all the necessary modules
import pygame import array import time import os import glob import warnings import random from Hover_library import Hover
Initialize hover
hover = Hover(address=0x42, ts=23, reset=24)
This next function takes an argument (trackNum), clears the screen, loads the next track from the array, plays it and prints the name
# function to play given track def playTrack (trackNum): # clear screen os.system('clear'); pygame.mixer.music.load(playFiles[trackNum]) pygame.mixer.music.play() filepath,filename = os.path.split(playFiles[trackNum]) print ("Playing track ",trackNum, filename)
The location of the mp3 files. Fills the array playFiles with the files in the directory
# music files location - Change only here toPath = '<mp3 files location - full path>'
# Declare array for mp3 files playFiles = []
# Get the files for root, dirs, files in os.walk(toPath): playFiles += glob.glob(os.path.join(root, '*.mp3'))
Get the number of tracks to play
#get size of array
numOfTracks = len (playFiles)
Initialize the pygame libraries
pygame.mixer.init()
Plays the first track
# start playing playTrack (sTrack)
Now, the hover stuff.
The next piece of code gets the status of hover and reads an event – if any
if (hover.getStatus() == 0): # Read i2c data message = hover.getEvent()
The next code gets the event and, with if statements check the time and act accordingly
type (message) if (message == "00100100"): sTrack -= 1 if (sTrack <= 0): print ("No previous track!") sTrack = 0 else: playTrack (sTrack)
The rest of the code is self explanatory !
I’m not a python programmer – the code may contain errors and I’m sure an experienced python programmer would make a lot of improvements
Here’s a small video of Hover in action