usb_vcp.py
# この作品はMITライセンスの下で提供されています。
# Copyright (c) 2013-2023 OpenMV LLC. 全著作権所有。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# USB VCPの例。
# この例では、USB VCPクラスを使用して、要求に応じてPCへ画像を送信する方法を示します。
#
# 警告:
# このスクリプトはIDEやコマンドラインから実行してはいけません。main.pyとして保存してください
# 以下のコメントアウトされたスクリプトは、ホスト側で画像を受信する方法を示しています。
#
# #!/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) # Windowsではdsrdtrは無視されます。
# 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() # センサーをリセットして初期化します。
csi0.pixformat(csi.RGB565) # ピクセルフォーマットをRGB565(またはGRAYSCALE)に設定します
csi0.framesize(csi.QVGA) # フレームサイズをQVGA(320x240)に設定します
csi0.snapshot(time=2000) # 設定が反映されるまで待ちます。
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)