Example: 50-OpenMV-Boards/52-Alif-Boards/50-Board-Control/spi_control.py
import csi
import time
import struct
from machine import Pin, SPI
WIDTH = 128
HEIGHT = 128
cs = Pin("P3", Pin.OUT, value=1)
rs = Pin("P8", Pin.OUT, value=1)
rst = Pin("P7", Pin.OUT, value=1)
spi = SPI(0, baudrate=WIDTH * HEIGHT * 60 * 16, polarity=0, phase=0)
def write_command(cmd, *args):
rs.low()
cs.low()
spi.write(bytes([cmd]))
cs.high()
rs.high()
if args:
cs.low()
spi.write(bytes(args))
cs.high()
def write_image(img):
pixels = struct.unpack("H" * (img.size() // 2), img)
swapped = struct.pack(">" + "H" * len(pixels), *pixels)
cs.low()
spi.write(swapped)
cs.high()
rst.low()
time.sleep_ms(100)
rst.high()
time.sleep_ms(100)
write_command(0xFD, 0x12)
write_command(0xFD, 0xB1)
write_command(0xB2, 0xA4, 0x00, 0x00)
write_command(0xB3, 0xF0)
write_command(0xCA, 0x7F)
write_command(0xA0, 0x74)
write_command(0xA2, 0x00)
write_command(0xB1, 0x32)
write_command(0xBB, 0x1F)
write_command(0xC7, 0x0A)
write_command(0xC1, 0xFF, 0xFF, 0xFF)
write_command(0xB6, 0x01)
write_command(0xAF)
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.VGA)
csi0.window((WIDTH, HEIGHT))
clock = time.clock()
while True:
clock.tick()
img = csi0.snapshot()
write_command(0x5C)
write_image(img)
print(clock.fps())