Engduino v1.0
Files | Functions
Driver for Engduino LEDS

This is the driver code for LEDs on the Engduino These LEDS are not directly connected to pins on the AtMega32u4 processor. More...

Files

file  EngduinoLEDs.cpp
 

Engduino LED driver.


file  EngduinoLEDs.h
 

Engduino LED driver.


Functions

void writeLEDs (volatile uint8_t *value)
 This is an internal routine to set the LED latches appropriately.
 ISR (TIMER4_COMPA_vect)
 ISR routine for comparator A of TIMER 4.
 EngduinoLEDsClass::EngduinoLEDsClass ()
 Constructor.
void EngduinoLEDsClass::begin ()
 begin function - must be called before using other functions
void EngduinoLEDsClass::end ()
 end function - switch off the LEDs
void EngduinoLEDsClass::setLED (uint8_t LEDNumber, colour c)
 Set the colour of a single LED at maximum brightness.
void EngduinoLEDsClass::setLED (uint8_t LEDNumber, colour c, uint8_t brightness)
 Set an LED to a given colour/brightness.
void EngduinoLEDsClass::setLED (uint8_t LEDNumber, uint8_t r, uint8_t g, uint8_t b)
 Set an LED to a given point in the rgb space.
void EngduinoLEDsClass::setAll (colour c)
 Set the colour of all LEDs at maximum brightness.
void EngduinoLEDsClass::setAll (colour c, uint8_t brightness)
 Set all LEDs to a given colour/brightness.
void EngduinoLEDsClass::setAll (uint8_t r, uint8_t g, uint8_t b)
 Set all LEDs to a given point in the rgb space.
void EngduinoLEDsClass::setLEDs (colour c[16])
 Set the colour of all LEDs at maximum brightness from an array of individual values.
void EngduinoLEDsClass::setLEDs (colour colour[16], uint8_t brightness[16])
 Set all LEDs to a given colour/brightness from arrays of individual values.
void EngduinoLEDsClass::setLEDs (uint8_t r[16], uint8_t g[16], uint8_t b[16])
 Set all LEDs to a given point in the rgb space from arrays of individual values.
void EngduinoLEDsClass::setLEDs (uint8_t rgb[3][16])
 Set all LEDs to a given point in the rgb space a 2D array of individual values.
void EngduinoLEDsClass::setLEDs (uint8_t rgb[16][3])
 Set all LEDs to a given point in the rgb space a 2D array of individual values.

Detailed Description

This is the driver code for LEDs on the Engduino These LEDS are not directly connected to pins on the AtMega32u4 processor.

Instead they are connected through LED drivers so should only be accessed through this code.

The Engduino has 16 RGB LEDs on it, each of which can be controlled independently. To make the granularity of control greater, we implement a software PWM for each LED. This allows for 16 levels of brightness in each of the RGB channels on each LED with minimal flicker.

This implementation uses TIMER4 Comparator A to implement the PWM. The timer is set to run at ~320Hz. The clock is reset on interrupt, so you should be very wary about using the other comparators.


Function Documentation

void EngduinoLEDsClass::begin ( )

begin function - must be called before using other functions

The connection to the LEDs is not direct. Instead, it happens through three LED drivers - one each for the R, G, and B channels. These are connected through a daisy-chained SPI connection. There is a single latch line connected to all driver chips, which latches the value set by SPI when the line is pulsed high. The output from each driver chip can be switched on or off with a separate output enable line to each driver - when these are LOW the driver output lines go to the LEDs; when HIGH, they do not.

Definition at line 56 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::end ( )

end function - switch off the LEDs

We drive the output enables for all three drivers high. This disconnects the driver from the LEDs and so all subsequent changes will have no effect. We also stop the timer interrupt.

Definition at line 156 of file EngduinoLEDs.cpp.

EngduinoLEDsClass::EngduinoLEDsClass ( )

Constructor.

C++ constructor for this class. Empty.

Definition at line 39 of file EngduinoLEDs.cpp.

ISR ( TIMER4_COMPA_vect  )

ISR routine for comparator A of TIMER 4.

TIMER4 interrupt code ticks at ~320Hz, which means we can do 16 brightness levels at 20Hz, so flickering is invisible to the naked eye. This ISR implements the software PWM and calls the LED display function.

Because we must update the LEDs very frequently, and because it still takes a significant period to write all 3*16 values to the LED drivers, we need this code to run as fast as possible. We also need to ensure that we do not prevent other interrupts from occurring whilst we process this. To that end we stop interrupts for this ISR to prevent any nesting, reset the counter and re-enable interrupts generally before calling setting the LED values.

