TLC5940 library which supports SWSPI, has API to specify grayscale PWM period and has API like Arduino library.
Fork of TLC5940 by
TLC5940.cpp@1:b188393f5b49, 2015-10-12 (annotated)
- Committer:
- deton
- Date:
- Mon Oct 12 04:28:01 2015 +0000
- Revision:
- 1:b188393f5b49
- Parent:
- 0:bdf7a64b89a7
- Child:
- 2:b411648dfe54
use SWSPI for nRF51822 that does not support 12 bits format SPI.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Julepalme | 0:bdf7a64b89a7 | 1 | #include "TLC5940.h" |
deton | 1:b188393f5b49 | 2 | #define GSCLK_PERIOD 5 |
Julepalme | 0:bdf7a64b89a7 | 3 | |
deton | 1:b188393f5b49 | 4 | TLC5940::TLC5940(PinName mosi, PinName miso, PinName sck, PinName xlat, PinName blank, PinName gsclk): |
deton | 1:b188393f5b49 | 5 | _spi(SWSPI(mosi, miso, sck)), |
deton | 1:b188393f5b49 | 6 | _gsclk(PwmOut(gsclk)), |
deton | 1:b188393f5b49 | 7 | _sclk(DigitalOut(sck)), |
deton | 1:b188393f5b49 | 8 | _xlat(DigitalOut(xlat)), |
deton | 1:b188393f5b49 | 9 | _blank(DigitalOut(blank)), |
deton | 1:b188393f5b49 | 10 | _t(Ticker()) |
Julepalme | 0:bdf7a64b89a7 | 11 | { |
deton | 1:b188393f5b49 | 12 | _spi.format(12,0); |
deton | 1:b188393f5b49 | 13 | _spi.frequency(30000000); |
deton | 1:b188393f5b49 | 14 | |
deton | 1:b188393f5b49 | 15 | // Turn off GSCLK |
deton | 1:b188393f5b49 | 16 | _gsclk.write(0.0f); |
deton | 1:b188393f5b49 | 17 | _gsclk.period_us(GSCLK_PERIOD); |
deton | 1:b188393f5b49 | 18 | |
Julepalme | 0:bdf7a64b89a7 | 19 | // Reset to 0 |
Julepalme | 0:bdf7a64b89a7 | 20 | for(int i = 0; i < 16; i++) |
Julepalme | 0:bdf7a64b89a7 | 21 | { |
deton | 1:b188393f5b49 | 22 | gs_data[i] = 0; |
deton | 1:b188393f5b49 | 23 | int whoami = _spi.write(0); |
Julepalme | 0:bdf7a64b89a7 | 24 | } |
deton | 1:b188393f5b49 | 25 | |
deton | 1:b188393f5b49 | 26 | _xlat.write(1); |
deton | 1:b188393f5b49 | 27 | _xlat.write(0); |
Julepalme | 0:bdf7a64b89a7 | 28 | } |
Julepalme | 0:bdf7a64b89a7 | 29 | |
deton | 1:b188393f5b49 | 30 | void TLC5940::set(int channel, uint16_t brightness) |
Julepalme | 0:bdf7a64b89a7 | 31 | { |
deton | 1:b188393f5b49 | 32 | gs_data[15-channel] = brightness; |
deton | 1:b188393f5b49 | 33 | } |
deton | 1:b188393f5b49 | 34 | |
deton | 1:b188393f5b49 | 35 | void TLC5940::setAll(uint16_t brightness) |
deton | 1:b188393f5b49 | 36 | { |
deton | 1:b188393f5b49 | 37 | for (int i = 0; i < 16; i++) |
deton | 1:b188393f5b49 | 38 | { |
deton | 1:b188393f5b49 | 39 | gs_data[i] = brightness; |
deton | 1:b188393f5b49 | 40 | } |
Julepalme | 0:bdf7a64b89a7 | 41 | } |
Julepalme | 0:bdf7a64b89a7 | 42 | |
Julepalme | 0:bdf7a64b89a7 | 43 | void TLC5940::flush() |
Julepalme | 0:bdf7a64b89a7 | 44 | { |
deton | 1:b188393f5b49 | 45 | _sclk.write(1); |
deton | 1:b188393f5b49 | 46 | _sclk.write(0); |
deton | 1:b188393f5b49 | 47 | |
Julepalme | 0:bdf7a64b89a7 | 48 | for(int i = 0; i < 16; i++){ |
deton | 1:b188393f5b49 | 49 | _spi.write(gs_data[i]); |
Julepalme | 0:bdf7a64b89a7 | 50 | } |
deton | 1:b188393f5b49 | 51 | |
deton | 1:b188393f5b49 | 52 | _xlat.write(1); |
deton | 1:b188393f5b49 | 53 | _xlat.write(0); |
Julepalme | 0:bdf7a64b89a7 | 54 | } |
Julepalme | 0:bdf7a64b89a7 | 55 | |
Julepalme | 0:bdf7a64b89a7 | 56 | void TLC5940::run() |
Julepalme | 0:bdf7a64b89a7 | 57 | { |
deton | 1:b188393f5b49 | 58 | _gsclk.write(0.5f); |
deton | 1:b188393f5b49 | 59 | _t.attach_us(this, &TLC5940::refresh, GSCLK_PERIOD*4096); |
Julepalme | 0:bdf7a64b89a7 | 60 | } |
Julepalme | 0:bdf7a64b89a7 | 61 | |
Julepalme | 0:bdf7a64b89a7 | 62 | void TLC5940::refresh() |
Julepalme | 0:bdf7a64b89a7 | 63 | { |
deton | 1:b188393f5b49 | 64 | _blank.write(1); |
deton | 1:b188393f5b49 | 65 | _blank.write(0); |
deton | 1:b188393f5b49 | 66 | } |