usb_vcp.py

# Questo lavoro è concesso in licenza MIT.
# Copyright (c) 2013-2023 OpenMV LLC. Tutti i diritti riservati.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Esempio USB VCP.
# Questo esempio mostra come usare la classe USB VCP per inviare un'immagine al PC su richiesta.
#
# ATTENZIONE:
# Questo script NON deve essere eseguito dall'IDE o dalla riga di comando, deve essere salvato come main.py
# Nota: il seguente script commentato mostra come ricevere l'immagine lato host.
#
# #!/usr/bin/env python2.7
# import sys, serial, struct
# port = '/dev/ttyACM0'
# sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,
#             xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True)
# sp.setDTR(True) # dsrdtr viene ignorato su Windows.
# sp.write("snap")
# sp.flush()
# size = struct.unpack('<L', sp.read(4))[0]
# img = sp.read(size)
# sp.close()
#
# with open("img.jpg", "w") as f:
#     f.write(img)

import csi
import struct
from pyb import USB_VCP

usb = USB_VCP()

csi0 = csi.CSI()
csi0.reset()  # Resetta e inizializza il sensore.
csi0.pixformat(csi.GRAYSCALE)  # Imposta il formato pixel su RGB565 (o GRAYSCALE)
csi0.framesize(csi.QVGA)  # Imposta la dimensione del fotogramma su QVGA (320x240)
csi0.snapshot(time=2000)  # Attendi che le impostazioni abbiano effetto.

while True:
    cmd = usb.recv(4, timeout=5000)
    if cmd == b"snap":
        img = csi0.snapshot().to_jpeg()
        usb.send(struct.pack("<L", img.size()))
        usb.send(img)

results matching ""

    No results matching ""