栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

用到的第三方库的官方文档:Introduction — Luma.LCD: Display drivers for PCD8544, ST7735, ST7789, HT1621, UC1701X, ST7567, ILI9341, ILI9486, HD44780 2.9.0 documentationhttps://luma-lcd.readthedocs.io/en/latest/intro.html

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

屏幕参数:1.8寸 120x160 RGB_TFT

安装库:

sudo -H pip install --upgrade luma.lcd

安装依赖:

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

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程序将运行的用户帐户添加到授予对这些接口的访问权限的组中。

sudo usermod -a -G spi,gpio,i2c pi

SPI与BCM接口:

cs = 24   # 片选
dc = 25   # 数据/命令 切换
sda = 19  # 数据
scl = 23  # 时钟
rst = 27  # 复位

示例代码:

# -*- 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.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 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 drawText(self, x, y, msg, color='white'):
        self.draw.text((x, y), msg, fill=color)
        self.device.display(self.buffer)

    def drawTextCN(self, x, y, msg, color='white'):
        self.draw.text((x, y), msg, font=self.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(20, 40, '小锋学长')
        self.drawTextCN(5, 65, '生活大爆炸')
        self.drawText(25, 100, "xfxuezhang.cn", "red")
    
try:
    screen = Screen()
    screen.initGPIO()
    screen.openScreen()
    screen.drawDemo()
    time.sleep(2)
    screen.clearScreen()
    print(screen.width)
    print(screen.height)
    screen.showInfo()
except:
    pass
finally:
    screen.closeGPIO()
    
    
    

实际效果:

​​​​​​​

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号