LED bus driver on any GPIO pin for addressable RGB LEDs (like NeoPixels or other WS2812 based LEDs)
Revision 3:67e68c46daef, committed 2017-06-14
- Comitter:
- koengroener
- Date:
- Wed Jun 14 20:37:31 2017 +0000
- Parent:
- 2:735bb1b9cfc2
- Commit message:
- Added LEDArray class, for a more simplistic way of communcation with the LED strip
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LEDArray.cpp Wed Jun 14 20:37:31 2017 +0000 @@ -0,0 +1,42 @@ +#include "LEDArray.h" + +LEDArray::LEDArray(PinName wirePin, ColorByteOrder byteOrder, unsigned int numberOfLEDs, float t0h_us, float t0l_us, float t1h_us, float t1l_us, float tReset_us) : _ledBus(wirePin, byteOrder, t0h_us, t0l_us, t1h_us, t1l_us, tReset_us) +{ + initialize(numberOfLEDs); +} + +LEDArray::LEDArray(PinName wirePin, ColorByteOrder byteOrder, unsigned int numberOfLEDs) : _ledBus(wirePin, byteOrder, 0.35, 0.8, 0.7, 0.6, 50) +{ + initialize(numberOfLEDs); +} + +LEDArray::~LEDArray() +{ +} + +void LEDArray::initialize(unsigned int numberOfLEDs) +{ + _numberOfLEDs = numberOfLEDs; + _leds = new Color*[_numberOfLEDs]; + for(uint32_t i = 0; i < _numberOfLEDs; ++i) + { + _leds[i] = new Color(0,0,0); + } +} + +void LEDArray::setPixelColor(unsigned int pixel, uint8_t r, uint8_t g, uint8_t b) +{ + _leds[pixel]->red = r; + _leds[pixel]->green = g; + _leds[pixel]->blue = b; +} + +void LEDArray::setPixelColor(unsigned int pixel, Color& color) +{ + setPixelColor(pixel, color.red, color.green, color.blue); +} + +void LEDArray::show() +{ + _ledBus.write(_leds, _numberOfLEDs); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LEDArray.h Wed Jun 14 20:37:31 2017 +0000 @@ -0,0 +1,70 @@ +#ifndef _LED_ARRAY_PIN_H_ +#define _LED_ARRAY_PIN_H_ + +#include "mbed.h" +#include "LEDBus.h" + +/** + The LEDArray class allows for a bit more simplistic way of communication with an addressable LED strip +*/ +class LEDArray +{ + +private: + LEDBus _ledBus; + Color** _leds; + unsigned int _numberOfLEDs; + + void initialize(unsigned int numberOfLEDs); +protected: + +public: + + + /** + * Initializes the addressable led strip + * + * @param wirePin - The output pin on wich the addressable leds are connected + * @param byteOrder - The order in wich the r, g and b bytes are expected + * @param numberOfLEDs - The number of leds in this strip + * @param t0h_us - T0H as found in the addressable led datasheet. The duration, in microseconds, the pin will stay high for sending a 0 bit + * @param t0l_us - T0L as found in the addressable led datasheet. The duration, in microseconds, the pin will stay low for sending a 0 bit + * @param t1h_us - T1H as found in the addressable led datasheet. The duration, in microseconds, the pin will stay high for sending a 1 bit + * @param t1l_us - T1L as found in the addressable led datasheet. The duration, in microseconds, the pin will stay low for sending a 1 bit + * @param tReset_us - TReset as found in the addressable led datasheet. The duration, in microsecond, the pin will stay low for sending a reset command, + */ + LEDArray(PinName wirePin, ColorByteOrder byteOrder, unsigned int numberOfLEDs, float t0h_us, float t0l_us, float t1h_us, float t1l_us, float tReset_us); + + /** + * Initializes the addressable led array with default values (taken from WS2812 datasheet): + * T0H = 0.35, + * T0L = 0.8, + * T1H = 0.7, + * T1L = 0.6, + * TReset = 50 + * + * @param wirePin - The output pin on wich the addressable leds are connected + * @param byteOrder - The order in wich the r, g and b bytes are expected + * @param numberOfLEDs - The number of leds in this strip + */ + LEDArray(PinName wirePin, ColorByteOrder byteOrder, unsigned int numberOfLEDs); + + ~LEDArray(); + + /** + * Sets the color of the given pixel (index) with r, g and b values + */ + void setPixelColor(unsigned int pixel, uint8_t r, uint8_t g, uint8_t b); + + /** + * Sets the color of the given pixel (index) with a given color. + */ + void setPixelColor(unsigned int pixel, Color& color); + + /** + * Sends all configured colors to the actual led strip, this method is required to call in order to update the LEDs + */ + void show(); +}; + +#endif \ No newline at end of file
--- a/LEDBus.h Wed Jun 14 19:51:33 2017 +0000 +++ b/LEDBus.h Wed Jun 14 20:37:31 2017 +0000 @@ -15,7 +15,7 @@ /** - LEDBus + The LEDBus class allows for 'low' level communication to an addressable LED bus/strip */ class LEDBus {