Create a “Wheel of Excuses” With BASIC and the New Raspberry Pi

RISC OS and a retro language make hardware interfacing easy

Advertisement

Many years ago in the offices here at IEEE Spectrum, we had a “Wheel of Excuses” pinned to the outside wall of a cubicle. Made from paper and cardboard in the style of a small lottery wheel, it could be spun to suggest plausible excuses to luckless editors seeking to explain a blown deadline. The wheel had its limitations however, chief among them being the small number of excuses that could be squeezed onto a disk just 25 centimeters across, and a tendency to fall off the wall if spun with too much vigor or desperation.

Spectrum’s offices were renovated this year, the dingy cubicle walls swept away for a modern office layout. So I thought it might be time for an updated Wheel of Excuses—a digital one, naturally. I wanted a one-touch system that would show an animation of a spinning wheel, followed by the display of a randomly selected excuse from an extended list.

Initially, I thought I would make the new wheel using an Arduino and a touch-screen add-on shield. But the speed at which graphics could be drawn on the touch screen proved too slow for the kind of animation I wanted, and the 2.8-inch display was on the small side in any case.

So I turned to the US $35 Raspberry Pi microcomputer, which had the final release of its first generation in July—the Model B+. Among other changes, the Model B+ has two more USB ports than the Model B along with an expanded general-purpose input/output (GPIO) connector, and it relies more heavily on HDMI for video output.

The Pi was first released in 2012 as a “spiritual successor” to the BBC Microcomputer System, which was created by Acorn Computers in 1981 for Britain’s national Computer Literacy Project. The naming scheme for Pi models echoes that of the BBC Micro series, and like the original BBC Micro, the Pi has rapidly spread beyond the classroom.

The links to the BBC Micro are more than just circumstantial. The Pi is built around an ARM chip (a Broadcom BCM2835), and while ARM currently dominates the world of smartphones and tablets, the architecture was originally developed to provide a high-performance coprocessor for BBC Micros, and it later powered the Archimedes line of PCs. The Archimedes came with RISC OS, a graphical user interface–based operating system that has since been ported to the Pi.

I first used Acorn’s dialect of BASIC way back in the day on a BBC Micro. One of the nice things about it was that it let you mix BASIC commands with assembly code for the BBC Micro’s 6502 processor. I was pleased to discover that RISC OS has retained a great deal of compatibility with the systems it grew out of, right back to that original dialect.

RISC OS’s version of BBC BASIC—version VI—is, of course, greatly expanded compared with its 8-bit ancestor: As I said when I first tried it out, “it’s like meeting someone you palled around with in high school, and now they own a business and have two kids.” But it still includes an in-line assembler for combining machine code subroutines—now ARM code, of course—with BASIC. The integration allows for streamlined passing of variables back and forth between a BASIC program and machine code—for example, a set of BASIC integer variables, A% through H%, are automatically copied into the first eight registers of the ARM chip when a subroutine is called.

This integration let me quickly write the spinning wheel animation and display code in BASIC, reaching back across the years to cobble together commands to draw colored segments of a circle and store the text of excuses using “data” and “read” commands. (When I started programming, BASIC code would have been too slow for the wheel’s animation, but 30 years of Moore’s Law has solved that problem.) I needed to dip into assembly only in order to read the state of a button connected to the GPIO hardware. The button triggers the animation and has the program select and display an excuse.

I wired the button to the Pi’s GPIO port using a $10 RasPiO Breakout Pro, which provides basic protection against miswiring. (Unlike the more robust Arduino, which can handle enough current to drive a servo, the Pi’s GPIO can be damaged if connected to circuits that expose it to more than a few milliamperes or exceed 3.3 volts.) The Breakout Pro is designed for the GPIO on earlier Pi models, but the B+’s expanded port keeps the same pin configuration for the first 26 pins, so I was able to use the Breakout Pro and simply ignore the B+’s extra pins.

Reading the GPIO hardware was a good chance to get acquainted with the guts of a system using a reduced-instruction-set-computing architecture (so many registers!)—the last time I programmed on the metal was for the 6502. The Pi’s GPIO pins are mapped into the system’s memory as a series of 3-bit segments stored within 32-bit status words, so my machine code subroutine has to do some bit bashing to set a GPIO pin as an input. Then my subroutine reads the relevant GPIO status word and passes it back to BASIC. (For my code, I combined some snippets from Bruce Smith’s book Raspberry Pi Assembly Language RISC OS Beginners and a Raspberry Pi online forum.) My BASIC program then simply uses a loop that calls the subroutine and looks for any changes in the status word, indicating a button press.

With the software written, all that was left to do was build a case (from a few dollars’ worth of basswood) and hook the video output up to an old monitor. And voilà! A new era of digitally driven excuses.

This article originally appeared in print as “Back to BASIC.”

Advertisement