spi_control.py

# Questo lavoro è concesso in licenza MIT.
# Copyright (c) 2013-2024 OpenMV LLC. Tutti i diritti riservati.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Controllo SPI
#
# Questo esempio mostra come utilizzare il bus SPI per controllare direttamente lo shield LCD senza
# utilizzare il driver LCD integrato. È necessario lo shield LCD per eseguire questo esempio.

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)
# Il bus SPI hardware della OpenMV Cam è sempre il bus SPI 1.

# NOTA: la frequenza di clock SPI non sarà sempre quella richiesta. L'hardware supporta solo
# frequenze pari alla frequenza del bus divisa per un prescaler (che può essere 2, 4, 8, 16, 32, 64, 128 o 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()


# Resetta l'LCD.
rst.low()
time.sleep_ms(100)
rst.high()
time.sleep_ms(100)

write_command(0x11)  # Uscita da sleep
time.sleep_ms(120)

# Controllo di accesso ai dati in memoria
# Scrivere 0xC8 per la modalità BGR.
write_command(0x36, 0xC0)

# Formato pixel dell'interfaccia
write_command(0x3A, 0x05)

csi0 = csi.CSI()
csi0.reset()  # Inizializza il sensore della fotocamera.
csi0.pixformat(csi.RGB565)  # deve essere questo
csi0.framesize((128, 160))  # deve essere questo
csi0.snapshot(time=2000)  # Lasciare che le nuove impostazioni abbiano effetto.

clock = time.clock()  # Traccia gli FPS.

while True:
    clock.tick()  # Traccia i millisecondi trascorsi tra le chiamate a snapshot().
    img = csi0.snapshot()  # Scatta una foto e restituisce l'immagine.

    write_command(0x2C)  # Comando di scrittura immagine...
    write_image(img)

    # Display acceso
    write_command(0x29)

    print(clock.fps())  # Nota: la tua OpenMV Cam funziona a circa metà velocità quando
    # è collegata al computer. Il framerate dovrebbe aumentare una volta scollegata.

results matching ""

    No results matching ""