![]()
Note: This article is out of date but archived here for future reference.
Today we present a basic-function Arduino library for Peggy 2.0. This brings Peggy 2.0 up to the level of having useful compatibility with the Arduino software environment: you can use high-level commands to control what shows up on the Peggy display. For example, the type of high-level command might be something of the form "Peggy_SetPoint(x, y)" which would turn on the LED located at position (x,y) in the grid. |
|
As we have discussed earlier, Peggy 2.0 is Arduino compatible in the sense that supports programming through a USB-TTL cable, using the popular Arduino software environment.
Note 1: You can also program Peggy 2.0 in the Arduino environment without the USB-TTL cable if you have an AVR ISP compatible programmer like the USBtinyISP. See this note on how to make that work. First things first: This is not (by any stretch) intended as a full Arduino tutorial; there are plenty of very good examples out there already, and other lists of resources to point you in the right direction. Still, if you have done some programming, this may well be enough of a guide to get you up and running. Let's get started. Hardware:
Software: First, install the Arduino software if you don't already have it. Uncompress the Peggy2 library, and place it in the hard/ware/library/ directory where your Arduino software is installed. (If you've put it in the right place, it will be amongst a few other libraries in similar folders there, with names like "EEPROM" and "Wire.") Now open up the Arduino program itself. From the menu, select File>Sketchbook>Examples>Library-Peggy2>peggy2_firstdemo. Click the "Verify" button to compile the program. (It's the one with the "play" symbol at the upper left of the window.) In a few moments, it should indicate that it's done compiling. Now, to hook up to the Peggy. If using the USB-TTL cable, it hooks up to your computer and to the connector (J3) on the left side of the Peggy, by the chips. Note that the wires on the cable are color-coded: the end where the green wire goes is marked "green" on the circuit board.
Next, we'll actually send the demo program to the board. From the menu, make sure that Tools>Board>Arduino Diecimila is selected. (Peggy 2.0 is not a Diecimila, but the architecture is compatible.)
Then, to actually program the board, press the "Upload to I/O Board" button at the top of the Arduino program window (it's the other "right arrow" button). If it works, you may see Peggy do some erratic things while the program is being uploaded, typically about 15 seconds, and then it will start to run the program. If it does not work, make sure that Peggy has power and is turned on. You may also wish to play with the serial port selection under the Tools menu.
Example programs
peggy2_firstdemo
peggy2_FrameAnim
peggy2_FrameGray
peggy2_minimal
Gory Details::Working with the library
Importing the library #include <Peggy2.h>
You can add this line semi-automatically by selecting it from the menu: Sketch>Import Library>Peggy2
Creating a frame buffer If we're doing an animation that has four frames that we switch between, we might want to use four frame buffers. Or, if we're drawing live on the screen, we might only want one frame buffer, which we could keep drawing in. In any case, you can have one or several different frame buffers (limited by the amount of available RAM), each of which needs to have a different name. Normally, frame buffers are declared (created) at the beginning of the program, right after the #includes. A new frame buffer is declared by using the keyword "Peggy2." To make a new frame buffer named Fred, we would call Peggy2 Fred;
If you wanted four frame buffers (named frame1, frame2, frame3, and frame4), you could create them like so: Peggy2 frame1;
Hardware initialization
The Your
void setup()
That's it for initialization: make (at least) one frame buffer and do the hardware initialization. What remains are functions and procedures that can be executed at your discretion later in the
Peggy_RefreshAll
which takes an (unsigned) integer argument that says how many times to draw it on the screen. For a fast, single update of your frame buffer named Fred, you might put the following in your
Fred.Peggy_RefreshAll(1);
while for a more complex arrangement, you might have
frame1.Peggy_RefreshAll(2);
which would draw both frames frame1 and frame2 on the Peggy. By the persistence of vision effect, you would see both frames frame1 and frame2 at the same time, drawn on top of each other. And, since frame2 is drawn for longer, (with the same brightness) it would appear brighter than the image in frame1.
Peggy_Clear(); Example usage:
if (n > 100)
Peggy_SetPoint As before, this function acts on a specific frame buffer. The (x,y) coordinates are the usual type for computer graphics: origin in the upper left corner, with column/row location given by the ordered pair of unsigned integers, which should in this case be in the range 0-24. Example usage, to turn on pixel located at (x,y) = (4, 10), in a frame buffer named Ginger:
Ginger.Peggy_SetPoint(4,10);
See the code examples for additional usage demonstrations, particularly peggy2_firstdemo.
Peggy_ClearPoint Very much like Peggy_SetPoint, above. Example usage, to turn off pixel located at (x,y) = (4, 10), in a frame buffer named Ginger:
Ginger.Peggy_ClearPoint(4,10);
See the code examples for additional usage demonstrations.
Caveats |
Evil Mad Scientist Laboratories
http://www.evilmadscientist.com/article.php/ardlibarchive