ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Static Public Member Functions | List of all members
DMD Class Reference

Handle large dot matrix displays composed of LED's. More...

#include <DMD.h>

Inheritance diagram for DMD:
Bitmap

Public Member Functions

 DMD (int widthPanels=1, int heightPanels=1)
 Constructs a new dot matrix display handler for a display that is widthPanels x heightPanels in size. More...
 
 ~DMD ()
 Destroys this dot matrix display handler.
 
bool doubleBuffer () const
 Returns true if the display is double-buffered; false if single-buffered. The default is false. More...
 
void setDoubleBuffer (bool doubleBuffer)
 Enables or disables double-buffering according to doubleBuffer. More...
 
void swapBuffers ()
 Swaps the buffers that are used for rendering to the display. More...
 
void swapBuffersAndCopy ()
 Swaps the buffers that are used for rendering to the display and copies the former back buffer contents to the new back buffer. More...
 
void loop ()
 Performs regular display refresh activities from the application's main loop. More...
 
void refresh ()
 Refresh the display. More...
 
void enableTimer1 ()
 Enables Timer1 overflow interrupts for updating this display. More...
 
void disableTimer1 ()
 Disables Timer1 overflow interrupts. More...
 
void enableTimer2 ()
 Enables Timer2 overflow interrupts for updating this display. More...
 
void disableTimer2 ()
 Disables Timer2 overflow interrupts. More...
 
- Public Member Functions inherited from Bitmap
 Bitmap (int width, int height)
 Constructs a new in-memory bitmap that is width x height pixels in size. More...
 
 ~Bitmap ()
 Destroys this bitmap.
 
bool isValid () const
 Returns true if the memory for this bitmap is valid; false otherwise. More...
 
int width () const
 Returns the width of the bitmap in pixels. More...
 
int height () const
 Returns the height of the bitmap in pixels. More...
 
int stride () const
 Returns the number of bytes in each line of the bitmap's data() buffer. More...
 
int bitsPerPixel () const
 Returns the number of bits per pixel for the bitmap; always 1. More...
 
uint8_t * data ()
 Returns a pointer to the start of the bitmap's data buffer. More...
 
const uint8_t * data () const
 Returns a constant pointer to the start of the bitmap's data buffer. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void clear (Color color=Black)
 Clears the entire bitmap to the specified color. More...
 
Color pixel (int x, int y) const
 Returns the color of the pixel at (x, y); either Black or White. More...
 
void setPixel (int x, int y, Color color)
 Sets the pixel at (x, y) to color. More...
 
void drawLine (int x1, int y1, int x2, int y2, Color color=White)
 Draws a line from (x1, y1) to (x2, y2) in color. More...
 
void drawRect (int x1, int y1, int x2, int y2, Color borderColor=White, Color fillColor=NoFill)
 Draws a rectangle from (x1, y1) to (x2, y2), with the outline in borderColor and the interior filled with fillColor. More...
 
void drawFilledRect (int x1, int y1, int x2, int y2, Color color=White)
 Draws a filled rectangle from (x1, y1) to (x2, y2) in color. More...
 
void drawCircle (int centerX, int centerY, int radius, Color borderColor=White, Color fillColor=NoFill)
 Draws a circle with a specific center (centerX, centerY) and radius, with the outline in borderColor and the interior filled with fillColor. More...
 
void drawFilledCircle (int centerX, int centerY, int radius, Color color=White)
 Draws a filled circle with a specific center (centerX, centerY) and radius in color. More...
 
void drawBitmap (int x, int y, const Bitmap &bitmap, Color color=White)
 Draws bitmap at (x, y) in color. More...
 
void drawBitmap (int x, int y, Bitmap::ProgMem bitmap, Color color=White)
 Draws bitmap at (x, y) in color. More...
 
void drawInvertedBitmap (int x, int y, const Bitmap &bitmap)
 Draws bitmap at (x, y) in inverted colors. More...
 
