Make IDE Handle Disconnect/Connect Gracefully

* Fix issue #10
This commit is contained in:
iabdalkader 2014-09-23 15:40:51 +02:00
parent 3a04f1c74a
commit 392e613d3d
2 changed files with 117 additions and 51 deletions

View File

@ -438,27 +438,14 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child> <child>
<object class="GtkToolButton" id="toolbutton1"> <object class="GtkToolButton" id="connect_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="use_action_appearance">False</property> <property name="use_action_appearance">False</property>
<property name="label" translatable="yes">toolbutton1</property> <property name="label" translatable="yes">toolbutton1</property>
<property name="use_underline">True</property> <property name="use_underline">True</property>
<property name="stock_id">gtk-connect</property> <property name="stock_id">gtk-connect</property>
</object> <signal name="clicked" handler="on_connect_clicked" swapped="no"/>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">toolbutton2</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-disconnect</property>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
@ -477,7 +464,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkToolButton" id="toolbutton3"> <object class="GtkToolButton" id="exec_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Execute Script</property> <property name="tooltip_text" translatable="yes">Execute Script</property>
@ -493,7 +480,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkToolButton" id="toolbutton4"> <object class="GtkToolButton" id="stop_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Stop Script</property> <property name="tooltip_text" translatable="yes">Stop Script</property>
@ -562,7 +549,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkToolButton" id="toolbutton5"> <object class="GtkToolButton" id="zoomout_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="use_action_appearance">False</property> <property name="use_action_appearance">False</property>
@ -577,7 +564,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkToolButton" id="toolbutton8"> <object class="GtkToolButton" id="bestfit_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="use_action_appearance">False</property> <property name="use_action_appearance">False</property>
@ -592,7 +579,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkToolButton" id="toolbutton7"> <object class="GtkToolButton" id="zoomin_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="use_action_appearance">False</property> <property name="use_action_appearance">False</property>
@ -607,7 +594,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkToolButton" id="toolbutton9"> <object class="GtkToolButton" id="refresh_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="use_action_appearance">False</property> <property name="use_action_appearance">False</property>

View File

