Highly optimized ILI9341 (320x240 TFT color display) library

I'm actually new using github, hehe...

Is this what i need to do to create a pull request?

Screen Shot 2015-12-18 at 4.12.32 PM.png
 
GitHub and I didn't mesh and it took some getting used to for me have it work- then they changed it . . . managed a new Pull from GitHub in Windows - fights my learned cmdline source control instincts
@Vitor: you might want to open your own thread for this scroll work?
I changed my background from white to lightgrey and it shows characters going left justified to y=0 leaving their scroll pane - using the PJRC ComicSans font - somehow the width gets miscalculated on overflow, also the lowercase f&l are taller than all else (font issue?) in scroll #1 that starts green only?
This sample stops now for USB input - where the damage is done as I put a long string to each scroll. With SerMon - press Enter and it steps and repeats. Works perfectly with adafruit fonts that are used in 'even' passes
My sketch : View attachment scrollTest2e1.ino {needs ComicSansMS.? like your original}
 
I am using this display as an alarm clock which is part of an iOT network. There are three devices in the network using the teensy with esp8266 devices. The optimised library works great and the Arial font is just, well, lovely.

The issue I have is that I am writing strings to the display, but instead of clearing the whole screen between each update, I would like to write the string with both foreground and background color. This worked just great in the past using the stands Adafruit library (by using setcolor with background set to black). Is there a similar technique for the Arial and other fonts?

Thx,
Tom
 
And ... (drumroll), you are also updating the cursor, it seems. Again; thanks. It allows blanking the rectangle between the end of the text and the right hand border.
 
I wrote this function that given the old text writes it in known background color then writes the new string in foreground color - starting from the current cursor position on entry:
Code:
//Self Cleaning String print
void tftSCprintsz( char *oldV, char *newV, uint16_t fgc, uint16_t bgc ) {
  int xxt, yyt;
  tft.setTextColor(bgc);
  xxt = tft.getCursorX();
  yyt = tft.getCursorY();
  tft.print(oldV);
  tft.setCursor(xxt, yyt);
  tft.setTextColor(fgc);
  tft.print(newV);
  return;
}
 
blackener, you have also implement setcliprect which is very helpful for an alarm clock as the top line is just a large font with a 24Hour clock. Better than trying to manually do a clip rectangle. My expertise is in measurement and networking, hence appreciated that somebody has thought of this before. Where do I send the money?
 
I have a problem, setcliprect does not respect y1. I can do a cliprect from 0,0 to a x,y position but it can not clip from a,b to an a1,b1 position. Hence the following code does not do what I thought it would:
tft.setCursor(0, 0);
tft.setFont(Arial_72);
tft.setTextColor(ILI9341_BLUE,ILI9341_BLACK);
tft.setClipRect(0,0,tft.width(),73);
tft.printf("g%ld ",lct-=1);
tft.fillRect(tft.getCursorX(),tft.getCursorY(),tft.width()-tft.getCursorX(),73,ILI9341_BLACK);

tft.setClipRect(74,0,tft.width(),146);
tft.setTextColor(ILI9341_YELLOW,ILI9341_BLACK);
tft.printf("Monkey ");
tft.fillRect(tft.getCursorX(),tft.getCursorY(),tft.width()-tft.getCursorX(),73,ILI9341_BLACK);
 
Note that the X offset is included just for testing. I would expect to create a cliprect with y offsets and heights only. Please help with this.
 
It seems that the X coordinate of the clip region >0 is not recognised. I will do some more testing and posting of code to explain. Again it works for me compared to the old t3 code, with the only challenge being the arbitrary row print.
 
Hm, I don't see that problem here.

In the code you posted above, "tft.setClipRect(0,0,tft.width(),73);" clips to a rectangle 73 pixels high and the full width of the screen.

And "tft.setClipRect(74,0,tft.width(),146);" clips to a rectangle that starts 74 pixels from the left and extends the full width of the screen and also starts at the top and is 146 pixels tall.

Any drawing after either of these calls (printf or fillRect or anything else) will be clipped to within those rectangles, like a window.

I'm not sure exactly what you are trying to accomplish here Is it to clear the space after the text?
 
Ahem. You are right. I have now modified the code, and it now works as expected. Thanks for the help with this.
 
Is the touch functionality working properly? I've spent the afternoon trying to get things working using the configuration on the connections diagram (https://www.pjrc.com/store/display_ili9341_touch.html) but I'm having no luck. The display output is working fine.

I've been experimenting with onoffbutton and the Aidafruit STMPE610 touchtest projects. They are failing at Adafruit_STMPE610::begin(). Trying to isolate the problem I have swapped out the 2.8" TFT (I purchased two from pjrc) and the Teensy (I have a 3.1 and a 3.2).

It looks like the problem is caused by getVersion() returning OxF8F8 but expecting 0x811. This is addressed back in post 23 of this thread and evidently was fixed back then with a new release of the ILI9341_t3-master library. As a sanity check I just downloaded the latest from Git and rebooted but to no avail.

I'm guessing that somehow the SPI is getting messed up. Is there a different library that I should try to use or some other tests that I should try?

