Programming Meggy Jr RGB: How to Get Started
- Download Arduino 0015 (or newer) and install.
- Download latest Meggy Jr RGB Arduino Library from here.
- Unzip the library, and place it…
[For Arduino 16 or older] in a sub-directory of the hardware/libraries sub-directory of the Arduino application directory. T
[For Arduino 17 or newer], in the “libraries” subdirectory of your sketch folder. (See this blog post for additional details.)
- Launch the Arduino environment; you should see the library in the Sketch>Import Library menu.
- Make sure that the correct menu option under Tools>Board> is selected. Board type should be Duemilanove (or Diecimila) with ATmega168 or with ATmega328P selected, depending on which your kit came with.
- Load an example program from your menu: File>Sketchbook>Examples>Library-MeggyJr>MeggyJr_MeggyBrite
- Verify (compile) the program by pressing the Verify button in the upper-left of the Arduino window; it has the “play” symbol, a right facing triangle.
- Connect your USB-TTL cable to your computer and Meggy Jr. The black and green ends of the Meggy side connector are labeled on the circuit board.
- To program Meggy Jr, press the “Upload to I/O Board” button at the top of the Arduino program window (the other “right arrow”). It typically takes about 15 seconds.
One of the demo programs included with the library, MeggyJr_MeggyBrite is a pixel art drawing program that shows off simple functions that make drawing pixels and making sounds surprisingly easy. These functions are documented in the programming guide.
To go forward, try out the other demo programs too and look through their code. Start small, by modifying these programs to make them still work but do different things. Once you understand that basic process, you’re good to go!
The Meggy Jr Library
The function of the Meggy Jr Library is to provide a software interface to the Meggy Jr RGB hardware. One of the things that it does is to allocate a chunk of the AVR microcontroller’s RAM to act as Display Memory, analogous to the video memory in a desktop computer system. The Display Memory is a sizeable array of data that fully describes the state of all 200 LEDs on the Meggy Jr RGB. The LED display is constantly redrawn at a rate of 150 times per second, fully reading out the contents of that Display Memory and using it to control the LEDs.
The Meggy Jr Library provides interface calls to directly set and read values in the Display Memory. While this can give you great control over the LEDs, it can also involve unwieldy data manipulation. In most cases the Meggy Jr Simplified Library is a much better place to start, so we will save discussion of the Display Memory for later.
The Meggy Jr Simplified Library
The Meggy Jr Simplified Library (MJSL) is a set of macros and functions on top of the Meggy Jr Library that let you get started quickly without
deep knowledge of the Display Memory or how the hardware works.
It also removes the excess complexity that is normally
associated with libraries in the Arduino environment, so you can just use the darned thing. And it really works well.
(Note: the MJSL is *fully compatible* with
the main library, since it is built on top of that. Any of the main library functions
can be made to work here as well, however it is usually not necessary to do so.)
Under the hood, the Meggy Jr Simplified Library uses the (full) Meggy Jr
Library, with its Display Memory and internal functions,
to constantly refresh the screen at 150 Hz.
The Simplified Library also adds an additional memory
array that makes drawing to the LED display more intuitive. Besides the Display Memory already mentioned,
the MJSL uses a second off-screen drawing space called the “Game Slate,” where we actually perform the drawing.
(You don’t have to use it for games, that’s just the name of it.) The
Game Slate is an 8×8 array of memory locations– one for each
pixel on the LED display– and in each position we store the name of a pre-defined color that will be drawn in the corresponding pixel. Because the Game Slate is not part of the actual Display Memory, we can take our time drawing a new image without affecting what appears on the LED display. When we’re finished drawing, we copy the Game Slate data to the Display Memory, looking up the color definitions in a look-up table.
For complete documentation, please see the Meggy Jr RGB Programing guide, which you can download here as a 600 kB PDF file.