TLC5940 library which supports SWSPI, has API to specify grayscale PWM period and has API like Arduino library.

Fork of TLC5940 by Stefan Nielsen

TLC5940.cpp

Committer:
deton
Date:
2015-10-12
Revision:
1:b188393f5b49
Parent:
0:bdf7a64b89a7
Child:
2:b411648dfe54

File content as of revision 1:b188393f5b49:

#include "TLC5940.h"
#define GSCLK_PERIOD 5

TLC5940::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk):
    _spi(SWSPI(mosi, miso, sck)),
    _gsclk(PwmOut(gsclk)),
    _sclk(DigitalOut(sck)),
    _xlat(DigitalOut(xlat)),
    _blank(DigitalOut(blank)),
    _t(Ticker())
{
    _spi.format(12,0);
    _spi.frequency(30000000);

    // Turn off GSCLK
    _gsclk.write(0.0f);
    _gsclk.period_us(GSCLK_PERIOD);

    // Reset to 0
    for(int i = 0; i < 16; i++)
    {
        gs_data[i] = 0;
        int whoami = _spi.write(0);
    }

    _xlat.write(1);
    _xlat.write(0);
}

void TLC5940::set(int channel, uint16_t brightness)
{
    gs_data[15-channel] = brightness;
}

void TLC5940::setAll(uint16_t brightness)
{
    for (int i = 0; i < 16; i++)
    {
        gs_data[i] = brightness;
    }
}

void TLC5940::flush()
{
    _sclk.write(1);
    _sclk.write(0);

    for(int i = 0; i < 16; i++){
        _spi.write(gs_data[i]);
    }

    _xlat.write(1);
    _xlat.write(0);
}

void TLC5940::run()
{
    _gsclk.write(0.5f);
    _t.attach_us(this, &TLC5940::refresh, GSCLK_PERIOD*4096);
}

void TLC5940::refresh()
{
    _blank.write(1);
    _blank.write(0);
}