2014年11月17日 星期一

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

Many years ago in the offices here at IEEE Spectrum, we had a “Wheel of Excuses” pinned to the outside wall of a embedded computer cubicle. So I turned to the US $35 Raspberry Pi single board computer, 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.

photo of Model B+ RaspberryPi
The Model B+ Raspberry Pi has an upgraded version of the I/O hardware in the Model B. RasPiO breakout board Using a RasPiO breakout board, I connected a button to the 40-pin GPIO header. screenshot of presented excuse Button presses generate excuses, which appear on a monitor attached via an HDMI cable. Old-school BBC Micro users will note my use of text mode 7, which supports Teletext commands for things like displaying double-height characters.

The Pi was first released in 2012 as a “spiritual successor” to the BBC Microcomputer System, which was created by Acorn single board computer 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 embedded computer coprocessor for BBC Micros, and it later powered the Archimedes line of PCs. The embedded sbc 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 embedded sbc 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 single board computer 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 embedded computer 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 embedded computer 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 embedded sbc 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 voila! A new era of digitally driven excuses.

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

refer to: 
http://spectrum.ieee.org/geek-life/hands-on/create-a-wheel-of-excuses-with-basic-and-the-new-raspberry-pi

沒有留言:

張貼留言