This library lets you control the addressable RGB LED strips from Pololu Robotics. Forked to add selectable colour order (Support RGB or GRB Leds)

Fork of PololuLedStrip by David Grayson

Committer:
thegink
Date:
Sat May 19 14:16:38 2018 +0000
Revision:
27:9a62663f3de2
Parent:
19:46d7ab0ba3e7
Added #define for RGB/GRB order for WS2811 and WS2812b based strips, respectively. Ideally this should be selectable per instance, but it's late, and I'm tired.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DavidEGrayson 0:06475317f283 1 #include "mbed.h"
DavidEGrayson 0:06475317f283 2
DavidEGrayson 0:06475317f283 3 #ifndef _POLOLU_LED_STRIP_H
DavidEGrayson 0:06475317f283 4 #define _POLOLU_LED_STRIP_H
DavidEGrayson 0:06475317f283 5
thegink 27:9a62663f3de2 6 //If multiple colour orders are enabled, GRB will override RGB.
thegink 27:9a62663f3de2 7
thegink 27:9a62663f3de2 8 //Use RGB colour order for WS2811 leds
thegink 27:9a62663f3de2 9 #define _POLOLU_ORDER_RGB
thegink 27:9a62663f3de2 10
thegink 27:9a62663f3de2 11 //Use GRB colour order for WS2812b leds
thegink 27:9a62663f3de2 12 //#define _POLOLU_ORDER_GRB
thegink 27:9a62663f3de2 13
thegink 27:9a62663f3de2 14
DavidEGrayson 0:06475317f283 15 namespace Pololu
DavidEGrayson 0:06475317f283 16 {
DavidEGrayson 0:06475317f283 17 #ifndef _POLOLU_RGB_COLOR
DavidEGrayson 0:06475317f283 18 #define _POLOLU_RGB_COLOR
thegink 27:9a62663f3de2 19
DavidEGrayson 1:102307d9b701 20 /** Represents an RGB color. */
DavidEGrayson 0:06475317f283 21 typedef struct rgb_color
DavidEGrayson 0:06475317f283 22 {
DavidEGrayson 1:102307d9b701 23 uint8_t red; /*!< A number between 0 and 255 that represents the brightness of the red component. */
DavidEGrayson 1:102307d9b701 24 uint8_t green; /*!< A number between 0 and 255 that represents the brightness of the green component. */
DavidEGrayson 1:102307d9b701 25 uint8_t blue; /*!< A number between 0 and 255 that represents the brightness of the blue component. */
DavidEGrayson 0:06475317f283 26 } rgb_color;
thegink 27:9a62663f3de2 27 #endif
DavidEGrayson 0:06475317f283 28 extern "C" int led_strip_write_color(rgb_color *, volatile uint32_t * set, volatile uint32_t * clear, uint32_t mask);
DavidEGrayson 0:06475317f283 29
DavidEGrayson 18:34ba573573df 30 /** This class lets you control the addressable RGB LED strips from Pololu</a>,
DavidEGrayson 1:102307d9b701 31 or any other LED strip based on the TM1804 chip. */
DavidEGrayson 0:06475317f283 32 class PololuLedStrip
DavidEGrayson 0:06475317f283 33 {
DavidEGrayson 0:06475317f283 34 gpio_t gpio;
DavidEGrayson 1:102307d9b701 35
DavidEGrayson 0:06475317f283 36 public:
DavidEGrayson 1:102307d9b701 37
DavidEGrayson 1:102307d9b701 38 /** This constructor lets you make an led strip object by specifying the pin name.
DavidEGrayson 1:102307d9b701 39 There are no restrictions on what pin you can choose.
DavidEGrayson 1:102307d9b701 40
DavidEGrayson 1:102307d9b701 41 Example:
DavidEGrayson 1:102307d9b701 42 @code
DavidEGrayson 1:102307d9b701 43 PololuLedStrip ledStrip(p8);
DavidEGrayson 1:102307d9b701 44 @endcode
DavidEGrayson 1:102307d9b701 45 */
DavidEGrayson 0:06475317f283 46 PololuLedStrip(PinName pin);
DavidEGrayson 1:102307d9b701 47
DavidEGrayson 1:102307d9b701 48 /** Writes the specified series of colors to the LED strip.
DavidEGrayson 1:102307d9b701 49 @param colors should be a pointer to an array of rgb_color structs.
DavidEGrayson 1:102307d9b701 50 @param count should be the number of colors to write.
DavidEGrayson 1:102307d9b701 51
DavidEGrayson 1:102307d9b701 52 The first color in the array will be written to the LED closest to the data input connector.
DavidEGrayson 1:102307d9b701 53 To update all the LEDs in the LED strip, count should be equal to or greater than the number of LEDs in the strip.
DavidEGrayson 1:102307d9b701 54 If count is less than the number of LEDs in the strip, then some LEDs near the end of the strip will not be updated.
DavidEGrayson 1:102307d9b701 55
DavidEGrayson 19:46d7ab0ba3e7 56 The colors are sent in series and each color takes about 45 microseconds to send.
DavidEGrayson 1:102307d9b701 57 This function disables interrupts temporarily while it is running.
DavidEGrayson 1:102307d9b701 58 This function waits for over 10 us at the end before returning to allow the colors to take effect.
DavidEGrayson 1:102307d9b701 59 */
DavidEGrayson 0:06475317f283 60 void write(rgb_color * colors, unsigned int count);
DavidEGrayson 7:9a088f042ee0 61
DavidEGrayson 1:102307d9b701 62 /** This option defaults to <code>false</code>.
DavidEGrayson 1:102307d9b701 63 Setting this to true changes the behavior of the write function, making it enable interrupts
DavidEGrayson 1:102307d9b701 64 after each color is sent, about every 60 microseconds.
DavidEGrayson 1:102307d9b701 65 This allows your program to respond to interrupts faster, but makes it possible for an interrupt
DavidEGrayson 1:102307d9b701 66 that takes longer than 8 microseconds to screw up the transmission of colors to the LED strip.
DavidEGrayson 1:102307d9b701 67
DavidEGrayson 1:102307d9b701 68 Example:
DavidEGrayson 1:102307d9b701 69 @code
DavidEGrayson 1:102307d9b701 70 PololuLedStrip::interruptFriendly = true;
DavidEGrayson 1:102307d9b701 71 @endcode
DavidEGrayson 1:102307d9b701 72 */
DavidEGrayson 0:06475317f283 73 static bool interruptFriendly;
DavidEGrayson 7:9a088f042ee0 74
DavidEGrayson 7:9a088f042ee0 75 static void calculateDelays();
DavidEGrayson 0:06475317f283 76 };
DavidEGrayson 0:06475317f283 77 }
DavidEGrayson 0:06475317f283 78
DavidEGrayson 0:06475317f283 79 using namespace Pololu;
DavidEGrayson 0:06475317f283 80
DavidEGrayson 0:06475317f283 81 #endif