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

Fork of TLC5940 by Stefan Nielsen

Committer:
deton
Date:
Sat Oct 17 06:52:23 2015 +0000
Revision:
5:c35b2b62f2f3
Parent:
3:2c0af5f5fa13
Child:
6:04354724a9c6
template for SWSPI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Julepalme 0:bdf7a64b89a7 1 #include "TLC5940.h"
Julepalme 0:bdf7a64b89a7 2
deton 5:c35b2b62f2f3 3 template<typename T>
deton 5:c35b2b62f2f3 4 TLC5940<T>::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk,
deton 3:2c0af5f5fa13 5 int gspwmcycle_us, bool gspwmcycle_repeat):
deton 3:2c0af5f5fa13 6 _spi(mosi, miso, sck),
deton 3:2c0af5f5fa13 7 _gsclk(gsclk),
deton 3:2c0af5f5fa13 8 _xlat(xlat),
deton 3:2c0af5f5fa13 9 _blank(blank),
deton 3:2c0af5f5fa13 10 _gspwmcycle_us(gspwmcycle_us),
deton 3:2c0af5f5fa13 11 _gspwmcycle_repeat(gspwmcycle_repeat)
Julepalme 0:bdf7a64b89a7 12 {
deton 1:b188393f5b49 13 _spi.format(12,0);
deton 1:b188393f5b49 14 _spi.frequency(30000000);
deton 3:2c0af5f5fa13 15 _gsclk.period_us(gspwmcycle_us / 4096);
deton 1:b188393f5b49 16
Julepalme 0:bdf7a64b89a7 17 // Reset to 0
Julepalme 0:bdf7a64b89a7 18 for(int i = 0; i < 16; i++)
Julepalme 0:bdf7a64b89a7 19 {
deton 1:b188393f5b49 20 gs_data[i] = 0;
deton 1:b188393f5b49 21 int whoami = _spi.write(0);
Julepalme 0:bdf7a64b89a7 22 }
deton 1:b188393f5b49 23
deton 1:b188393f5b49 24 _xlat.write(1);
deton 1:b188393f5b49 25 _xlat.write(0);
Julepalme 0:bdf7a64b89a7 26 }
Julepalme 0:bdf7a64b89a7 27
deton 5:c35b2b62f2f3 28 template<typename T>
deton 5:c35b2b62f2f3 29 void TLC5940<T>::set(int channel, uint16_t brightness)
Julepalme 0:bdf7a64b89a7 30 {
deton 1:b188393f5b49 31 gs_data[15-channel] = brightness;
deton 1:b188393f5b49 32 }
deton 1:b188393f5b49 33
deton 5:c35b2b62f2f3 34 template<typename T>
deton 5:c35b2b62f2f3 35 void TLC5940<T>::flush()
Julepalme 0:bdf7a64b89a7 36 {
deton 2:b411648dfe54 37 //_sclk.write(1);
deton 2:b411648dfe54 38 //_sclk.write(0);
deton 1:b188393f5b49 39
Julepalme 0:bdf7a64b89a7 40 for(int i = 0; i < 16; i++){
deton 1:b188393f5b49 41 _spi.write(gs_data[i]);
Julepalme 0:bdf7a64b89a7 42 }
deton 1:b188393f5b49 43
deton 3:2c0af5f5fa13 44 _blank.write(1);
deton 1:b188393f5b49 45 _xlat.write(1);
deton 1:b188393f5b49 46 _xlat.write(0);
deton 3:2c0af5f5fa13 47 _blank.write(0);
Julepalme 0:bdf7a64b89a7 48 }
Julepalme 0:bdf7a64b89a7 49
deton 5:c35b2b62f2f3 50 template<typename T>
deton 5:c35b2b62f2f3 51 void TLC5940<T>::run()
Julepalme 0:bdf7a64b89a7 52 {
deton 3:2c0af5f5fa13 53 if (_gspwmcycle_repeat) {
deton 3:2c0af5f5fa13 54 _t.attach_us(this, &TLC5940::refresh, _gspwmcycle_us);
deton 3:2c0af5f5fa13 55 }
deton 1:b188393f5b49 56 _gsclk.write(0.5f);
Julepalme 0:bdf7a64b89a7 57 }
Julepalme 0:bdf7a64b89a7 58
deton 5:c35b2b62f2f3 59 template<typename T>
deton 5:c35b2b62f2f3 60 void TLC5940<T>::refresh()
Julepalme 0:bdf7a64b89a7 61 {
deton 1:b188393f5b49 62 _blank.write(1);
deton 1:b188393f5b49 63 _blank.write(0);
deton 1:b188393f5b49 64 }
deton 5:c35b2b62f2f3 65
deton 5:c35b2b62f2f3 66 template class TLC5940<SPI>;
deton 5:c35b2b62f2f3 67 #include "SWSPI.h"
deton 5:c35b2b62f2f3 68 template class TLC5940<SWSPI>;