TLC5940 library which supports SWSPI, has API to specify grayscale PWM period and has API like Arduino library.
Fork of TLC5940 by
TLC5940.cpp@6:04354724a9c6, 2015-10-18 (annotated)
- Committer:
- deton
- Date:
- Sun Oct 18 12:06:51 2015 +0000
- Revision:
- 6:04354724a9c6
- Parent:
- 5:c35b2b62f2f3
Avoid unexpected refresh in flush() for use of GS PWM as blink.; Otherwise, blink period varies.
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:04354724a9c6 | 44 | if (!_gspwmcycle_repeat) { // avoid unexpected refresh for use of GS PWM as blink |
deton | 6:04354724a9c6 | 45 | _blank.write(1); |
deton | 6:04354724a9c6 | 46 | } |
deton | 1:b188393f5b49 | 47 | _xlat.write(1); |
deton | 1:b188393f5b49 | 48 | _xlat.write(0); |
deton | 6:04354724a9c6 | 49 | if (!_gspwmcycle_repeat) { |
deton | 6:04354724a9c6 | 50 | _blank.write(0); |
deton | 6:04354724a9c6 | 51 | } |
Julepalme | 0:bdf7a64b89a7 | 52 | } |
Julepalme | 0:bdf7a64b89a7 | 53 | |
deton | 5:c35b2b62f2f3 | 54 | template<typename T> |
deton | 5:c35b2b62f2f3 | 55 | void TLC5940<T>::run() |
Julepalme | 0:bdf7a64b89a7 | 56 | { |
deton | 3:2c0af5f5fa13 | 57 | if (_gspwmcycle_repeat) { |
deton | 3:2c0af5f5fa13 | 58 | _t.attach_us(this, &TLC5940::refresh, _gspwmcycle_us); |
deton | 3:2c0af5f5fa13 | 59 | } |
deton | 1:b188393f5b49 | 60 | _gsclk.write(0.5f); |
Julepalme | 0:bdf7a64b89a7 | 61 | } |
Julepalme | 0:bdf7a64b89a7 | 62 | |
deton | 5:c35b2b62f2f3 | 63 | template<typename T> |
deton | 5:c35b2b62f2f3 | 64 | void TLC5940<T>::refresh() |
Julepalme | 0:bdf7a64b89a7 | 65 | { |
deton | 1:b188393f5b49 | 66 | _blank.write(1); |
deton | 1:b188393f5b49 | 67 | _blank.write(0); |
deton | 1:b188393f5b49 | 68 | } |
deton | 5:c35b2b62f2f3 | 69 | |
deton | 5:c35b2b62f2f3 | 70 | template class TLC5940<SPI>; |
deton | 5:c35b2b62f2f3 | 71 | #include "SWSPI.h" |
deton | 5:c35b2b62f2f3 | 72 | template class TLC5940<SWSPI>; |