RA8875 device driver
October 24, 2020 ยท View on GitHub
This driver provides MicroPython access to a subset of the capabilities of the
RA8875 chip. The subset was chosen to be close to the minimum functionality
required to support the touch GUI display. The ra8875.py driver provides an
RA8875 class. This is usable for applications other than the GUI and could
readily be extended to support additional primitives provided by the chip.
The module tft.py subclasses RA8875 to provide a TFT class. This provides
a compatibility layer between the GUI and the driver. It handles 'greying out'
of disabled widgets by modifying the colors passed to the superclass methods
and methods for rendering strings. TFT is documented here.
The RA8875 documentation is not the world's best and the chip has bugs. The driver was written by referring to a number of sources, notably the Adafruit libraries; data values for display control registers were copied from their code. Accordingly the use of their displays is highly recommended: other units may not be electrically compatible with the driver. See references for details of the supported hardware and for the sources of information used.
GUI docs
1. The TFT class
Terminology
Where color is specified this is an (r, g, b) tuple where r, g and b are in
range 0 to 255 inclusive. fgcolor and bgcolor represent foregound and
background colors.
Text styles are 3-tuples comprising (fgcolor, bgcolor, font). font may be
the name of an imported Python font or an instance of an internal font (defined
in constants.py).
Class
Constructor. This takes the following mandatory arguments.
spiAn initialised SPI bus instance.pincsAn initialisedPininstance defined for output with initial value of 1. Used for chip select.pinrstAn initialisedPininstance defined for output with initial value of 1. Used to perform a hardware reset.widthDisplay width and height. Supported values are 480x272 and 800x480.heighttdelayTouch delay (in ms). If a value > 0 is passed, the GUI suppresses multiple touches by delaying responses. See section 3.loopAn event loop instance orNone. In the latter case the touch panel will be inoperative.
Methods.
text_styleArgstyle. Accepts a text style, returns one with colors modified by the current greyed-out status.desaturateArgvalue=None. If aboolis passed, defines whether greying-out is done by dimming (False) or desaturating (True) objects. By default the current status is returned.dimArgfactor=NoneIf a numeric value is passed, sets the amount of dimming to be used if this option is selected. A typical value is 2, meaning that the brightness of colors is halved. Returns the current factor.usegreyArgval. Set current greyed-out status. IfTrueis passed, subsequent pixels will be rendered with the specifiedcolormodified to the current greyed-out style. IfFalseis passed they will be rendered normally. Note thatdesaturateanddimmethods set the grey style. They only affect rendering when the greyed-out status is set viausegrey.print_centeredArgsx, y, s, style. Renders stringscentred atx, y. 6print_leftArgsx, y, s, style, tab=32. Renders stringsstarting atx, y. Thetabvalue represents the size of a tab stop in pixels.draw_rectangleArgsx1, y1, x2, y2, colorDraw a rectangle.fill_rectangleArgsx1, y1, x2, y2, colorDraw a filled rectangle.draw_clipped_rectangleArgsx1, y1, x2, y2, colorDraw a clipped rectangle.fill_clipped_rectangleArgsx1, y1, x2, y2, colorDraw a filled clipped rectangle.draw_circleArgsx, y, radius, colorDraw a circle.fill_circleArgsx, y, radius, colorDraw a filled circle.draw_vlineArgsx, y, l, colorDraw a vertical line lengthl.draw_hlineArgsx, y, l, colorDraw a horizontal line lengthl.draw_lineArgsx1, y1, x2, y2, colorDraw a line fromx1, y1tox2, y2.
Static method:
get_stringsizeArgs:s, font. Returns the metrics of a stringsas a 2-tuple comprising(rows, cols)in pixels.
In methods 7-15 the passed color will be modified before rendering if the
current status is greyed-out.
2. The RA8875 class
Colors are specified as (r, g, b) tuples. They are converted to 16 bit RGB565
values by the RA8875._to_rgb565 private static method.
Constructor. This takes the following mandatory arguments.
spiAn initialised SPI bus instance.pincsAn initialisedPininstance defined for output with initial value of 1. Used for chip select.pinrstAn initialisedPininstance defined for output with initial value of 1. Used to perform a hardware reset.widthDisplay width and height. Supported values are 480x272 and 800x480.heighttouch=TrueIfFalsethe touch panel will be disabled.
Display methods required for GUI:
clr_scrNo args. Clear screen (all pixels off). In testing this sometimes filled the screen with a color;fill_rectangleis reliable.draw_rectangleArgsx1, y1, x2, y2, colorDraw a rectangle.fill_rectangleArgsx1, y1, x2, y2, colorDraw a filled rectangle.draw_clipped_rectangleArgsx1, y1, x2, y2, colorDraw a clipped rectangle.fill_clipped_rectangleArgsx1, y1, x2, y2, colorDraw a filled clipped rectangle.draw_circleArgsx, y, radius, colorDraw a circle.fill_circleArgsx, y, radius, colorDraw a filled circle.draw_vlineArgsx, y, l, colorDraw a vertical line lengthl.draw_hlineArgsx, y, l, colorDraw a horizontal line lengthl.draw_lineArgsx1, y1, x2, y2, colorDraw a line fromx1, y1tox2, y2.draw_glyphArgsmv, x, y, rows, cols, fgcolor, bgcolorThemvarg is amemoryviewinto abytesobject holding a glyph bitmap. The bitmap uses one bit per pixel as a horizontally mapped array. It is rendered atx, yand the glyph is organised asrows, cols. Rendering uses the supplied foreground and background colors.draw_strArgss, x, y, fgcolor, bgcolor, scale=0Render a stringsat locationx,yusing the RA8875 internal font. Rendering uses the supplied foreground and background colors. Glyphs are fixed-pitch and default to 8 bits wide by 16 high. They can be scaled by factors of 2, 3 or 4 by passingscalevalues of 1-3.
Touchpanel (TP) methods required by GUI:
readyNo args. ReturnsTrueif TP data is available.touchedNo args. ReturnsTrueif TP is currently being touched.get_touchNo args. Return touch panel datax, yas screen coordinates. Caller should checkreadybefore calling. The returned coordinates will be raw values unlesscalibratehas been called, when they will be adjusted.
Additional methods:
calibrateArgsxmin, ymin, xmax, ymaxApply user-specified calibration values to future touchpanel readings. See below.widthNo args. Return display width in pixels.heightNo args. Return display height in pixels.draw_pixelArgsx, y, colordraw a single pixel.
Calibration
The user runs cal.py to determine the reported coordinates of the top left
and bottom right of the display. The code in tft_local.py is modified to call
.calibrate with those values.
This ensures that the GUI acquires corrected coordinates from the touch panel.
3. Chip limitations
The RA8875 touch interface has an unexpected behaviour. A single brief touch
always appears as at least two touch events with different coordinates. The
last of these seems to be the correct one (subject to calibration). The driver
makes no attempt to mitigate this. The setting of the debounce bit had no
evident effect on this behaviour. This can cause flicker as widgets update
multiple times. The TFT constructor arg tdelay attempts to mitigate this by
delaying any response until tdelay ms after the last touch event.
The RA8875 claims to provide a means of reading back the contents of the frame buffer. This seems broken. I removed functionality which aimed to support it as the outcome was not deterministic. GUI controls which formerly relied on this were redesigned so as not to need it.
As stated above the clear_scr method is not reliable. Despite conforming to
the datasheet it sometimes fills the screen with a non-black color.
4. References
CircuitPython library written by Melissa LeBlanc-Williams for Adafruit Industries. Arduino library written by Limor Fried/Ladyada for Adafruit Industries. Datasheet App note
This driver by Max Mc Costa.
Supported hardware