openMV驱动自己的LCD屏

用openMV点亮自己的屏幕

openMV官方配套的屏幕实在贵得一批,一块SPI屏要上百块,自己买才二、三十。都是SPI屏,为啥不能用自己的屏幕代替配套的呢

# myLcd.py
# TFT 驱动IC ILI9341
# 最大分辨率 240*320 QVGA 2.2寸


import time
from pyb import Pin, SPI

# IO接线方法
# 屏         openMV
# SDO/MISO  ---> P1(MISO)
# LED       ---> P6 (背光控制)
# SCK       ---> P2(SCLK)
# SDI/MOSI  ---> P0(MOSI)
# DO/RS     ---> P5
# RESET     ---> P4
# CS        ---> P3
# GND       ---> GND
# VCC       ---> 3.3V

cs  = Pin("P3", Pin.OUT_OD)
rst = Pin("P4", Pin.OUT_PP)
rs  = Pin("P5", Pin.OUT_PP)
bl  = Pin("P6", Pin.OUT_PP)     # 背光控制

# 定义横屏/竖屏
USE_HORIZONTAL  = True         # 定义横屏
IMAGE_INVER     = False         # 旋转180°

# TFT resolution 240*320
X_MIN_PIXEL = 0
Y_MIN_PIXEL = 0
if USE_HORIZONTAL:
    X_MAX_PIXEL = 320               # 定义屏幕宽度
    Y_MAX_PIXEL = 240               # 定义屏幕高度
else:
    X_MAX_PIXEL = 240               # 定义屏幕宽度
    Y_MAX_PIXEL = 320               # 定义屏幕高度

# 常用颜色表
RED     = 0XF800
GREEN   = 0X07E0
BLUE    = 0X001F
BLACK   = 0X0000
YELLOW  = 0XFFE0
WHITE   = 0XFFFF

CYAN    = 0X07FF
BRIGHT_RED = 0XF810
GRAY1   = 0X8410
GRAY2   = 0X4208

# OpenMV 仅留出 SPI2 总线
spi = SPI(2, SPI.MASTER, baudrate=int(10000000000/66), polarity=0, phase=0)

# SPI 写命令
def write_command_byte(c):
    cs.low()
    rs.low()
    spi.send(c)
    cs.high()

# SPI 写数据
def write_data_byte(c):
    cs.low()
    rs.high()
    spi.send(c)
    cs.high()

def write_command(c, *data):
    write_command_byte(c)
    if data:
        for d in data:
            if d > 255:
                write_data_byte(d >> 8)
                write_data_byte(d&0xFF)
            else:
                write_data_byte(d)

def write_image(img):
    cs.low()
    rs.high()
    spi.send(img)
    cs.high()


def SetXY(xpos, ypos):
    write_command(0x2A, xpos>>8, xpos&0XFF)
    write_command(0x2B, ypos>>8, ypos&0XFF)
    write_command(0x2C)

def SetRegion(xStar, yStar, xEnd, yEnd):
    write_command(0x2A, xStar>>8, xStar&0XFF, xEnd>>8, xEnd&0XFF)
    write_command(0x2B, yStar>>8, yStar&0XFF, yEnd>>8, yEnd&0XFF)
    write_command(0x2C)

# 在指定位置绘制一个点
def DrawPoint(x, y, Color):
    SetXY(x, y)
    write_data_byte(Color >> 8)
    write_data_byte(Color&0XFF)

def ReadPoint(x, y):
    data = 0
    SetXY(x, y)
    write_data_byte(data)
    return data

def Clear(Color):
    global X_MAX_PIXEL, Y_MAX_PIXEL
    SetRegion(0, 0, X_MAX_PIXEL - 1, Y_MAX_PIXEL - 1)

    # rs.high()
    for i in range(0, Y_MAX_PIXEL):
        for m in range(0, X_MAX_PIXEL):
            write_data_byte(Color >> 8)
            write_data_byte(Color&0xFF)

def init():
    rst.low()
    time.sleep(100)
    rst.high()
    time.sleep(100)

    write_command(0xCB, 0x39, 0x2c, 0x00, 0x34, 0x02)
    write_command(0xCF, 0x00, 0XAA, 0XE0)
    write_command(0xE8, 0x85, 0x11, 0x78)
    write_command(0xEA, 0x00, 0x00)
    write_command(0xED, 0x67, 0x03, 0X12, 0X81)
    write_command(0xF7, 0x20)
    write_command(0xC0, 0x21)       # Power control, VRH[5:0]
    write_command(0xC1, 0x11)       # Power control, SAP[2:0];BT[3:0]
    write_command(0xC5, 0x24, 0x3C) # VCM control, 对比度调节
    write_command(0xC7, 0xB7)       # VCM control2, --

    # Memory Data Access Control
    if USE_HORIZONTAL:      # //C8   //48 68竖屏//28 E8 横屏
        if IMAGE_INVER:
            write_command(0x36, 0xE8)   # 从右到左 e8/68
        else:
            write_command(0x36, 0x28)   # 从左到右 28
    else:
        if IMAGE_INVER:
            write_command(0x36, 0xC8)   # 从下到上刷
        else:
            write_command(0x36, 0x48)   # 从上到下刷

    global X_MAX_PIXEL, Y_MAX_PIXEL
    SetRegion(0, 0, X_MAX_PIXEL - 1, Y_MAX_PIXEL - 1)

    # Interface Pixel Format
    write_command(0x3A, 0x55)

    write_command(0xB1, 0x00, 0x18)
    write_command(0xB6, 0x08, 0x82, 0x27)   # Display Function Control
    write_command(0xF2, 0x00)               # 3Gamma Function Disable
    write_command(0x26, 0x01)               # Gamma curve selected
    write_command(0xB7, 0x06)

    # set gamma
    write_command(0xE0, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00)
    write_command(0XE1, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F)

    write_command(0x11) # Sleep Exit
    time.sleep(120)

    # Display On
    write_command(0x29)
    write_command(0x2C)
    bl.high()   # 拉背光

def display(image):
    write_command(0x2C)
    write_image(image)


if __name__ == "__main__":
    import sensor, image
    # import new_lcd as lcd

    sensor.reset() # Initialize the camera sensor.
    sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
    sensor.set_framesize(sensor.QVGA) # Special 128x160 framesize for LCD Shield.
    init() # Initialize the lcd screen.
    #Clear(BLUE)
    #DrawPoint(50, 50, RED)

    while(True):
        #pass
        display(sensor.snapshot()) # Take a picture and display the image.


保存完运行,得到如下结果,屏幕驱动完成!
openMV驱动自己的LCD屏_第1张图片

你可能感兴趣的:(openMV,python)