haas506开发教程-example-TFT
- 1.介绍
- 2.测试代码
- 3.测试结果
- 4.待解决
- 5.总结
1.介绍
模块引脚
当前案例为了接线方便,将DC、RST接到了haas506开发板的i2c接口的SCL(GPIO19)、SDA(GPIO20)引脚上。
pin | note |
GND | 电压地 |
VDD | 电压正3.3V |
SCL | SPI时钟线(与haas506的SPI_CLK相接) |
SDA | SPI数据线(与haas506的SPI_MOSI相接) |
RST | 复位引脚 |
DC | 数据/命令引脚 |
CS | SPI片选(与haas506的SPI_CS相接) |
BLK | 背光控制开关,可不接 |
- 模块实物图1
- 模块实物图2
2.测试代码
- main.py
from st7735s import ST7735S
import utime as time
tft=ST7735S()
tft.tft_begin()
#画空心圆
tft.tft_drawCircle(64,64,40,0xffff,0)
tft.tft_drawCircle(64,64,30,0xffff,0)
tft.tft_drawCircle(64,64,20,0xffff,0)
time.sleep(2)
#清屏
tft.tft_clear()
#画实心圆
tft.tft_drawCircle(64,64,20,0xffff,1)
time.sleep(2)
tft.tft_clear()
#画三角形
tft.tft_drawTriangel(0,0,100,64,120,32,0xf800)
time.sleep(2)
tft.tft_clear()
#画像素点/画线
tft.tft_drawPoint(32,32,0xf800)
tft.tft_drawPoint(64,32,0xf800)
tft.tft_drawPoint(64,64,0xf800)
tft.tft_drawPoint(32,64,0xf800)
time.sleep(2)
tft.tft_clear()
tft.tft_drawLine(32,32,100,32,0xf800)
tft.tft_drawLine(64,64,120,120,0xf800)
time.sleep(2)
tft.tft_clear()
#显示BMP图片,当前图片存放在项目文件夹内
tft.tft_showBmp('/data/pyamp/test128x160.bmp')
time.sleep(2)
tft.tft_clear()
tft.tft_showBmp('/data/pyamp/flower64x48.bmp')
time.sleep(2)
tft.tft_clear()
#显示字符(6*8字体大小)
tft.tft_showString(0,0,"haas506",0xf800)
tft.tft_showString(0,32,"hello world",0xffff)
tft.tft_showString(0,64,"123456789",0X07FF)
time.sleep(2)
tft.tft_clear()
#显示中文字符 (16*16字体大小)
tft.tft_showCHN(0,0,1,0xffff,0)
tft.tft_showCHN(0,16,2,0xffff,0)
tft.tft_showCHN(0,32,3,0xffff,0)
tft.tft_showCHN(0,48,4,0xffff,0)
tft.tft_showCHN(0,64,5,0xffff,0)
time.sleep(2)
tft.tft_clear()
#显示中文字符 (24*24字体大小)
tft.tft_showCHN(0,0,1,0xffff,1)
tft.tft_showCHN(0,24,2,0xffff,1)
tft.tft_showCHN(0,48,3,0xffff,1)
tft.tft_showCHN(0,72,4,0xffff,1)
tft.tft_showCHN(0,96,5,0xffff,1)
time.sleep(2)
tft.tft_clear()
#显示自定义字符 (8*16字体大小)
tft.tft_showCHN(0,0,7,0xffff,2)
- st7735s.py
from driver import SPI
from driver import GPIO
import utime as time
from font import terminalfont,F16_16,F24_24,F8_16
USE_HORIZONTAL=1 #设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏
# Constants for interacting with display registers.
ST7735_TFTWIDTH = 128
ST7735_TFTHEIGHT = 160
ST7735_NOP = 0x00
ST7735_SWRESET = 0x01
ST7735_RDDID = 0x04
ST7735_RDDST = 0x09
ST7735_SLPIN = 0x10
ST7735_SLPOUT = 0x11
ST7735_PTLON = 0x12
ST7735_NORON = 0x13
# ILI9341_RDMODE = 0x0A
# ILI9341_RDMADCTL = 0x0B
# ILI9341_RDPIXFMT = 0x0C
# ILI9341_RDIMGFMT = 0x0A
# ILI9341_RDSELFDIAG = 0x0F
ST7735_INVOFF = 0x20
ST7735_INVON = 0x21
# ILI9341_GAMMASET = 0x26
ST7735_DISPOFF = 0x28
ST7735_DISPON = 0x29
ST7735_CASET = 0x2A
ST7735_RASET = 0x2B
ST7735_RAMWR = 0x2C
ST7735_RAMRD = 0x2E
ST7735_PTLAR = 0x30
ST7735_MADCTL = 0x36
# ST7735_PIXFMT = 0x3A
ST7735_COLMOD = 0x3A
ST7735_FRMCTR1 = 0xB1
ST7735_FRMCTR2 = 0xB2
ST7735_FRMCTR3 = 0xB3
ST7735_INVCTR = 0xB4
# ILI9341_DFUNCTR = 0xB6
ST7735_DISSET5 = 0xB6
ST7735_PWCTR1 = 0xC0
ST7735_PWCTR2 = 0xC1
ST7735_PWCTR3 = 0xC2
ST7735_PWCTR4 = 0xC3
ST7735_PWCTR5 = 0xC4
ST7735_VMCTR1 = 0xC5
# ILI9341_VMCTR2 = 0xC7
ST7735_RDID1 = 0xDA
ST7735_RDID2 = 0xDB
ST7735_RDID3 = 0xDC
ST7735_RDID4 = 0xDD
ST7735_GMCTRP1 = 0xE0
ST7735_GMCTRN1 = 0xE1
ST7735_PWCTR6 = 0xFC
# Colours for convenience
ST7735_BLACK = 0x0000 # 0b 00000 000000 00000 黑色
ST7735_BLUE = 0x001F # 0b 00000 000000 11111 蓝色
ST7735_GREEN = 0x07E0 # 0b 00000 111111 00000 绿色
ST7735_RED = 0xF800 # 0b 11111 000000 00000 红色
ST7735_CYAN = 0x07FF # 0b 00000 111111 11111 橘黄
ST7735_MAGENTA = 0xF81F # 0b 11111 000000 11111 紫红色
ST7735_YELLOW = 0xFFE0 # 0b 11111 111111 00000 黄色
ST7735_WHITE = 0xFFFF # 0b 11111 111111 11111
ST7735_CYAN =0x7FFF
ST7735_BROWN =0XBC40 #棕色
ST7735_BRRED =0XFC07 #棕红色
ST7735_GRAY =0X8430 #灰色
ST7735_DARKBLUE =0X01CF #深蓝色
ST7735_LIGHTBLUE =0X7D7C #浅蓝色
ST7735_GRAYBLUE =0X5458 #灰蓝色
ST7735_LIGHTGREEN =0X841F #浅绿色
ST7735_LGRAY =0XC618 #浅灰色(PANNEL),窗体背景色
ST7735_LGRAYBLUE =0XA651 #浅灰蓝色(中间层颜色)
ST7735_LBBLUE =0X2B12 #浅棕蓝色(选择条目的反色)
class ST7735S(object):
# --------------------------------------------------------------
# Prototype : __init__(width=ST7735_TFTWIDTH,height=ST7735_TFTHEIGHT)
# Parameters : width,height为屏幕的大小,如128*160
# Description : 初始化spi,cs,rst,dc引脚
# --------------------------------------------------------------
def __init__(self,width=ST7735_TFTWIDTH,height=ST7735_TFTHEIGHT):
self.width = width
self.height = height
#creat spi-instance,cs-instance,rst,dc
#open spi,cs,rst,dc
self._spi=SPI()
self._spi.open('ST7735S')
self.cs=GPIO()
self.cs.open('cs')
self._rst=GPIO()
self._rst.open('rst')
self._dc=GPIO()
self._dc.open('dc')
# --------------------------------------------------------------
# Prototype : _initialize()
# Parameters : 无
# Description : 初始化
# --------------------------------------------------------------
def _initialize(self):
self.writeCmd(ST7735_SWRESET) # Software reset
time.sleep_ms(150) # delay 150 ms
self.writeCmd(ST7735_SLPOUT) #Out of sleep mode
time.sleep_ms(120)
#------------------------------------ST7735S Frame Rate-----------------------------------------#
self.writeCmd(ST7735_FRMCTR1)
self.writeDat(0x05)
self.writeDat(0x3C)
self.writeDat(0x3C)
self.writeCmd(ST7735_FRMCTR2)
self.writeDat(0x05)
self.writeDat(0x3C)
self.writeDat(0x3C)
self.writeCmd(ST7735_FRMCTR3)
self.writeDat(0x05)
self.writeDat(0x3C)
self.writeDat(0x3C)
self.writeDat(0x05)
self.writeDat(0x3C)
self.writeDat(0x3C)
#------------------------------------End ST7735S Frame Rate---------------------------------#
self.writeCmd(ST7735_INVCTR) #Display inversion ctrl
# self.writeDat(0x03) # option1
self.writeDat(0x07) # option2
#------------------------------------ST7735S Power Sequence---------------------------------#
self.writeCmd(ST7735_PWCTR1)
self.writeDat(0x28)
self.writeDat(0x08)
self.writeDat(0x04)
self.writeCmd(ST7735_PWCTR2)
self.writeDat(0XC0)
self.writeCmd(ST7735_PWCTR3)
self.writeDat(0x0D)
self.writeDat(0x00)
self.writeCmd(ST7735_PWCTR4)
self.writeDat(0x8D)
self.writeDat(0x2A)
self.writeCmd(ST7735_PWCTR5)
self.writeDat(0x8D)
self.writeDat(0xEE)
#---------------------------------End ST7735S Power Sequence-------------------------------------#
self.writeCmd(ST7735_VMCTR1) #VCOM
self.writeDat(0x1A)
self.writeCmd(ST7735_MADCTL) #MX, MY, RGB mode
if USE_HORIZONTAL==0:
self.writeDat(0x00)
elif USE_HORIZONTAL==1:
self.writeDat(0xC0)
elif USE_HORIZONTAL==2:
self.writeDat(0x70)
else:
self.writeDat(0xA0)
# if USE_HORIZONTAL==0:
# self.writeDat(0xC8)
# elif USE_HORIZONTAL==1:
# self.writeDat(0x08)
# elif USE_HORIZONTAL==2:
# self.writeDat(0x78)
# else:
# self.writeDat(0xA8)
#------------------------------------ST7735S Gamma Sequence---------------------------------#
self.writeCmd(ST7735_GMCTRP1)
self.writeDat(0x04)
self.writeDat(0x22)
self.writeDat(0x07)
self.writeDat(0x0A)
self.writeDat(0x2E)
self.writeDat(0x30)
self.writeDat(0x25)
self.writeDat(0x2A)
self.writeDat(0x28)
self.writeDat(0x26)
self.writeDat(0x2E)
self.writeDat(0x3A)
self.writeDat(0x00)
self.writeDat(0x01)
self.writeDat(0x03)
self.writeDat(0x13)
self.writeCmd(ST7735_GMCTRN1)
self.writeDat(0x04)
self.writeDat(0x16)
self.writeDat(0x06)
self.writeDat(0x0D)
self.writeDat(0x2D)
self.writeDat(0x26)
self.writeDat(0x23)
self.writeDat(0x27)
self.writeDat(0x27)
self.writeDat(0x25)
self.writeDat(0x2D)
self.writeDat(0x3B)
self.writeDat(0x00)
self.writeDat(0x01)
self.writeDat(0x04)
self.writeDat(0x13)
##------------------------------------End ST7735S Gamma Sequence-----------------------------#
self.writeCmd(ST7735_COLMOD) #65k mode set color mode
self.writeDat(0x05)
self.writeCmd(ST7735_DISPON) #Display on
#
#black
time.sleep_ms(200)
self.tft_fill(0,0,128,160,0x0000)
# Initialize the display. Broken out as a separate function so it can
# be overridden by other displays in the future.
# self.writeCmd(ST7735_SWRESET) # Software reset
# time.sleep_ms(150) # delay 150 ms
# self.writeCmd(ST7735_SLPOUT) # Out of sleep mode
# time.sleep_ms(500) # delay 500 ms
# self.writeCmd(ST7735_FRMCTR1) # Frame rate ctrl - normal mode
# self.writeDat(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D)
# self.writeDat(0x2C)
# self.writeDat(0x2D)
# self.writeCmd(ST7735_FRMCTR2) # Frame rate ctrl - idle mode
# self.writeDat(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D)
# self.writeDat(0x2C)
# self.writeDat(0x2D)
# self.writeCmd(ST7735_FRMCTR3) # Frame rate ctrl - partial mode
# self.writeDat(0x01) # Dot inversion mode
# self.writeDat(0x2C)
# self.writeDat(0x2D)
# self.writeDat(0x01) # Line inversion mode
# self.writeDat(0x2C)
# self.writeDat(0x2D)
# self.writeCmd(ST7735_INVCTR) # Display inversion ctrl
# self.writeDat(0x07) # No inversion
# self.writeCmd(ST7735_PWCTR1) # Power control
# self.writeDat(0xA2)
# self.writeDat(0x02) # -4.6V
# self.writeDat(0x84) # auto mode
# self.writeCmd(ST7735_PWCTR2) # Power control
# self.writeDat(0x0A) # Opamp current small
# self.writeDat(0x00) # Boost frequency
# self.writeCmd(ST7735_PWCTR4) # Power control
# self.writeDat(0x8A) # BCLK/2, Opamp current small & Medium low
# self.writeDat(0x2A)
# self.writeCmd(ST7735_PWCTR5) # Power control
# self.writeDat(0x8A)
# self.writeDat(0xEE)
# self.writeCmd(ST7735_VMCTR1) # Power control
# self.writeDat(0x0E)
# self.writeCmd(ST7735_INVOFF) # Don't invert display
# self.writeCmd(ST7735_MADCTL) # Memory access control (directions)
# self.writeDat(0xC8) # row addr/col addr, bottom to top refresh
# self.writeCmd(ST7735_COLMOD) # set color mode
# self.writeDat(0x05) # 16-bit color
# self.writeCmd(ST7735_CASET) # Column addr set
# self.writeDat(0x00) # XSTART = 0
# self.writeDat(0x00)
# self.writeDat(0x00) # XEND = 127
# self.writeDat(0x7F)
# self.writeCmd(ST7735_RASET) # Row addr set
# self.writeDat(0x00) # XSTART = 0
# self.writeDat(0x00)
# self.writeDat(0x00) # XEND = 159
# self.writeDat(0x9F)
# #
# self.writeCmd(ST7735_GMCTRP1) # Set Gamma
# self.writeDat(0x02)
# self.writeDat(0x1c)
# self.writeDat(0x07)
# self.writeDat(0x12)
# self.writeDat(0x37)
# self.writeDat(0x32)
# self.writeDat(0x29)
# self.writeDat(0x2d)
# self.writeDat(0x29)
# self.writeDat(0x25)
# self.writeDat(0x2B)
# self.writeDat(0x39)
# self.writeDat(0x00)
# self.writeDat(0x01)
# self.writeDat(0x03)
# self.writeDat(0x10)
# self.writeCmd(ST7735_GMCTRN1) # Set Gamma
# self.writeDat(0x03)
# self.writeDat(0x1d)
# self.writeDat(0x07)
# self.writeDat(0x06)
# self.writeDat(0x2E)
# self.writeDat(0x2C)
# self.writeDat(0x29)
# self.writeDat(0x2D)
# self.writeDat(0x2E)
# self.writeDat(0x2E)
# self.writeDat(0x37)
# self.writeDat(0x3F)
# self.writeDat(0x00)
# self.writeDat(0x00)
# self.writeDat(0x02)
# self.writeDat(0x10)
# self.writeCmd(ST7735_NORON) # Normal display on
# time.sleep_ms(10) # 10 ms
# self.writeCmd(ST7735_DISPON) # Display on
# time.sleep_ms(100) # 100 ms
# # self.tft_fill(0,0,128,160,0x00)
# --------------------------------------------------------------
# Prototype : writeCmd(command)
# Parameters : command
# Description : 写命令
# --------------------------------------------------------------
def writeCmd(self, command):
"""Send command byte to display."""
#spi write.
#DC = 0 :write command
self._dc.write(0)
writeBuf=bytearray(1)
self.cs.write(0)
writeBuf[0]=command
self._spi.write(writeBuf,1)
self.cs.write(1)
# --------------------------------------------------------------
# Prototype : writeDat(data)
# Parameters : data
# Description : 写8bits数据
# --------------------------------------------------------------
def writeDat(self, data):
"""Send byte of data(8bits) to display."""
# spi write.
#DC = 1:write data
self._dc.write(1)
writeBuf=bytearray(1)
self.cs.write(0)
writeBuf[0]=data
self._spi.write(writeBuf,1)
self.cs.write(1)
# --------------------------------------------------------------
# Prototype : writeDat_16(data)
# Parameters : data
# Description : 写16bits数据,例如显示颜色
# --------------------------------------------------------------
def writeDat_16(self, data):
"""Send byte of data(16bits) to display."""
# spi write.
#DC = 1:write data
self._dc.write(1)
writeBuf=bytearray(2)
self.cs.write(0)
writeBuf[0]=data>>8
writeBuf[1]=data
self._spi.write(writeBuf,2)
self.cs.write(1)
# --------------------------------------------------------------
# Prototype : reset()
# Parameters : 无
# Description : 重启显示屏,如果reset引脚已连接
# --------------------------------------------------------------
def reset(self):
if self._rst is not None:
self._rst.write(1)
time.sleep_ms(500)
self._rst.write(0)
time.sleep_ms(500)
self._rst.write(1)
time.sleep_ms(500)
# --------------------------------------------------------------
# Prototype : tft_off()
# Parameters : 无
# Description : 关闭后,只亮白光
# --------------------------------------------------------------
def tft_off(self):
self.writeCmd(ST7735_DISPOFF)
# --------------------------------------------------------------
# Prototype : tft_on()
# Parameters : 无
# Description : 打开屏幕显示,默认背景颜色为黑色
# --------------------------------------------------------------
def tft_on(self):
self.writeCmd(ST7735_DISPON)
# --------------------------------------------------------------
# Prototype : tft_begin()
# Parameters : 无
# Description : 屏幕初始化
# --------------------------------------------------------------
def tft_begin(self):
self.reset()
self._initialize()
# --------------------------------------------------------------
# Prototype : tft_setPos(x0, y0, x1, y1)
# Parameters : x0,x1 设置x轴的起始和结束地址
# y0,y1 设置y轴的起始和结束地址
# Description : 设置起始和结束坐标
# --------------------------------------------------------------
def tft_setPos(self, x0, y0, x1, y1):
"""Set the pixel address window for proceeding drawing commands. x0 and
x1 should the minimum and maximum x pixel bounds. y0 and y1
should the minimum and maximum y pixel bound. If no parameters
are specified the default will be to update the entire display from 0,0
to width-1,height-1.
"""
# self.writeCmd(ST7735_CASET);#//列地址设置
# self.writeDat(x0+2)
# self.writeDat(x1+2)
# self.writeCmd(ST7735_RASET)#//行地址设置
# self.writeDat(y0+1)
# self.writeDat(y1+1)
# self.writeCmd(ST7735_RAMWR) #//储存器写
self.writeCmd(ST7735_CASET)
self.writeDat(x0>>8)
self.writeDat(x0) # XSTART
self.writeDat(x1>>8)
self.writeDat(x1) # XEND
self.writeCmd(ST7735_RASET) # Row addr set
self.writeDat(y0>>8)
self.writeDat(y0) # YSTART
self.writeDat(y1>>8)
self.writeDat(y1) # XEND
self.writeCmd(ST7735_RAMWR) # write to RAM
# --------------------------------------------------------------
# Prototype : tft_clear()
# Parameters : 无
# Description : 清屏,全黑
# --------------------------------------------------------------
def tft_clear(self):
self.tft_fill(0,0,128,160,ST7735_BLACK)
# --------------------------------------------------------------
# Prototype : tft_fill(x0,y0,x1,y1,color)
# Parameters : x0,y0 起始坐标
# x1,y1 终止坐标
# color 要填充的颜色
# Description : 填充指定块区域
# --------------------------------------------------------------
def tft_fill(self,x0,y0,x1,y1,color):
self.tft_setPos(x0,y0,x1-1,y1-1)
for i in range(y0,y1):
for j in range(x0,x1):
self.writeDat_16(color)
# --------------------------------------------------------------
# Prototype : tft_drawPoint(x,y,color)
# Parameters : x,y为所画的点的坐标,color为点的颜色
# Description : x取值范围(0,128),y取值范围(0,160)
# --------------------------------------------------------------
def tft_drawPoint(self,x,y,color):
self.tft_setPos(x,y,x,y)
self.writeDat_16(color)
# --------------------------------------------------------------
# Prototype : tft_drawLine(x0,y0,x1,y1,color)
# Parameters : (x0,y0)是所画线的起始坐标,(x1,y1)是所画线的终点坐标,color为线的颜色
# Description : x0,x1的取值范围为(0,128),y0,y1的取值范围为(0,160)
# --------------------------------------------------------------
def tft_drawLine(self,x0,y0,x1,y1,color):
xerr,yerr=0,0
delta_x,delta_y,distance=0,0,0
uRow=x0
uCol=y0
delta_x=x1-x0 #计算坐标增量
delta_y=y1-y0
if delta_x>0:
incx=1 #设置单步方向
elif delta_x==0:
incx=0 #垂直线
else:
incx=-1
delta_x=-delta_x
if delta_y>0:
incy=1
elif delta_y==0:
incy=0 #水平线
else:
incy=-1
delta_y=-delta_y
if delta_x>delta_y:
distance=delta_x #选取基本增量坐标轴
else:
distance=delta_y
for i in range(distance+2): #画线输出
self.tft_drawPoint(uRow,uCol,color) #画点
xerr+=delta_x
yerr+=delta_y
if xerr>distance:
xerr-=distance
uRow+=incx
if yerr>distance:
yerr-=distance
uCol+=incy
# --------------------------------------------------------------
# Prototype : tft_draw_circle_8(xc,yc,x,y,color)
# Description : 画出圆的外切矩形的四个点
# --------------------------------------------------------------
def tft_draw_circle_8(self,xc,yc,x,y,color):
self.tft_drawPoint(xc+x,yc+y,color)
self.tft_drawPoint(xc-x,yc+y,color)
self.tft_drawPoint(xc+x,yc-y,color)
self.tft_drawPoint(xc-x,yc-y,color)
self.tft_drawPoint(xc+y,yc+x,color)
self.tft_drawPoint(xc-y,yc+x,color)
self.tft_drawPoint(xc+y,yc-x,color)
self.tft_drawPoint(xc-y,yc-x,color)
# --------------------------------------------------------------
# Prototype : tft_drawCircle(xc,yc,r,color,mode)
# Parameters : xc,yc为圆的圆心坐标
# r为园的半径
# color为圆的颜色
# mode是圆的模式选择,0-空心圆,1-实心圆
# Description : 以指定坐标,半径画圆
# --------------------------------------------------------------
def tft_drawCircle(self,xc,yc,r,color,mode):
x=0
y=r
d=3-2*r
if mode:
#画实心圆
while x<=y:
for yi in range(x,y+1):
self.tft_draw_circle_8(xc,yc,x,yi,color)
if d<0:
d=d+4*x+6
else:
d=d+4*(x-y)+10
y-=1
x+=1
else:
#画空心圆
while x<=y:
self.tft_draw_circle_8(xc,yc,x,y,color)
if d<0:
d=d+4*x+6
else:
d=d+4*(x-y)+10
y-=1
x+=1
# --------------------------------------------------------------
# Prototype : tft_drawTriangel(x0,y0,x1,y1,x2,y2,color)
# Parameters : x0,y0为三角形的一个坐标
# x1,y1为三角形的一个坐标
# x2,y2为三角形的一个坐标
# color为三角形的颜色
# Description : 指定3个坐标 画三角形
# --------------------------------------------------------------
def tft_drawTriangel(self,x0,y0,x1,y1,x2,y2,color):
self.tft_drawLine(x0,y0,x1,y1,color)
self.tft_drawLine(x1,y1,x2,y2,color)
self.tft_drawLine(x2,y2,x0,y0,color)
# --------------------------------------------------------------
# Prototype : tft_showSignleChar(x, y, char,color, sizex=1, sizey=1)
# Parameters : x,y--为单个字符起始坐标
# char--为单个字符内容
# color--单个字符的颜色
# Description : 指定起始位置,显示单个字符,当前字体大小为 6*8(width*height)
# --------------------------------------------------------------
def tft_showSingleChar(self, x, y, char,color, sizex=1, sizey=1):
startchar = terminalfont['start']
endchar = terminalfont['end']
ci = ord(char)
print("ci=",ci)
if (startchar <= ci <= endchar):
width = terminalfont['width']
height = terminalfont['height']
ci = (ci - startchar) * width
ch = terminalfont['data'][ci:ci + width]
px = x
if (sizex <= 1 and sizey <= 1):
for c in ch:
py = y
for _ in range(height):
if c & 0x01:
self.tft_drawPoint(px, py, color)
py += 1
c >>= 1
px += 1
# --------------------------------------------------------------
# Prototype : tft_showString(x,y,string,color,size=1)
# Parameters : x,y--为字符串起始坐标
# string--为字符串的内容
# color--字符串的颜色
# Description : 指定起始位置,显示字符串,当前字体大小为 6*8
# --------------------------------------------------------------
def tft_showString(self,x,y,string,color,size=1):
width=size*terminalfont['width']-1
px=x
for c in string:
self.tft_showSingleChar(px,y,c,color,1,1)
px+=width
if px+width>self.width:
y+=terminalfont['height']*size+1
px=x
# --------------------------------------------------------------
# Prototype : tft_showSingleCN(x,y,cn,color,mode)
# Parameters : x,y为中文/特定英文字符显示的起始坐标,
# cn为tft_showSingleCN(x,y,cn,color,mode)传入的cn
# color为中文颜色
# mode=0显示16*16中文字体大小,mode=1显示24*24中文字体大小,mode=2显示8*16英文字符
# Description : 显示单个中文字符/特的字体大小的英文字符 ,需要与tft_showSingleCN()配合使用
# --------------------------------------------------------------
def tft_showSingleCN(self,x,y,cn,color,mode):
if mode==0:
px = x
ch=cn[:32]
for c in ch:
py=y
for _ in range(8):
if c & 0x01:
self.tft_drawPoint(px, py, color)
py += 1
c >>= 1
px += 1
if px==(16+x) :
px=0+x
y=8+y
continue
if mode==1:
px = x
ch=cn[:72]
for c in ch:
py=y
for _ in range(8):
if c & 0x01:
self.tft_drawPoint(px, py, color)
py += 1
c >>= 1
px += 1
if px==(24+x) :
px=0+x
y=8+y
continue
if mode==2:
px = x
ch=cn[:16]
for c in ch:
py=y
for _ in range(8):
if c & 0x01:
self.tft_drawPoint(px, py, color)
py += 1
c >>= 1
px += 1
if px==(8+x) :
px=0+x
y=8+y
continue
# --------------------------------------------------------------
# Prototype : tft_showCHN(x,y,n,color,mode)
# Parameters : x,y为中文/特定英文字符显示的起始坐标,
# n为要显示汉字的个数,个数不能超过字库中的汉字个数
# color为中文颜色
# mode=0显示16*16中文字体大小,mode=1显示24*24中文字体大小,mode=2显示8*16英文字符
# Description : 显示中文字符/特定字体大小的英文字符
# --------------------------------------------------------------
def tft_showCHN(self,x,y,n,color,mode):
#显示n个汉字/字符串
for i in range(n):
if mode==0:
cn=F16_16[i*32:(i+1)*32]
self.tft_showSingleCN(x,y,cn,color,mode)
x+=16
if mode==1:
cn=F24_24[i*72:(i+1)*72]
self.tft_showSingleCN(x,y,cn,color,mode)
x+=24
if mode==2:
cn=F8_16[i*16:(i+1)*16]
self.tft_showSingleCN(x,y,cn,color,mode)
x+=8
# --------------------------------------------------------------
# Prototype : TFTColor(aR, aG, aB)
# Parameters : aR, aG, aB,
# Description : rgb转换
# --------------------------------------------------------------
def TFTColor(self,aR, aG, aB ) :
'''Create a 16 bit rgb value from the given R,G,B from 0-255.
This assumes rgb 565 layout and will be incorrect for bgr.'''
return ((aR & 0xF8) << 8) | ((aG & 0xFC) << 3) | (aB >> 3)
# --------------------------------------------------------------
# Prototype : tft_showBmp(path)
# Parameters : path-图片存放的位置
# Description : 显示BMP图片
# --------------------------------------------------------------
def tft_showBmp(self,path):
f=open(path,'rb')
if f.read(2) == b'BM': #header
dummy = f.read(8) #file size(4), creator bytes(4)
offset = int.from_bytes(f.read(4), 'little')
hdrsize = int.from_bytes(f.read(4), 'little')
width = int.from_bytes(f.read(4), 'little')
height = int.from_bytes(f.read(4), 'little')
if int.from_bytes(f.read(2), 'little') == 1: #planes must be 1
depth = int.from_bytes(f.read(2), 'little')
if depth == 24 and int.from_bytes(f.read(4), 'little') == 0:#compress method == uncompressed
print("Image size:", width, "x", height)
rowsize = (width * 3 + 3) & ~3
if height < 0:
height = -height
flip = False
else:
flip = True
w, h = width, height
if w > 128: w = 128
if h > 160: h = 160
self.tft_setPos(0,0,w - 1,h - 1)
for row in range(h):
if flip:
pos = offset + (height - 1 - row) * rowsize
else:
pos = offset + row * rowsize
if f.tell() != pos:
dummy = f.seek(pos)
for col in range(w):
bgr = f.read(3)
self.writeDat_16(self.TFTColor(bgr[2],bgr[1],bgr[0]))
- font.py
#6*8
terminalfont = {
"width": 6,
"height": 8,
"start": 32,
"end": 127,
"data": bytearray([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x5F, 0x06, 0x00,
0x00, 0x07, 0x03, 0x00, 0x07, 0x03,
0x00, 0x24, 0x7E, 0x24, 0x7E, 0x24,
0x00, 0x24, 0x2B, 0x6A, 0x12, 0x00,
0x00, 0x63, 0x13, 0x08, 0x64, 0x63,
0x00, 0x36, 0x49, 0x56, 0x20, 0x50,
0x00, 0x00, 0x07, 0x03, 0x00, 0x00,
0x00, 0x00, 0x3E, 0x41, 0x00, 0x00,
0x00, 0x00, 0x41, 0x3E, 0x00, 0x00,
0x00, 0x08, 0x3E, 0x1C, 0x3E, 0x08,
0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,
0x00, 0x00, 0xE0, 0x60, 0x00, 0x00,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08,
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,
0x00, 0x20, 0x10, 0x08, 0x04, 0x02,
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,
0x00, 0x62, 0x51, 0x49, 0x49, 0x46,
0x00, 0x22, 0x49, 0x49, 0x49, 0x36,
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,
0x00, 0x2F, 0x49, 0x49, 0x49, 0x31,
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,
0x00, 0x01, 0x71, 0x09, 0x05, 0x03,
0x00, 0x36, 0x49, 0x49, 0x49, 0x36,
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,
0x00, 0x00, 0x6C, 0x6C, 0x00, 0x00,
0x00, 0x00, 0xEC, 0x6C, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,
0x00, 0x24, 0x24, 0x24, 0x24, 0x24,
0x00, 0x00, 0x41, 0x22, 0x14, 0x08,
0x00, 0x02, 0x01, 0x59, 0x09, 0x06,
0x00, 0x3E, 0x41, 0x5D, 0x55, 0x1E,
0x00, 0x7E, 0x11, 0x11, 0x11, 0x7E,
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,
0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,
0x00, 0x7F, 0x41, 0x41, 0x41, 0x3E,
0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,
0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,
0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,
0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,
0x00, 0x30, 0x40, 0x40, 0x40, 0x3F,
0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,
0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,
0x00, 0x7F, 0x02, 0x04, 0x02, 0x7F,
0x00, 0x7F, 0x02, 0x04, 0x08, 0x7F,
0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,
0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,
0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,
0x00, 0x7F, 0x09, 0x09, 0x19, 0x66,
0x00, 0x26, 0x49, 0x49, 0x49, 0x32,
0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,
0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,
0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,
0x00, 0x3F, 0x40, 0x3C, 0x40, 0x3F,
0x00, 0x63, 0x14, 0x08, 0x14, 0x63,
0x00, 0x07, 0x08, 0x70, 0x08, 0x07,
0x00, 0x71, 0x49, 0x45, 0x43, 0x00,
0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,
0x00, 0x02, 0x04, 0x08, 0x10, 0x20,
0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,
0x00, 0x04, 0x02, 0x01, 0x02, 0x04,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x00, 0x00, 0x03, 0x07, 0x00, 0x00,
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,
0x00, 0x7F, 0x44, 0x44, 0x44, 0x38,
0x00, 0x38, 0x44, 0x44, 0x44, 0x28,
0x00, 0x38, 0x44, 0x44, 0x44, 0x7F,
0x00, 0x38, 0x54, 0x54, 0x54, 0x08,
0x00, 0x08, 0x7E, 0x09, 0x09, 0x00,
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,
0x00, 0x7F, 0x04, 0x04, 0x78, 0x00,
0x00, 0x00, 0x00, 0x7D, 0x40, 0x00,
0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,
0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,
0x00, 0x00, 0x00, 0x7F, 0x40, 0x00,
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,
0x00, 0x7C, 0x04, 0x04, 0x78, 0x00,
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,
0x00, 0xFC, 0x44, 0x44, 0x44, 0x38,
0x00, 0x38, 0x44, 0x44, 0x44, 0xFC,
0x00, 0x44, 0x78, 0x44, 0x04, 0x08,
0x00, 0x08, 0x54, 0x54, 0x54, 0x20,
0x00, 0x04, 0x3E, 0x44, 0x24, 0x00,
0x00, 0x3C, 0x40, 0x20, 0x7C, 0x00,
0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,
0x00, 0x3C, 0x60, 0x30, 0x60, 0x3C,
0x00, 0x6C, 0x10, 0x10, 0x6C, 0x00,
0x00, 0x9C, 0xA0, 0x60, 0x3C, 0x00,
0x00, 0x64, 0x54, 0x54, 0x4C, 0x00,
0x00, 0x08, 0x3E, 0x41, 0x41, 0x00,
0x00, 0x00, 0x00, 0x77, 0x00, 0x00,
0x00, 0x00, 0x41, 0x41, 0x3E, 0x08,
0x00, 0x02, 0x01, 0x02, 0x01, 0x00,
0x00, 0x3C, 0x26, 0x23, 0x26, 0x3C
])}
#点阵为:宽x高=16x16
#取模方式:列行式
#阴码
F16_16=[
0x80,0x70,0x00,0xFF,0x10,0x08,0x00,0x7E,0x2A,0xAA,0x2A,0x2A,0x2A,0x7E,0x00,0x00,
0x80,0x60,0x18,0x07,0x08,0x50,0x42,0x4A,0x52,0x42,0x43,0x42,0x52,0x4A,0x42,0x00, #煜
0x84,0x84,0xFC,0x84,0x84,0x00,0x04,0xC4,0x5F,0x44,0xF4,0x44,0x5F,0xC4,0x04,0x00,
0x10,0x30,0x1F,0x08,0x08,0x00,0x84,0x47,0x24,0x14,0x0F,0x14,0x24,0x47,0x84,0x00, #瑛
0x40,0x3C,0x10,0xFF,0x10,0x10,0x20,0x10,0x8F,0x78,0x08,0xF8,0x08,0xF8,0x00,0x00,
0x02,0x06,0x02,0xFF,0x01,0x01,0x04,0x42,0x21,0x18,0x46,0x81,0x40,0x3F,0x00,0x00, #物
0x02,0xFE,0x92,0x92,0xFE,0x02,0x00,0x10,0x11,0x16,0xF0,0x14,0x13,0x10,0x00,0x00,
0x10,0x1F,0x08,0x08,0xFF,0x04,0x81,0x41,0x31,0x0D,0x03,0x0D,0x31,0x41,0x81,0x00, #联
0x00,0xFE,0x02,0x22,0x42,0x82,0x72,0x02,0x22,0x42,0x82,0x72,0x02,0xFE,0x00,0x00,
0x00,0xFF,0x10,0x08,0x06,0x01,0x0E,0x10,0x08,0x06,0x01,0x4E,0x80,0x7F,0x00,0x00, #网
]
#点阵为:宽x高=24x24
#取模方式:列行式
#阴码
F24_24=[
0x00,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0xC0,0x40,0xF8,0xF8,0x48,0x48,0x48,0x48,0x48,0x48,0x48,0xF8,0x08,0x00,0x00,0x00,
0x00,0x08,0x0E,0x00,0x00,0xFF,0x82,0x01,0x00,0x20,0x27,0xA7,0x22,0x22,0x26,0x3A,0x22,0x22,0xE2,0xA3,0x30,0x20,0x00,0x00,
0x00,0x40,0x20,0x18,0x0F,0x01,0x00,0x01,0x27,0x20,0x20,0x20,0x27,0x2E,0x20,0x20,0x38,0x26,0x21,0x20,0x20,0x30,0x20,0x00,#/*"煜",0*/
0x00,0x10,0x10,0x10,0xF0,0xF0,0x10,0x10,0x10,0x20,0x20,0x20,0xFC,0x20,0xA0,0x20,0x20,0xFC,0x20,0x20,0x20,0x20,0x00,0x00,
0x00,0x04,0x04,0x04,0xFF,0xFF,0x04,0x86,0x84,0x80,0xFC,0x84,0x84,0x84,0xF4,0xFF,0x85,0x84,0x84,0xFC,0x84,0x80,0x80,0x00,
0x00,0x04,0x0C,0x04,0x07,0x03,0x02,0x42,0x41,0x21,0x20,0x10,0x08,0x06,0x03,0x00,0x03,0x04,0x08,0x10,0x30,0x20,0x20,0x00,#/*"瑛",1*/
0x00,0x00,0x00,0xF0,0x90,0x80,0xFE,0x84,0x80,0x80,0x00,0xC0,0x38,0x2E,0xE0,0x60,0x20,0xA0,0xE0,0x20,0x20,0xE0,0x00,0x00,
0x00,0x8C,0x83,0x80,0x40,0x40,0xFF,0x20,0x10,0x92,0x41,0x20,0x18,0x06,0x01,0xC0,0x78,0x0F,0x01,0x00,0xF0,0xFF,0x00,0x00,
0x00,0x00,0x01,0x00,0x00,0x00,0x7F,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x10,0x10,0x70,0x30,0x30,0x1F,0x01,0x00,0x00,#/*"物",2*/
0x00,0x08,0x08,0xF8,0x08,0x08,0x08,0x08,0xF8,0x08,0x88,0x80,0x82,0xFC,0xB0,0x80,0x80,0xE0,0x9C,0x8C,0xC4,0x80,0x00,0x00,
0x00,0x00,0x00,0xFF,0x11,0x11,0x11,0x11,0xFF,0x10,0x90,0x10,0x10,0x10,0x10,0xFF,0x3F,0xD0,0x10,0x10,0x10,0x18,0x10,0x00,
0x00,0x00,0x04,0x07,0x02,0x02,0x02,0x01,0x7F,0x41,0x40,0x20,0x10,0x08,0x06,0x01,0x00,0x01,0x06,0x1C,0x30,0x30,0x20,0x00,#/*"联",3*/
0x00,0x00,0x00,0xF8,0x08,0x88,0x08,0x08,0x08,0xC8,0x88,0x08,0x08,0x08,0x08,0x08,0xC8,0x88,0x08,0x08,0xFC,0x00,0x00,0x00,
0x00,0x00,0x00,0xFF,0x00,0x00,0xC1,0x36,0x0E,0x73,0xE0,0x00,0x01,0xC2,0x3C,0x1C,0x73,0xC0,0x00,0x00,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x7F,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,0x00,0x00,0x21,0x20,0x70,0x3F,0x00,0x00,0x00,#/*"网",4*/
]
#点阵为:宽x高=8x16
#取模方式:列行式
#阴码
F8_16=[
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,#/*"H",0*/
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,#/*"A",1*/
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,#/*"A",2*/
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,#/*"S",3*/
0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00,#/*"5",4*/
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,#/*"0",5*/
0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00,#/*"6",6*/
]
- board.json
{
"name": "haas506",
"version": "1.0.0",
"io": {
"OLED": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 60
},
"KEY1": {
"type": "GPIO",
"port": 44,
"dir": "irq",
"pull": "pullup",
"intMode": "rising"
},
"led1": {
"type": "GPIO",
"port": 7,
"dir": "output",
"pull": "pulldown"
},
"cs": {
"type": "GPIO",
"port": 15,
"dir": "output",
"pull": "pullup"
},
"dc":{
"type":"GPIO",
"port": 19,
"dir": "output",
"pull":"pullup"
},
"rst":{
"type":"GPIO",
"port": 20,
"dir": "output",
"pull":"pullup"
},
"ST7735S": {
"type": "SPI",
"port": 0,
"mode": "master",
"freq": 2000000
},
"serial1": {
"type": "UART",
"port": 0,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial2": {
"type": "UART",
"port": 1,
"dataWidth": 8,
"baudRate": 9600,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial3": {
"type": "UART",
"port": 2,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
}
},
"debugLevel": "ERROR",
"repl": "enable",
"replPort": 0
}
3.测试结果
- 清屏/上电初始化完成
- 画空心圆
- 画实心圆
- 画三角形
- 画像素点/画线
- 显示BMP图片
- 显示英文字符,大小6*8
4.待解决
- 更换字库后,字符镜像显示问题
- 更换字库,字符显示出错
- 显示特定大小的英文字符,字体大小:8*16
- 中文字符输出问题
已解决
- 字体大小:24*24
- 字体大小:16*16
- 屏幕刷新速率问题
5.总结
本节实现了128*160TFT(驱动ST7735S)屏幕点亮、画点、画线、画圆、显示字符串、显示BMP图片功能。