Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 2 1 2 LastLast
Results 1 to 25 of 42

Thread: Optimized ILI9341 TFT Library

  1. #1

    Optimized ILI9341 TFT Library

    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.

  2. #2
    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.

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    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...

  4. #4
    Senior Member Projectitis's Avatar
    Join Date
    Feb 2018
    Location
    New Zealand
    Posts
    146
    @luke.saber, if bit order is the issue take a look at these bit-hacks for reversing bit order:
    https://graphics.stanford.edu/~seand...ReverseObvious

    I also have a python script as part of my tilemap code (colors.py) which I use to generate color codes for different pixel formats (RGB888, RGB444, RGB565 etc).
    If you are ok with python, feel free to modify the script to add RGB565 in reverse order (here is how to reverse bits in python).

  5. #5
    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.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,060
    Quote Originally Posted by luke.saber View Post
    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.

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,060
    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.

  8. #8
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    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.

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,060
    Sounds great @KurtE - that example helpful to know about.

  10. #10
    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?

  11. #11
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    @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.

  12. #12
    The display I have is a ST7789 with 240 x 240. The link to my display is in an earlier message with you.

  13. #13
    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.

  14. #14
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    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...

  15. #15
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    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...rary/issues/81
    Last edited by KurtE; 06-27-2019 at 01:49 PM.

  16. #16
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    OK I pushed up changes for ST7789_t3 that hopefully make rotation(2) and rotation(3) work better.
    https://github.com/KurtE/ST7735_t3/tree/T4_beta

    May need to see if similar change might help ST7735 as well

  17. #17
    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.

  18. #18
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    Quote Originally Posted by luke.saber View Post
    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/c...fc29fe9016ca16

    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

  19. #19
    I implemented the changes that you posted and it works great now. Thanks a ton!

  20. #20
    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?

  21. #21
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    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 ...

  22. #22
    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.

  23. #23
    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

  24. #24
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,066
    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

  25. #25
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •