This library lets you control the addressable RGB LED strips from Pololu Robotics & Electronics.

Dependents:   WoYaoChengGOng V2-WoYaoChengGOng STM32_MagneticLight tape_Led_Sample ... more

Summary

This is a library for the mbed that allows you to control these addressable RGB LED products from Pololu:

This library is optimized for the SK6812 and WS2812B, so it transmits colors in green-red-blue order.

If you have a WS2811 LED or a high-speed TM1804 LED strip, please note that its red and green channels are swapped relative to the WS2812B, so you will need to swap those channels in your code. You might prefer to use the version of the library from October 2013, which does not require you to swap red and green in your code.

If you need to control the older, low-speed LED strips (items #2540, #2541, and #2542), you will need to use the version of this library from March 2013.

This library allows complete control over the color of an arbitrary number of LED strips with an arbitrary number of LEDs. Each LED can be individually controlled, and LED strips can be chained together.

This library should also work with any other LED strip based on the SK6812, WS281x, or TM1804.

Supported Platforms

This library has been tested on the mbed NXP LPC1768 (Cortex-M3 at 96 MHz), the mbed NXP LPC11U24 (Cortex-M0 at 48 MHz), and the NUCLEO-F303K8 (Cortex-M4 at 72 MHz). It will probably work on many other boards without modification.

This library does not work on chips families such as the STM32F4 where there is a single register for setting and clearing the value of an output pin. The library checks for the GPIO_IP_WITHOUT_BRR preprocessor macro and triggers a compile-time error if that macro is set.

Getting Started

Software

Here are two example programs that show how to use the library:

Import programLedStripRainbow

This is an example program for the PololuLedStrip library. It generates a simple moving rainbow pattern.

Import programLedStripGradient

This is an example program for the PololuLedStrip library. It generates a simple moving gradient pattern.

As a first step, you should compile and upload one of these to the mbed. When the program runs, the mbed should output color data on pin p8 dozens of times per second. The expected signal is documented on the Pololu website. The example programs only send colors for 60 LEDs, but they can easily be changed to send more for a longer strip.

Hardware

The addressable RGB LED strips can be purchased on Pololu's website using the links above.

The LED strip’s data input connector has two pins that should be connected to the Arduino. The LED strip’s ground will need to be connected to one of the mbed’s GND pins, and the LED strip’s signal input line will be need to be connected to one of the Arduino’s I/O lines. Our example programs assume the signal line is connected to p8. These connections can be made using two Male-Female Premium Jumper Wires, with the female ends plugging into the LED strip and the male ends plugged into a breadboard that houses the mbed.

You will also need to connect a suitable power supply to the LED strip using one of the power connectors. The power supply must be at the right voltage and provide enough current to meet the LED strip's requirements.

If everything works properly, you will see a moving pattern of colors on the LED strip.

Timing Details

This library takes about 1.3 ms to update 30 LEDs (1 meter). The LED strips use a high speed one-wire protocol with relatively strict timing requirements, so this library disables interrupts to ensure reliable color transmission. Unfortunately, disabling the interrupts could cause problems in other libraries that uses interrupts.

This library provides an interruptFriendly option that can let it coexist with interrupt-based libraries. When this option is enabled, the library will temporarily enable interrupts after each color is sent, about every 45 microseconds. If you can keep all of your interrupts short enough, then this option should allow this library to work in conjunction with your interrupt-based libraries. However, if you have an interrupt enabled that takes too long, then this interrupt will sometimes cause an extra long low pulse to emitted, which will be interpreted by the LED strip as a reset command. This can cause visible flickering in the LED strip. To turn on the interruptFriendly option, add this line to the beginning of your main() function:

PololuLedStrip::interruptFriendly = true;

Chaining LED Strips together

No special code is required to chain LED strips together. An X-meter LED strip chained to a Y-meter LED strip can be controlled in exactly the same way as a single (X+Y)-meter LED strip.

led_strip_write_color.s

Committer:
DavidEGrayson
Date:
2016-11-03
Revision:
25:d72818ba17cc
Parent:
24:5c01a6fa1556

File content as of revision 25:d72818ba17cc:

    AREA asm_func, CODE, READONLY
    EXPORT led_strip_write_color
    IMPORT led_strip_write_delays

    MACRO
    delay $id
    ldr r5, =led_strip_write_delays
    ldrb r5, [r5, $id]
    ldr r4, =delay_region_end
    subs r4, r4, r5
    blx r4
    MEND

led_strip_write_color
    ; Register usage:
    ; These are the first 4 arguments to the method:
    ;   R0:  pointer to the color to send
    ;   R1:  pointer to the register for setting the pin
    ;   R2:  pointer to the register for clearing the pin
    ;   R3:  pin mask
    ; Additionally, we use these registers:
    ;   R4:  temporary register
    ;   R5:  temporary register
    ;   R6:  shift register that holds the 24-bit color
    ;   R7:  the number of bits we still need to send
    ;   R13: Link Register, holds return addresses.

    ; Push those registers so we can restore them later.
    push {r4, r5, r6, r7, lr}

    ldrb r6, [r0, #1]  ; Load green.
    lsls r6, r6, #24   ; Put green in MSB of r6.
    ldrb r4, [r0, #0]  ; Load red.
    lsls r4, r4, #16
    orrs r6, r6, r4    ; Put red in r6.
    ldrb r4, [r0, #2]  ; Load blue.
    lsls r4, r4, #8
    orrs r6, r6, r4    ; Put blue in r6.
   
    ; On the Cortex-M3 we simply did:
    ;    ldr r6, [r0]       ; Read the color.  Now we have:     xxBbGgRr
    ;    rbit r6, r6        ; Reverse the order of the bits:    rRgGbBxx
    ;    rev r6, r6         ; Reverse the order of the bytes:   xxbBgGrR
    ; and then we used rrxs for shifting to the right through carry.
    
    ldr r7, =24        ; Initialize the loop counter register.
    
send_led_strip_bit
    str r3, [r1]       ; Drive the line high.
    
    ; It doesn't really matter exactly how long we delay here as long as it is
    ; less than the LED's threshold between 0 and 1.
    nop
    nop
    nop
    nop
    nop
    nop
    
    ldr r4, =0x80000000
    tst r6, r4
    bne delay0
    str r3, [r2]       ; If the bit to send it 0, drive the line low.

delay0
    delay #0

    ldr r4, =0x80000000
    tst r6, r4
    beq delay1
    str r3, [r2]       ; If the bit to send is 1, drive the line low.

delay1
    delay #1
    
    lsls r6, r6, #1           ; Shift color bits.
    subs r7, r7, #1           ; Decrement the loop counter.
    bne send_led_strip_bit    ; Send another bit if we have not reached zero.   
    pop {r4, r5, r6, r7, pc}  ; Otherwise, restore the registers and return.
    bx lr;

delay_region
    ; The following is 128 nops.
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
delay_region_end
    bx lr    ; return
    
    END