@ -47,6 +47,11 @@ class OMVGtk:
self.buffer.set_language(lang_manager.get_language("python")) self.buffer.set_language(lang_manager.get_language("python"))
self.buffer.connect("changed", self.text_changed) self.buffer.connect("changed", self.text_changed)
#configure the terminal
self.fd = -1
self.terminal = self.builder.get_object('terminal')
self.terminal.set_size(80,24)
# open last opened file # open last opened file
if os.path.isfile(config_path): if os.path.isfile(config_path):
with open(config_path, "r") as file: with open(config_path, "r") as file:
@ -60,12 +65,6 @@ class OMVGtk:
sourceview.set_buffer(self.buffer) sourceview.set_buffer(self.buffer)
# open VCP and configure the terminal
self.terminal = self.builder.get_object('terminal')
self.fd = os.open("/dev/ttyACM0", os.O_RDWR)
self.terminal.set_size(80,24)
self.terminal.set_pty(self.fd)
# get drawingarea # get drawingarea
self.pixbuf = None self.pixbuf = None
self.drawingarea = self.builder.get_object("drawingarea") self.drawingarea = self.builder.get_object("drawingarea")
@ -88,6 +87,7 @@ class OMVGtk:
#connect signals #connect signals
signals = { signals = {
"on_top_window_destroy" : self.quit, "on_top_window_destroy" : self.quit,
"on_connect_clicked" : self.connect_clicked,
"on_execute_clicked" : self.execute_clicked, "on_execute_clicked" : self.execute_clicked,
"on_stop_clicked" : self.stop_clicked, "on_stop_clicked" : self.stop_clicked,
"on_motion_notify" : self.motion_notify, "on_motion_notify" : self.motion_notify,
@ -106,12 +106,86 @@ class OMVGtk:
} }
self.builder.connect_signals(signals) self.builder.connect_signals(signals)
# init openmv self.connected = False
openmv.init() self.builder.get_object('connect_button').set_sensitive(True)
self.builder.get_object('exec_button').set_sensitive(False)
self.builder.get_object('stop_button').set_sensitive(False)
self.builder.get_object('zoomin_button').set_sensitive(False)
self.builder.get_object('zoomout_button').set_sensitive(False)
self.builder.get_object('bestfit_button').set_sensitive(False)
self.builder.get_object('refresh_button').set_sensitive(False)
# interrupt any running code
openmv.stop_script() def show_message_dialog(self, msg_type, msg):
sleep(0.1) message = gtk.MessageDialog(parent=self.window, flags=gtk.DIALOG_DESTROY_WITH_PARENT,
type=msg_type, buttons=gtk.BUTTONS_OK, message_format=msg)
message.run()
message.destroy()
def connect(self):
self.terminal = self.builder.get_object('terminal')
try:
# open VCP and configure the terminal
self.fd = os.open("/dev/ttyACM0", os.O_RDWR)
self.terminal.reset(True, True)
self.terminal.set_size(80,24)
self.terminal.set_pty(self.fd)
except Exception, e:
self.show_message_dialog(gtk.MESSAGE_ERROR, "Faild to connect to OpenMV\n%s"%e)
return
try:
# init openmv
openmv.init()
# interrupt any running code
openmv.stop_script()
sleep(0.1)
except Exception, e:
self.show_message_dialog(gtk.MESSAGE_ERROR, "Faild to connect to OpenMV\n%s"%e)
return
self.connected = True
self.builder.get_object('connect_button').set_sensitive(False)
self.builder.get_object('exec_button').set_sensitive(True)
self.builder.get_object('stop_button').set_sensitive(True)
self.builder.get_object('zoomin_button').set_sensitive(True)
self.builder.get_object('zoomout_button').set_sensitive(True)
self.builder.get_object('bestfit_button').set_sensitive(True)
self.builder.get_object('refresh_button').set_sensitive(True)
def disconnect(self):
try:
# close VCP
os.close(self.fd)
except OSError:
pass
#reset terminal
self.terminal.set_pty(-1)
self.terminal.reset(True, True)
try:
# stop running code
openmv.stop_script();
except:
pass
# release OpenMV
openmv.release()
self.connected = False
self.builder.get_object('connect_button').set_sensitive(True)
self.builder.get_object('exec_button').set_sensitive(False)
self.builder.get_object('stop_button').set_sensitive(False)
self.builder.get_object('zoomin_button').set_sensitive(False)
self.builder.get_object('zoomout_button').set_sensitive(False)
self.builder.get_object('bestfit_button').set_sensitive(False)
self.builder.get_object('refresh_button').set_sensitive(False)
def connect_clicked(self, widget):
self.connect()
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())
@ -166,21 +240,29 @@ class OMVGtk:
self.statusbar.push(self.statusbar_ctx, rgb) self.statusbar.push(self.statusbar_ctx, rgb)
def update_drawing(self): def update_drawing(self):
# read drawingarea if (not self.connected):
fb = openmv.fb_dump() return True
if fb:
# 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]*SCALE, fb[1]*SCALE, gtk.gdk.INTERP_BILINEAR)
self.drawingarea.realize(); try:
cm = self.drawingarea.window.get_colormap() # read drawingarea
gc = self.drawingarea.window.new_gc(foreground=cm.alloc_color('#FFFFFF',True,False)) fb = openmv.fb_dump()
if fb:
# 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]*SCALE, fb[1]*SCALE, gtk.gdk.INTERP_BILINEAR)
self.drawingarea.set_size_request(fb[0]*SCALE, fb[1]*SCALE) self.drawingarea.realize();
self.drawingarea.window.draw_pixbuf(gc, self.pixbuf, 0, 0, 0, 0) cm = self.drawingarea.window.get_colormap()
if self.selection_started or self.da_menu.flags() & gtk.MAPPED: gc = self.drawingarea.window.new_gc(foreground=cm.alloc_color('#FFFFFF',True,False))
self.drawingarea.window.draw_rectangle(gc, False, self.x1, self.y1, self.x2-self.x1, self.y2-self.y1)
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)
except Exception, e:
self.disconnect()
self.show_message_dialog(gtk.MESSAGE_ERROR, "Faild to update FB\n%s"%e)
return True
return True return True
@ -270,16 +352,13 @@ class OMVGtk:
self.save_button.set_sensitive(True) self.save_button.set_sensitive(True)
def quit(self, widget): def quit(self, widget):
# disconnect
self.disconnect()
# write last opened file # write last opened file
with open(config_path, "w") as file: with open(config_path, "w") as file:
file.write(self.file_path) file.write(self.file_path)
# stop any running code
openmv.stop_script();
# close VCP
os.close(self.fd)
# release OpenMV
openmv.release()
# exit # exit
sys.exit(0) sys.exit(0)