ST7735S - TFT_SPI彩屏
ST7735是一款由Sitronix公司生产的单芯片TFT-LCD控制器/驱动器,带有内置的DC/DC转换器和可调整的VCOM生成电路。这款芯片专为彩色液晶显示屏设计,提供不同分辨率和颜色模式,并支持多种接口,如并行8080系列MCU接口和串行接口。

现在的彩屏库继承自FrameBuffer,一个彩屏会使用width*height*2字节的内存,比如对于128*128尺寸的彩屏,则会消耗128*128*2=32k字节的数据。在使用时注意内存消耗。
彩屏所有的绘制函数,比如描点,绘制矩形,绘制二维码等函数仅仅是将数据写入缓存,不会真正显示在屏幕上,只有当调用“ST7735S彩屏显示生效”图形块,才会将缓存数据显示出来。
一、彩屏初始化

# 头文件
import st7735s
# 类
class st7735s.ST7735S(spi,*,dc,rst,cs,bl=None, width=128, height=128)- 参数
spi:spi对象。图形块会根据参数自动生成。dc:dc引脚对象。rst:rst引脚对象。cs:cs引脚对象。widh:彩屏宽度。height:彩屏高度。
目前只支持128*128, 128*160(未测试),80*160(未测试)分辨率的屏幕。
- 返回值
无
二、彩屏清除

ST7735S.fill(c)将彩屏全屏设置为特定的颜色。
- 参数
c:颜色(RGB565格式,小端序)。
三、彩屏显示生效

ST7735S.show()将缓存数据写入彩屏。只有调用该函数后彩屏才会真正显示出来。
使用示例:

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0x00,0x00)))
tft.show()显示效果如下:

四、显示文本

ST7735S.DispChar(s, x, y, fg, bg=None, auto_return=True)- 参数
s:需要显示的文本(支持中文、字母、数字)。x:文本左上角的x坐标。y:文本左上角的y坐标。fg:文本的颜色。bg:文本背景的颜色。可以设置为空。当为空时,背景颜色则不会显示。auto_return:可选,是否开启自动换行,默认开启。
使用示例:

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.DispChar(str('你好,世界'),0,0, (st7735s.rgb565(0xff,0x00,0x00)), (st7735s.rgb565(0x00,0x00,0x00)))
tft.DispChar(str('Hello, TWen'),0,16, (st7735s.rgb565(0xff,0x00,0x00)), None)
tft.show()实际显示效果如下:

五、描点

ST7735S.pixel(x, y, c)- 参数
x:该点的x坐标。y:该点的y坐标。c:该点的颜色值。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.pixel(10,10, (st7735s.rgb565(0xff,0x00,0x00)))
tft.show()实际显示效果如下:

可以看到在左上角(10,10)的位置有正确显示点。
六、绘制进度条

ST7735S.ProgressBar(x, y, width, height, progress, fc, bc)- 参数
x:进度条左上角x坐标。y:进度条左上角y坐标。width:进度条的宽度。height:进度条的高度。progress:进度条的进度值(0-100)。fc:进度条的颜色。bc:进度条边框的颜色。
- 注意:如果进度条进度和边框的颜色不对,请更新最新的固件以及重新加载库。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.ProgressBar(x=30, y=30, width=70, height=8, progress=20, fc=(st7735s.rgb565(0xff,0x00,0x00)), bc=(st7735s.rgb565(0x00,0x00,0x00)))
tft.show()实际显示效果如下:

七、绘制柱状图

ST7735S.stripBar(x, y, width, height, progress, dir=1, fc=0, bc=0, frame=1)- 参数
x:柱状图左上角x坐标。y:柱状图左上角y坐标。width:宽度。height:高度progress:柱状图进度(0-100)。dir:方向,0:垂直;1:水平。fc:柱状图前景色。bc:柱状图背景色。frame:可选,外围框架的宽度。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.stripBar(x=64, y=20, width=6, height=30, progress=20, dir=0, fc=(st7735s.rgb565(0xff,0x00,0x00)), bc=(st7735s.rgb565(0x00,0x00,0x00)))
tft.show()实际显示效果如下:

八、绘制垂直/水平线段

ST7735S.vline(x, y, h, c)- 参数
x:垂直线起点的x坐标。y:垂直线起点的y坐标。h:长度。c:颜色值。

ST7735S.hline(x, y, w, c)- 参数
x:垂直线起点的x坐标。y:垂直线起点的y坐标。h:长度。c:颜色值。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.hline(10, 10, 30, (st7735s.rgb565(0xff,0x00,0x00)))
tft.vline(20, 20, 20, (st7735s.rgb565(0xff,0x00,0x00)))
tft.show()实际显示效果如下:

九、绘制线段

ST7735S.line(x1, y1, x2, y2, c)- 参数
x1:线段的起点x坐标。y1:线段的起点y坐标。x2:线段的终点x坐标。y2:线段的终点y坐标。c:颜色。
使用示例

十、绘制矩形

ST7735S.rect(x, y, w, h, c[, f])- 参数
x:空心矩形的左上角x坐标。y:空心矩形的左上角y坐标。w:空心矩形的宽度。h:空心矩形的高度。c:空心矩形的颜色。f:可选,矩形边框的宽度。

ST7735S.fill_rect(x, y, w, h, c)- 参数
x:实心矩形的左上角x坐标。y:实心矩形的左上角y坐标。w:实心矩形的宽度。h:实心矩形的高度。c:实心矩形的颜色。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.rect(20, 20, 20, 15, (st7735s.rgb565(0xff,0x00,0x00)))
tft.fill_rect(20, 60, 20, 15, (st7735s.rgb565(0xff,0x00,0x00)))
tft.show()实际显示效果如下:

十一、绘制弧角边框

ST7735S.RoundRect(x, y, width, height, radius, c)- 参数
x:弧角边框左上角x坐标。y:弧角边框左上角y坐标。w:弧角边框宽度。h:弧角边框高度。r:弧角边框半径。c:颜色值。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.RoundRect(40, 20, 50, 30, 10, (st7735s.rgb565(0xff,0x00,0x00)))
tft.show()实际显示效果如下:

十二、绘制圆

ST7735S.circle(x, y, radius, c)- 参数
x:圆心的x坐标。y:圆心的y坐标。radius:圆的半径。c:圆的颜色。

ST7735S.fill_circle(x, y, radius, c)- 参数
- x:圆心的x坐标。
- y:圆心的y坐标。
- radius:圆的半径。
- c:圆的颜色。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.circle(64, 32, 15, (st7735s.rgb565(0xff,0x00,0x00)))
tft.fill_circle(64, 64, 15, (st7735s.rgb565(0xff,0x00,0x00)))
tft.show()实际显示效果如下

十三、绘制三角形

ST7735S.triangle(x0,y0,x1,y1,x2,y2,c)- 参数
x0,x1,x2:三角形三个角的x坐标。y0,y1,y2:三角形三个角的y坐标。c:颜色。

ST7735S.fill_triangle(x0,y0,x1,y1,x2,y2,c)- 参数
- x0,x1,x2:三角形三个角的x坐标。
- y0,y1,y2:三角形三个角的y坐标。
- c:颜色。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.triangle(84, 20, 44, 20, 64, 40, (st7735s.rgb565(0xff,0x00,0x00)))
tft.fill_triangle(84, 84, 44, 84, 64, 104, (st7735s.rgb565(0xff,0x00,0x00)))
tft.show()实际显示效果如下

十四、绘制二维码

ST7735S.qr_code(str, x, y, scale=2)绘制二维码,默认采用类型为3,纠错级别为L的配置。
- 参数
str:字符串,二维码数据。最大长度为53个字节数据(在UTF-8编码中,英文字符占一个字节,中文字符一般占3个字符,有个别会占用4个字节)。x:二维码左上角x坐标。y:二维码左上角y坐标。scale:放大倍数,可以为1(29*29),2(58*58)。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.qr_code(str('http://www.twen51.com'), 35, 35, scale=2)
tft.show()实际显示效果如下

十五、使用16进制数据显示图像

ST7735S.blit(fbuf, x, y, key=-1, palette=None)
# fbuf为一个FrameBuffer对象
class framebuf.FrameBuffer(buffer, width, height, format, stride=width, /)使用该图像块会消耗大量内存,显示时一般根据图片一行或者几行显示。一般不推荐使用。
十六、显示取模的图像

ST7735S.draw_bin(x, y, width, height, path, read_line=1)经过实测,使用取模过的数据显示图片(128*128分辨率)能够达到50ms以内。
- 参数
x:图片左上角x坐标。y:图片左上角y坐标。width:图片的宽度。height:图片的高度。path:图片的路径(当使用"http://"或"https://"开头时可以获取网络上取模过的数据,否则读取本地里的图片)。read_line:可选,每次刷新行数,默认为1。
ST7735S彩屏案例素材(需要解压后放入文件系统):
如果使用Image2Lcd取模,则参数如下所示:

使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.draw_bin(0, 0, 128, 128, 'img.bin')
tft.show()实际显示如下

十七、显示24位BMP图片

ST7735S.draw_bmp(x, y, path)- 参数
x:图片的左上角x坐标。y:图片的左上角y坐标。path:图片的路径(当使用"http://"或"https://"开头时可以获取网络上取模过的数据,否则读取本地里的图片)。
注意:该图形块只支持24位深度的图片。查看该图片的属性应如下:

我们可以直接使用电脑自带的画图工具将任意图片转为24位BMP图片:


使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.draw_bmp(30, 30, '1.bmp')
tft.show()实际显示如下

十八、初始化一个时钟准备在屏幕上绘制

ST7735S.initClock(x, y, radius)- 参数
x:时钟的圆心x坐标。y:时钟的圆心y坐标。radius:时钟的半径。
十九、绘制时钟

ST7735S.drawClock(dial_c=0, hour_c=0, min_c=0, sec_c=0)- 参数
dial_c:表盘的颜色。hour_c:时钟的颜色。min_c:分钟的颜色。sec_c:秒钟的颜色。
二十、时钟获取时间

ST7735S.settime()二十一、清除时钟

ST7735S.clearClock(c)- 参数
c:颜色值。
使用示例

from board import *
from machine import SPI
import st7735s
import time
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(0xff,0xff,0xff)))
tft.initClock(64, 64, 30)
while True:
tft.clearClock((st7735s.rgb565(0xff,0xff,0xff)))
tft.settime()
tft.drawClock((st7735s.rgb565(0xff,0x00,0x00)), (st7735s.rgb565(0xff,0x00,0x00)), (st7735s.rgb565(0xff,0x00,0x00)), (st7735s.rgb565(0xff,0x00,0x00)))
tft.show()
time.sleep(1)实际显示如下

二十二、设置显示模式

ST7735S.rotate(angle, rgb_ord=1)配置屏幕显示的角度以及显示模式。
- 参数
angle:旋转角度,可设置为0,90,180,270。rgb_ord:可选,可以配置为0或者1。图形块显示的是RGB和BGR,不过市面上部分屏幕RGB和BGR值是不一样的,所以如果你的屏幕显示颜色不对,即可修改该值测试一下。
二十三、颜色值

st7735s.rgb565(r,g,b)将R,G和B值转换为rgb565格式(小端格式)。
使用该块能够更任意的绘制各种颜色的图案。
使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
print(st7735s.rgb565(255,0,0))终端大于信息如下(248即0x00F8):

使用示例

from board import *
from machine import SPI
import st7735s
spi = SPI(1, baudrate=40000000, polarity=0, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
tft = st7735s.ST7735S(spi, dc=Pin(5), rst=Pin(6), cs=Pin(7), width=128, height=128)
tft.fill((st7735s.rgb565(255,0,0)))
tft.show()实际显示如下:
