LED bus driver on any GPIO pin for addressable RGB LEDs (like NeoPixels or other WS2812 based LEDs)

Revision:
0:1e68c70236a4
Child:
2:735bb1b9cfc2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDBus.h	Tue May 10 20:07:28 2016 +0000
@@ -0,0 +1,159 @@
+#ifndef _LED_BUS_H_
+#define _LED_BUS_H_
+
+#include "mbed.h"
+
+#define MICRO_SECOND 1000000.f
+
+/**
+    RGB Color
+*/
+struct Color {
+    
+    /**
+        Constructor with rgb initializing
+    
+        @param r - the red byte
+        @param g - the green byte
+        @param b - the blue byte
+    */
+    Color(uint8_t r, uint8_t g, uint8_t b) {
+        red = r;
+        green = g;
+        blue = b;
+    }
+
+    /**
+        Default constructor
+    */
+    Color() {
+    }
+
+    /**
+        Red byte
+    */
+    uint8_t red;
+
+    /**
+        Green byte
+    */
+    uint8_t green;
+
+    /**
+        Blue byte
+    */
+    uint8_t blue;
+};
+
+/**
+    Callback method, supplying a Color struct to edit and the led index
+*/
+typedef void (*ColorGenerator)(Color* out, uint32_t index);
+
+/**
+    Order of r, g and b bytes
+*/
+enum ByteOrder {
+    RGB,
+    RBG,
+    GRB,
+    GBR,
+    BRG,
+    BGR,
+};
+
+/**
+    LEDBus
+*/
+class LEDBus
+{
+
+private:
+    DigitalOut _wire;
+    ByteOrder _byteOrder;
+
+    unsigned int _delayT1H, _delayT0H, _delayT1L, _delayT0L, _delayReset;
+
+    void write(uint8_t byte);
+    void delay(unsigned int ticks);
+protected:
+
+public:
+
+
+    /**
+    *   Initializes the addressable led bus
+    *
+    *   @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 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,
+    */
+    LEDBus(PinName wirePin, ByteOrder byteOrder, float t0h_us, float t0l_us, float t1h_us, float t1l_us, float tReset_us);
+
+    ~LEDBus();
+
+    /**
+    *  Writes the byte buffer directly to the addressable leds
+    */
+    void write(uint8_t* buffer, unsigned int size);
+
+    /**
+    *  Writes the color buffer translated to bytes directly to the addressable leds
+    * @code
+    * // The led bus control class.
+    * LEDBus ledBus(p9, RGB, 0.5f, 2.0f, 1.25f, 1.25f, 50.0f);
+    *
+    * Color* red = new Color(255,0,0);
+    * Color* green = new Color(0,255,0);
+    * Color* blue = new Color(0,0,255);
+    *
+    * Color* buffer[] = { red, green, blue };
+    *
+    * ledBus.write(buffer, 3);
+    * @endcode
+    */
+    void write(Color** buffer, unsigned int size);
+
+    /**
+    *  Updates the leds by using a color generator callback method to generate a color for each led
+        * @code
+        *    int offset = 0;
+        *
+        *    // Sample generator:
+        *    void generate(Color* color, uint32_t index)
+        *    {
+        *        switch((index+offset)%3) {
+        *            case 0:
+        *                color->red = 255;
+        *                break;
+        *            case 1:
+        *                color->green = 255;
+        *                break;
+        *            case 2:
+        *                color->blue = 255;
+        *                break;
+        *        }
+        *    }
+        *
+        *    int main()
+        *    {
+        *        // The led bus control class.
+        *        LEDBus ledBus(p9, RGB, 0.35f, 1.36f, 1.36f, 0.35f, 50.0f);
+        *
+        *        while (1) {
+        *            ledBus.update(generate, 3);
+        *            offset++;
+        *            wait_ms(250);
+        *        }
+        *    }
+        * @endcode
+    */
+    void update(ColorGenerator generator, unsigned int numberOfLEDs);
+
+};
+
+#endif
\ No newline at end of file