Thanks
 
Last edited:
I'm having a problem with the library. I'm running the adafruit ILI9341 on a Teensy 3.2. The Adafruit graphics test sketch works fine with the adafruit library, but slowly of course. The ili9341_t3 library and sketches compile and upload fine, but the Teensy locks up on the first call to any of the drawing functions. Tft.begin(), setTextColor, and setTextSize complete, but calling setRotation, fillScreen, drawPixel, etc, causes full lockup.

Code:
Code:
#include "SPI.h"
#include "ILI9341_t3.h"

#define TFT_DC      4
#define TFT_CS      5
#define TFT_RST    255  // 255 = unused, connect to 3.3V
#define TFT_MOSI     7
#define TFT_SCLK    9
#define TFT_MISO    8
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);

void setup() {
  Serial.begin(9600);
  while (!Serial && millis() < 1000) ; // wait for Arduino Serial Monitor
  Serial.println("ILI9341 Test!"); 
  tft.begin();
  Serial.println("tftbegin");
  tft.setTextColor(ILI9341_YELLOW);
  Serial.println("setcolor");
  tft.setTextSize(2);
  Serial.println("textsize");
tft.drawPixel(1,1,ILI9341_YELLOW);
Serial.println("Pixel Drawn");
}

void loop(void) {
}

Serial Output:
Code:
ILI9341 Test!
tftbegin
setcolor
textsize

It never prints the last Serial line because it locks up before drawing the requested pixel. Same thing happens with any sketch using ILI9341_t3, but they all work fine on adafruit_ILI9341. Any suggestions?

EDIT: Evidently all pins are not created equal. The custom pins I was using were fine for the ada library, but were not fast enough to keep up with the t3 library. Display is working with standard pin assignments now, and :eek: it's sooooo fast. Nice work.
 
Last edited:
So, I wrote some extra code. Pixel length of strings when using fonts, fixed linefeeds on font text, unpacking colors to RGB, and gradient rectangle fills. I thought I might contribute some of it back to the Github, but I wasn't sure what the best way to do it is.
I checked on the Adafruit version, and evidently they have split the code into board support and a separate GFX library that already does some of what I added myself, and has a lot of new features too.
So, should I be contributing my possibly redundant changes to your fork, or will there be some sort of re-synchronization with Adafruit that I should put my changes in?
 
I got what I believe is an equvalent display on ali express for 4 dollars and wanted to see if I coule use it to replace my oled on a project. I am currently getting errors. I am using sublime text with arduino 1.0.6 and newest teensyduino. It appears to mainly be an error of the SPI label so I thought maybe there is a different deffinition for 3.2? I am using the audio board and I have made the audio board changes in the doc on your teensy page for this lib

note: the actual display that I recieved is slightly different then pictured in the ali page. It has a micro sd and additional pins on the header that are all labeled NC Ill post a pic if I can figure out how.
http://www.aliexpress.com/item/Whol...39997.html?spm=2114.10010108.100010.47.It2BIh

I am trying out the adafruit lib now which at least compiles and uploads but no activity on the display. I also tried with arduino IDE 1.6.5.

[Stino - Start building "onoffbutton"...]
[ 1%] Creating /Users/AxiomCrux/Dropbox/Sketchbook/_Builds/onoffbutton/onoffbutton.ino.cpp.o...
In file included from /Applications/Arduino.1.0.6.app/Contents/Resources/Java/libraries/ILI9341_t3/examples/onoffbutton/onoffbutton.ino:8:0:
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::waitFifoNotFull()':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:219:9: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::waitFifoEmpty()':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:227:9: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::waitTransmitComplete()':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:233:12: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:234:9: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::waitTransmitComplete(uint32_t)':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:239:18: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:243:3: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::writecommand_cont(uint8_t)':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:250:3: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::writedata8_cont(uint8_t)':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:254:3: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::writedata16_cont(uint16_t)':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:258:3: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::writecommand_last(uint8_t)':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:263:3: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::writedata8_last(uint8_t)':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:268:3: error: 'KINETISK_SPI0' was not declared in this scope
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h: In member function 'void ILI9341_t3::writedata16_last(uint16_t)':
/Users/AxiomCrux/Dropbox/Sketchbook/libraries/ILI9341_t3/ILI9341_t3.h:273:3: error: 'KINETISK_SPI0' was not declared in this scope

[Stino - Exit with error code 1.]
 
Last edited:
Wow, this is a very long thread and I confess not having read it in its entirety. The fonts are great and work well with both the 2.2 and 2.8 inch displays from PJRC. They do take a lot of memory though, especially when the larger ones are being used.

In my case I am using only font_LiberationSansNarrowBold and have restricted it to 4 sizes. Still the combined memory hit was about 50 K for the sizes 12, 16, 48 and 96. I am using the small ones to display some descriptions and explanations, i.e. letters are used but the big ones only display numbers - measurements and parameters. This may be the case for many applications not just mine.

Large memory saving could be achieved, if numerics (in my case just numbers and a decimal) would be available as a separate library, which could be called font_LiberationSansNarrowBoldNumbers as an example.
 
Back
Top