void drawInvertedBitmap (int x, int y, Bitmap::ProgMem bitmap)
 Draws bitmap at (x, y) in inverted colors. More...
 
Font font () const
 Returns the currently selected font, or null if none selected. More...
 
void setFont (Font font)
 Sets the font for use with drawText() and drawChar(). More...
 
Color textColor () const
 Returns the color that will be used for drawing text with drawText() and drawChar(). The default is White. More...
 
void setTextColor (Color color)
 Sets the color that will be used for drawing text with drawText() and drawChar(). More...
 
void drawText (int x, int y, const char *str, int len=-1)
 Draws the len characters of str at (x, y). More...
 
void drawText (int x, int y, const String &str, int start=0, int len=-1)
 Draws len characters starting at start from str to the screen at (x, y). More...
 
int drawChar (int x, int y, char ch)
 Draws a single character ch at (x, y). More...
 
int charWidth (char ch) const
 Returns the width in pixels of ch in the current font(). More...
 
int textWidth (const char *str, int len=-1) const
 Returns the width in pixels of the len characters of str in the current font(), including inter-character spacing. More...
 
int textWidth (const String &str, int start=0, int len=-1) const
 Returns the width in pixels of the len characters of str in the current font(), starting at start, including inter-character spacing. More...
 
int textHeight () const
 Returns the height in pixels of the current text drawing font(); or zero if font() is not set. More...
 
void copy (int x, int y, int width, int height, Bitmap *dest, int destX, int destY)
 Copies the width x height pixels starting at top-left corner (x, y) to (destX, destY) in the bitmap dest. More...
 
void fill (int x, int y, int width, int height, Color color)
 Fills the width x height pixels starting at top-left corner (x, y) with color. More...
 
void fill (int x, int y, int width, int height, Bitmap::ProgMem pattern, Color color=White)
 Fills the width x height pixels starting at top-left corner (x, y) with the contents of pattern. More...
 
void scroll (int dx, int dy, Color fillColor=Black)
 Scrolls the entire contents of the bitmap by dx and dy. More...
 
void scroll (int x, int y, int width, int height, int dx, int dy, Color fillColor=Black)
 Scrolls the width x height pixels starting at top-left corner (x, y) by dx and dy. More...
 
void invert (int x, int y, int width, int height)
 Inverts the width x height pixels starting at top-left corner (x, y). More...
 

Static Public Member Functions

static Color fromRGB (uint8_t r, uint8_t g, uint8_t b)
 Converts an RGB value into a pixel color value. More...
 

Additional Inherited Members

- Public Types inherited from Bitmap
typedef uint8_t Color
 Type that represents the color of a pixel in a bitmap. More...
 
typedef PGM_VOID_P ProgMem
 Type that represents a bitmap within program memory.
 
typedef PGM_VOID_P Font
 Type that represents a font within program memory.
 
- Static Public Attributes inherited from Bitmap
static const Color Black = 0
 Color value corresponding to "black".
 
static const Color White = 1
 Color value corresponding to "white". If the bitmap is displayed on a LED array, then it may have a different physical color. More...
 
static const Color NoFill = 2
 Special color value that is used with drawRect() and drawCircle() to indicate that the interior of the shape should not be filled. For all other uses, NoFill is equivalent to White.
 

Detailed Description

Handle large dot matrix displays composed of LED's.

This class is designed for use with Freetronics Large Dot Matrix Displays. These displays have 512 LED's arranged in a 32x16 matrix and controlled by an SPI interface. The displays are available in red, blue, green, yellow, and white variations (for which this class always uses the constant White regardless of the physical color).

Drawing

DMD inherits from Bitmap so that any of the drawing functions in that class can be used to draw directly to dot matrix displays. The following example initializes a single display panel and draws a rectangle and a circle into it at setup time:

#include <DMD.h>
DMD display;
void setup() {
display.drawRect(5, 2, 27, 13);
display.drawCircle(16, 8, 4);
}

The display must be updated frequently from the application's main loop:

void loop() {
display.loop();
}

