Off-The-Shelf Hacker: Run a Slide Deck from the Raspberry Pi
We could jump right into the typical “plug pin A into breadboard connector B” approach to helping TNS readers become familiar with microcontrollers, sensors, Nano-Linux devices and the maker movement. Instead, I’ll cover a small real-world application to which most techies can relate: Putting slides up on a screen at a tech conference.
You Like To Speak?
That’s right! I enjoy doing tech talks.
Getting in front of a knowledgeable crowd of techies and sharing bleeding-edge tips that I’ve picked up in my work is fun. The best part is interacting with curious and enthusiastic attendees during Q&A. My ROI for speaking is attractive, as well.
As you might guess, I’m also a gadget geek.
I designed my “Steampunk Presentation Manipulation and Display Apparatus” as a one-off attention grabber, for displaying my presentation slides and showing small parts during a hardware demo. Instead of using a notebook, this device just plugs into the HDMI projector, I whip out a wireless keyboard/mousepad, fire it up and go. Notable features are a Raspberry Pi 2, LibreOffice Impress, a hacked webcam and a couple of buttons to advance/reverse the slides. The buttons are today’s topic of discussion.
Arduino of Raspberry Pi?
Lot’s of people lump the Arduino and Raspberry Pi together under the “microcontroller” heading. While the Arduino is an awesome device, it is actually quite different from the Raspberry Pi in a number of important aspects:
|Arduino||Raspberry Pi 2|
|No concept of a user.||Multi-user environment.|
|Single-tasking environment.||Multi-tasking environment.|
|Externally programmable firmware, execution on boot-up.||Fully self-contained programming languages/IDEs, shells, libraries and desktop-based, automated, command line or remote execution.|
|Firmware is the “operating system.”||Linux operating system.|
|Limited, dedicated program memory (typically 32 K of flash memory). Limited data storage usually on SD cards.||Large memory capacity (typically 512 MB to 1 GB) of RAM, nearly unlimited data storage on USB drives or SD cards.|
|Numerous, very fast input/output to/from dedicated general purpose (analog, digital, pulse width modulation) hardware pins.||Moderately fast input/output to/from general purpose (digital, pulse width modulation) hardware pins.|
|Serial, I2C, SPI, USB interfaces. No HDMI, video or audio interfaces.||Serial, I2C, SPI, USB, Ethernet, dedicated camera and LCD panel, HDMI, composite video, audio interfaces|
|0.7” x1.3” (Pro Mini), 2.1” x 2.7” (Uno)||2.2” x 3.3”|
|Arduino Pro Mini – $10, Arduino Uno – $25.||Raspberry Pi 2 Model B – $42.|
As you may have observed, both the Arduino and Raspberry Pi are capable of recognizing and reacting to the push of a couple of buttons. But, for the job of running a slide presentation or viewing small printed circuit board details up on the big screen, the Arduino is wholly inappropriate.
An Arduino’s job is fairly straightforward. In a nutshell, it reads some inputs, maybe does a few calculations, then based on those inputs or calculations sets some outputs. Inputs might be a push button, photocell, switch, a logic-level voltage and so on. A calculation might be adding the two inputs together and if they total above a certain value (in this case maybe => 2), set an output. An output might be turning on an LED, running a motor or activating a relay that controls a kitchen appliance.
The Raspberry Pi, by contrast, is easily capable of displaying a full desktop on an HDMI-connected projector or monitor. Using a run-of-the-mill webcam, the Pi can also show real-time video of what’s in the camera’s field of vision, including a tiny close-up of a printed circuit board or even an Arduino module itself. The Pi can also effortlessly run slides using LibreOffice Impress, including detailed graphics and cool slides transitions.
The credit card-sized Pi acts just like a Linux desktop machine, with the added capability to read inputs, do some calculations and set outputs, much like the single-minded Arduino.
All of this Pi-based presentation and physical computing goodness happens under the watchful eye of our ultra-reliable and constant computing companion: Linux.
Read Buttons, Change Slides
Let’s look at how to change slides by pushing a button.
The Pi uses a 40-pin connector for most of its interface connections. I found the easiest way to hook up devices was to pirate an old 40-pin PC hard-drive cable and take the connector apart. You can then solder solid-core 24 gauge wire scavenged from some old CAT 5 cable, to the pins and run the wires to the buttons. See graphic #1 for a close-up of the connector.
One side of a button #1 (Up) goes to GPIO pin 23 (physical pin #16). Likewise, one side of button #2 (Down) goes to GPIO pin 24 (physical pin #18). Here’s a link to the Raspberry Pi pin layout. The other side of both buttons is soldered to pin GND (physical pin #14). There is no need for a voltage divider circuit, as you would use with an Arduino, because the pins on the Pi have pull-up/pull-down resistors built-in.
For testing purposes, I simply shorted each pair of wire together for a button push, as I hadn’t hooked up any physical buttons yet.
Once the “buttons” are ready, it’s time to turn attention to detecting a press and advancing the slide.
As you would expect, the Pi runs an X-Windows-based display system. LibreOffice works great under X-Windows. The trick is to detect one of two button pushes and tell LibreOffice to change the slide while going through the X-Windows system.
My solution was to read the input from the button and then emulate either a Down-Arrow or Up-Arrow, fooling LibreOffice into thinking I pressed those keys on a keyboard. You can do all this within a Python script, which is the generally accepted default programming language on the Pi.
There’s a little known command-line program called xdotool that does the job. The Python script simply runs a loop that repeatedly checks the state of the buttons and when it detects a push, executes the xdotool command with a system call.
Here’s the Python script. I named it scrollslides.py.
import RPi.GPIO as GPIO
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
input_state = GPIO.input(23)
if input_state == False:
os.system("xdotool search --name 'Impress' key Up")
print('Up Button Pressed')
input_state = GPIO.input(24)
if input_state == False:
os.system("xdotool search --name 'Impress' key Down")
print('Down Button Pressed')
The program is straightforward. Libraries are referenced with the “import” lines. Then, the general purpose input/output lines are initialized with the “GPIO” statements. Next, an endless loop is started where each input state is read. If a button is pushed, the state registers as false and the system call to xdotool is made. The button state is also printed to the terminal.
The xdotool command searches for an X-Window named “Impress” and then sends either an Up or Down key, depending on which button was pushed. X-Windows have various names, so all you have to do is figure out the Window name. It will find either and entire or partial X-Window name.
You can also use the xwininfo command to discover any windows active on your machine.
rob% xwininfo -tree -root
Time For The Show
My procedure for doing a show is to plug the HDMI cable, USB wireless keyboard fob and power cord into the Raspberry Pi. Linux will boot into the standard desktop. From there I’ll open up a terminal and run the Python script with the sudo command, since it will be accessing the GPIO pins and needs the correct permissions.
<i> </i>rob% sudo python scrollslides.py
With the scrollslides.py (button detection) script running, it’s time to open LibreOffice Impress and load the conference slides. Next, click on the Slide Show tab and bring up the first slide.
At this point, the buttons will handle stepping forward and backward through the slides. Of course, you can also use the up/down arrow on the keyboard to control the slides, too.
That’s it, a basic use of physical computing on a Raspberry Pi.
There are other applications of reading inputs, doing calculations or comparisons and setting outputs (or emulating keystrokes) on a Raspberry Pi you might investigate.
One idea might be to use a couple of buttons to go forward or backward when watching a video or audio track. Try this technique with mplayer or Audacity. You might consider putting the buttons on a board that you could operate with your foot, to control things hands-free.
Take a look at my OSCON version of the Presentation Machine at Raspberry Pi Geek Magazine.
So, there you have it. A simple real-world example of physical computing. I’ll talk about (and use) my Steampunk Presentation Manipulation and Display Apparatus at Fossetcon, in a couple of weeks. Swing by the session and let me know that you are a TNS reader.