Optimized ILI9341 TFT Library

Status
Not open for further replies.

luke.saber

Active member
I have used this library successfully with a different display. The adafruit 240px x 240px 220ppi display is the one I used. I am curious how to change the colors though, as they do not correlate with the names given to them. I looked into the hexadecimal values and broke down the binary values. They seem to be notted and in the opposite bit order. Any help would be greatly appreciated, thank you a ton for any help you can give me.
 
I know that I could simply replace the color definitions with the ones that are accurate to the display, but in case I want to use an arbitrary color not there I would like to have the input color equal the display's output color.
 
Sorry, maybe would help to know things like: Which optimized ILI9341 library you are using? ILI9341_t3?

Also which display? This one? https://www.adafruit.com/product/3787 ST7789 based?

Note: I mentioned in another thread I have a version of the ST7735 library that works on ST7789...

The obvious answer would be to look at the data sheets for both displays... And/Or the code bases.

Example the ili9341_t3.h file has:#define CL(_r,_g,_b) ((((_r)&0xF8)<<8)|(((_g)&0xFC)<<3)|((_b)>>3))

As for ST7789 I believe in 16 bit color mode it also uses the 5,6,5 bit encoding...
 
I apologize for being a little vague in my description there. Yes, I am using the ILI9341_t3 library. Also, the display that you have listed is the correct display that I am using. The code seems to mostly work, but the colors are messed up. There are pairs of colors that match each other's descriptions when used.
The bottom line of what I want to do is use a teensy 3.2 to run this display as fast as possible. I chose the ILI9341_t3 library simply because it works and utilizes the teensy 3.2 FIFO to be really fast. If there is an alternative library that also does this to achieve the same kind of speed then I am open to suggestions. If not, I am curious how I can approach this issue of mine.
 
I apologize for being a little vague in my description there. Yes, I am using the ILI9341_t3 library. Also, the display that you have listed is the correct display that I am using. The code seems to mostly work, but the colors are messed up. There are pairs of colors that match each other's descriptions when used.
The bottom line of what I want to do is use a teensy 3.2 to run this display as fast as possible. I chose the ILI9341_t3 library simply because it works and utilizes the teensy 3.2 FIFO to be really fast. If there is an alternative library that also does this to achieve the same kind of speed then I am open to suggestions. If not, I am curious how I can approach this issue of mine.

The linked display is the 7789 so using the recent drive @KurtE has done on the T4_Beta thread may be the one to try.

I have that loaded on a T4 now - for a ST7735 with only 128x128 bits and plugging an ILI9341 in place of that the image is inverted and the area of the Quarter of the screen not covered is just flashing white.

So suggest using the proper library and it should come together.

Updated _GFX :: github.com/KurtE/Adafruit-GFX-Library/tree/T4_Beta

And Beta display lib :: github.com/KurtE/ST7735_t3/tree/T4_beta

Assuming both of those still run on T_3.2's? Would be good to comment if you find otherwise.
 
Changes are needed at some point to the _GFX code because it was updated by Adafruit. If you put that copy in your (sketch)\libraries folder it should show as being used in the verbose build console.

@KurtE is modifying those two in sequence - so using them together will assure any problems are not from a conflict between them.

With the 7735 code in \libraries - it has some alternate code for the 7789 - I'm not sure how to select that for the display you have - but you'll see it is there.
 
I updated the GraphicTest program for that branch to do like what was done in the Adafruit St7735 display library, and included both headers and put the
definition for using the ST7789 as a comment and likewise it uses a different tft.init(240, 240); Again commented out.
 
So, the library that @defragster recommended to me works great and runs at the speed I want it to. I would be perfectly happy if not for this. I want to use the setRotation function to set the rotation to 2 and turn the diaplay 180 degrees. After trying to do so, The display rotates 180 degrees, but is shifted downwards almost halfway on the screen, which isn't so great. The same thing happens when setting the rotation value to 3. I figured it was something to do with the y value having something unnecessary added or subtracted from it, but I am not sure. After searching in the .h and .cpp files of SD7789_t3, SD7735_t3, and the new Adafruit-GFX-Library that were supplied in the files suggested to me, I found that not much would help my situation. Any ideas?
 
@luke.saber - which display do you have? ST7735 or ST7789? Guessing ST7735? Which size 128x128?

There are probably a few different versions of these, where some use the top of memory and some use the lower part of memory.
 
As a last result, I can always re-wire the screen and physically turn it 180 degrees, but I would like to do that through the code first if possible.
 
I will take a look and see. There is probably some offset or the like that is not properly set. Now that know which type display I will try it out...
 
Confirmed issue... Current simple test:
Code:
#include <Adafruit_GFX.h>    // Core graphics library
#include <ST7735_t3.h> // Hardware-specific library
#include <ST7789_t3.h> // Hardware-specific library
#include <SPI.h>

