TLC5940 library which supports SWSPI, has API to specify grayscale PWM period and has API like Arduino library.
Fork of TLC5940 by
TLC5940.cpp@3:2c0af5f5fa13, 2015-10-17 (annotated)
- Committer:
- deton
- Date:
- Sat Oct 17 01:36:58 2015 +0000
- Revision:
- 3:2c0af5f5fa13
- Parent:
- 2:b411648dfe54
- Child:
- 5:c35b2b62f2f3
Add arguments gspwmcycle_us and gspwmcycle_repeat.; gspwmcycle_repeat argument is for avoiding use of Ticker; which may not work after about 80 minutes on mbed HRM1017(nRF51822).
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 | 3:2c0af5f5fa13 | 3 | TLC5940::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk, |
deton | 3:2c0af5f5fa13 | 4 | int gspwmcycle_us, bool gspwmcycle_repeat): |
deton | 3:2c0af5f5fa13 | 5 | _spi(mosi, miso, sck), |
deton | 3:2c0af5f5fa13 | 6 | _gsclk(gsclk), |
deton | 3:2c0af5f5fa13 | 7 | _xlat(xlat), |
deton | 3:2c0af5f5fa13 | 8 | _blank(blank), |
deton | 3:2c0af5f5fa13 | 9 | _gspwmcycle_us(gspwmcycle_us), |
deton | 3:2c0af5f5fa13 | 10 | _gspwmcycle_repeat(gspwmcycle_repeat) |
Julepalme | 0:bdf7a64b89a7 | 11 | { |
deton | 1:b188393f5b49 | 12 | _spi.format(12,0); |
deton | 1:b188393f5b49 | 13 | _spi.frequency(30000000); |
deton | 3:2c0af5f5fa13 | 14 | _gsclk.period_us(gspwmcycle_us / 4096); |
deton | 1:b188393f5b49 | 15 | |
Julepalme | 0:bdf7a64b89a7 | 16 | // Reset to 0 |
Julepalme | 0:bdf7a64b89a7 | 17 | for(int i = 0; i < 16; i++) |
Julepalme | 0:bdf7a64b89a7 | 18 | { |
deton | 1:b188393f5b49 | 19 | gs_data[i] = 0; |
deton | 1:b188393f5b49 | 20 | int whoami = _spi.write(0); |
Julepalme | 0:bdf7a64b89a7 | 21 | } |
deton | 1:b188393f5b49 | 22 | |
deton | 1:b188393f5b49 | 23 | _xlat.write(1); |
deton | 1:b188393f5b49 | 24 | _xlat.write(0); |
Julepalme | 0:bdf7a64b89a7 | 25 | } |
Julepalme | 0:bdf7a64b89a7 | 26 | |
deton | 1:b188393f5b49 | 27 | void TLC5940::set(int channel, uint16_t brightness) |
Julepalme | 0:bdf7a64b89a7 | 28 | { |
deton | 1:b188393f5b49 | 29 | gs_data[15-channel] = brightness; |
deton | 1:b188393f5b49 | 30 | } |
deton | 1:b188393f5b49 | 31 | |
Julepalme | 0:bdf7a64b89a7 | 32 | |
Julepalme | 0:bdf7a64b89a7 | 33 | void TLC5940::flush() |
Julepalme | 0:bdf7a64b89a7 | 34 | { |
deton | 2:b411648dfe54 | 35 | //_sclk.write(1); |
deton | 2:b411648dfe54 | 36 | //_sclk.write(0); |
deton | 1:b188393f5b49 | 37 | |
Julepalme | 0:bdf7a64b89a7 | 38 | for(int i = 0; i < 16; i++){ |
deton | 1:b188393f5b49 | 39 | _spi.write(gs_data[i]); |
Julepalme | 0:bdf7a64b89a7 | 40 | } |
deton | 1:b188393f5b49 | 41 | |
deton | 3:2c0af5f5fa13 | 42 | _blank.write(1); |
deton | 1:b188393f5b49 | 43 | _xlat.write(1); |
deton | 1:b188393f5b49 | 44 | _xlat.write(0); |
deton | 3:2c0af5f5fa13 | 45 | _blank.write(0); |
Julepalme | 0:bdf7a64b89a7 | 46 | } |
Julepalme | 0:bdf7a64b89a7 | 47 | |
Julepalme | 0:bdf7a64b89a7 | 48 | void TLC5940::run() |
Julepalme | 0:bdf7a64b89a7 | 49 | { |
deton | 3:2c0af5f5fa13 | 50 | if (_gspwmcycle_repeat) { |
deton | 3:2c0af5f5fa13 | 51 | _t.attach_us(this, &TLC5940::refresh, _gspwmcycle_us); |
deton | 3:2c0af5f5fa13 | 52 | } |
deton | 1:b188393f5b49 | 53 | _gsclk.write(0.5f); |
Julepalme | 0:bdf7a64b89a7 | 54 | } |
Julepalme | 0:bdf7a64b89a7 | 55 | |
Julepalme | 0:bdf7a64b89a7 | 56 | void TLC5940::refresh() |
Julepalme | 0:bdf7a64b89a7 | 57 | { |
deton | 1:b188393f5b49 | 58 | _blank.write(1); |
deton | 1:b188393f5b49 | 59 | _blank.write(0); |
deton | 1:b188393f5b49 | 60 | } |