Adsense HTML/JavaScript

Tuesday, November 2, 2021

MicroPython/PyBoard exercise: + 1.8" 128x160 TFT ST7735 SPI

It's a MicroPython/PyBoard exercise run on Modified MicroPython pyboard PYBv1.1, to drive 1.8" 128x160 TFT ST7735 SPI using GuyCarver/MicroPython lib.




Prepare library:

Visit https://github.com/GuyCarver/MicroPython/tree/master/lib copy ST7735.py, seriffont.py, sysfont.py and terminalfont.py to your pyboard.


Connection:

Refer to __init__() of class tft in ST7735.py:

  def __init__( self, aLoc, aDC, aReset ) :
    '''aLoc SPI pin location is either 1 for 'X' or 2 for 'Y'.
       aDC is the DC pin and aReset is the reset pin.'''
	.
	.
	.
    cs = "X5" if aLoc == 1 else "Y5"
	.
	.
    self.spi = pyb.SPI(aLoc, ...)
	.
	.

And refer to pyb SPI class document.


That means 
when aloc = 1:
SPI(1) is on the X position: (CS, SCK, MISO, MOSI) = (X5, X6, X7, X8)
when aloc = 2:
SPI(2) is on the Y position: (CS, SCK, MISO, MOSI) = (Y5, Y6, Y7, Y8)

In my exercise code below marked RED, display of ST7735.tft is created with aLoc=1.
display = ST7735.tft(aLoc=1, aDC='Y9', aReset='Y10')
So the connection is:
ST7735	Pyboard
	aLoc=1	(if aLoc=2)
----------------------------
LED	3V3		
SCK	X6	Y6
SDA	X8	Y8
A0	Y9
RESET	Y10
CS	X5	Y5
GND	GND
VCC	3V3

Exercise code:

mpyPyb_tft.py
"""
MicroPython/PyBoard exercise run on Pyb v1.1,
+ 1.8" 128x160 TFT ST7735 SPI

using GuyCarver/MicroPython
https://github.com/GuyCarver/MicroPython
"""
import uos
import usys
import time
import random
import ST7735
from seriffont import seriffont
from sysfont import sysfont
from terminalfont import terminalfont

print("====================================")
print(usys.implementation[0], uos.uname()[3],
      "\nrun on", uos.uname()[4])
print("------------------------------------")

print("====================================")

display = ST7735.tft(aLoc=1, aDC='Y9', aReset='Y10')

displaysize = display.size()
print(displaysize)

display.initg() # or initr()/initb() according to your display

display.rotation(1)
display.fill(display._BLACK)
display.text((0,0),
             usys.implementation[0]+' '+uos.uname()[3],
             display._WHITE,
             terminalfont)
time.sleep(1)
display.text((0,30),
             "run on "+uos.uname()[4],
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont)
time.sleep(3)

#font test
display.fill(display._BLACK)
display.text((0,0),
             "seriffont",
             display._RED,
             seriffont)
display.text((0,10),
             "abcdefghijklmnopqrstuvwxyz",
             display._WHITE,
             seriffont)
display.text((0,30),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display._WHITE,
             seriffont)
display.text((0,50),
             "sysfont",
             display._RED,
             sysfont)
display.text((0,60),
             "abcdefghijklmnopqrstuvwxyz",
             display._WHITE,
             sysfont)
display.text((0,70),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display._WHITE,
             sysfont)
display.text((0,80),
             "terminalfont",
             display._RED,
             terminalfont)
display.text((0,90),
             "abcdefghijklmnopqrstuvwxyz",
             display._WHITE,
             terminalfont)
display.text((0,110),
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
             display._WHITE,
             terminalfont)
time.sleep(5)

display.rotation(0)
display.fill(display._RED)
display.text((10,10),
             "RED",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

display.fill(display._GREEN)
display.text((10,10),
             "GREEN",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

display.fill(display._BLUE)
display.text((10,10),
             "BLUE",
             ST7735.TFTColor(0xFF, 0xFF, 0xFF),
             terminalfont,
             aSize=2)
time.sleep(1)

#rotation test
display.rotation(0)
display.fill(display._BLACK)
display.fillrect((1,1),
                 (display.size()[0]-2, display.size()[1]-2),
                 display._WHITE)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._BLACK,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

display.rotation(1)
display.fillrect((2,2),
                 (display.size()[0]-4, display.size()[1]-4),
                 display._BLACK)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._WHITE,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

display.rotation(2)
display.fillrect((3,3),
                 (display.size()[0]-6, display.size()[1]-6),
                 display._WHITE)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._BLACK,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

display.rotation(3)
display.fillrect((4,4),
                 (display.size()[0]-8, display.size()[1]-8),
                 display._BLACK)
display.text((0,0),
             "rotate = "+str(display.rotate)+" : "+str(display.size()),
             display._WHITE,
             terminalfont)
print("rotate = "+str(display.rotate)+" : "+str(display.size()))
time.sleep(2)

#Random pixel
for p in range(1000):
    x = random.randint(5, display.size()[0]-10)
    y = random.randint(5, display.size()[1]-10)
    c = ST7735.TFTColor(random.randint(0, 0xFF),
                        random.randint(0, 0xFF),
                        random.randint(0, 0xFF))
    display.pixel((x, y), c)
    
#Random line
for l in range(100):
    display.line((random.randint(5, display.size()[0]-10),
                  random.randint(5, display.size()[1]-10)),
                 (random.randint(5, display.size()[0]-10),
                  random.randint(5, display.size()[1]-10)),
                 ST7735.TFTColor(random.randint(0, 0xFF),
                        random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))

#Random circle
for l in range(20):
    display.circle((random.randint(5, display.size()[0]-10),
                    random.randint(5, display.size()[1]-10)),
                   random.randint(1, 50),
                   ST7735.TFTColor(random.randint(0, 0xFF),
                                   random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))
    
#Random fillcircle
for l in range(20):
    display.fillcircle((random.randint(5, display.size()[0]-10),
                    random.randint(5, display.size()[1]-10)),
                   random.randint(1, 50),
                   ST7735.TFTColor(random.randint(0, 0xFF),
                                   random.randint(0, 0xFF),
                        random.randint(0, 0xFF)))

print("~ bye ~")
When you run the exercise code, it will fail with:
NameError: name 'BLACK' isn't defined

To fix it, edit ST7735.py in pyboard.
  def fill( self, aColor = BLACK ) :
to
  def fill( self, aColor = _BLACK ) :
Re-run again, it should work as expected.




Related:
MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library. (in my another new blogspot)

No comments:

Post a Comment