spi_control.py

# この作品はMITライセンスの下で提供されています。
# Copyright (c) 2013-2024 OpenMV LLC. 全著作権所有。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# SPI制御
#
# この例は、SPIバスを使用してLCDシールドを直接制御する方法を示します。
# 組み込みのLCDドライバは使用しません。この例を実行するにはLCDシールドが必要です。

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

cs = Pin("P3", Pin.OUT)
rst = Pin("P7", Pin.OUT)
rs = Pin("P8", Pin.OUT)
# OpenMV CamのハードウェアSPIバスは常にSPIバス1です。

# 注意: SPIクロック周波数は必ずしも指定した周波数にはなりません。ハードウェアは
# バス周波数をプリスケーラ(2, 4, 8, 16, 32, 64, 128, 256のいずれか)で割った周波数のみをサポートします。
spi = SPI(1, baudrate=int(1000000000 / 66), 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):
    cs.low()
    rs.high()
    reversed_img = struct.unpack('H' * (img.size() // 2), img)
    reversed_array = struct.pack('>' + 'H' * len(reversed_img), *reversed_img)
    spi.write(reversed_array)
    cs.high()


# LCDをリセットします。
rst.low()
time.sleep_ms(100)
rst.high()
time.sleep_ms(100)

write_command(0x11)  # スリープ解除
time.sleep_ms(120)

# メモリデータアクセス制御
# BGRモードにするには0xC8を書き込みます。
write_command(0x36, 0xC0)

# インターフェースピクセルフォーマット
write_command(0x3A, 0x05)

csi0 = csi.CSI()
csi0.reset()  # カメラセンサーを初期化します。
csi0.pixformat(csi.RGB565)  # これでなければなりません
csi0.framesize((128, 160))  # これでなければなりません
csi0.snapshot(time=2000)  # 新しい設定を反映させます。

clock = time.clock()  # FPSを計測します。

while True:
    clock.tick()  # snapshot()呼び出し間の経過ミリ秒を計測します。
    img = csi0.snapshot()  # 画像を撮影して返します。

    write_command(0x2C)  # 画像書き込みコマンド...
    write_image(img)

    # ディスプレイオン
    write_command(0x29)

    print(clock.fps())  # 注: OpenMV Camは接続時、速度が約半分になります
    # コンピュータに接続されている間。切断するとFPSは向上します。

results matching ""

    No results matching ""