usb_vcp.py
# هذا العمل مرخص بموجب ترخيص MIT.
# حقوق النشر (c) 2013-2023 OpenMV LLC. جميع الحقوق محفوظة.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# مثال USB VCP.
# يوضح هذا المثال كيفية استخدام صنف USB VCP لإرسال صورة إلى الحاسوب عند الطلب.
#
# تحذير:
# يجب عدم تشغيل هذا السكربت من الـ 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) # يتم تجاهل dsrdtr على نظام 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() # إعادة ضبط المستشعر وتهيئته.
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)