Example: 50-OpenMV-Boards/53-N6-Boards/50-Board-Control/spi_control.py
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)
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):
pixels = struct.unpack("H" * (img.size() // 2), img)
swapped = struct.pack(">" + "H" * len(pixels), *pixels)
cs.low()
rs.high()
spi.write(swapped)
cs.high()
rst.low()
time.sleep_ms(100)
rst.high()
time.sleep_ms(100)
write_command(0x11)
time.sleep_ms(120)
write_command(0x36, 0xC0)
write_command(0x3A, 0x05)
write_command(0x29)
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)
csi0.snapshot(time=2000)
ROI = ((320 - 160) // 2, 0, 160, 200)
SCALE = WIDTH / ROI[2]
clock = time.clock()
while True:
clock.tick()
img = csi0.snapshot()
img = img.scale(roi=ROI, x_scale=SCALE, y_scale=SCALE)
write_command(0x2C)
write_image(img)
print(clock.fps())