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

Fork of TLC5940 by Stefan Nielsen

Revision:
1:b188393f5b49
Parent:
0:bdf7a64b89a7
Child:
2:b411648dfe54
--- a/TLC5940.cpp	Sun Mar 24 16:23:29 2013 +0000
+++ b/TLC5940.cpp	Mon Oct 12 04:28:01 2015 +0000
@@ -1,55 +1,66 @@
 #include "TLC5940.h"
+#define GSCLK_PERIOD 5
 
-TLC5940::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk)
+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 = new SPI(mosi, miso, sck);
-    _xlat = new DigitalOut(xlat);
-    _blank = new DigitalOut(blank);
-    _gsclk = new PwmOut(gsclk);
-    _t = new Ticker();
-    
-    // Setup SPI
-    _spi->format(12,0);
-    _spi->frequency(5000000);
-    
-    //Turn off GSCLK
-    _gsclk->write(0.0f);
-    _gsclk->period_us(5);
-    
+    _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] = 4095;
-           int whoami = _spi->write(4095);
+        gs_data[i] = 0;
+        int whoami = _spi.write(0);
     }
-        
-    _xlat->write(1);
-    _xlat->write(0);
+
+    _xlat.write(1);
+    _xlat.write(0);
 }
 
-void TLC5940::setServo(int ch, int val)
+void TLC5940::set(int channel, uint16_t brightness)
 {
-    gs_data[15-ch] = val;
+    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]);
+        _spi.write(gs_data[i]);
     }
-    
-    _xlat->write(1);
-    _xlat->write(0);
+
+    _xlat.write(1);
+    _xlat.write(0);
 }
 
 void TLC5940::run()
 {
-    _gsclk->write(0.5f);
-    _t->attach_us(this, &TLC5940::refresh, 5*4096);
+    _gsclk.write(0.5f);
+    _t.attach_us(this, &TLC5940::refresh, GSCLK_PERIOD*4096);
 }
 
 void TLC5940::refresh()
 {
-    _blank->write(1);
-    _blank->write(0);
-} 
\ No newline at end of file
+    _blank.write(1);
+    _blank.write(0);
+}