An Arduino library for Peggy 2
Here we describe a basic-function Arduino library for Peggy 2.0. Note: This article describes the new 0.3b version of the library, dated 7/8/2008.
The Peggy 2.0 Arduino library 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 "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 latest version of the Arduino software (0015 or later) 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.") If you were using an older version of the Peggy2 library, archive and save your old version just in case-- some code changes are necessary to use the new version. 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. You need to select the right hardware from the Tools>Board menu. For most Peggy 2.0 kits, which use the ATmega168 microcontroller, select "Arduino Diecimila or Duemilanove w/ ATmega168" (the exact wording varies between versions of the Arduino software). If you have a newer Peggy 2.0 that shipped with an ATmega328 or if you have upgraded your Peggy to use the ATmega328, select "Arduino Duemilanove w/ ATmega328." (Peggy 2.0 is not an Arduino Diecimila or Duemilanove, 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_bounce
peggy2_16level
peggy2_Cube
peggy2_GetPoint
peggy2_Line
peggy2_MoveToLineTo
peggy2_static
peggy2_fewdots
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
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.RefreshAll(1);
while for a more complex arrangement, you might have
frame1.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.
Clear();
Example usage:
if (n > 100)
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.SetPoint(4,10);
See the code examples for additional usage demonstrations, particularly peggy2_firstdemo.
ClearPoint
Very much like SetPoint, above. Example usage, to turn off pixel located at (x,y) = (4, 10), in a frame buffer named Ginger:
Ginger.ClearPoint(4,10);
See the code examples for additional usage demonstrations.
WriteRow
This is a more advanced routine that may possibly be useful in some circumstances. It writes an entire row of data to the buffer at a time, where each bit from position 0 to 24 represents the LED position in the row. WriteRow is fast, but may not actually save time once you format your data to take advantage of that. Under most circumstances, it is more straightforward and reasonably efficient to use the WritePoint function instead. Example usage, to turn on pixel located at (x,y) = (4, 10), in a frame buffer named Ginger:
Ginger.WriteRow(10, 16);
Why? It's row 10, and the LED 4 is given by 2^4 = 16. Second example, to turn on pixels 3-18 in row number 10:
Ginger.WriteRow(10, (unsigned long) 65535 << 3);
WritePoint
If Value equals one (or is any nonzero value), then the point at coordinates (x,y) will be turned on in the buffer named BufferName. Otherwise-- if Value equals zero-- then the point at coordinates (x,y) will be turned off in the buffer named BufferName. A very powerful routine, since you can turn on or turn off a single point depending on the value of a variable or outcome of a comparison. Example usage, to turn on a pixel located at (x,y) = (2, 10), in a frame buffer named Ginger:
Ginger.WritePoint(2, 10, 1);
Second example, to turn off a pixel located at (x,y) = (4, 10):
Ginger.WritePoint(4, 10, 0);
GetPoint
Returns 1 if the pixel at coordinates (x,y) is on, or 0 if the pixel is off at coordinates (x,y) , in the buffer named BufferName. Example usage, to see if the pixel at (x,y) = (2, 10), in a frame buffer named Ginger, is on or off:
unsigned int j;
j = Ginger.GetPoint(2, 10);
Line
Draw a line from (x1,y1) to (x2,y2) in the buffer named BufferName. Example usage, to draw a line from (0,0) to (24,24), in a frame buffer named Ginger:
Ginger.Line(0,0,24,24);
Second example, to turn off a pixel located at (x,y) = (4, 10):
Ginger.WritePoint(4, 10, 0);
MoveTo and LineTo
Move (invisible) cursor to position from (x,y) in the buffer named BufferName. Used in conjunction with LineTo to make lines where you want them. Example usage, to draw a line from (0,0) to (24,24) and from (0,24) to (24,0), in a frame buffer named Ginger:
Ginger.MoveTo(0,0);
Ginger.LineTo(24,24);
Ginger.MoveTo(0,24);
Ginger.LineTo(24,0);
Drag line from current cursor position to new position. Update cursor position as well. Move cursor to position from (x,y) in the buffer named BufferName. Used in conjunction with MoveTo to make lines where you want them. Example usage, to draw a line from (0,0) to (24,24) and from (0,24) to (24,0), in a frame buffer named Ginger:
Ginger.MoveTo(0,0);
Ginger.LineTo(24,24);
Ginger.MoveTo(0,24);
Ginger.LineTo(24,0);
Caveats |





Story Options