ST7789驱动器在MicroPython中的应用指南

ST7789驱动器在MicroPython中的应用指南

st7789_mpy Fast pure-C driver for MicroPython that can handle display modules on ST7789 chip st7789_mpy 项目地址: https://gitcode.com/gh_mirrors/st7/st7789_mpy


概览

本文档旨在指导您安装与使用基于ST7789芯片的显示器驱动程序,适用于MicroPython环境。该驱动程序纯C语言编写,支持ESP8266、ESP32以及STM32平台,完美适配240x240和135x240两种分辨率的屏幕。

ST7789显示模块


安装指南

环境准备

首先,请按照MicroPython官方手册配置构建工具。确保您可以独立编译不包含此显示模块的固件。

获取源码

通过Git克隆本驱动程序到本地:

git clone https://github.com/devbis/st7789_mpy.git

编译固件

选择您的MCU类型(ESP8266或ESP32),并进入相应的MicroPython端口目录:

  • 对于ESP8266

    cd micropython/ports/esp8266
    make USER_C_MODULES=../../../st7789_mpy/ all
    
  • 对于ESP32

    cd micropython/ports/esp32
    make USER_C_MODULES=../../../st7789_mpy/ all
    

如果您有其他用户模块,需将编译好的st7789_driver/st7789复制到用户模块目录下。

最后,使用esptool.py上传固件至MCU,遵循MicroPython文档中的指示进行操作。


使用说明

驱动程序已成功测试于ESP32和ESP8266上,需要提供一个machine.SPI对象以及用于重置和数据控制(DC)的引脚。

示例代码

  • ESP8266示例:

    import machine
    import st7789
    spi = machine.SPI(1, baudrate=40000000, polarity=1)
    display = st7789.ST7789(spi, 240, 240, reset=machine.Pin(5, machine.Pin.OUT), dc=machine.Pin(4, machine.Pin.OUT))
    display.init()
    
  • ESP32特例(使用VSPI):

    spi = machine.SPI(2, baudrate=40000000, polarity=1, sck=machine.Pin(18), mosi=machine.Pin(23))
    display = st7789.ST7789(spi, 240, 240, reset=machine.Pin(4, machine.Pin.OUT), dc=machine.Pin(2, machine.Pin.OUT))
    display.init()
    

对于STM32,初始化过程相似,但引脚不同。


API使用文档

该驱动程序仅支持RGB565格式的16位颜色。

  • fill(color): 填充整个屏幕为指定颜色。
  • pixel(x, y, color): 设置指定坐标像素的颜色。
  • line(x0, y0, x1, y1, color): 绘制从(x0, y0)(x1, y1)的直线。
  • hline(x, y, length, color): 绘制水平线。
  • vline(x, y, length, color): 绘制垂直线。
  • rect(x, y, width, height, color): 绘制矩形。
  • fill_rect(x, y, width, height, color): 绘制并填充矩形。
  • blit_buffer(buffer, x, y, width, height): 将字节缓冲区的内容复制到屏幕内存。

此外,定义了预设颜色常量如BLACK, BLUE, RED, 等等。


结语

本驱动提供了高效的图形绘制方法,并且通过对比显示了其在不同硬件上的性能优势。正确遵循上述步骤后,开发者可以轻松地集成ST7789显示屏至他们的MicroPython项目中,享受高效且便捷的显示控制体验。若遇到内存溢出等常见问题,请参考文中提供的解决办法。

st7789_mpy Fast pure-C driver for MicroPython that can handle display modules on ST7789 chip st7789_mpy 项目地址: https://gitcode.com/gh_mirrors/st7/st7789_mpy

猜你喜欢

转载自blog.csdn.net/gitblog_01212/article/details/143050559