mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Use configparser to store/load configuration
This commit is contained in:
parent
1706cbdb83
commit
a1699f5f3b
@ -12,14 +12,20 @@ import usb.core
|
|||||||
import usb.util
|
import usb.util
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from os.path import expanduser
|
from os.path import expanduser
|
||||||
|
try:
|
||||||
|
# 3.x name
|
||||||
|
import configparser
|
||||||
|
except ImportError:
|
||||||
|
# 2.x name
|
||||||
|
configparser = __import__("ConfigParser")
|
||||||
|
|
||||||
|
IDE_PATH = os.path.dirname(os.path.realpath(__file__))
|
||||||
UI_PATH =os.path.dirname(os.path.realpath(__file__))+"/openmv-ide.glade"
|
GLADE_PATH = IDE_PATH+"/openmv-ide.glade"
|
||||||
CONFIG_PATH = expanduser("~")+"/.openmvide.config"
|
CONFIG_PATH = IDE_PATH+"/openmv.config"
|
||||||
EXAMPLE_PATH = os.path.dirname(os.path.realpath(__file__))+"/examples"
|
EXAMPLE_PATH = IDE_PATH+"/examples"
|
||||||
SCRIPTS_PATH = os.path.dirname(os.path.realpath(__file__))+"/scripts"
|
SCRIPTS_PATH = IDE_PATH+"/scripts"
|
||||||
FWBIN_PATH = "/home/mux/src/c/stm32f4/openmv/src/build/openmv.bin"
|
FWBIN_PATH = "/home/mux/src/c/stm32f4/openmv/src/build/openmv.bin"
|
||||||
DFU_CMD = "dfu-util -d 0483:df11 -c 1 -i 0 -a 0 -s 0x08000000 -D %s"
|
DFU_CMD = "dfu-util -d 0483:df11 -c 1 -i 0 -a 0 -s 0x08000000 -D %s"
|
||||||
|
|
||||||
SCALE =1
|
SCALE =1
|
||||||
flash_offsets= [0x08000000, 0x08004000, 0x08008000, 0x0800C000,
|
flash_offsets= [0x08000000, 0x08004000, 0x08008000, 0x0800C000,
|
||||||
@ -30,7 +36,7 @@ class OMVGtk:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
#Set the Glade file
|
#Set the Glade file
|
||||||
self.builder = gtk.Builder()
|
self.builder = gtk.Builder()
|
||||||
self.builder.add_from_file(UI_PATH)
|
self.builder.add_from_file(GLADE_PATH)
|
||||||
|
|
||||||
# get top window
|
# get top window
|
||||||
self.window = self.builder.get_object("top_window")
|
self.window = self.builder.get_object("top_window")
|
||||||
@ -120,13 +126,18 @@ class OMVGtk:
|
|||||||
}
|
}
|
||||||
self.builder.connect_signals(signals)
|
self.builder.connect_signals(signals)
|
||||||
|
|
||||||
# open last opened file
|
# load config
|
||||||
|
self.config = configparser.ConfigParser()
|
||||||
|
try:
|
||||||
|
self.config.read(CONFIG_PATH)
|
||||||
|
except Exception as e:
|
||||||
|
print ("Failed to open config file %s"%(e))
|
||||||
|
|
||||||
|
# current file path
|
||||||
self.file_path= None
|
self.file_path= None
|
||||||
if os.path.isfile(CONFIG_PATH):
|
# path = self.config.get("main", "last_opened_file")
|
||||||
with open(CONFIG_PATH, "r") as file:
|
# if os.path.isfile(path):
|
||||||
path = file.read()
|
# self._load_file(path)
|
||||||
if os.path.isfile(path):
|
|
||||||
self._load_file(path)
|
|
||||||
|
|
||||||
# build examples menu
|
# build examples menu
|
||||||
if os.path.isdir(EXAMPLE_PATH):
|
if os.path.isdir(EXAMPLE_PATH):
|
||||||
@ -157,11 +168,11 @@ class OMVGtk:
|
|||||||
self.terminal = self.builder.get_object('terminal')
|
self.terminal = self.builder.get_object('terminal')
|
||||||
try:
|
try:
|
||||||
# open VCP and configure the terminal
|
# open VCP and configure the terminal
|
||||||
self.fd = os.open("/dev/openmvcam", os.O_RDWR)
|
self.fd = os.open(self.config.get("main", "serial_port"), os.O_RDWR)
|
||||||
self.terminal.reset(True, True)
|
self.terminal.reset(True, True)
|
||||||
self.terminal.set_size(80,24)
|
self.terminal.set_size(80,24)
|
||||||
self.terminal.set_pty(self.fd)
|
self.terminal.set_pty(self.fd)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
self.show_message_dialog(gtk.MESSAGE_ERROR, "Failed to connect to OpenMV\n%s"%e)
|
self.show_message_dialog(gtk.MESSAGE_ERROR, "Failed to connect to OpenMV\n%s"%e)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -172,7 +183,7 @@ class OMVGtk:
|
|||||||
# interrupt any running code
|
# interrupt any running code
|
||||||
openmv.stop_script()
|
openmv.stop_script()
|
||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
self.show_message_dialog(gtk.MESSAGE_ERROR, "Failed to connect to OpenMV\n%s"%e)
|
self.show_message_dialog(gtk.MESSAGE_ERROR, "Failed to connect to OpenMV\n%s"%e)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -365,7 +376,7 @@ class OMVGtk:
|
|||||||
try:
|
try:
|
||||||
# read drawingarea
|
# read drawingarea
|
||||||
fb = openmv.fb_dump()
|
fb = openmv.fb_dump()
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
self._update_title()
|
self._update_title()
|
||||||
return True
|
return True
|
||||||
@ -390,6 +401,12 @@ class OMVGtk:
|
|||||||
def on_ctrl_scale_value_changed(self, adjust):
|
def on_ctrl_scale_value_changed(self, adjust):
|
||||||
openmv.set_attr(adjust.attr, int(adjust.value))
|
openmv.set_attr(adjust.attr, int(adjust.value))
|
||||||
|
|
||||||
|
def save_config(self):
|
||||||
|
# TODO set config items from GUI
|
||||||
|
#self.config.set("section", "key", value)
|
||||||
|
with open(CONFIG_PATH, "w") as file:
|
||||||
|
self.config.write(file)
|
||||||
|
|
||||||
def _update_title(self):
|
def _update_title(self):
|
||||||
if (self.file_path==None):
|
if (self.file_path==None):
|
||||||
title = "Untitled"
|
title = "Untitled"
|
||||||
@ -508,10 +525,7 @@ class OMVGtk:
|
|||||||
# disconnect
|
# disconnect
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
if (self.file_path):
|
self.save_config()
|
||||||
# write last opened file
|
|
||||||
with open(CONFIG_PATH, "w") as file:
|
|
||||||
file.write(self.file_path)
|
|
||||||
|
|
||||||
# exit
|
# exit
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|||||||
4
usr/openmv.config
Normal file
4
usr/openmv.config
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[main]
|
||||||
|
board = openmv1
|
||||||
|
serial_port = /dev/openmvcam
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user