adafruit tft lcd commands quotation

Hi there, I recently picked up an Adafruit 2.8"" TFT display for use with a Raspberry Pi 2. I installed the recommended Adafruit image, updated Raspbian, and everything seems to be working great, but have found the screen is so small, I can"t actually make use of the entire desktop. For instance, when I open a window, it cuts short, preventing me from accessing the lower options and commands. Most of the icons overlap, too. I"ve tried using the ctrl-left-click method to scan across the desktop, but that doesn"t seem to be working. I"ve also tried installing Kodi, but it doesn"t seem to want to load, either. Lastly, I tried changing the resolution settings through config.txt, but they haven"t seemed to have taken effect. Would I be right in saying that a fixed resolution may be hard-coded into the screen itself?

adafruit tft lcd commands quotation

Only US$26.24, buy best geekcreit® uno r3 improved version + 2.8tft lcd touch screen + 2.4tft touch screen display module kit geekcreit for arduino - products that work with official arduino boards sale online store at wholesale price.

adafruit tft lcd commands quotation

Displays are one of the best ways to provide feedback to users of a particular device or project and often the bigger the display, the better. For today’s tutorial, we will look on how to use the relatively big, low cost, ILI9481 based, 3.5″ Color TFT display with Arduino.

This 3.5″ color TFT display as mentioned above, is based on the ILI9481 TFT display driver. The module offers a resolution of 480×320 pixels and comes with an SD card slot through which an SD card loaded with graphics and UI can be attached to the display. The module is also pre-soldered with pins for easy mount (like a shield) on either of the Arduino Mega and Uno, which is nice since there are not many big TFT displays that work with the Arduino Uno.

To easily write code to use this display, we will use the GFX and TFT LCD libraries from “Adafruit” which can be downloaded here. With the library installed we can easily navigate through the examples that come with it and upload them to our setup to see the display in action. By studying these examples, one could easily learn how to use this display. However, I have compiled some of the most important functions for the display of text and graphics into an Arduino sketch for the sake of this tutorial. The complete sketch is attached in a zip file under the download section of this tutorial.

As usual, we will do a quick run through of the code and we start by including the libraries which we will use for the project, in this case, the Adafruit GFX and TFT LCD libraries.

With this done, the Void Setup() function is next. We start the function by issuing atft.reset() command to reset the LCD to default configurations. Next, we specify the type of the LCD we are using via the LCD.begin function and set the rotation of the TFT as desired. We proceed to fill the screen with different colors and display different kind of text using diverse color (via the tft.SetTextColor() function) and font size (via the tft.setTextSize() function).

The Adafruit library helps reduce the amount of work one needs to do while developing the code for this display, leaving the quality of the user interface to the limitations of the creativity and imagination of the person writing the code.

adafruit tft lcd commands quotation

In this Arduino touch screen tutorial we will learn how to use TFT LCD Touch Screen with Arduino. You can watch the following video or read the written tutorial below.

As an example I am using a 3.2” TFT Touch Screen in a combination with a TFT LCD Arduino Mega Shield. We need a shield because the TFT Touch screen works at 3.3V and the Arduino Mega outputs are 5 V. For the first example I have the HC-SR04 ultrasonic sensor, then for the second example an RGB LED with three resistors and a push button for the game example. Also I had to make a custom made pin header like this, by soldering pin headers and bend on of them so I could insert them in between the Arduino Board and the TFT Shield.

Here’s the circuit schematic. We will use the GND pin, the digital pins from 8 to 13, as well as the pin number 14. As the 5V pins are already used by the TFT Screen I will use the pin number 13 as VCC, by setting it right away high in the setup section of code.

I will use the UTFT and URTouch libraries made by Henning Karlsen. Here I would like to say thanks to him for the incredible work he has done. The libraries enable really easy use of the TFT Screens, and they work with many different TFT screens sizes, shields and controllers. You can download these libraries from his website, RinkyDinkElectronics.com and also find a lot of demo examples and detailed documentation of how to use them.

After we include the libraries we need to create UTFT and URTouch objects. The parameters of these objects depends on the model of the TFT Screen and Shield and these details can be also found in the documentation of the libraries.

So now I will explain how we can make the home screen of the program. With the setBackColor() function we need to set the background color of the text, black one in our case. Then we need to set the color to white, set the big font and using the print() function, we will print the string “Arduino TFT Tutorial” at the center of the screen and 10 pixels  down the Y – Axis of the screen. Next we will set the color to red and draw the red line below the text. After that we need to set the color back to white, and print the two other strings, “by HowToMechatronics.com” using the small font and “Select Example” using the big font.

adafruit tft lcd commands quotation

