Example: 50-OpenMV-Boards/53-N6-Boards/50-Board-Control/spi_control.py

# 本作品采用MIT许可证授权。
# Copyright (c) 2013-2026 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# SPI 控制
#
# 此示例展示了如何在您的OpenMV Cam上使用SPI总线直接
# drive the LCD shield without using the built-in lcd shield driver. You
# will need the LCD shield to run this example.

import csi
import time
import struct
from machine import Pin, SPI

WIDTH = 128
HEIGHT = 160

cs = Pin("P3", Pin.OUT)
rst = Pin("P7", Pin.OUT)
rs = Pin("P8", Pin.OUT)
# The hardware SPI bus for your OpenMV N6 LCD Shield is SPI bus 2.

spi = SPI(2, baudrate=15000000, polarity=0, phase=0)


def write_command_byte(c):
    cs.low()
    rs.low()
    spi.write(bytes([c]))
    cs.high()


def write_data_byte(c):
    cs.low()
    rs.high()
    spi.write(bytes([c]))
    cs.high()


def write_command(c, *data):
    write_command_byte(c)
    if data:
        for d in data:
            write_data_byte(d)


def write_image(img):
    # The LCD controller expects RGB565 with the high byte first while the
    # OpenMV image stores RGB565 as little-endian uint16, so byte-swap before
    # sending the pixels.
    pixels = struct.unpack("H" * (img.size() // 2), img)
    swapped = struct.pack(">" + "H" * len(pixels), *pixels)
    cs.low()
    rs.high()
    spi.write(swapped)
    cs.high()


# 重置LCD。
rst.low()
time.sleep_ms(100)
rst.high()
time.sleep_ms(100)

write_command(0x11)  # 睡眠退出
time.sleep_ms(120)

# 内存数据访问控制
# 写入0xC8以启用BGR模式。
write_command(0x36, 0xC0)

# 接口像素格式
write_command(0x3A, 0x05)

# 显示开启
write_command(0x29)

csi0 = csi.CSI()
csi0.reset()  # 初始化相机传感器。
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)  # 320x200 on the OpenMV N6.
csi0.snapshot(time=2000)  # 让新设置生效。

# Crop a 4:5 region (matching the 128x160 LCD aspect ratio) from the center
# of the 320x200 frame, then scale it down to the LCD size.
ROI = ((320 - 160) // 2, 0, 160, 200)
SCALE = WIDTH / ROI[2]

clock = time.clock()  # 跟踪FPS。

while True:
    clock.tick()  # Track elapsed milliseconds between snapshots().
    img = csi0.snapshot()  # 拍照并返回图像。

    # img.scale() crops the source ROI first and then scales it.
    img = img.scale(roi=ROI, x_scale=SCALE, y_scale=SCALE)

    write_command(0x2C)  # 写入图像命令...
    write_image(img)

    print(clock.fps())  # 注意:您的 OpenMV Cam 在连接到
    # 计算机时运行速度会减半。断开连接后,FPS 应该会增加。

results matching ""

    No results matching ""