Added methods and features
Fork of SPI_TFT_ILI9341 by
Revision 10:2d505d14b7eb, committed 2014-04-12
- Comitter:
- wim
- Date:
- Sat Apr 12 20:33:24 2014 +0000
- Parent:
- 9:6d30a225a5c7
- Child:
- 11:7aabc3810093
- Commit message:
- Added more methods and docs
Changed in this revision
SPI_TFT_ILI9341.cpp | Show annotated file Show diff for this revision Revisions of this file |
SPI_TFT_ILI9341.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SPI_TFT_ILI9341.cpp Mon Apr 07 20:25:09 2014 +0000 +++ b/SPI_TFT_ILI9341.cpp Sat Apr 12 20:33:24 2014 +0000 @@ -25,8 +25,19 @@ SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name) //WH : _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc), GraphicsDisplay(name) - : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc) + : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _dc(dc) { + + // The hardware Reset pin is optional. Test and make sure whether it exists or not to prevent illegal access. + if (reset != NC) { + _reset = new DigitalOut(reset, 1); //Construct new pin, Deactivated +// _reset->write(1); //Deactivate + } + else { + // No Hardware Reset pin + _reset = NULL; //Construct dummy pin + } + //WH clk = sclk; //WH orientation = 0; _origin = Origin_LeftTop; @@ -35,10 +46,22 @@ _transparancy = false; // set_font(Arial12x12); //Default font // set_font(FONT8x8); //Default font, shame it doesnt fit format.. waste of flash space at moment - + tft_reset(); } + +/** Destruct a SPI_TFT LCD object + * + * @param none + * @return none + */ +SPI_TFT_ILI9341::~SPI_TFT_ILI9341() { + if (_reset != NULL) {delete _reset;} // HW Reset pin +} + + + int SPI_TFT_ILI9341::width() { // if (orientation == 0 || orientation == 2) return 240; @@ -110,6 +133,16 @@ _transparancy = state; } +// HW Reset to tft +void SPI_TFT_ILI9341::_hwreset() +{ + // _reset is an optional pin which defaults to NC. Make sure it does not hang mbed lib + if (_reset != NULL) {_reset->write(0);} //Clear _reset pin + wait_us(50); + if (_reset != NULL) {_reset->write(1);} //Set _reset pin + wait_ms(5); +} + // write command to tft register void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd) @@ -187,18 +220,15 @@ //WH _spi.format(8,3); // 8 bit spi Mode 3 _spi.format(8,0); // 8 bit spi mode 0 - _spi.frequency(10000000); // 10 Mhz SPI ... should work on current version of mbed F103 lib after fix for HSI/HSE... // _spi.frequency(4000000); // 4 Mhz SPI clock // _spi.frequency(8000000); // 8 Mhz SPI clock - + _spi.frequency(10000000); // 10 Mhz SPI ... works on current version of mbed F103 lib after fix for HSI/HSE... + _cs = 1; // cs high _dc = 1; // dc high - _reset = 0; // display reset - wait_us(50); - _reset = 1; // end hardware reset - wait_ms(5); - + _hwreset(); // HW reset + //WH wr_cmd(0x01); // SW reset wr_cmd(ILI9341_DISPLAY_RST); // SW reset wait_ms(5); @@ -495,6 +525,71 @@ #endif + + +void SPI_TFT_ILI9341::newcls (void) +{ + int pixels = height() * width(); + int i; + int color = _background; +#if (SPI_16 != 1) + int msb, lsb; +#endif + + window(0,0,width(),height()); + + wr_cmd(ILI9341_GRAM); // send pixel + +#if (SPI_16 == 1) + // 16 Bit SPI + _spi.format(16,0); // switch to 16 bit Mode 0 + + //unroll loop in chunks of 8 pixels + for (i = 0; i < (pixels>>3); i++) { + _spi.write(color); + _spi.write(color); + _spi.write(color); + _spi.write(color); + + _spi.write(color); + _spi.write(color); + _spi.write(color); + _spi.write(color); + } + + //remainder + for (i = 0; i < (pixels & 0x07); i++) + _spi.write(color); + + _spi.format(8,0); // switch back to 8 bit Mode 0 +#else + // 8 Bit SPI + msb = color >> 8; + lsb = color & 0xff; + + for (i = 0; i < (pixels>>3); i+=8) { + _spi.write(msb); _spi.write(lsb); + _spi.write(msb); _spi.write(lsb); + _spi.write(msb); _spi.write(lsb); + _spi.write(msb); _spi.write(lsb); + + _spi.write(msb); _spi.write(lsb); + _spi.write(msb); _spi.write(lsb); + _spi.write(msb); _spi.write(lsb); + _spi.write(msb); _spi.write(lsb); + } + + for (i = 0; i < (pixels & 0x07); i++) { + _spi.write(msb); _spi.write(lsb); + } +#endif + + _cs = 1; +} + + + + void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color) { @@ -673,7 +768,9 @@ void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color) { int i, w; +#if (SPI_16 != 1) int msb, lsb; +#endif w = x1 - x0 + 1; window(x0,y,w,1); // wr_cmd(0x2C); // send pixel @@ -730,7 +827,9 @@ void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color) { int i, h; +#if (SPI_16 != 1) int msb, lsb; +#endif h = y1 - y0 + 1; window(x,y0,1,h); @@ -886,17 +985,22 @@ int h = y1 - y0 + 1; int w = x1 - x0 + 1; int pixels = h * w; - int i, msb, lsb; + int i; +#if (SPI_16 != 1) + int msb, lsb; +#endif window(x0,y0,w,h); // wr_cmd(0x2C); // send pixel wr_cmd(ILI9341_GRAM); // send pixel -#if (SPI_16 == 1) +#if (SPI_16 == 1) // 16 Bit SPI _spi.format(16,0); // switch to 16 bit Mode 0 + for (i = 0; i < pixels; i++) _spi.write(color); + _spi.format(8,0); // switch back to 8 bit Mode 0 #else // 8 Bit SPI
--- a/SPI_TFT_ILI9341.h Mon Apr 07 20:25:09 2014 +0000 +++ b/SPI_TFT_ILI9341.h Sat Apr 12 20:33:24 2014 +0000 @@ -26,8 +26,8 @@ #define SPI_16 0 #else //Disable anyhow since 16 bit SPI does not really seem to improve performance.. -#define SPI_16 0 -//#define SPI_16 1 +//#define SPI_16 0 +#define SPI_16 1 #endif /*Enable characters with transparant background color */ @@ -54,7 +54,7 @@ //#define min(a,b) (((a)<(b))?(a):(b)) //#define ABS(X) ((X) > 0 ? (X) : -(X)) -/* Some RGB color definitions in 888 format */ +/* Some RGB color definitions in 565 format and equivalent 888 format */ #define Black 0x0000 /* 0, 0, 0 */ #define Navy 0x000F /* 0, 0, 128 */ #define DarkGreen 0x03E0 /* 0, 128, 0 */ @@ -181,12 +181,20 @@ * @param miso pin connected to SDI of display * @param sclk pin connected to RS of display * @param cs pin connected to CS of display - * @param reset pin connected to RESET of display + * @param reset pin connected to RESET of display (may be NC) * @param dc pin connected to WR of display * The IM pins have to be set to 1110 (3-0). Note: the M24SR board uses 0110 which also works. */ SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT"); - + + + /** Destructor for SPI_TFT object + * @param none + * @return none + */ + virtual ~SPI_TFT_ILI9341(); + + /** Get the width of the screen in pixels * * @returns width of screen in pixels @@ -309,6 +317,15 @@ */ virtual void cls(); + + /** Fill the screen with _background color + * @param none + * @return none + */ + virtual void newcls(); + + + /** calculate the max number of char in a line * * @returns max columns @@ -483,6 +500,11 @@ */ void tft_reset(); + + /** HW Reset to ILI9341 controller + */ + void _hwreset(); + /** Write data to the LCD controller * @@ -518,14 +540,20 @@ /** Read byte from the LCD controller * - * @param cmd comand to controller + * @param cmd command to controller * @returns data from LCD controller * */ char rd_byte(unsigned char cmd); - int rd_32(unsigned char cmd); + /** Read int from the LCD controller + * + * @param cmd command to controller + * @returns data from LCD controller + * + */ + int rd_32(unsigned char cmd); /** Write a value to the to a LCD register @@ -545,7 +573,7 @@ SPI _spi; DigitalOut _cs; - DigitalOut _reset; + DigitalOut* _reset; DigitalOut _dc; unsigned char* _font;