mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Update OpenMV module/ide
* Update commands * Minor UI fixes * Set screen size in openmv-fb
This commit is contained in:
parent
2d833b2ab5
commit
f15aaae0f7
@ -14,25 +14,24 @@ pygame.init()
|
|||||||
openmv.init()
|
openmv.init()
|
||||||
|
|
||||||
# init screen
|
# init screen
|
||||||
screen = pygame.display.set_mode((160, 120), pygame.DOUBLEBUF, 32)
|
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
img_size = 160*120*2
|
|
||||||
Clock = pygame.time.Clock()
|
Clock = pygame.time.Clock()
|
||||||
font = pygame.font.SysFont("monospace", 15)
|
font = pygame.font.SysFont("monospace", 15)
|
||||||
while running:
|
while running:
|
||||||
Clock.tick(60)
|
Clock.tick(60)
|
||||||
|
|
||||||
# read framebuffer
|
# read framebuffer
|
||||||
fb = openmv.dump_fb()
|
fb = openmv.fb_dump()
|
||||||
|
|
||||||
if fb == None:
|
if fb == None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# create image from RGB888
|
# create image from RGB888
|
||||||
image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB')
|
image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB')
|
||||||
|
# TODO check if res changed
|
||||||
|
screen = pygame.display.set_mode((fb[0], fb[1]), pygame.DOUBLEBUF, 32)
|
||||||
|
|
||||||
# blit stuff
|
# blit stuff
|
||||||
screen.blit(image, (0, 0))
|
screen.blit(image, (0, 0))
|
||||||
screen.blit(font.render("FPS %.2f"%(Clock.get_fps()), 1, (255, 0, 0)), (0, 0))
|
screen.blit(font.render("FPS %.2f"%(Clock.get_fps()), 1, (255, 0, 0)), (0, 0))
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,8 @@ from os.path import expanduser
|
|||||||
ui_path =os.path.dirname(os.path.realpath(__file__))+"/openmv-ide.glade"
|
ui_path =os.path.dirname(os.path.realpath(__file__))+"/openmv-ide.glade"
|
||||||
config_path = expanduser("~")+"/.openmvide.config"
|
config_path = expanduser("~")+"/.openmvide.config"
|
||||||
|
|
||||||
|
SCALE =1
|
||||||
|
|
||||||
class OMVGtk:
|
class OMVGtk:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
#Set the Glade file
|
#Set the Glade file
|
||||||
@ -102,6 +104,9 @@ class OMVGtk:
|
|||||||
# init openmv
|
# init openmv
|
||||||
openmv.init()
|
openmv.init()
|
||||||
|
|
||||||
|
# interrupt any running code
|
||||||
|
openmv.stop_script()
|
||||||
|
sleep(0.1)
|
||||||
|
|
||||||
def execute_clicked(self, widget):
|
def execute_clicked(self, widget):
|
||||||
buf = self.buffer.get_text(self.buffer.get_start_iter(), self.buffer.get_end_iter())
|
buf = self.buffer.get_text(self.buffer.get_start_iter(), self.buffer.get_end_iter())
|
||||||
@ -133,31 +138,35 @@ class OMVGtk:
|
|||||||
y = int(event.y)
|
y = int(event.y)
|
||||||
self.x2 = int(event.x)
|
self.x2 = int(event.x)
|
||||||
self.y2 = int(event.y)
|
self.y2 = int(event.y)
|
||||||
# if x < self.pixbuf.get_width() and y < self.pixbuf.get_height():
|
if self.pixbuf and x < self.pixbuf.get_width() and y < self.pixbuf.get_height():
|
||||||
# pixel = self.pixbuf.get_pixels_array()[y][x]
|
pixel = self.pixbuf.get_pixels_array()[y][x]
|
||||||
# rgb = "(%d, %d, %d)" %(pixel[0], pixel[1], pixel[2])
|
rgb = "(%d, %d, %d)" %(pixel[0], pixel[1], pixel[2])
|
||||||
# self.statusbar.pop(self.statusbar_ctx)
|
self.statusbar.pop(self.statusbar_ctx)
|
||||||
# self.statusbar.push(self.statusbar_ctx, rgb)
|
self.statusbar.push(self.statusbar_ctx, rgb)
|
||||||
|
|
||||||
def update_drawing(self):
|
def update_drawing(self):
|
||||||
# read drawingarea
|
# read drawingarea
|
||||||
fb = openmv.dump_fb()
|
fb = openmv.fb_dump()
|
||||||
|
|
||||||
|
# reschedule callback
|
||||||
|
gobject.idle_add(omvgtk.update_drawing);
|
||||||
|
|
||||||
|
if fb == None:
|
||||||
|
return
|
||||||
|
|
||||||
# convert to RGB888 and blit
|
# convert to RGB888 and blit
|
||||||
self.pixbuf = gtk.gdk.pixbuf_new_from_array(fb[2].reshape((fb[1], fb[0], 3)), gtk.gdk.COLORSPACE_RGB, 8)
|
self.pixbuf = gtk.gdk.pixbuf_new_from_array(fb[2].reshape((fb[1], fb[0], 3)), gtk.gdk.COLORSPACE_RGB, 8)
|
||||||
self.pixbuf = self.pixbuf.scale_simple(fb[0]*2, fb[1]*2, gtk.gdk.INTERP_BILINEAR)
|
self.pixbuf = self.pixbuf.scale_simple(fb[0]*SCALE, fb[1]*SCALE, gtk.gdk.INTERP_BILINEAR)
|
||||||
|
|
||||||
self.drawingarea.realize();
|
self.drawingarea.realize();
|
||||||
cm = self.drawingarea.window.get_colormap()
|
cm = self.drawingarea.window.get_colormap()
|
||||||
gc = self.drawingarea.window.new_gc(foreground=cm.alloc_color('#FFFFFF',True,False))
|
gc = self.drawingarea.window.new_gc(foreground=cm.alloc_color('#FFFFFF',True,False))
|
||||||
|
|
||||||
self.drawingarea.set_size_request(fb[0]*2, fb[1]*2)
|
self.drawingarea.set_size_request(fb[0]*SCALE, fb[1]*SCALE)
|
||||||
self.drawingarea.window.draw_pixbuf(gc, self.pixbuf, 0, 0, 0, 0)
|
self.drawingarea.window.draw_pixbuf(gc, self.pixbuf, 0, 0, 0, 0)
|
||||||
if self.selection_started or self.da_menu.flags() & gtk.MAPPED:
|
if self.selection_started or self.da_menu.flags() & gtk.MAPPED:
|
||||||
self.drawingarea.window.draw_rectangle(gc, False, self.x1, self.y1, self.x2-self.x1, self.y2-self.y1)
|
self.drawingarea.window.draw_rectangle(gc, False, self.x1, self.y1, self.x2-self.x1, self.y2-self.y1)
|
||||||
|
|
||||||
# reschedule callback
|
|
||||||
gobject.idle_add(omvgtk.update_drawing);
|
|
||||||
|
|
||||||
|
|
||||||
def on_ctrl_scale_value_changed(self, adjust):
|
def on_ctrl_scale_value_changed(self, adjust):
|
||||||
@ -197,7 +206,7 @@ class OMVGtk:
|
|||||||
template.set_from_pixbuf(self.pixbuf.subpixbuf(x, y, w, h))
|
template.set_from_pixbuf(self.pixbuf.subpixbuf(x, y, w, h))
|
||||||
dialog.set_default_response(gtk.RESPONSE_OK)
|
dialog.set_default_response(gtk.RESPONSE_OK)
|
||||||
if dialog.run() == gtk.RESPONSE_OK:
|
if dialog.run() == gtk.RESPONSE_OK:
|
||||||
openmv.save_template(x/2, y/2, w/2, h/2, entry.get_text()) #Use Scale
|
openmv.save_template(x/SCALE, y/SCALE, w/SCALE, h/SCALE, entry.get_text()) #Use Scale
|
||||||
dialog.hide()
|
dialog.hide()
|
||||||
|
|
||||||
def save_file(self, widget):
|
def save_file(self, widget):
|
||||||
|
|||||||
@ -4,6 +4,9 @@ import usb.core
|
|||||||
import usb.util
|
import usb.util
|
||||||
import struct
|
import struct
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
import time
|
||||||
|
import StringIO
|
||||||
|
|
||||||
__dev = None
|
__dev = None
|
||||||
# VID/PID
|
# VID/PID
|
||||||
@ -11,23 +14,23 @@ __VID=0xf055
|
|||||||
__PID=0x9800
|
__PID=0x9800
|
||||||
|
|
||||||
# Debug __INTERFACE
|
# Debug __INTERFACE
|
||||||
__INTERFACE = 0
|
__INTERFACE = 0
|
||||||
__ALTSETTING= 1
|
__ALTSETTING = 1
|
||||||
__IN_EP =0x81
|
__IN_EP =0x81
|
||||||
__OUT_EP =0x01
|
__OUT_EP =0x01
|
||||||
__TIMEOUT =5000
|
__TIMEOUT =1000
|
||||||
|
__FB_HDR_SIZE =12
|
||||||
|
|
||||||
# USB Debug commands
|
# USB Debug commands
|
||||||
__USBDBG_FB_SIZE=1
|
__USBDBG_FRAME_SIZE=1
|
||||||
__USBDBG_DUMP_FB=2
|
__USBDBG_FRAME_DUMP=2
|
||||||
__USBDBG_EXEC_SCRIPT=3
|
__USBDBG_FRAME_READY=3
|
||||||
__USBDBG_READ_SCRIPT=4
|
__USBDBG_SCRIPT_EXEC=4
|
||||||
__USBDBG_WRITE_SCRIPT=5
|
__USBDBG_SCRIPT_STOP=5
|
||||||
__USBDBG_STOP_SCRIPT=6
|
__USBDBG_SCRIPT_SAVE=6
|
||||||
__USBDBG_SAVE_TEMPLATE=7
|
__USBDBG_TEMPLATE_SAVE=7
|
||||||
__USBDBG_SET_ATTR=8
|
__USBDBG_ATTR_READ=8
|
||||||
__USBDBG_GET_ATTR=9
|
__USBDBG_ATTR_WRITE=9
|
||||||
|
|
||||||
ATTR_CONTRAST=0
|
ATTR_CONTRAST=0
|
||||||
ATTR_BRIGHTNESS=1
|
ATTR_BRIGHTNESS=1
|
||||||
@ -75,36 +78,60 @@ def fb_to_arr(buff, bpp):
|
|||||||
def fb_size():
|
def fb_size():
|
||||||
global __dev
|
global __dev
|
||||||
# read fb header
|
# read fb header
|
||||||
__dev.ctrl_transfer(0xC1, __USBDBG_FB_SIZE, 12, __INTERFACE, 0, __TIMEOUT)
|
buf = __dev.ctrl_transfer(0xC1, __USBDBG_FRAME_SIZE, 0, __INTERFACE, __FB_HDR_SIZE, __TIMEOUT)
|
||||||
size = struct.unpack("III", __dev.read(__IN_EP, 12, __INTERFACE, __TIMEOUT)[0:12])
|
size = struct.unpack("III", buf)
|
||||||
return size
|
return size
|
||||||
|
|
||||||
def dump_fb():
|
def fb_ready():
|
||||||
global __dev
|
global __dev
|
||||||
|
buf = __dev.ctrl_transfer(0xC1, __USBDBG_FRAME_READY, 0, __INTERFACE, 1, __TIMEOUT)
|
||||||
|
return struct.unpack("B", buf)[0]
|
||||||
|
|
||||||
|
def fb_dump():
|
||||||
|
global __dev
|
||||||
|
|
||||||
|
if (fb_ready() == 0):
|
||||||
|
return None
|
||||||
|
|
||||||
size = fb_size()
|
size = fb_size()
|
||||||
num_bytes = size[0]*size[1]*size[2]
|
if (size[2] > 2): #JPEG
|
||||||
|
num_bytes = size[2]
|
||||||
|
else:
|
||||||
|
num_bytes = size[0]*size[1]*size[2]
|
||||||
|
|
||||||
# read fb data
|
# read fb data
|
||||||
__dev.ctrl_transfer(0xC1, __USBDBG_DUMP_FB, num_bytes, __INTERFACE, 0, __TIMEOUT)
|
__dev.ctrl_transfer(0xC1, __USBDBG_FRAME_DUMP, num_bytes, __INTERFACE, 0, __TIMEOUT)
|
||||||
buff = __dev.read(__IN_EP, num_bytes, __INTERFACE, __TIMEOUT)
|
buff = __dev.read(__IN_EP, num_bytes, __INTERFACE, __TIMEOUT)
|
||||||
|
|
||||||
return (size[0], size[1], fb_to_arr(buff, size[2]))
|
#print size, num_bytes
|
||||||
|
if (size[2] > 2):
|
||||||
|
try:
|
||||||
|
#img = Image.frombytes("RGB", (size[0], size[1]), buff, "jpeg", "RGB", "")
|
||||||
|
__write_img(buff, "swap.jpeg")
|
||||||
|
img = Image.open("swap.jpeg")
|
||||||
|
buff = np.fromstring(img.tobytes(), dtype=np.uint8)
|
||||||
|
except Exception, e:
|
||||||
|
print "JPEG decode error (%s)"%e
|
||||||
|
sys.exit(0)
|
||||||
|
return (size[0], size[1], buff)
|
||||||
|
else:
|
||||||
|
return (size[0], size[1], fb_to_arr(buff, size[2]))
|
||||||
|
|
||||||
def exec_script(buf):
|
def exec_script(buf):
|
||||||
__dev.ctrl_transfer(0x41, __USBDBG_EXEC_SCRIPT, len(buf), __INTERFACE, None, __TIMEOUT)
|
__dev.ctrl_transfer(0x41, __USBDBG_SCRIPT_EXEC, len(buf), __INTERFACE, None, __TIMEOUT)
|
||||||
__dev.write(__OUT_EP, buf, __INTERFACE, __TIMEOUT)
|
__dev.write(__OUT_EP, buf, __INTERFACE, __TIMEOUT)
|
||||||
|
|
||||||
def stop_script():
|
def stop_script():
|
||||||
__dev.ctrl_transfer(0x41, __USBDBG_STOP_SCRIPT, 0, __INTERFACE, None, __TIMEOUT)
|
__dev.ctrl_transfer(0x41, __USBDBG_SCRIPT_STOP, 0, __INTERFACE, None, __TIMEOUT)
|
||||||
|
|
||||||
def save_template(x, y, w, h, name):
|
def save_template(x, y, w, h, name):
|
||||||
buf = struct.pack("IIII", x, y, w, h)
|
buf = struct.pack("IIII", x, y, w, h)
|
||||||
__dev.ctrl_transfer(0x41, __USBDBG_SAVE_TEMPLATE, len(buf), __INTERFACE, None, __TIMEOUT)
|
__dev.ctrl_transfer(0x41, __USBDBG_TEMPL_SAVE, len(buf), __INTERFACE, None, __TIMEOUT)
|
||||||
__dev.write(__OUT_EP, buf, __INTERFACE, __TIMEOUT)
|
__dev.write(__OUT_EP, buf, __INTERFACE, __TIMEOUT)
|
||||||
|
|
||||||
def set_attr(attr, value):
|
def set_attr(attr, value):
|
||||||
buf = struct.unpack(">H", struct.pack("bb", attr, value))[0]
|
buf = struct.unpack(">H", struct.pack("bb", attr, value))[0]
|
||||||
__dev.ctrl_transfer(0x41, __USBDBG_SET_ATTR, buf, __INTERFACE, None, __TIMEOUT)
|
__dev.ctrl_transfer(0x41, __USBDBG_ATTR_WRITE, buf, __INTERFACE, None, __TIMEOUT)
|
||||||
|
|
||||||
def get_attr(attr):
|
def get_attr(attr):
|
||||||
return 0
|
return 0
|
||||||
@ -117,3 +144,8 @@ if __name__ == '__main__':
|
|||||||
buf = fin.read()
|
buf = fin.read()
|
||||||
init()
|
init()
|
||||||
exec_script(buf)
|
exec_script(buf)
|
||||||
|
|
||||||
|
def __write_img(buff, path):
|
||||||
|
with open(path, "wb") as f:
|
||||||
|
f.write(buff)
|
||||||
|
f.close()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user