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

Manage an array of LED's in a charlieplexed arrangement. More...

#include <Charlieplex.h>

Public Member Functions

 Charlieplex (const uint8_t *pins, uint8_t numPins)
 Constructs a new charliexplexing array where the output pins are specified by the numPins entries in pins. More...
 
 ~Charlieplex ()
 Destroys this charlieplexed array.
 
int count () const
 Returns the number of LED's in this charlieplexed array based on the number of pins. More...
 
bool led (int index) const
 Returns the value of the LED at index in the charplexed array; true if lit; false if not lit. More...
 
void setLed (int index, bool value)
 Sets the value of the LED at index in the charliplexed array. More...
 
uint8_t pwmLed (int index) const
 Returns the PWM value of the LED at index in the charplexed array; between 0 and 255. More...
 
void setPwmLed (int index, uint8_t value)
 Sets the PWM value of the LED at index in the charliplexed array; between 0 and 255. More...
 
unsigned long holdTime () const
 Returns the number of microseconds that each LED should be held on for before moving onto the next in loop(). More...
 
void setHoldTime (unsigned long us)
 Sets the number of microseconds that each LED should be held on for before moving onto the next in loop() to us. More...
 
void loop ()
 Runs the multiplexing loop, to display the LED states on the charlieplexed array. More...
 
void refresh ()
 Refreshes the charlieplexed array by advancing to the next LED that needs to be lit. More...
 

Detailed Description

Manage an array of LED's in a charlieplexed arrangement.

Charlieplexing is a technique for multiplexing large numbers of LED's on a small number of microcontroller output pins. LED's are arranged in complementary pairs; the simplest being for two output pins:

charlieplex2pin.png

When Pin1 is 1 and Pin2 is 0, LED1 will be lit. When Pin1 is 0 and Pin2 is 1, then LED2 will be lit. The technique extends to 3 pins as follows:

charlieplex3pin.png

In this case, LED5 is lit when Pin1 is 1, Pin3 is 0, and Pin2 is set to a high-impedance input to "disconnect" it.

Charlieplex presents a simple array of led() values that indicate whether each LED is on, off, or in an intermediate PWM state (if setPwmLed() is used). The application must call loop() or refresh() on a regular basis to ensure that the multiplexed display is kept up to date. The following example drives 6 LED's connected to the output pins D9, D10, and D11:

#include <Charlieplex.h>
byte pins[3] = {9, 10, 11};
Charlieplex charlie(pins, sizeof(pins));
void setup() {
charlie.setLed(0, true); // Turn on LED1
charlie.setLed(3, true); // Turn on LED4
charlie.setPwmLed(5, 64); // Set LED6 to one-quarter on
}
void loop() {
charlie.loop();
}

The following diagram extends the circuit for 4 output pins and 12 LED's:

charlieplex4pin.png

The following diagram extends the circuit for 5 output pins and 20 LED's:

charlieplex5pin.png

Circuits for higher numbers of LED's get increasingly complex. For those cases it can be easier to use traditional multiplexing matrix arrangements and shift registers. The DMD class does this for a specific kind of large dot matrix display. Otherwise, use the following pseudocode to determine how to connect the LED's for higher numbers of pins:

n = 1
for Pass = 1 to NumPins-1:
for Pin = 1 to NumPins-Pass:
LED[n] is connected between Pin (anode) and Pin+Pass (cathode)
LED[n+1] is connected between Pin+Pass (anode) and Pin (cathode)
n = n + 2

Note: while the above circuit diagrams and psuedocode use 1-based numbering for LED's, Charlieplex uses 0-based numbering in the led(), setLed(), pwmLed(), and setPwmLed() functions.

It isn't necessary to wire up all LED's. If you only need 10 LED's, then use the 4-output circuit and omit LED11 and LED12. Charlieplex only drives LED's that are lit; LED's that are unlit or unused will be skipped during the refresh scan. The maximum number of LED's that that can be driven by a specific number of pins is given by the following table:

Number of PinsNumber of LED's
22
36
412
520
630
742
856
972
1090
nn * (n - 1)

