HackSpace – September 2019

(Wang) #1
FORGE

Above
The wiring is the
same whichever
language is used

In the File > Examples menu, you’ll find Adafruit
ILI9341 > graphics test. This is, as you may guess, a
test for this particular TFT. There are a few
configurable options on this. Above the setup
method, you’ll need the following code:


#include “SPI.h”
#include “Adafruit_GFX.h”
#include “Adafruit_ILI9341.h”

// For grand central. Other boards might need
different pins
#define TFT_DC 49
#define TFT_CS 53
#define TFT_MOSI 51
#define TFT_CLK 52
#define TFT_RST 48
#define TFT_MISO 50

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS,
TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

With that saved, you can upload it to your board and
you should see various different graphics tests appear
on the screen. That’s the screen running. What you
do with it is up to you. If you have a look through the
example code, you should get an understanding of
how to use the GFX library to display whatever
graphics you want.


GETTING SNAKEY
Now let’s try with CircuitPython. First you’ll need to
flash your board with CircuitPython 4.1.0 or later.
There are two sections to the code. The first part
sets up the display. You’ll need to use this whenever
you’re using this screen. It is as follows:


import board
import displayio
import adafruit_ili9341
from time import sleep

displayio.release_displays()

display_bus = displayio.FourWire(board.SPI(),
command=board.D49,
chip_select=board.D53,
reset=board.D48)
display = adafruit_
ili9341.ILI9341(display_bus, width=320,
height=240)

This creates a display object that you can draw to. For
more information on what to do with this, take a look
at the CircuitPython tutorial that builds on the display.
However, for the purposes of this article, we just
need a simple test to make sure everything’s wired
up properly and working. For this, you can enter the
following code under the above:

splash = displayio.Group(max_size=10)
display.show(splash)

color_bitmap = displayio.Bitmap(320, 240, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFF0000
bg_sprite = displayio.TileGrid(color_bitmap,
pixel_shader=color_palette,
x=0, y=0)
splash.append(bg_sprite)

while True:
pass

If everything’s worked properly, the display turns red.
That’s how you get your ILI9341 display wired up
and running in both languages. There are pros and
cons to both. You’ll get much better performance
from the Arduino language, but it can be quicker to
get started with CircuitPython. Your choice may also
be determined by the hardware, as almost all hobbyist
boards run Arduino, while a smaller (but growing)
number run CircuitPython.
Free download pdf