Update OpenMV module/ide

* Update commands
* Minor UI fixes
* Set screen size in openmv-fb
This commit is contained in:
iabdalkader 2014-06-25 19:52:08 +02:00
parent 2d833b2ab5
commit f15aaae0f7
3 changed files with 81 additions and 41 deletions

View File

@ -14,25 +14,24 @@ pygame.init()
openmv.init()
# init screen
screen = pygame.display.set_mode((160, 120), pygame.DOUBLEBUF, 32)
running = True
img_size = 160*120*2
Clock = pygame.time.Clock()
font = pygame.font.SysFont("monospace", 15)
while running:
Clock.tick(60)
# read framebuffer
fb = openmv.dump_fb()
fb = openmv.fb_dump()
if fb == None:
continue
# create image from RGB888
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(font.render("FPS %.2f"%(Clock.get_fps()), 1, (255, 0, 0)), (0, 0))

View File

@ -16,6 +16,8 @@ from os.path import expanduser
ui_path =os.path.dirname(os.path.realpath(__file__))+"/openmv-ide.glade"
config_path = expanduser("~")+"/.openmvide.config"
SCALE =1
class OMVGtk:
def __init__(self):
#Set the Glade file
@ -102,6 +104,9 @@ class OMVGtk:
# init openmv
openmv.init()
# interrupt any running code
openmv.stop_script()
sleep(0.1)
def execute_clicked(self, widget):
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)
self.x2 = int(event.x)
self.y2 = int(event.y)
# if x < self.pixbuf.get_width() and y < self.pixbuf.get_height():
# pixel = self.pixbuf.get_pixels_array()[y][x]
# rgb = "(%d, %d, %d)" %(pixel[0], pixel[1], pixel[2])
# self.statusbar.pop(self.statusbar_ctx)
# self.statusbar.push(self.statusbar_ctx, rgb)
if self.pixbuf and x < self.pixbuf.get_width() and y < self.pixbuf.get_height():
pixel = self.pixbuf.get_pixels_array()[y][x]
rgb = "(%d, %d, %d)" %(pixel[0], pixel[1], pixel[2])
self.statusbar.pop(self.statusbar_ctx)
self.statusbar.push(self.statusbar_ctx, rgb)
def update_drawing(self):
# 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
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();
cm = self.drawingarea.window.get_colormap()
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)
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)
# reschedule callback
gobject.idle_add(omvgtk.update_drawing);
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))
dialog.set_default_response(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()
def save_file(self, widget):

View File

@ -4,6 +4,9 @@ import usb.core
import usb.util
import struct
import numpy as np
from PIL import Image
import time
import StringIO
__dev = None
# VID/PID
@ -11,23 +14,23 @@ __VID=0xf055
__PID=0x9800
# Debug __INTERFACE
__INTERFACE = 0
__ALTSETTING= 1
__IN_EP =0x81
__OUT_EP =0x01
__TIMEOUT =5000
__INTERFACE = 0
__ALTSETTING = 1
__IN_EP =0x81
__OUT_EP =0x01
__TIMEOUT =1000
__FB_HDR_SIZE =12
# USB Debug commands
__USBDBG_FB_SIZE=1
__USBDBG_DUMP_FB=2
__USBDBG_EXEC_SCRIPT=3
__USBDBG_READ_SCRIPT=4
__USBDBG_WRITE_SCRIPT=5
__USBDBG_STOP_SCRIPT=6
__USBDBG_SAVE_TEMPLATE=7
__USBDBG_SET_ATTR=8
__USBDBG_GET_ATTR=9
__USBDBG_FRAME_SIZE=1
__USBDBG_FRAME_DUMP=2
__USBDBG_FRAME_READY=3
__USBDBG_SCRIPT_EXEC=4
__USBDBG_SCRIPT_STOP=5
__USBDBG_SCRIPT_SAVE=6
__USBDBG_TEMPLATE_SAVE=7
__USBDBG_ATTR_READ=8
__USBDBG_ATTR_WRITE=9
ATTR_CONTRAST=0
ATTR_BRIGHTNESS=1
@ -75,36 +78,60 @@ def fb_to_arr(buff, bpp):
def fb_size():
global __dev
# read fb header
__dev.ctrl_transfer(0xC1, __USBDBG_FB_SIZE, 12, __INTERFACE, 0, __TIMEOUT)
size = struct.unpack("III", __dev.read(__IN_EP, 12, __INTERFACE, __TIMEOUT)[0:12])
buf = __dev.ctrl_transfer(0xC1, __USBDBG_FRAME_SIZE, 0, __INTERFACE, __FB_HDR_SIZE, __TIMEOUT)
size = struct.unpack("III", buf)
return size
def dump_fb():
def fb_ready():
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()
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
__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)
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):
__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)
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):
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)
def set_attr(attr, value):
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):
return 0
@ -117,3 +144,8 @@ if __name__ == '__main__':
buf = fin.read()
init()
exec_script(buf)
def __write_img(buff, path):
with open(path, "wb") as f:
f.write(buff)
f.close()