• 【MicroPython ESP32】1.8“tft ST7735驱动3Dcube图形显示示例


    【MicroPython ESP32】1.8“tft ST7735驱动3Dcube图形显示示例


    • 本实例基于Thonny平台开发
    • 效果演示(Gif录制,帧率上不来,有重影,实际效果没有,很流畅)
      在这里插入图片描述

    本示例借鉴了坛友三十岁开始学编程的大叔的《micropython 旋转立方体》:https://live.csdn.net/v/226877?spm=1001.2014.3001.5501

    驱动屏幕和开发板

    • 驱动屏幕采用的是合宙1.8"tft屏幕

    1.8寸SPI串口模块TFT彩屏,16位彩色,分辨率:128*160,65K色,驱动IC:ST7735;
    在这里插入图片描述

    • 开发板采用的esp32Dev
      在这里插入图片描述

    接线说明

    sck(SCL) =Pin(18)
    mosi(SDA)=Pin(23)
    dc = Pin(21)
    cs = Pin(16)
    rst(RES)= Pin(22)
    BL--不接
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    MicroPython固件

    • 采用的是带中文字库的固件,固件资源下载地址:https://github.com/kaixindelele/ssd1306-MicroPython-ESP32-Chinese/

    根据个人开发板情况选择对应的固件,虽然本示例未使用字体,但是需要使用此固件才能运行,不然运行会报错。

    在这里插入图片描述

    ST7735驱动模块

    • st7735.py代码(保存到MicroPython设备上)
    from time import sleep_ms
    from ustruct import pack
    from machine import SPI,Pin
    from micropython import const
    import framebuf
    
    #   ST7735V registers definitions
    
    NOP     = const(0x00) # No Operation
    SWRESET = const(0x01) # Software reset
    
    SLPIN   = const(0x10) # Sleep in & booster off
    SLPOUT  = const(0x11) # Sleep out & booster on
    PTLON   = const(0x12) # Partial mode on
    NORON   = const(0x13) # Partial off (Normal)
    
    INVOFF  = const(0x20) # Display inversion off
    INVON   = const(0x21) # Display inversion on
    DISPOFF = const(0x28) # Display off
    DISPON  = const(0x29) # Display on
    CASET   = const(0x2A) # Column address set
    RASET   = const(0x2B) # Row address set
    RAMWR   = const(0x2C) # Memory write
    RGBSET  = const(0x2D) # Display LUT set
    
    PTLAR   = const(0x30) # Partial start/end address set
    COLMOD  = const(0x3A) # Interface pixel format
    MADCTL  = const(0x36) # Memory data access control
    
    # panel function commands
    FRMCTR1 = const(0xB1) # In normal mode (Full colors)
    FRMCTR2 = const(0xB2) # In Idle mode (8-colors)
    FRMCTR3 = const(0xB3) # In partial mode + Full colors
    INVCTR  = const(0xB4) # Display inversion control
    
    PWCTR1  = const(0xC0) # Power control settings
    PWCTR2  = const(0xC1) # Power control settings
    PWCTR3  = const(0xC2) # In normal mode (Full colors)
    PWCTR4  = const(0xC3) # In Idle mode (8-colors)
    PWCTR5  = const(0xC4) # In partial mode + Full colors
    VMCTR1  = const(0xC5) # VCOM control
    
    GMCTRP1 = const(0xE0)
    GMCTRN1 = const(0xE1)
    
    
    
    class ST7735(framebuf.FrameBuffer):
        def __init__(self, width, height, spi, dc, rst, cs, rot=0, bgr=0):
            if dc is None:
                raise RuntimeError('TFT must be initialized with a dc pin number')
            dc.init(dc.OUT, value=0)
            if cs is None:
                raise RuntimeError('TFT must be initialized with a cs pin number')
            cs.init(cs.OUT, value=1)
            if rst is not None:
                rst.init(rst.OUT, value=1)
            else:
                self.rst =None
            self.spi = spi
            self.rot = rot
            self.dc = dc
            self.rst = rst
            self.cs = cs
            self.height = height
            self.width = width
            self.buffer = bytearray(self.height * self.width*2)
            super().__init__(self.buffer, self.width, self.height, framebuf.RGB565SW, self.width)
            if (self.rot ==0):
                madctl=0x00
            elif (self.rot ==1):
                madctl=0xa0
            elif (self.rot ==2):
                madctl=0xc0
            else :
                madctl=0x60
            if bgr==0:
                madctl|=0x08
            self.madctl = pack('>B', madctl)
            self.reset()
    
            self._write(SLPOUT)
            sleep_ms(120)
            for command, data in (
                (COLMOD,  b"\x05"),
                (MADCTL,  pack('>B', madctl)),
                ):
                self._write(command, data)
            if self.width==80 or self.height==80:
                self._write(INVON, None)
            else:
                self._write(INVOFF, None)
            buf=bytearray(128)
            for i in range(32):
                buf[i]=i*2
                buf[i+96]=i*2
            for i in range(64):
                buf[i+32]=i
            self._write(RGBSET, buf)
            #self._write(NORON)
            #sleep_ms(10)
            self.show()
            self._write(DISPON)
            #sleep_ms(100)
        def reset(self):
            if self.rst is None:
                self._write(SWRESET)
                sleep_ms(50)
                return
            self.rst.off()
            sleep_ms(50)
            self.rst.on()
            sleep_ms(50)
        def _write(self, command, data = None):
            self.cs.off()
            self.dc.off()
            self.spi.write(bytearray([command]))
            self.cs.on()
            if data is not None:
                self.cs.off()
                self.dc.on()
                self.spi.write(data)
                self.cs.on()
        def show(self):
            if self.width==80 or self.height==80:
                if self.rot==0 or self.rot==2:
                    self._write(CASET,pack(">HH", 26, self.width+26-1))
                    self._write(RASET,pack(">HH", 1, self.height+1-1))
                else:
                    self._write(CASET,pack(">HH", 1, self.width+1-1))
                    self._write(RASET,pack(">HH", 26, self.height+26-1))
            else:
                if self.rot==0 or self.rot==2:
                    self._write(CASET,pack(">HH", 0, self.width-1))
                    self._write(RASET,pack(">HH", 0, self.height-1))
                else:
                    self._write(CASET,pack(">HH", 0, self.width-1))
                    self._write(RASET,pack(">HH", 0, self.height-1))
                
            self._write(RAMWR,self.buffer)
        def rgb(self,r,g,b):
            return ((r&0xf8)<<8)|((g&0xfc)<<3)|((b&0xf8)>>3)
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    文件架构

    • 运行代码前先上传st7735.py模块到Micropython设备上。
      在这里插入图片描述

    实例代码

    from math import cos,sin,pi
    from st7735 import ST7735
    from machine import Pin,SPI
    
    
    cube=[[-35,-35,-35],[-35,35,-35],[35,35,-35],[35,-35,-35],[-35,-35,35],[-35,35,35],[35,35,35],[35,-35,35]]
    lineid=[1,2,2,3,3,4,4,1,5,6,6,7,7,8,8,5,8,4,7,3,6,2,5,1]
    
    # 初始化SPI
    spi=SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23))
    
    # 初始化LCD  rot 是显示方向,bgr是默认显示的颜色
    lcd= ST7735 (128, 160, spi,dc=Pin(21),cs=Pin(16),rst=Pin(22),rot=2,bgr=0)
    
    def matconv(a,matrix):
        res=[0,0,0]
        for i in range(0,3):
            res[i]=matrix[i][0]*a[0]+matrix[i][1]*a[1]+matrix[i][2]*a[2]
        for i in range(0,3):
            a[i]=res[i]
        return a
    
    def rotate(obj,x,y,z):
        x=x/pi
        y=y/pi
        z=z/pi
        rz=[[cos(z),-sin(z),0],[sin(z),cos(z),0],[0,0,1]]
        ry=[[1,0,0],[0,cos(y),-sin(y)],[0,sin(y),cos(y)]]
        rx=[[cos(x),0,sin(x)],[0,1,0],[-sin(x),0,cos(x)]]
        matconv(matconv(matconv(obj,rz),ry),rx)
        
    def drawcube(x,y,z):
        lcd.fill(0x9527)# 背景颜色
        for i in range(0,8):
            rotate(cube[i],x,y,z)
        for i in range(0,24,2):
            x1=int(64+cube[lineid[i]-1][0])
            y1=int(72+cube[lineid[i]-1][1])
            x2=int(64+cube[lineid[i+1]-1][0])
            y2=int(72+cube[lineid[i+1]-1][1])
            lcd.line(x1,y1,x2,y2,0xf88f)
            #print(64+cube[lineid[i]-1][0],32+cube[lineid[i]-1][1],64+cube[lineid[i+1]-1][0],32+cube[lineid[i+1]-1][1])
        lcd.show()
    while True:
        drawcube(0.1,0.2,0.3)
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    lxcfs 源码安装(RHEL)
    【目标检测】45、YOLOv3 | 针对小目标效果提升的 YOLO 网络
    24个 JavaScript 循环遍历方法,你都知道吗?
    一文带你了解ServletContext
    HTTP - HTTP 1.0、HTTP 1.1、HTTP 2.0的区别
    Redis群集
    Ant-design中表单多级对象做嵌套表单校验
    Collection集合的常见使用(Java)
    MySQL面试题大全(陆续更新)
    Java面试记录
  • 原文地址:https://blog.csdn.net/weixin_42880082/article/details/126614083