Sounds like you are using proportionally-spaced fonts instead of the original classic fonts that ships with Adafruit_GFX. Historically, when using the default classic fonts one could set the background color option of the text to the same color as the background of the screen (usually black). This would overwrite the old screen contents with new data and work because the characters using the classic fonts are a uniform size. When using proportionally-spaced fonts, the background color option for the font is disabled by design.

I hope that I have understood what the nature of your problem is and answered it in a satisfactory manner. If nothing else, at least now you know why custom font"s will not work with the so called background color feature that works with the original "classic" Adafruit fonts.

adafruit tft lcd commands quotation

Just wanted to put a note on here for anyone trying to connect the Adafruit 2.8" TFT Resistive Touch Shield to a Photon, through a Particle Shield Shield. I couldn’t get it to work, then figured this out and thought I’d share the knowledge…

The TFT shield is hardwired to use the default hardware SPI pins for Arduino, but on the Shield Shield, these pins actually map to Particle’s SPI1, not SPI (there are two SPI ports on the Particle…I’ve read there is perhaps a third?).

This will have your Particle talking to the correct pins of the TFT shield, through the Shield Shield. And of course, if coming from Arduino, you will need to do the other typical porting changes covered in this and other threads.

adafruit tft lcd commands quotation

After that I have been able to connect the touch-sensitive part of the screen as well, and it reads x+, x-, Y+ and y- fairly well using Touch-Screen-Library (https://github.com/adafruit/Touch-Screen-Library) (I still have to some more calibration though).

I have been running into problems when I wanted to actually display something on the screen using TFT LCD library (https://github.com/adafruit/TFTLCD-Library) (and the GFX library (https://github.com/adafruit/Adafruit-GFX-Library))...

Connect the seventh pin RST (Reset) to the Arduino Reset line. This will reset the panel when the Arduino is Reset. You can also use a digital pin for the LCD reset but this will save us a pin.

I checked Nick Gammons Uno R3 reference sheet (http://www.adafruit.com/blog/2012/05/25/handy-arduino-r3-pinout-diagram/) and can only see that Analog 0-3 are not used for any special communications protocol and should be handled as "default" analog pins.

Adafruit is also selling a shield version of you display and I think you can use this to verify you pin assignment. With this: https://github.com/adafruit/TFTshield/blob/master/schem.png

I just got my teensy3 displaying the graphicstest on my LCD. Your description here of how to do it was just fine, though it took me a couple of tries to get the wiring right (my fault, though)

I got this going as well thanks to your guidance. While I was fiddling with the pinouts etc. in pinmagic, I did a couple of other things, in Adafruit_TFTLCD:

inline Adafruit_GFX& operator() (uint8_t x, uint8_t y,uint16_t c) {setCursor(x,y); setTextColor(c); return *this;} //use along w Streaming.h to support: tft(col,line,color)<<"a="<

inline Adafruit_GFX& operator() (uint8_t x, uint8_t y,uint16_t c,uint8_t s) {setCursor(x,y); setTextColor(c); setTextSize(s); return *this;} //use along w Streaming.h to support: tft(col,line,color,size)<<"a="<

http://www.ebay.de/itm/New-2-4-inch-TFT-LCD-Module-display-240-x-320-Screen-ILI9325-with-touch-pen-/281090515728?pt=UK_BOI_Electrical_Components_Suppl ies_ET&hash=item41724ce310

Just wanted to say: I just got this working and compared the speeds of this screen on a Teensy 3.1 vs a Teensy ++2.0. Holy CRAP the 3.1 is fast! The Adafruit graphics test on the Teensy ++ 2.0 takes 10,565ms to run (according to it"s Serial Monitor metrics), while the Teensy 3.1 does it in 1903ms. 5.55x faster! I"m assuming it"s not the full 6x faster (16mhz vs 96mhz) due to overhead of using pinMode (and probably some other stuff) for the T3.

Yes, sort of. I"m using an older version, that"s been modified to work with Teensy 3, Teensy 2++, and I macheted in support for the ILI9341 driver before Adafruit did on their version.

They are not using the current library, as you guessed. I tried patching pin_magic.h with the teensy 3 mods, but more is required for the current TFTLCD library. The library now supports DUE, and the constructor does a lot of work mapping the control pins into port register access for set/clear operations. Maybe the teensy equivalent would be to use

OK, I got teensy 3.0 and 3.1 working with current Adafruit libraries. For teensy 3.1, I had to add delayMicroseconds(1) to read8inline so it looks like

And I measured 32.7ma at 3.3v for the TFTLCD, so I powered it from the teensy 3 3.3v pin. This is on older Adafruit TFTLCD, http://www.adafruit.com/products/335

OK, maybe I"m confused, but does that library drive the Adafruit display in 8-bit mode (as described at https://learn.adafruit.com/adafruit-2-dot-8-color-tft-touchscreen-breakout-v2/8-bit-wiring)? The TFTLCD library has the disadvantage of needing more pins, but my impression was that it could achieve a higher throughput of data to the display, or am I mistaken in that as well?

Well, this is awkward. I had a 2.8" ILI9325 screen from Adafruit working on a Teensy 3.0 a couple of years back. I just tried to rebuild the code for that and I seem to have lost some files. Specifically I"m looking for the right Adafruit_TFTLCD library (well, I think that"s all I"m looking for). The current library from Github doesn"t compile with a Teensy3, and I recall I had to edit the pin_magic file as per ZTiK.nl (earlier in this thread). But the later files from Github look different now. I don"t seem to be able to go back to an earlier branch on Github.

adafruit tft lcd commands quotation

A while ago, I noticed an article on Hackaday about how Paul Stoffregen (and crew) had optimized the Adafruit GFX SPI LCD driver for the Teensy 3.1 to achieve "warp speed" (see TFT LCDS HIT WARP SPEED WITH TEENSY 3.1). It was a nice demonstration of using advanced features of the 32-bit Teensy 3.1 micro-controller (along with code optimization). They used things like hardware /CS control and a hardware SPI FIFO to really speed things up from the generic Arduino API version (even when recompiled for the faster Teensy 3.1). Previously I had purchased an Adafruit SPI LCD breakout board that used this same controller and found it to be disappointingly slow (my AVR LCD gaming dreams were mostly dashed, and I didn"t do much with it). At the time I just chalked it up to the fact that 8-bit AVR just wasn"t up to the task of LCD graphics (especially over a slow-ish SPI bus). After seeing the impressive gain that the Teensy 3.1 was able to get, I decided it would be interesting to see if I could perhaps significantly speed up the library for Arduino AVR users without any "fancy 32-bit hardware". [Even though I have a Teensy 3.1 and they are great, I like an optimization challenge. :-) ]

In the "before" logic analyzer capture picture, you can see part of a "drawPixel" command being sent from the AVR 328P to the LCD controller over the SPI bus. In case you aren"t familiar with SPI or logic analyzer captures, the important thing to notice here is "channel-3". This is the SPI clock signal (called SCK). It goes high and then low once for every bit transfered over the SPI bus. The Adafruit library normally uses the AVR hardware SPI channel, as it is in this case (it can also "bit-bang" SPI, but that is much slower). They "crank up" the SPI speed to the maximum supported by a 16MHz AVR, which is 8MHz (this means one bit can be transferred every two AVR clock cycles). So the "blue chunks" on channel-3 represent 8-bits getting sent over the SPI bus (or one byte). Now, this is the "fastest" speed that the AVR can possibly send data over SPI (I believe the LCD SPI controller can go a maximum of ~25MHz and some micro-controllers and devices support 100MHz SPI or more, for comparison). However, while the bytes are clocked out at the maximum (fixed) speed, you can see there is a lot of "dead time" between each byte that could in theory be used to speed things up. Another thing I noticed is that the Adafruit library toggles the /CS signal on channel-0 almost every single byte sent. The /CS signal is "chip-select", when it is low the LCD will listen to the SPI bus (that "/" means active-low signal), when it is high it will ignore the bus so it can be shared with another device). Since we are going to be sending a whole bunch of commands all to the LCD, it seemed to me we can just pull /CS low once, do a bunch of commands (until we are going to return to the calling sketch) and then restore /CS back to high once (in case the SPI bus will be used to talk to another device, like the SD card that is on many of these LCD modules).

I also did some examination of the "actual code" that runs for the library. To do this I needed to disassemble the code. Everything you need to do this is included in the IDE (which includes a complete "GNU toolchain" for AVR). I first enabled the "verbose compilation" option in the Arduino IDE and compiled an Adafruit GFX benchmark sketch. Then I opened a command prompt (I am using Windows, but this is much the same on Linux or OS X) and "cd""d to the temporary folder the IDE was using (cut and paste is handy here, and you may also need to add double quotes if there is a space in any paths - Windows users may need to "reverse" slashes too). Then I cut and pasted the path the IDE was using for the toolchain commands (e.g., in front of "avr-gcc"), Then, without hitting return, I appended "avr-objdump -d --demangle *.elf" and redirected the output to a file (e.g., using "> disassembly.txt"). Okay, now I was able to examine this file and see what kind of code was being generated for the various graphics and SPI functions. I admit it is not always easy to decipher the assembly commands, but it is still a useful skill. The main things I noticed poking around were that virtual function calls and "this" pointer member access was costing a fair number of operations (and cycles). This added virtual overhead gets added whenever a library calls gets "redirected" to a driver library (and many hundreds of primitives can be used drawing something complex). Since the AVR is an 8-bit MCU with 16-bit pointers, every pointer operation generally takes at least two operations, this is made worse by the fact it only has a few special pointer register pairs. Of the three pairs (X, Y and Z) the compiler generally uses one for the stack, so it ends up shuffling other pointers around a lot. On 32-bit processors, the overhead from "this" pointer access and (generally) from virtual functions is not so great (or non-existent), but on AVR, when multiplied by each graphic primitive operation it can really add up (and contributes to the large "dead spaces" between the SPI bytes mentioned earlier). It also uses a handful of extra bytes for each primitive call (using flash space faster) to load the "this" pointer (which acts as an invisible first argument to member functions).

One tenet of C (and to a lesser extent C++) is the design principle, "if you don"t use it, you shouldn"t pay for it". While it is possible one would require the use of multiple LCDs (or even multiple types of LCDs) at once on an Arduino, I suspect it is a somewhat exceptional case. Given this, all of the cycles used up for virtual function calls and "this" member variable access aren"t really needed. But one problem is that the Adafruit_GFX library acts as the common code for all the driver libraries, and since the Adafruit_GFX library doesn"t "know" what drivers it will be used with (it was compiled separately from the drivers), it needs some kind of "indirect" way to call a driver (currently a virtual function call). Now you could solve this by duplicating the common code in each driver, but that is not ideal. One idea I had is to (somehow) do all the binding of the driver to the GFX common code at compile time (since we "know" what LCD we will be using when we compile our sketch). I was asking some template questions on #C++ on http://Freenode.net IRC regarding my problem and someone mentioned "check out the CRTP pattern". I wasn"t sure what cathode ray-tubes had to do with anything, but I found out this stood for the "Curiously Recurring Template Pattern". With such a fun name, I had to look into it, and of course Wikipedia had a nice article (https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern). I work a lot with heavy C++ templates and what-not, but it still took me a little while to get my head around this odd technique. Without bogging down in to too many C++ template details, this method was just what I was looking for that would let me "bind" the driver code and the common code together without either having to know about the other before hand (other than some "agreed upon" conventions).

One particular reason CRTP is cool is not only can it eliminate virtual function calls and "this" pointer overhead, but it can be done in such a way that the source code of the sketch using the library would be "identical" (even though the code generated by it would be significantly different - and more efficient). To illustrate this, lets look into what code is generated by a call to "drawPixel" and "drawLine" primitives. DrawPixel takes two 16-bit arguments (X and Y) and a 16-bit color to draw and drawLine is similar except it also has ending X and Y arguments. Here are how these calls are compiled in Adafruit GFX (copied from the disassembly mentioned above):

With the Adafruit library this would generate the something like the following function call with 4 16-bit arguments (because of the hidden "this" argument pointing to "tft"):

Alright enough research, time to start actually modifying the library. I won"t go into detail about all my modifications (feel free to compare my library vs. Adafruit original), but I will try to list the major changes (and why).Fork Adafruit library, rename things to be "PDQ" instead of "Adafruit"

Moved everything into the header files (.h) leaving pretty much nothing in the .cpp files (except "glcdfont" font data in PDQ_GFX). C++ template code pretty much needs to be in header files (and this allows the compiler to "customize" it to the current sketches needs at compile time, generating the efficient run-time code I am aiming for).

Make all methods in common library and driver libraries "static" (so no "this" pointer). The one exception is the remaining "write" virtual function that is kept to retain compatibility with Arduino library "Print" functions (so we can still do e.g., tft.Println("Hello");)

Added in a copy of FastPin.h template library from the excellent http://FastLED.io project. This replaces digitalWrite, digitalRead and pinMode Arduino library calls with templated versions that "compile down" to a direct port read/write instruction (but while still using "friendly" standard Arduino pin numbering). This saves a few cycles every GPIO access (the Adafruit library was already bypassing the slow library functions, but were doing GPIO via a port pointer and bit variables which adds some overhead).

At this point the library is looking more efficient and smaller than Adafruit version, but the gaps between SPI bytes are still "too large" (not fast enough - yet).

Now that much of the higher-level parts of the library will generate more optimal code, time to look at the low-level side of things. After reading the AVR 328 data-sheet and some experimentation, it seems the AVR can at most send one byte of data over the SPI bus every 18 clock cycles - 8 bits at 2 cycles per bit (with 8MHz SPI clock) and then it adds an idle period for 2 cycles (which is an unwelcome "buglet" in AVR hardware, SPI does not require this delay). The "normal" way of using hardware SPI on AVR is with a code snippet like this (copied right out of the data-sheet, and nearly the exact same code is used in spiWrite function of Adafruit SPI driver libraries):void SPI_MasterTransmit(char cData){

To take advantage of this technique, I wrote a small assortment of "spiWrite" functions all with slightly different "delay properties". For example, there is the basic "spiWrite" that writes one byte and delays for exactly 17 cycles. This function can be called inline back to back and the 2nd write will not corrupt the first (because 18 cycles will have elapsed due to the delay). I also have another function called "spiWrite_preCmd". This one outputs a byte and delays 15 cycles. This is designed to give a 2 cycle period to toggle the DC signal (sometimes called RS on these displays) to differentiate between a controller command and data (like a color value). This DC signal in on channel-2 in the logic analyzer captures. Similarly there are a handful of other "customized" spiWrite commands for various things like sending 16-bit data, repeating the same 16-bit data value as fast as possible, and one designed to be called from the lineDraw function (etc.).

The other low-level SPI optimization that I touched on earlier is /CS signal management. The Adafruit drivers tended to toggle /CS from high to low for every byte transferred and then restore it to high (or maybe they would keep it low for a few bytes in places as an optimization). Since I was not concerned about sharing the SPI bus mid-graphics primitive, I set /CS low on entry to any primitive and keep it low until it returns. In a similar vein, while I do not support the "SPI transaction API", I do support saving and restoring the SPI bus settings (optionally, if you #define _SAVE_SPCR). This allows you to share the SPI bus with another device that uses different SPI settings (and uses a different /CS signal).

I also removed some redundant setting of the DC signal. PDQ_GFX drivers always assume it is set to "data" and only set it to "command" right before sending a command and then restore it to "data" after (the Adafruit drivers tend to keep setting it to "data" every or "command" every byte transferred).

Since basic spiWrite low-level function is basically a single instruction (and then a delay), it actually takes more instructions to call a function than to inline it directly (more or less, but especially when factoring in the registers that need to get saved and restored on the stack during a function call). So PDQ drivers make a lot of use of inline functions. However, there is a balance, you can "burn" a lot of flash space for a small speed gain (in some cases). I did a fair number of tests and inlined things that helped significantly and made sure the overall library size was well below Adafruit original (seemed a reasonable guideline). There are a few #define macros that can be tweaked to balance size vs speed (if you need things especially small or especially fast). I also added some #if"s so that when using an ATtiny85 part it would (essentially) prohibit many inline things (due to squeezing into that parts small 8K flash - with anything left over for sketch).

The lineDraw function can be a bottleneck for many applications and was a "fun" case to optimize because of being able to continue to calculate the "next pixel" while the AVR hardware SPI was shifting out the last data byte. One other optimization I will mention that I found paid off in the lineDraw function was to reduce the number of "setAddressWindow" commands needed. Essentially this command sets the "pixel cursor" position (where the next write will happen) and also controls how it will "wrap" inside of a defined two-dimensional area. Setting this window takes ten SPI bytes, and so this can dominate the three bytes needed to actually draw a pixel. The code I added to drawLine will only issue a setAddressWindow command when moving on the "minor" axis (X or Y axis, whichever has the least change from start of line). It also sets the window to "maximum size" (so it will not have to set it again unless it moves on its minor axis - just pump color data). As mentioned previously I also used some special spiWrite functions customized with the minimum safe delay (to get the maximum overlap between computation and SPI transfer).

Here is a table showing the output of the included "benchmark" program (included with the PDQ drivers, modified from the Adafruit example). I used my modified benchmark, but switched it to use Adafruit_GFX with ILI9341 and then PDF_GFX with IL9341 on the same hardware (just a few lines changed at the top to switch libraries).

These SPI displays are never going to be "barn burners" on AVR, but they are a great alternative to even slower (and boring) monochrome character LCDs. I am pretty happy with the improvements achieved and I think they help make these low-cost display modules even more useful. The speed is more than adequate for a nice GUI interface or instrument readout (in most cases), and even enough for games without to much screen area being redrawn each frame.

I want to give a shout-out to Adafruit for creating the GFX library and making it open source with a license that makes it possible for me to goof around. Their version is still much better for portability to other non-AVR platforms (and probably easier to understand the code). I will also mention that at this point I now have a nice little collection of various LCD display modules and shields, and the Adafruit ones really are much better quality (displays aren"t attached crooked and generally have nice level conversion for 5V Arduino).