Interrupt-driven display refresh

The loop() method simplifies updating the display from the application's main loop but it can sometimes be inconvenient to arrange for it to be called regularly, especially if the application wishes to use delay() or delayMicroseconds().

DMD provides an asynchronous display update mechanism using Timer1 interrupts. The application turns on interrupts using enableTimer1() and then calls refresh() from the interrupt service routine:

#include <DMD.h>
DMD display;
ISR(TIMER1_OVF_vect)
{
display.refresh();
}
void setup() {
display.enableTimer1();
}

If Timer1 is already in use by some other part of your application, then Timer2 can be used as an alternative interrupt source:

#include <DMD.h>
DMD display;
ISR(TIMER2_OVF_vect)
{
display.refresh();
}
void setup() {
display.enableTimer2();
}

DMD can also be used with third-party timer libraries such as TimerOne:

#include <DMD.h>
#include <TimerOne.h>
DMD display;
void refreshDisplay()
{
display.refresh();
}
void setup() {
Timer1.initialize(5000);
Timer1.attachInterrupt(refreshDisplay);
}

Double buffering

When using interrupts, the system can sometimes exhibit "tearing" artifacts where half-finished images are displayed because an interrupt fired in the middle of a screen update.

This problem can be alleviated using double buffering: all rendering is done to an off-screen buffer that is swapped onto the screen once it is ready for display. Rendering then switches to the other buffer that is now off-screen. The following example demonstrates this:

#include <DMD.h>
DMD display;
ISR(TIMER1_OVF_vect)
{
display.refresh();
}
void setup() {
display.setDoubleBuffer(true);
display.enableTimer1();
}
void loop() {
updateDisplay();
display.swapBuffers();
delay(50); // Delay between frames.
}
void updateDisplay() {
// Draw the new display contents into the off-screen buffer.
display.clear();
...
}

The downside of double buffering is that it uses twice as much main memory to manage the contents of the screen.

Multiple panels

Multiple panels can be daisy-chained together using ribbon cables. If there is a single row of panels, then they must be connected to the Arduino board as follows:

dmd-4x1.png

