Micropython graphics (GFX) and fonts for ILI9225 display for Raspberry Pi Pico
The following code has been tested on Raspberry Pi Pico W. With the exception of the examples, all .py modules were saved to the Raspberry Pi Pico using, for example, Thonny.
The driver object, TFT_22_ILI9225, is derived from: v1.4.5
The graphics library object, GFX, is minimally edited from CircuitPython pixel graphics drawing library: (https://github.com/adafruit/Adafruit_CircuitPython_GFX.git). A Method, drawBitmap, to draw XBitMap files (.xbm) exported from GIMP, has been added.
A font object, glcFont, is derived from font methods found in: (https://github.com/Nkawu/TFT_22_ILI9225/releases/tag/v1.4.5), which use fonts generated by MikroElektronika GLCD Font Creator 1.2.0.0, MikroElektronika 2011, http://www.mikroe.com.
A font object, gfxFont, is derived from font method, drawGFXChar(), found in TFT_22_ILI9225.cpp (see github.com/Nkawu, above), in which character glyphs are separated from the character bitmaps. These are the font structures used in Adafruit_GFX 1.1 and later.
This is a library for the ILI9225 based 2.2" 176x220 TFT LCD shields commonly found on eBay, originally forked from the screen_4D_22_library library. The ability to use GLCD fonts has been added and the syntax has been changed to match the Adafruit libraries somewhat.
Documentation describing physical module connections can be found in the repo's Wiki
spi_object = SPI(0))lcd_object = TFT_22_ILI9225(spi_object, dc=Pin(15), cs=Pin(17), rst=Pin(14))
reset()perform a reset to the ILI9225 chipclear()fill the entire screen with black pixelssetDisplay(flag)switch the display on (flag=True) or off (flag=False)setOrientation(orientation)set the coordinate orientation of the screen:0=portrait, 1=right rotated landscape, 2=reverse portrait, 3=left rotated landscapegetOrientation()returns the current orientationsetBackgroundColor(color)set background color to "color"getBackgroundColor()return background colormaxX()returns maximum screen X dimensionmaxY()returns maximum screen Y dimensionThe folowing methods are typically screen driver primatives passed to the GFX class:drawPixel(x, y, color)draw the pixel at (x,y) with "color"fillRectangle(x, y, width, height, color)fill rectangle of width and height lower left corner at (x,y) with "color"fastHline(x, y, width, color)draws horizontal "color"ed line of widthfastVline(x, y, height, color)draws vertical "color"ed line of height
gfx_object = GFX(width, height, pixel,hline=Hline, # A function to quickly draw a horizontal line on the displayvline=Vline, # A function to quickly draw a vertical line on the displayfill_rect=fillRect, # A function to quickly draw a solid rectangletext=drawText, # A function to quickly place text on the screenfont=Font # Alternative dict font)where "width" is the drawing area width in pixels,"height" is the drawing area height in pixels,and "pixel" is a function to call when a pixel is drawn on the display (see driver methods above).
pixel(x, y, color)draw a pixel at x,yrect(x, y, width, heigth, color)draw a single pixel wide rectangle of width x height with (x,y) at the bottom leftfill_rect(x, y, width, height, color)draw a filled rectangle of width x height with (x,y) at the bottom lefthline(x, y, width, color)draw a horzontal line of width starting from (x,y)vline(x, y, height, color)draw a vertical line of height starting from (x,y)line(x0, y0, x1, y1, color)draw a single pixel wide line starting at x0, y0 and ending at x1, y1circle(x0, y0, radius, color)draw a single pixel wide circle with center at x0, y0 and the specified radiusfill_circle(x0, y0, radius, color)draw a filled circle with center at x0, y0 and the specified radiustriangle(x0, y0, x1, y1, x2, y2, color)draw a single pixel wide triangle around the points (x0, y0), (x1, y1),and (x2, y2)fill_triangle(x0, y0, x1, y1, x2, y2, color)draw a filled triangle around the points (x0, y0), (x1, y1), and (x2, y2)round_rect(x0, y0, width, height, radius, color)draw the outline of a rectangle with rounded corners of radius with(x0,y0) at the bottom leftfill_round_rect(x0, y0, width, height, radius, color)draw a filled rectangle with rounded corners of radius with(x0,y0) at the bottom lefttext(x, y, string, size, color)draw string starting at (x,y) of size using internal dictionary font.drawBitmap(x, y, bitmapTuple, color, bg=0, transparent=True, Xbit=True)draw XBitMap Files (.xbm) exported from GIMP with (x,y) at the bottomleft, where bitmapTupleis (bitmap_array, width, height).
A class to represent a GLCD font in X-GLCD formatFont Generated by MikroElektronika GLCD Font Creator 1.2.0.0MikroElektronika 2011http://www.mikroe.comglcFont_object = glcFont(tft, font,monoSp=False, # default use individual char widths. monoSp=True,use fixed width chars.eraseMode=False # default char written without background.eraseMode=True, write background.)tft: TFT_22_ILI9225 display object.font: A bytearray of characters (each row of self.offset bytes representsa character).font[0] = width: Maximum pixel width of font.font[1] = height: Pixel height of font.font[2] = start_char: ASCII ordinate of first character.font[3] = number of characters in the bytearray.font[offset] = width of individual character in pixels.
text(x, y, string, color)prints text string beginning at (x,y). Returns x-coordinate fornext character print.getFontParams()returns tuple, [offset, width, height, bytes_per_character] foroverall font.getTextWidth(string)returns string width in pixels.
A class to represent structures for newer Adafruit_GFX (1.1 and later).Example fonts are included in 'Fonts' directory.To use a font in your Micropython script, import the corresponding .pyfile and pass Font tuple to the class __init__, e.g.from FreeMono9pt7b import FreeMono9pt7bfx = gfxFont(display_object, FreeMono9pt7b)bitmap = array.array('B', [...])Font data stored PER GLYPHglyph = (bitmapOffset, # Offset into GFXfont bitmapwidth, # Bitmap dimensions in pixelsheight, # Bitmap dimensions in pixelsxAdvance, # Distance to advance cursor (x axis)xOffset, # X dist from cursor pos to UL corneryOffset) # Y dist from cursor pos to UL cornerData tuple for FONT AS A WHOLEFont = (bitmap # Bitmaps array of bytesglyph # Glyph array of integersfirst # ASCII extents (first char)last # ASCII extents (last char)yAdvance) # Newline distance (y axis)
text(x, y, string, color)prints text string beginning at (x,y). Returns x-coordinate for next character print.getTextWidth(string)returns string width in pixels.