The PWM implementation relies on counting in units of the brightness on a channel and setting the apropriate colour channel on for an LED when the accumulated value overflows. This is slightly faster if the rgb values are in the range 0-255 than 0-15, because we can do it with a simple comparison.

This function then calls writeLEDs for each of the RGB values, in reverse order. The drivers are daisy chained and we need to shift the blue values to the far end of that chain before pulsing the latch. This written using low level C programming primitives of changing the voltages on ATMega32U4 pins rather than the Arduino digitalWrite equivalents. This has to be done in this way for speed - it is around 18 times faster than the alternative and, because we update the values rather frequently to avoid flickering, speed is important.

Notes: The PWM implementation will occasionally put one more dark period in than it should, but we can live with this for simplicity. PB7 is pin 12 on the ATMega32U4 and is attached to the LED LATCH

Definition at line 583 of file EngduinoLEDs.cpp.

References writeLEDs().

void EngduinoLEDsClass::setAll ( colour  c)

Set the colour of all LEDs at maximum brightness.

Parameters:
cColour, as chosen from the colour enum

Set the colour of all LEDs. The colour value here is chosen from an enum and corresponds to the primary and secondary colours of light, plus white and off.

Definition at line 339 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setAll ( colour  c,
uint8_t  brightness 
)

Set all LEDs to a given colour/brightness.

Parameters:
cColour, as chosen from the colour enum
brightnessA 0-MAX_BRIGHTNESS(=15) value

Set the colour of all LEDs. The colour value here is chosen from an enum and corresponds to the primary and secondary colours of light, plus white and off. Brightness ranges from 0 to MAX_BRIGHTNESS (currently 15). Choosing a brightness of zero will turn all LEDs off.

Definition at line 358 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setAll ( uint8_t  r,
uint8_t  g,
uint8_t  b 
)

Set all LEDs to a given point in the rgb space.

Parameters:
rBrightness of the red channel from 0-MAX_BRIGHTNESS (15)
gBrightness of the green channel from 0-MAX_BRIGHTNESS (15)
bBrightness of the blue channel from 0-MAX_BRIGHTNESS (15)

Set the colour/brightness of all LEDs to a point in rgb space. The brightness in each channel may range from 0 to MAX_BRIGHTNESS (currently 15).

Note: in the Engduino 1.0, we cannot use the full brightness for the LEDs when the colour white, or colours close to white are requested, simply because this causes too big a drain on current whilst the device is attached to the USB. This causes a system reset, which is both irritating and makes reprogramming rather tedious. So we limit the sum of the rgb values to a number we chose empirically, and we reduce the chosen rgb values until they sum to less than that chosen.

Definition at line 384 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLED ( uint8_t  LEDNumber,
colour  c 
)

Set the colour of a single LED at maximum brightness.

Parameters:
LEDNumberLED number, ranging from 1 (for LED1) to 16 (LED16)
cColour, as chosen from the colour enum

Set the colour of an LED. The colour value here is chosen from an enum and corresponds to the primary and secondary colours of light, plus white and off.

Definition at line 280 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLED ( uint8_t  LEDNumber,
colour  c,
uint8_t  brightness 
)

Set an LED to a given colour/brightness.

Parameters:
LEDNumberLED number, ranging from 1 (for LED1) to 16 (LED16)
cColour, as chosen from the colour enum
brightnessA 0-MAX_BRIGHTNESS(=15) value

Set the colour of an LED. The colour value here is chosen from an enum and corresponds to the primary and secondary colours of light, plus white and off. Brightness ranges from 0 to MAX_BRIGHTNESS (currently 15). Choosing a brightness of zero will turn an LED off.

Definition at line 299 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLED ( uint8_t  LEDNumber,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)

Set an LED to a given point in the rgb space.

Parameters:
LEDNumberLED number, ranging from 1 (for LED1) to 16 (LED16)
rBrightness of the red channel from 0-MAX_BRIGHTNESS (15)
gBrightness of the green channel from 0-MAX_BRIGHTNESS (15)
bBrightness of the blue channel from 0-MAX_BRIGHTNESS (15)

Set the colour/brightness of an LED to a point in rgb space. The brightness in each channel may range from 0 to MAX_BRIGHTNESS (currently 15).

Note: in the Engduino 1.0, we cannot use the full brightness for the LEDs when the colour white, or colours close to white are requested, simply because this causes too big a drain on current whilst the device is attached to the USB. This causes a system reset, which is both irritating and makes reprogramming rather tedious. So we limit the sum of the rgb values to a number we chose empirically, and we reduce the chosen rgb values until they sum to less than that chosen.

Definition at line 324 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLEDs ( colour  c[16])

Set the colour of all LEDs at maximum brightness from an array of individual values.

Parameters:
cArray of colour values, as chosen from the colour enum

