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

本文档详细介绍了如何使用Luma.LCD库在树莓派上驱动不同类型的LCD屏幕,包括安装依赖、配置SPI接口及示例代码。通过实例展示了如何初始化GPIO、打开和关闭屏幕、绘制图形和文本。适用于进行嵌入式系统开发的读者。
摘要由CSDN通过智能技术生成

用到的第三方库的官方文档: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.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()
    
    
    

实际效果:

树莓派4B引脚图:

 

 

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
树莓派LCD驱动/2.8/3.2/3.5/3.97/4.3/5/7等! You can focus on the following GitHub web site, for the latest lcd drivers: https://github.com/goodtft/LCD-show =============================================================================================================== "LCD-show-170315.tar.gz" just for Raspbian (Release date:2017-03-02) or later, DO NOT use it for the Raspbian version before 2017-03-02. ================================================================================================================ "LCD-show-160701.tar.gz" support Raspbian version before Raspbian-2017-02-16,such as(2017-02-16,2017-01-11, 2016-11-25,2016-09-23,2016-05-27,2016-05-10,2016-03-18,or earlier). And This Driver also tesed on "kali-2.1.2" ,"ubuntu-mate-16.04-beta2" ================================================================================================================ How to Install: 1.)Step1, Install Raspbian official mirror a)Download Raspbian official mirror: https://www.raspberrypi.org/downloads/ b)Use“SDFormatter.exe”to Format your TF Card c)Use“Win32DiskImager.exe” Burning mirror to TF Card 2.) Step2, Clone my repo onto your pi git clone https://github.com/goodtft/LCD-show.git chmod -R 755 LCD-show cd LCD-show/ 3.)Step3, According to your LCD's type, excute: In case of 2.8" LCD sudo ./LCD28-show In case of 3.2" LCD sudo ./LCD32-show In case of 3.5" LCD sudo ./LCD35-show In case of 3.97" LCD sudo ./LCD397-show In case of 4.3" LCD sudo ./LCD43-show In case of 5" LCD sudo ./LCD5-show In case of 7inch(B)-800X480 RPI LCD sudo ./LCD7B-show In case of 7inch(C)-1024X600 RPI LCD sudo ./LCD7C-show If you need to switch back to the traditional HDMI display sudo ./LCD-hdmi Wait a few minutes,the system will restart automaticall , enjoy with your LCD.
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小锋学长生活大爆炸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值