WS2812B

Dependents:   high speed light Bracelet

Fork of PololuLedStrip by David Grayson

PololuLedStrip.cpp

Committer:
DavidEGrayson
Date:
2013-03-01
Revision:
12:b6df8ac053c8
Parent:
10:f1bb84b97788
Child:
13:9c72841ec45e

File content as of revision 12:b6df8ac053c8:

#include "PololuLedStrip.h"

bool PololuLedStrip::interruptFriendly = false;

uint8_t led_strip_write_delays[3];

static const uint8_t delay_fudges[] = { 32, 0, 0 };

void PololuLedStrip::calculateDelays()
{
    // Get the clock frequency in MHz.
    int f_mhz = SystemCoreClock / 1000000;
    
    // Arrange for a 700 nanosecond delay between the rise time and the fall time for a 0 bit.
    led_strip_write_delays[0] = 700*f_mhz/1000 - 32;
    
    // Arrange for a 600 nanosecond delay between the fall time for a 0 bit and the fall time for a 1 bit.
    // This means the pulses representing a 1 will be 700+600 = 1300 nanoseconds.
    led_strip_write_delays[1] = 600*f_mhz/1000;
    
    // Arrange for a 1200 nanosecond delay between the fall time for a 1 bit and rise time of the next bit.
    // This means the period of the signal will be 2500 nanoseconds.
    led_strip_write_delays[2] = 1200*f_mhz/1000;
    
    for(int i = 0; i < 3; i++)
    {
        if (led_strip_write_delays[i] < delay_fudges[i])
        {
            led_strip_write_delays[i] = 0;
        }
        else
        {
            led_strip_write_delays[i] -= delay_fudges[i];
            led_strip_write_delays[i] <<= 1;
        }
    }
}

PololuLedStrip::PololuLedStrip(PinName pinName)
{
    gpio_init(&gpio, pinName, PIN_OUTPUT);
}

void PololuLedStrip::write(rgb_color * colors, unsigned int count)
{
    calculateDelays();

    __disable_irq();   // Disable interrupts temporarily because we don't want our pulse timing to be messed up.

    while(count--)
    {
        led_strip_write_color(colors, gpio.reg_set, gpio.reg_clr, gpio.mask);
        colors++;
         
        if (interruptFriendly)
        {
            __enable_irq();
            __nop();
            __nop();
            __nop();
            __disable_irq();
        }
    }
        
    __enable_irq();   // Re-enable interrupts now that we are done.
    wait_us(24);      // Hold the line low for 24 microseconds to send the reset signal.
    
    //*(gpio.reg_set) = gpio.mask;
    //*(gpio.reg_clr) = gpio.mask;

}