If there are multiple rows of panels, then alternating rows are flipped upside-down so that the short ribbon cables provided by Freetronics reach (this technique is thanks to Chris Debenham; see http://www.adebenham.com/category/arduino/dmd/ for more details):

dmd-4x2.png

This technique can be repeated for as many rows as required, with the bottom row always right-way-up:

dmd-4x3.png

DMD automatically takes care of flipping the data for panels in the alternating rows. No special action is required by the user except to physically connect the panels as shown and to initialize the DMD class appropriately:

#include <DMD.h>
DMD display(4, 2); // 4 panels wide, 2 panels high

Definition at line 28 of file DMD.h.

Constructor & Destructor Documentation

DMD::DMD ( int  widthPanels = 1,
int  heightPanels = 1 
)
explicit

Constructs a new dot matrix display handler for a display that is widthPanels x heightPanels in size.

Note: the parameters to this constructor are specified in panels, whereas width() and height() are specified in pixels.

See Also
width(), height()

Definition at line 237 of file DMD.cpp.

Member Function Documentation

void DMD::disableTimer1 ( )

Disables Timer1 overflow interrupts.

See Also
enableTimer1()

Definition at line 614 of file DMD.cpp.

void DMD::disableTimer2 ( )

Disables Timer2 overflow interrupts.

See Also
enableTimer2()

Definition at line 674 of file DMD.cpp.

bool DMD::doubleBuffer ( ) const
inline

Returns true if the display is double-buffered; false if single-buffered. The default is false.

See Also
setDoubleBuffer(), swapBuffers(), refresh()

Definition at line 34 of file DMD.h.

void DMD::enableTimer1 ( )

Enables Timer1 overflow interrupts for updating this display.

The application must also provide an interrupt service routine for Timer1 that calls refresh():

#include <DMD.h>
DMD display;
ISR(TIMER1_OVF_vect)
{
display.refresh();
}
void setup() {
display.enableTimer1();
}

If timer interrupts are being used to update the display, then it is unnecessary to call loop().

See Also
refresh(), disableTimer1(), enableTimer2(), setDoubleBuffer()

Definition at line 563 of file DMD.cpp.

void DMD::enableTimer2 ( )

Enables Timer2 overflow interrupts for updating this display.

The application must also provide an interrupt service routine for Timer2 that calls refresh():

#include <DMD.h>
DMD display;
ISR(TIMER2_OVF_vect)
{
display.refresh();
}
void setup() {
display.enableTimer2();
}

If timer interrupts are being used to update the display, then it is unnecessary to call loop().

See Also
refresh(), disableTimer2(), enableTimer1(), setDoubleBuffer()

Definition at line 646 of file DMD.cpp.

DMD::Color DMD::fromRGB ( uint8_t  r,
uint8_t  g,
uint8_t  b 
)
static

Converts an RGB value into a pixel color value.

Returns White if any of r, g, or b are non-zero; otherwise returns Black.

This function is provided for upwards compatibility with future displays that support full color. Monochrome applications should use the Black and White constants directly.

Definition at line 690 of file DMD.cpp.

void DMD::loop ( )

Performs regular display refresh activities from the application's main loop.

DMD display;
void loop() {
display.loop();
}

If you are using a timer interrupt service routine, then call refresh() in response to the interrupt instead of calling loop().

See Also
refresh()

Definition at line 420 of file DMD.cpp.

void DMD::refresh ( )

Refresh the display.

This function must be called at least once every 5 milliseconds for smooth non-flickering update of the display. It is usually called by loop(), but can also be called in response to a timer interrupt.

If this function is called from an interrupt service routine, then it is recommended that double-buffering be enabled with setDoubleBuffer() to prevent "tearing" artifacts that result from simultaneous update of a single shared buffer.

See Also
loop(), setDoubleBuffer(), enableTimer1()

Definition at line 478 of file DMD.cpp.

void DMD::setDoubleBuffer ( bool  doubleBuffer)

Enables or disables double-buffering according to doubleBuffer.

When double-buffering is enabled, rendering operations are sent to a memory buffer that isn't currently displayed on-screen. Once the application has completed the screen update, it calls swapBuffers() to display the current buffer and switch rendering to the other now invisible buffer.

Double-buffering is recommended if refresh() is being called from an interrupt service routine, to prevent "tearing" artifacts that result from simultaneous update of a single shared buffer.

This function will allocate memory for the extra buffer when doubleBuffer is true. If there is insufficient memory for the second screen buffer, then this class will revert to single-buffered mode.

See Also
doubleBuffer(), swapBuffers(), refresh()

Definition at line 314 of file DMD.cpp.

void DMD::swapBuffers ( )

Swaps the buffers that are used for rendering to the display.

When doubleBuffer() is false, this function does nothing. Otherwise the front and back rendering buffers are swapped. See the description of setDoubleBuffer() for more information.

The new rendering back buffer will have undefined contents and will probably need to be re-inialized with clear() or fill() before drawing to it. The swapBuffersAndCopy() function can be used instead to preserve the screen contents from one frame to the next.

See Also
swapBuffersAndCopy(), setDoubleBuffer()

Definition at line 363 of file DMD.cpp.

void DMD::swapBuffersAndCopy ( )

Swaps the buffers that are used for rendering to the display and copies the former back buffer contents to the new back buffer.

Normally when swapBuffers() is called, the new rendering back buffer will have undefined contents from two frames prior and must be cleared with clear() or fill() before writing new contents to it. This function instead copies the previous frame into the new rendering buffer so that it can be updated in-place.

This function is useful if the screen does not change much from one frame to the next. If the screen changes a lot between frames, then it is usually better to explicitly clear() or fill() the new back buffer.

See Also
swapBuffers(), setDoubleBuffer()

Definition at line 396 of file DMD.cpp.


The documentation for this class was generated from the following files: