树莓派4B驱动1.8寸ST7735S TFT屏幕

2021-10-08 09:59:38 浏览数 (3)

用到的第三方库的官方文档:Introduction — Luma.LCD: Display drivers for PCD8544, ST7735, ST7789, HT1621, UC1701X, ST7567, ILI9341, ILI9486, HD44780 2.9.0 documentation

https://luma-lcd.readthedocs.io/en/latest/intro.html

支持的IC驱动有:PCD8544、ST7735、ST7789、HT1621、UC1701X、ILI9341、HD44780

屏幕参数:1.8寸 120X160 RGB_TFT

安装库:

代码语言:javascript复制
sudo -H pip install --upgrade luma.lcd

安装依赖:

如果您使用的是 Raspbian Stretch 或 Raspberry Pi OS (Buster),您应该能够使用以下命令添加所需的包:

代码语言:javascript复制
sudo apt-get update
sudo apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 -y
sudo -H pip3 install luma.lcd

如果您不使用 Raspbian,则需要查阅 Linux 发行版的文档以确定安装依赖项的正确步骤。

授予权限:

luma.lcd使用需要访问权限的硬件接口。成功安装后,luma.lcd您可能希望将您的luma.lcd程序将运行的用户帐户添加到授予对这些接口的访问权限的组中。

代码语言:javascript复制
sudo usermod -a -G spi,gpio,i2c pi

SPI与BCM接口:

代码语言:javascript复制
cs = 24   # 片选
dc = 25   # 数据/命令 切换
sda = 19  # 数据
scl = 23  # 时钟
rst = 27  # 复位

示例代码:

代码语言:javascript复制
# -*- coding: UTF-8 -*-

from luma.core.interface.serial import spi
from luma.lcd.device import st7735
from PIL import Image, ImageDraw, ImageFont
from luma.core.render import canvas
import RPi.GPIO as GPIO
import time

BL = 24

class Screen:
    def __init__(self):
        self.height = 128
        self.width = 160
        self.serial = spi(port=0, device=0, gpio_DC=25, gpio_RST=27)
        self.device = st7735(self.serial, width=self.width, height=self.height, rotate=1, h_offset=0, v_offset=0, bgr=False)
        self.buffer = Image.new(self.device.mode, self.device.size)
        self.fontType = '/usr/share/fonts/ZaoZiGongFangZhiYanTi.ttf'
        self.fontTypeEN = '/usr/share/fonts/consolas/CONSOLAB.TTF'
        self.fontSize = 24
        self.font = ImageFont.truetype(self.fontType, self.fontSize)
        self.draw = ImageDraw.Draw(self.buffer)
    
    def initGPIO(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(BL,GPIO.OUT)
    
    def closeGPIO(self):
        GPIO.cleanup()

    def openScreen(self):
        GPIO.output(BL, GPIO.HIGH)

    def closeScreen(self):
        GPIO.output(BL, GPIO.LOW)
    
    def drawRect(self, x, y, w, h, color='black', outline=None):
        self.draw.rectangle((x, y, x w, y h), outline=outline, fill=color)
        self.device.display(self.buffer)
    
    def drawDemo(self):
        self.draw.rectangle((10,10,10 20,10 20), outline="white", fill="green")
        self.draw.text((30, 40), "Hello World", fill="red")
        self.draw.text((10, 70), "http://xfxuezhang.cn", "white")
        self.device.display(self.buffer)
        
        #with canvas(self.device) as draw:
        #    draw.rectangle((10,10,10 20,10 20), outline="white", fill="green")
        #    draw.text((30, 40), "Hello World", fill="red")
        #    draw.text((10, 70), "http://xfxuezhang.cn", "white")
    
    def drawTextEN(self, x, y, msg, color='white', fontSize=None, fontType=None):
        newType = fontType if fontType else self.fontTypeEN
        newSize = fontSize if fontSize else self.fontSize
        font = ImageFont.truetype(newType, newSize)
        self.drawRect(x, y, len(msg)*newSize/2, newSize, 'black')
        self.draw.text((x, y), msg, font=font, fill=color)
        self.device.display(self.buffer)

    def drawTextCN(self, x, y, msg, color='white', fontSize=None, fontType=None):
        newType = fontType if fontType else self.fontType
        newSize = fontSize if fontSize else self.fontSize
        font = ImageFont.truetype(newType, newSize)
        self.drawRect(x, y, len(msg)*newSize, newSize, 'black')
        self.draw.text((x, y), msg, font=font, fill=color)
        self.device.display(self.buffer)

    def clearScreen(self, color='black'):
        self.draw.rectangle(self.device.bounding_box, outline=None, fill=color)
        self.device.display(self.buffer)

    def showInfo(self):
        self.clearScreen()
        self.drawTextCN(18, 20, '小锋学长')
        self.drawTextCN(5, 45, '生活大爆炸')
        self.drawTextEN(20, 80, "xfxuezhang.cn", "red", fontSize=12)
    
    
if __name__ == '__main__':
    try:
        screen = Screen()
        screen.initGPIO()
        screen.openScreen()
        screen.drawDemo()
        time.sleep(2)
        screen.clearScreen()
        print(screen.width)
        print(screen.height)
        screen.showInfo()
        screen.drawTextCN(5, 125, '捕获总数=', color='green', fontSize=18)
        screen.drawTextEN(85, 125, str(1), color='red', fontSize=18)
        time.sleep(2)
        screen.drawTextEN(85, 125, str(2), color='red', fontSize=18)
    except:
        pass
    finally:
        screen.closeGPIO()

实际效果:

0 人点赞