Definition at line 28 of file Charlieplex.h.

Constructor & Destructor Documentation

Charlieplex::Charlieplex ( const uint8_t *  pins,
uint8_t  numPins 
)

Constructs a new charliexplexing array where the output pins are specified by the numPins entries in pins.

Note: numPins must be 2 or greater for correct operation.

See Also
count(), setLed()

Definition at line 121 of file Charlieplex.cpp.

Member Function Documentation

int Charlieplex::count ( ) const
inline

Returns the number of LED's in this charlieplexed array based on the number of pins.

Number of PinsNumber of LED's
22
36
412
520
630
742
856
972
1090
nn * (n - 1)
See Also
led()

Definition at line 34 of file Charlieplex.h.

unsigned long Charlieplex::holdTime ( ) const
inline

Returns the number of microseconds that each LED should be held on for before moving onto the next in loop().

The default value is calculated so that all LED's can be refreshed with a rate of at least 200 Hz, which is necessary for handling PWM output on multiple LED's. The less LED's that are lit at once, the faster the display will refresh.

See Also
setHoldTime(), loop()

Definition at line 42 of file Charlieplex.h.

bool Charlieplex::led ( int  index) const
inline

Returns the value of the LED at index in the charplexed array; true if lit; false if not lit.

If the LED is displaying a PWM value, then this function will return true for any non-zero PWM value.

See Also
setLed(), pwmLed()

Definition at line 36 of file Charlieplex.h.

void Charlieplex::loop ( )

Runs the multiplexing loop, to display the LED states on the charlieplexed array.

If holdTime() microseconds have elapsed since the last call to loop(), then the current LED is turned off and the next LED that needs to be lit is turned on.

LED's that do not need to be lit are skipped. The total time for a single pass through all lit LED's may be very short if only a few LED's are lit at once. If all LED's are lit, then the total time for a single pass will be count() * holdTime() microseconds.

If the application is using timer interrupts to drive the multiplexing process, then use refresh() instead of loop().

See Also
led(), pwmLed(), holdTime(), refresh()

Definition at line 277 of file Charlieplex.cpp.

uint8_t Charlieplex::pwmLed ( int  index) const
inline

Returns the PWM value of the LED at index in the charplexed array; between 0 and 255.

See Also
setPwmLed(), led()

Definition at line 39 of file Charlieplex.h.

void Charlieplex::refresh ( )

Refreshes the charlieplexed array by advancing to the next LED that needs to be lit.

This function is intended to be called from a timer interrupt service routine to advance the multiplexing state without the main application having to explicitly call loop().

See Also
loop()

Definition at line 296 of file Charlieplex.cpp.

void Charlieplex::setHoldTime ( unsigned long  us)
inline

Sets the number of microseconds that each LED should be held on for before moving onto the next in loop() to us.

See Also
holdTime(), loop()

Definition at line 43 of file Charlieplex.h.

void Charlieplex::setLed ( int  index,
bool  value 
)
inline

Sets the value of the LED at index in the charliplexed array.

The brightness of the LED will be proportional to the number of LED's that are currently lit, as the holdTime() refresh rate will cause the LED to appear to dim; the more LED's that are lit the less overall time each individual LED is held on. For best results, only a single LED should be lit at once or higher-brightness LED's should be used.

See Also
led(), setPwmLed()

Definition at line 37 of file Charlieplex.h.

void Charlieplex::setPwmLed ( int  index,
uint8_t  value 
)
inline

Sets the PWM value of the LED at index in the charliplexed array; between 0 and 255.

If this function is used, then it is assumed that the output pins are capable of PWM output.

The PWM-specified brightness of the LED will also be affected to the number of LED's that are currently lit, as the holdTime() refresh rate will cause the LED to appear to dim; the more LED's that are lit the less overall time each individual LED is held on. For best results, only a single LED should be lit at once or higher-brightness LED's should be used.

See Also
pwmLed(), setLed()

Definition at line 40 of file Charlieplex.h.


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