#if defined(__IMXRT1052__) || defined(__IMXRT1062__)  // Teensy 4.x
//#define USE_SPI2
//#define NON_SPI_PINS
#ifdef USE_SPI2
#define TFT_SCLK 37  // SCLK can also use pin 14
#define TFT_MOSI 35  // MOSI can also use pin 7
#elif defined(NON_SPI_PINS)
#define TFT_SCLK 7  // SCLK can also use pin 14
#define TFT_MOSI 8  // MOSI can also use pin 7
#else
#define TFT_SCLK 13  // SCLK can also use pin 14
#define TFT_MOSI 11  // MOSI can also use pin 7
#endif
#define TFT_CS   10  // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define TFT_DC    9  //  but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#define TFT_RST   23  // RST can use any pin
#define SD_CS     4  // CS for SD card, can use any pin
#else
// T3.X
//#define USE_SPI1
#define USE_SPI2
#ifdef USE_SPI1
#define TFT_SCLK 32  // T3.5/T3.6
#define TFT_MOSI 0   // 
#define TFT_CS   10  // random digital pin
#define TFT_DC   31   // Only Hardware CS pin on SPI1
#elif defined(USE_SPI2)
#define TFT_SCLK 46  // T3.5/T3.6
#define TFT_MOSI 44   // 
#define TFT_CS   54  // Hardware CS on SPI2
#define TFT_DC   55   // Hardware CS pin on SPI2
#else
#define TFT_SCLK 13  // SCLK can also use pin 14
#define TFT_MOSI 11  // MOSI can also use pin 7
#define TFT_CS   10  // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define TFT_DC    9  //  but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#endif
#define TFT_RST   8  // RST can use any pin
#define SD_CS     4  // CS for SD card, can use any pin
#endif

// Option 1: use any pins but a little slower
//ST7735_t3 tft = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

// Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
//ST7735_t3 tft = ST7735_t3(cs, dc, rst);
float p = 3.1415926;


void setup(void) {
  pinMode(SD_CS, INPUT_PULLUP);  // don't touch the SD card
  Serial.begin(9600);
  Serial.print("hello!");

  // Use this initializer if you're using a 1.8" TFT
  //tft.initR(INITR_BLACKTAB);
  // Use this initializer (uncomment) if you're using a 1.44" TFT
  //tft.initR(INITR_144GREENTAB);
  tft.init(240,240);  // use for ILI9488
  
  Serial.println("init");
}

uint8_t rotation = 0;
void loop() {
  tft.setRotation(rotation);
  Serial.printf("Set Rotation: %d width: %d height: %d\n", rotation, tft.width(), tft.height());
  rotation = (rotation + 1) & 0x3;
  elapsedMillis timer;
  uint16_t time = millis();
  tft.fillScreen(ST7735_RED);
  tft.fillRect(tft.width()/2-32, 20, 64, tft.height()-40, ST7735_GREEN);
  tft.fillRect(0, 0, 8, 8, ST7735_BLACK);
  tft.fillRect(tft.width()-8, tft.height()-8, 8, 8, ST7735_WHITE);
  // large block of text
  //delay(2500);
  Serial.println("Hit any key to continue");
  while (!Serial.available()) ; 
  while (Serial.read() != -1) ;
}
Sort of more complicated looking than it needs to be, but should handle both types of displays plus some other SPI ports...
Confirmed does not look right for rotation 2 and 3.... Investigating

EDIT/Update - Also confirmed same issue in Adafruit library as well, changed includes and object define to:
Code:
#include <Adafruit_ST7735.h>
#include <Adafruit_ST7789.h>
#include <Adafruit_ST77xx.h>
...
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
Still show wrong location for position 0, 0 (Black square)...
I created an issue on Adafruit: https://github.com/adafruit/Adafruit-ST7735-Library/issues/81
 
Last edited:
Just out of curiosity, what did you change? I'd like to see what u did in the code if you wouldn't mind. File names and line numbers would be great.
 
Just out of curiosity, what did you change? I'd like to see what u did in the code if you wouldn't mind. File names and line numbers would be great.
Probably the easiest thing to do is to look up on github:

https://github.com/KurtE/ST7735_t3/commit/1aa7194c71026fe238b3944e7efc29fe9016ca16

Also others - Made similar fix for ST7735 - Note I have something like green tab 128x128... Works with mine... Would be good if others tried as well with their ST7735 displays
 
My only other questions is this. Can I use other SPI devices while this library is using the SPI bus? I want to have a SPI nrf24L01 radio used in this system as well, but I am not sure how the different libraries will work together when considering the differences in SPI bus speed. Thoughts?
 
In theory it should work. Hopefully both set of libraries do things like SPI.beginTransaction(...)/endTransaction and assert and deassert their CS pins...
And hopefully neither device does something like hold the MISO pin in some state or ...
 
Both libraries work correctly on their own, I just don't know if they will conflict with each other cause of the advanced speed that the display uses. Am I be worrying over nothing, but I just wanna make sure I'm not gonna have any conflicts with SPI protocol.
 
So I have one more question for the moment. What would be the proper syntax to draw a bitmap onto the display that is stored in the code itself. I have a bitmap embedded in the code already, but am not sure how to display it on the screen. Any ideas? Thanks
 
Depends...

Adafruit_gfx I think has a drawBitmap function for monochrome bitmaps for color, my one branch has function writeRect which was brought over from ili9341_t3 library... it assumes a 16 color value for each pixel in rectangle...

There are other formats supported in ili9341_t3n library, not sure how many made it into other libraries
 
Is it possible to draw my bitmap based on the drawRGBBitmap function in the Adafruit GFX library. The code is in the earlier mentioned version of the GFX library, but it does not show the function as available in the IDE. is it possible to use this one for my bitmap? My concern is that the writeRect function will leave a large square around my bitmap in white or something. The drawBitmap function troubles me because it implies a monochrome basis for the bitmap. If I want to put something more colorful in my project eventually, I want to make sure that I can show only my bitmap and nothing behind it with all of the colors it includes. Which is the best way to go about drawing the bitmap this way? Any help is appreciated.
 
Status
Not open for further replies.
Back
Top