Friday Fun: Bluedot Presentation Tool

Presentations...fun aren't they?

For the serious presenter a "clicker" remote is a must! You know what I mean, the person at the front, wielding the power of the presentation using a clicker to advance the slides.

But clickers aren't cheap, and for most of us we only need one every so often...so can we make our own?

Of course we can.

For this project you need the following hardware

  • An Android mobile device
  • A Linux laptop or computer with Bluetooth or Raspberry Pi (This project does not work with Windows...sorry!)

All of the code for this project can be found on my Github repository

Bluedot



I'm lucky to consider Martin O'Hanlon a good friend. I've known Martin since PyCon UK 2013 and we have worked together many times delivering Picademy with the Raspberry Pi Foundation.

Martin is a clever lad and his latest clever project is Bluedot, a simple Bluetooth controller for the Raspberry Pi. The Bluedot app on your phone connects to a Raspberry Pi running the Bluedot Python software. Simple as that!

Android App Install

To install the Android app, simply head over to the Play Store and install it.

Bluetooth Setup

Then we need to pair our phone and computer together in the normal manner for our operating system.

On your Android phone:

  • Open Settings
  • Select Bluetooth
  • This will make your phone Discoverable

Using your Linux computer or Raspberry Pi:

  • Click the bluetooth icon on the taskbar
  • Turn on Bluetooth (if its off)
  • Click Make Discoverable (Pi 3 and Zero W)

  • Click Add Device (Or for Ubuntu click on the +)
  • Your phone will appear in the list, select it and click Pair
  • Enter / Confirm PIN code

On your Android phone:

  • Enter the same PIN code when prompted
  • Click Ok

Install the Python libraries

On your Linux computer / Raspberry Pi

We have two Python 3 libraries to install for this project, they are Bluedot which is used to connect our phone to the computer, and the second library is keyboard which emulates keyboard input.

From the desktop open a Terminal and enter the following.

sudo pip3 install bluedot keyboard

Now lets open a Python editor and start writing some code! I named the file Presentation_Controller.py so that I could remember what it did.

We start the Python code with importing four Python libraries, they are Bluedot for our Bluetooth connection, pause from the Signal library to prevent our code from exiting, keyboard to emulate a keyboard press and subprocess which we shall use to run commands on the operating system.

from bluedot import BlueDot
from signal import pause
import keyboard
import subprocess

Now lets create a function called controller (catchy and original!!) This function is really just a conditional statement that checks where the user is pressing Bluedot on their phone. We have five positions, top, bottom, left, right and the middle and when the app registers a press in any of those positions it will print text to the Python shell (handy for debug) before using the keyboard library, specifically pressing a certain key(s) to navigate through the slides.

So if the user presses the top of Bluedot then the Python code will print "Present from current slide" and then press "Shift+F5" on the keyboard, which is the shortcut in LibreOffice Impress to do just that.

def controller(pos):
    if pos.top:
        print("Present From Current Slide")
        keyboard.press_and_release('shift+f5')
    elif pos.bottom:
        print("Exit Presentation")
        keyboard.press_and_release('esc')
    elif pos.left:
        print("Previous Slide")
        keyboard.press_and_release('left')
    elif pos.right:
        print("Next Slide")
        keyboard.press_and_release('right')
    elif pos.middle:
        print("Present From Start")
        keyboard.press_and_release('f5')

Bonus: Audio Control



I added something to control the audio on my Ubuntu laptop, so this new function takes the current position of the users finger and converts that to a percentage, which is then used to set the volume level using the amixer command in the terminal, via subprocess. If you are doing this project with your Raspberry Pi then you will need to change Master to PCM.

def audio_control(pos):
    percentage = round(pos.distance * 100, 2)
    print("{}%".format(percentage))
    #Change Master to PCM for Pi audio
    subprocess.call(["amixer", "set", "Master", str(percentage)])

The last four lines of the project create an object called bd which is our connection to BlueDot. Then we say that when the user presses a position on Bluedot, that the controller function is called and the function reacts according to the position pressed. Then when the user slides up and down the Bluedot the audio_control function is called causing the volume to go up and down. Lastly we call the pause function which prevents the application from closing.

bd = BlueDot()
bd.when_pressed = controller
bd.when_moved = audio_control
pause()

Complete Code Listing

from bluedot import BlueDot
from signal import pause
import keyboard
import subprocess

def controller(pos):
    if pos.top:
        print("Present From Current Slide")
        keyboard.press_and_release('shift+f5')
    elif pos.bottom:
        print("Exit Presentation")
        keyboard.press_and_release('esc')
    elif pos.left:
        print("Previous Slide")
        keyboard.press_and_release('left')
    elif pos.right:
        print("Next Slide")
        keyboard.press_and_release('right')
    elif pos.middle:
        print("Present From Start")
        keyboard.press_and_release('f5')

def audio_control(pos):
    percentage = round(pos.distance * 100, 2)
    print("{}%".format(percentage))
    #Change Master to PCM for Pi audio
    subprocess.call(["amixer", "set", "Master", str(percentage)])

bd = BlueDot()
bd.when_pressed = controller
bd.when_moved = audio_control
pause()

Running the code

In order to use our new Python project we need to run the code as root or with sudo. Now this is not normally the done thing, but the keyboard library requires sudo because...

To avoid depending on X the Linux parts reads raw device files (/dev/input/input*) but this requries root.

Source

So to run the code we need to open a terminal and navigate to the location of the code and then type the following

sudo python3 Presentation_Controller.py

It should wait for a connection from your phone, so with Bluedot on your phone find the device you want to connect to, and click on it. It might take a couple of attempts to connect, but it will work, promise!

And then, open your presentation and dazzle your audience!