From ee21d7edf1ed6a1652761912384f9f75a7539718 Mon Sep 17 00:00:00 2001 From: Jan Hoffmann Date: Thu, 4 Apr 2019 14:11:10 +0200 Subject: [PATCH 1/4] transactional SPI --- Adafruit_TLC59711.cpp | 67 +++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/Adafruit_TLC59711.cpp b/Adafruit_TLC59711.cpp index 232800a..2a6267a 100644 --- a/Adafruit_TLC59711.cpp +++ b/Adafruit_TLC59711.cpp @@ -1,4 +1,4 @@ -/*************************************************** +/*************************************************** This is a library for our Adafruit 12-channel PWM/LED driver Pick one up today in the adafruit shop! @@ -6,18 +6,19 @@ Two SPI Pins are required to send data: clock and data pin. - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing products from Adafruit! - Written by Limor Fried/Ladyada for Adafruit Industries. + Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, all text above must be included in any redistribution ****************************************************/ - #include #include +SPISettings SPI_SETTINGS(100, MSBFIRST, SPI_MODE0); + /*! * @brief Instantiates a new Adafruit_TLC59711 class * @param n @@ -34,7 +35,7 @@ Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, uint8_t c, uint8_t d) { BCr = BCg = BCb = 0x7F; - pwmbuffer = (uint16_t *)calloc(2, 12*n); + pwmbuffer = (uint16_t *)calloc(2, 12 * n); } /*! @@ -50,16 +51,9 @@ Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, SPIClass *theSPI) { _dat = -1; _spi = theSPI; - _spi->setBitOrder(MSBFIRST); -#ifdef __arm__ - _spi->setClockDivider(42); -#else - _spi->setClockDivider(SPI_CLOCK_DIV8); -#endif - _spi->setDataMode(SPI_MODE0); BCr = BCg = BCb = 0x7F; - pwmbuffer = (uint16_t *)calloc(2, 12*n); + pwmbuffer = (uint16_t *)calloc(2, 12 * n); } /*! @@ -67,16 +61,16 @@ Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, SPIClass *theSPI) { * @param d * data */ -void Adafruit_TLC59711::spiwriteMSB(uint8_t d) { +void Adafruit_TLC59711::spiwriteMSB(uint8_t d) { if (_clk >= 0) { uint32_t b = 0x80; // b <<= (bits-1); - for (; b!=0; b>>=1) { + for (; b != 0; b >>= 1) { digitalWrite(_clk, LOW); - if (d & b) - digitalWrite(_dat, HIGH); + if (d & b) + digitalWrite(_dat, HIGH); else - digitalWrite(_dat, LOW); + digitalWrite(_dat, LOW); digitalWrite(_clk, HIGH); } } else { @@ -88,13 +82,15 @@ void Adafruit_TLC59711::spiwriteMSB(uint8_t d) { * @brief Writes PWM buffer to board */ void Adafruit_TLC59711::write() { + _spi->beginTransaction(SPI_SETTINGS); + uint32_t command; // Magic word for write command = 0x25; command <<= 5; - //OUTTMG = 1, EXTGCK = 0, TMGRST = 1, DSPRPT = 1, BLANK = 0 -> 0x16 + // OUTTMG = 1, EXTGCK = 0, TMGRST = 1, DSPRPT = 1, BLANK = 0 -> 0x16 command |= 0x16; command <<= 7; @@ -107,17 +103,17 @@ void Adafruit_TLC59711::write() { command |= BCb; noInterrupts(); - for (uint8_t n=0; n> 24); spiwriteMSB(command >> 16); spiwriteMSB(command >> 8); spiwriteMSB(command); // 12 channels per TLC59711 - for (int8_t c=11; c >= 0 ; c--) { + for (int8_t c = 11; c >= 0; c--) { // 16 bits per channel, send MSB first - spiwriteMSB(pwmbuffer[n*12+c]>>8); - spiwriteMSB(pwmbuffer[n*12+c]); + spiwriteMSB(pwmbuffer[n * 12 + c] >> 8); + spiwriteMSB(pwmbuffer[n * 12 + c]); } } @@ -125,11 +121,11 @@ void Adafruit_TLC59711::write() { delayMicroseconds(200); else delayMicroseconds(2); + + _spi->endTransaction(); interrupts(); } - - /*! * @brief Set PWM value on selected channel * @param chan @@ -138,12 +134,13 @@ void Adafruit_TLC59711::write() { * pwm value */ void Adafruit_TLC59711::setPWM(uint8_t chan, uint16_t pwm) { - if (chan > 12*numdrivers) return; - pwmbuffer[chan] = pwm; + if (chan > 12 * numdrivers) + return; + pwmbuffer[chan] = pwm; } /*! - * @brief Set RGB value for selected LED + * @brief Set RGB value for selected LED * @param lednum * selected LED number that for which value will be set * @param r @@ -153,10 +150,11 @@ void Adafruit_TLC59711::setPWM(uint8_t chan, uint16_t pwm) { * @param b * blue value */ -void Adafruit_TLC59711::setLED(uint8_t lednum, uint16_t r, uint16_t g, uint16_t b) { - setPWM(lednum*3, r); - setPWM(lednum*3+1, g); - setPWM(lednum*3+2, b); +void Adafruit_TLC59711::setLED(uint8_t lednum, uint16_t r, uint16_t g, + uint16_t b) { + setPWM(lednum * 3, r); + setPWM(lednum * 3 + 1, g); + setPWM(lednum * 3 + 2, b); } /*! @@ -164,7 +162,8 @@ void Adafruit_TLC59711::setLED(uint8_t lednum, uint16_t r, uint16_t g, uint16_t * @return If successful returns true, otherwise false */ boolean Adafruit_TLC59711::begin() { - if (!pwmbuffer) return false; + if (!pwmbuffer) + return false; if (_clk >= 0) { pinMode(_clk, OUTPUT); From 4471a823828b19989903a3f75b3d22743cbdacee Mon Sep 17 00:00:00 2001 From: Jan Hoffmann Date: Thu, 4 Apr 2019 14:14:14 +0200 Subject: [PATCH 2/4] changed spi frequency --- Adafruit_TLC59711.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_TLC59711.cpp b/Adafruit_TLC59711.cpp index 2a6267a..3a21bb8 100644 --- a/Adafruit_TLC59711.cpp +++ b/Adafruit_TLC59711.cpp @@ -17,7 +17,7 @@ #include #include -SPISettings SPI_SETTINGS(100, MSBFIRST, SPI_MODE0); +SPISettings SPI_SETTINGS(500000, MSBFIRST, SPI_MODE0); /*! * @brief Instantiates a new Adafruit_TLC59711 class From c9df7f3bad166c6b92a94633c13eb44aed442d40 Mon Sep 17 00:00:00 2001 From: Jan Hoffmann Date: Thu, 4 Apr 2019 14:49:20 +0200 Subject: [PATCH 3/4] added brightness control --- Adafruit_TLC59711.cpp | 54 +++++++++++++++++++++++++- Adafruit_TLC59711.h | 8 ++-- examples/tlc59711test/tlc59711test.ino | 15 ++++++- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/Adafruit_TLC59711.cpp b/Adafruit_TLC59711.cpp index 3a21bb8..0657f6c 100644 --- a/Adafruit_TLC59711.cpp +++ b/Adafruit_TLC59711.cpp @@ -33,7 +33,7 @@ Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, uint8_t c, uint8_t d) { _clk = c; _dat = d; - BCr = BCg = BCb = 0x7F; + BCr = BCg = BCb = 0x7F; // default 100% brigthness pwmbuffer = (uint16_t *)calloc(2, 12 * n); } @@ -51,7 +51,7 @@ Adafruit_TLC59711::Adafruit_TLC59711(uint8_t n, SPIClass *theSPI) { _dat = -1; _spi = theSPI; - BCr = BCg = BCb = 0x7F; + BCr = BCg = BCb = 0x7F; // default 100% brigthness pwmbuffer = (uint16_t *)calloc(2, 12 * n); } @@ -157,6 +157,56 @@ void Adafruit_TLC59711::setLED(uint8_t lednum, uint16_t r, uint16_t g, setPWM(lednum * 3 + 2, b); } +/*! + * @brief Set the brightness of LED channels to same value + * @param BC + * Brightness Control value + */ +void Adafruit_TLC59711::simpleSetBrightness(uint8_t BC) { + if (BC > 127) { + BC = 127; // maximum possible value since BC can only be 7 bit + } else if (BC < 0) { + BC = 0; + } + + BCr = BCg = BCb = BC; +} + +/*! + * @brief Set the brightness of LED channels to specific value + * @param bcr + * Brightness Control Red value + * @param bcg + * Brightness Control Green value + * @param bcb + * Brightness Control Blue value + */ +void Adafruit_TLC59711::setBrightness(uint8_t bcr, uint8_t bcg, uint8_t bcb) { + if (bcr > 127) { + bcr = 127; // maximum possible value since BC can only be 7 bit + } else if (bcr < 0) { + bcr = 0; + } + + BCr = bcr; + + if (bcg > 127) { + bcg = 127; // maximum possible value since BC can only be 7 bit + } else if (bcg < 0) { + bcg = 0; + } + + BCg = bcg; + + if (bcb > 127) { + bcb = 127; // maximum possible value since BC can only be 7 bit + } else if (bcb < 0) { + bcb = 0; + } + + BCb = bcb; +} + /*! * @brief Begins SPI connection if there is not empty PWM buffer * @return If successful returns true, otherwise false diff --git a/Adafruit_TLC59711.h b/Adafruit_TLC59711.h index 3ae5225..7706372 100644 --- a/Adafruit_TLC59711.h +++ b/Adafruit_TLC59711.h @@ -30,7 +30,7 @@ * TLC59711 Senor */ class Adafruit_TLC59711 { - public: +public: Adafruit_TLC59711(uint8_t n, uint8_t c, uint8_t d); Adafruit_TLC59711(uint8_t n, SPIClass *theSPI = &SPI); @@ -40,15 +40,15 @@ class Adafruit_TLC59711 { void setLED(uint8_t lednum, uint16_t r, uint16_t g, uint16_t b); void write(); void spiwriteMSB(uint8_t d); + void setBrightness(uint8_t bcr, uint8_t bcg, uint8_t bcb); + void simpleSetBrightness(uint8_t BC); - private: +private: uint16_t *pwmbuffer; uint8_t BCr, BCg, BCb; int8_t numdrivers, _clk, _dat; SPIClass *_spi; - }; - #endif diff --git a/examples/tlc59711test/tlc59711test.ino b/examples/tlc59711test/tlc59711test.ino index d6cbf89..05b414d 100644 --- a/examples/tlc59711test/tlc59711test.ino +++ b/examples/tlc59711test/tlc59711test.ino @@ -44,7 +44,7 @@ void loop() { colorWipe(0, 0, 65535, 100); // "Blue" (depending on your LED wiring) delay(200); - rainbowCycle(5); + increaseBrightness(); } @@ -83,3 +83,16 @@ void Wheel(uint8_t ledn, uint16_t WheelPos) { tlc.setLED(ledn, 0, 3*WheelPos, 65535 - 3*WheelPos); } } + +//All RGB Channels on full colour +//Cycles trough all brightness settings from 0 up to 127 +void increaseBrightness(){ + for(uint16_t i=0; i<8*NUM_TLC59711; i++) { + tlc.setLED(i, 65535, 65535, 65535); + } + for(int i = 0; i < 128; i++){ + tlc.simpleSetBrightness(i); + tlc.write(); + delay(1000); + } +} From a415c9ca99b385f6431b91300288d415218d11ab Mon Sep 17 00:00:00 2001 From: Jan Hoffmann Date: Thu, 4 Apr 2019 15:55:13 +0200 Subject: [PATCH 4/4] hotfix --- Adafruit_TLC59711.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Adafruit_TLC59711.cpp b/Adafruit_TLC59711.cpp index 0657f6c..70d2820 100644 --- a/Adafruit_TLC59711.cpp +++ b/Adafruit_TLC59711.cpp @@ -82,7 +82,9 @@ void Adafruit_TLC59711::spiwriteMSB(uint8_t d) { * @brief Writes PWM buffer to board */ void Adafruit_TLC59711::write() { - _spi->beginTransaction(SPI_SETTINGS); + if (_clk < 0) { + _spi->beginTransaction(SPI_SETTINGS); + } uint32_t command; @@ -121,8 +123,8 @@ void Adafruit_TLC59711::write() { delayMicroseconds(200); else delayMicroseconds(2); + _spi->endTransaction(); - _spi->endTransaction(); interrupts(); }