diff --git a/README.md b/README.md index 47af8ca..9122f7b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ TFT_22_ILI9225 ============== +**Version 1.2.2, released 2017-10-20, adds backlight brightness control.** Credit: [miro1360](https://github.com/miro1360) + +**Version 1.2.1, released 2017-09-18, fixed color table shift error.** + **Version 1.2.0, released 2017-01-18, speeds up Hardware SPI significantly.** This is a library for the ILI9225 TFT display products, forked from the screen_4D_22_library library. Ability to diff --git a/examples/Basic_Demo/Basic_Demo.ino b/examples/Basic_Demo/Basic_Demo.ino index 5383847..6b35114 100644 --- a/examples/Basic_Demo/Basic_Demo.ino +++ b/examples/Basic_Demo/Basic_Demo.ino @@ -9,10 +9,12 @@ #define TFT_CLK 13 // SCK #define TFT_LED 3 // 0 if wired to +5V directly +#define TFT_BRIGHTNESS 200 // Initial brightness of TFT backlight (optional) + // Use hardware SPI (faster - on Uno: 13-SCK, 12-MISO, 11-MOSI) -TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED); +TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, TFT_BRIGHTNESS); // Use software SPI (slower) -//TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_SDI, TFT_CLK, TFT_LED); +//TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_SDI, TFT_CLK, TFT_LED, TFT_BRIGHTNESS); // Variables and constants uint16_t x, y; diff --git a/keywords.txt b/keywords.txt index 0faaea7..98425ff 100644 --- a/keywords.txt +++ b/keywords.txt @@ -15,6 +15,7 @@ begin KEYWORD2 clear KEYWORD2 invert KEYWORD2 setBacklight KEYWORD2 +setBacklightBrightness KEYWORD2 setDisplay KEYWORD2 setOrientation KEYWORD2 getOrientation KEYWORD2 diff --git a/library.properties b/library.properties index 0a2f335..4d5a9e2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TFT_22_ILI9225 -version=1.2.2 +version=1.2.3 author=Nkawu maintainer=Nkawu sentence=ILI9225 2.2" 176x220 TFT LCD shield diff --git a/src/TFT_22_ILI9225.cpp b/src/TFT_22_ILI9225.cpp index a0221c8..72563c1 100644 --- a/src/TFT_22_ILI9225.cpp +++ b/src/TFT_22_ILI9225.cpp @@ -28,6 +28,20 @@ TFT_22_ILI9225::TFT_22_ILI9225(uint8_t rst, uint8_t rs, uint8_t cs, uint8_t sdi, _sdi = sdi; _clk = clk; _led = led; + _brightness = 255; // Set to maximum brightness + hwSPI = false; + checkSPI = true; +} + +// Constructor when using software SPI. All output pins are configurable. Adds backlight brightness 0-255 +TFT_22_ILI9225::TFT_22_ILI9225(uint8_t rst, uint8_t rs, uint8_t cs, uint8_t sdi, uint8_t clk, uint8_t led, uint8_t brightness) { + _rst = rst; + _rs = rs; + _cs = cs; + _sdi = sdi; + _clk = clk; + _led = led; + _brightness = brightness; hwSPI = false; checkSPI = true; } @@ -40,6 +54,21 @@ TFT_22_ILI9225::TFT_22_ILI9225(uint8_t rst, uint8_t rs, uint8_t cs, uint8_t led) _cs = cs; _sdi = _clk = 0; _led = led; + _brightness = 255; // Set to maximum brightness + hwSPI = true; + checkSPI = true; +} + +// Constructor when using hardware SPI. Faster, but must use SPI pins +// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.) +// Adds backlight brightness 0-255 +TFT_22_ILI9225::TFT_22_ILI9225(uint8_t rst, uint8_t rs, uint8_t cs, uint8_t led, uint8_t brightness) { + _rst = rst; + _rs = rs; + _cs = cs; + _sdi = _clk = 0; + _led = led; + _brightness = brightness; hwSPI = true; checkSPI = true; } @@ -127,12 +156,16 @@ void TFT_22_ILI9225::_setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t void TFT_22_ILI9225::begin() { - // Set up pins + // Set up reset pin if (_rst > 0) { pinMode(_rst, OUTPUT); digitalWrite(_rst, LOW); } - if (_led > 0) pinMode(_led, OUTPUT); + // Set up backlight pin, turn off initially + if (_led > 0) { + pinMode(_led, OUTPUT); + setBacklight(false); + } pinMode(_rs, OUTPUT); pinMode(_cs, OUTPUT); @@ -156,9 +189,6 @@ void TFT_22_ILI9225::begin() { *mosiport &= ~mosipinmask; } - // Turn on backlight - if (_led > 0) digitalWrite(_led, HIGH); - // Initialization Code if (_rst > 0) { digitalWrite(_rst, HIGH); // Pull the reset pin high to release the ILI9225C from the reset status @@ -239,6 +269,7 @@ void TFT_22_ILI9225::begin() { _writeRegister(ILI9225_DISP_CTRL1, 0x1017); if (hwSPI) spi_end(); + // Turn on backlight setBacklight(true); setOrientation(0); @@ -266,7 +297,14 @@ void TFT_22_ILI9225::invert(boolean flag) { void TFT_22_ILI9225::setBacklight(boolean flag) { - if (_led) digitalWrite(_led, flag); + blState = flag; + if (_led) analogWrite(_led, blState ? _brightness : 0); +} + + +void TFT_22_ILI9225::setBacklightBrightness(uint8_t brightness) { + _brightness = brightness; + setBacklight(blState); } diff --git a/src/TFT_22_ILI9225.h b/src/TFT_22_ILI9225.h index 810cecd..0c6a556 100644 --- a/src/TFT_22_ILI9225.h +++ b/src/TFT_22_ILI9225.h @@ -123,6 +123,8 @@ class TFT_22_ILI9225 { TFT_22_ILI9225(uint8_t RST, uint8_t RS, uint8_t CS, uint8_t SDI, uint8_t CLK, uint8_t LED); TFT_22_ILI9225(uint8_t RST, uint8_t RS, uint8_t CS, uint8_t LED); + TFT_22_ILI9225(uint8_t RST, uint8_t RS, uint8_t CS, uint8_t SDI, uint8_t CLK, uint8_t LED, uint8_t brightness); + TFT_22_ILI9225(uint8_t RST, uint8_t RS, uint8_t CS, uint8_t LED, uint8_t brightness); /// Initialization void begin(void); @@ -138,6 +140,10 @@ class TFT_22_ILI9225 { /// @param flag true=on, false=off void setBacklight(boolean flag); + /// Set backlight brightness + /// @param brightness sets backlight brightness 0-255 + void setBacklightBrightness(uint8_t brightness); + /// Switch display on or off /// @param flag true=on, false=off void setDisplay(boolean flag); @@ -299,12 +305,11 @@ class TFT_22_ILI9225 { uint16_t _maxX, _maxY, _bgColor; volatile uint8_t *mosiport, *clkport, *dcport, *rsport, *csport; - // int8_t _rst, _dc, _cs, _mosi, _sclk, _led, uint8_t _rst, _rs, _cs, _sdi, _clk, _led, - _orientation; + _orientation, _brightness; uint8_t mosipinmask, clkpinmask, cspinmask, dcpinmask; - boolean hwSPI, checkSPI; + boolean hwSPI, checkSPI, blState; _currentFont cfont;