Set the colour of all LEDs from an array of colour values - i.e. each LED can be set to a different colour, though all are at maximum brightness. The colour value here is chosen from an enum and corresponds to the primary and secondary colours of light, plus white and off.

Definition at line 402 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLEDs ( colour  c[16],
uint8_t  brightness[16] 
)

Set all LEDs to a given colour/brightness from arrays of individual values.

Parameters:
cArray of colour values, as chosen from the colour enum
brightnessArray of brightness values, from 0-MAX_BRIGHTNESS(=15)

Set the colour/brightness of all LEDs from two arrays of values - i.e. each LED can be set to a different colour/brightness value. The colour value here is chosen from an enum and corresponds to the primary and secondary colours of light, plus white and off. Brightness ranges from 0 to MAX_BRIGHTNESS (currently 15). Choosing a brightness of zero will turn an LED off.

Definition at line 424 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLEDs ( uint8_t  r[16],
uint8_t  g[16],
uint8_t  b[16] 
)

Set all LEDs to a given point in the rgb space from arrays of individual values.

Parameters:
rArray of red channel brightness, from 0-MAX_BRIGHTNESS (15)
gArray of green channel brightness, from 0-MAX_BRIGHTNESS (15)
bArray of blue channel brightness, from 0-MAX_BRIGHTNESS (15)

Set the colour/brightness of all LEDs to a point in rgb space from three arrays of values - i.e. each LED can be set to a different rgb point. The brightness in each channel may range from 0 to MAX_BRIGHTNESS (currently 15).

Note: in the Engduino 1.0, we cannot use the full brightness for the LEDs when the colour white, or colours close to white are requested, simply because this causes too big a drain on current whilst the device is attached to the USB. This causes a system reset, which is both irritating and makes reprogramming rather tedious. So we limit the sum of the rgb values to a number we chose empirically, and we reduce the chosen rgb values until they sum to less than that chosen.

Definition at line 452 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLEDs ( uint8_t  rgb[3][16])

Set all LEDs to a given point in the rgb space a 2D array of individual values.

The array is of form rgb[3][16]

Parameters:
rgb2D array of rgb brightnesses, from 0-MAX_BRIGHTNESS (15)

Set the colour/brightness of all LEDs to a point in rgb space from a 2D array values - i.e. each LED can be set to a different rgb point. The array is of form rgb[3][16] - i.e. the first index is the red/green/blue channel and the second is the LED (ranging from 0-15). The brightness in each channel may range from 0 to MAX_BRIGHTNESS (currently 15).

Note: in the Engduino 1.0, we cannot use the full brightness for the LEDs when the colour white, or colours close to white are requested, simply because this causes too big a drain on current whilst the device is attached to the USB. This causes a system reset, which is both irritating and makes reprogramming rather tedious. So we limit the sum of the rgb values to a number we chose empirically, and we reduce the chosen rgb values until they sum to less than that chosen.

Definition at line 479 of file EngduinoLEDs.cpp.

void EngduinoLEDsClass::setLEDs ( uint8_t  rgb[16][3])

Set all LEDs to a given point in the rgb space a 2D array of individual values.

The array is of form rgb[16][3]

Parameters:
rgb2D array of rgb brightnesses, from 0-MAX_BRIGHTNESS (15)

Set the colour/brightness of all LEDs to a point in rgb space from a 2D array values - i.e. each LED can be set to a different rgb point. The array is of form rgb[16][3] - i.e. the first index is the LED (ranging from 0-15). and the second is the red/green/blue channel. The brightness in each channel may range from 0 to MAX_BRIGHTNESS (currently 15).

Note: in the Engduino 1.0, we cannot use the full brightness for the LEDs when the colour white, or colours close to white are requested, simply because this causes too big a drain on current whilst the device is attached to the USB. This causes a system reset, which is both irritating and makes reprogramming rather tedious. So we limit the sum of the rgb values to a number we chose empirically, and we reduce the chosen rgb values until they sum to less than that chosen.

Definition at line 507 of file EngduinoLEDs.cpp.

void writeLEDs ( volatile uint8_t *  value) [inline]

This is an internal routine to set the LED latches appropriately.

Parameters:
valueThe on/off bits for each LED on a given channel

This function is written using low level C programming primitives for changing the voltages on ATMega32U4 pins rather than the Arduino digitalWrite equivalents. This has to be done in this way for speed - it is around 18 times faster than the alternative and, because we update the values rather frequently to avoid flickering, speed is important. This code manually emulates SPI functionality (mode 0), both for simplicity and speed

Notes: PB1 is pin 9 - SPI SCLK PB2 is pin 10 - SPI MOSI PB3 is pin 11 - SPI MISO

Definition at line 532 of file EngduinoLEDs.cpp.

Referenced by ISR().