moved config handling to separate file

This commit is contained in:
Martin Zietz
2021-02-07 14:54:12 +01:00
parent 47b4e447b0
commit 08d4ca463d
5 changed files with 169 additions and 160 deletions
+28 -26
View File
@@ -10,6 +10,7 @@ from os.path import exists
import globals as g
import cage_func as func
import csv_threading as csv
import config_handling as config
NORM_FONT = ()
HEADER_FONT = ("Arial", 13, "bold")
@@ -328,7 +329,7 @@ class Configuration(Frame):
"Field to be compensated", "ambient_field", 1e6],
"Resistances:": [[DoubleVar() for _ in range(3)], "\u03A9",
"Resistance of coils + equipment", "resistance", 1],
"Max. Power:": [[DoubleVar() for _ in range(3)], "W", "Max. allowed power", "max_watts", 1],
"Max. Current:": [[DoubleVar() for _ in range(3)], "A", "Max. allowed current", "max_amps", 1],
"Max. Voltage:": [[DoubleVar() for _ in range(3)], "V",
"Max. allowed voltage, must not exceed 16V!", "max_volts", 1],
"Arduino Pins:": [[IntVar() for _ in range(3)], "-", "Should be 15, 16, 17", "relay_pin", 1]
@@ -387,7 +388,7 @@ class Configuration(Frame):
self.update_fields()
def restore_defaults(self): # restore all default settings
func.reset_config_to_default(g.CONFIG_FILE) # overwrite config file with default
config.reset_config_to_default(config.CONFIG_FILE) # overwrite config file with default
func.setup_all() # setup everything with the defaults
self.update_fields() # update fields in config window
@@ -398,8 +399,8 @@ class Configuration(Frame):
for key in self.entries.keys():
for i in [0, 1, 2]:
value = func.read_from_config(g.AXIS_NAMES[i], self.entries[key][3],
g.CONFIG_OBJECT) # get value from config file
value = config.read_from_config(g.AXIS_NAMES[i], self.entries[key][3],
config.CONFIG_OBJECT) # get value from config file
self.entries[key][0][i].set(value) # set initial value on variable
type_value = self.entries[key][0][i].get() # get value with correct data type
factor = self.entries[key][4] # get unit conversion factor
@@ -415,8 +416,8 @@ class Configuration(Frame):
def write_values(self): # update config file with user inputs into entry fields and reinitialize
# set serial ports for PSUs:
func.edit_config("PORTS", "xy_port", self.XY_port.get())
func.edit_config("PORTS", "z_port", self.Z_port.get())
config.edit_config("PORTS", "xy_port", self.XY_port.get())
config.edit_config("PORTS", "z_port", self.Z_port.get())
# set numeric values for all axes
for key in self.entries.keys(): # go through rows of entry table
@@ -438,7 +439,7 @@ class Configuration(Frame):
axis = g.AXIS_NAMES[i] # get axis name for error messages
if value_ok == 'OK':
func.edit_config(g.AXIS_NAMES[i], config_key, value) # write new value to config file
config.edit_config(g.AXIS_NAMES[i], config_key, value) # write new value to config file
else: # value is not within limits
if value_ok == 'HIGH':
max_value = g.default_arrays[config_key][1][i] # get max value
@@ -462,7 +463,7 @@ class Configuration(Frame):
# becomes 'yes' or 'no' depending on user choice
if answer == 'yes': # user really wants the value
# call function to write new value to config file with override=True
func.edit_config(g.AXIS_NAMES[i], config_key, value, True)
config.edit_config(g.AXIS_NAMES[i], config_key, value, True)
# if user chooses 'no' nothing happens, old value is kept
def implement(self): # executed on button press
@@ -471,14 +472,15 @@ class Configuration(Frame):
self.update_fields() # update entry fields to show new values
def load_config(self): # load configuration from some config file
directory = os.path.dirname(os.path.abspath(g.CONFIG_FILE)) # get directory of current config file
directory = os.path.dirname(os.path.abspath(config.CONFIG_FILE)) # get directory of current config file
# open file selection dialogue and save path of selected file
filename = filedialog.askopenfilename(initialdir=directory, title="Select Config File",
filetypes=(("Config File", "*.ini*"), ("All Files", "*.*")))
if exists(filename): # does the file exist?
g.CONFIG_FILE = filename # set global config file to the new file
g.CONFIG_OBJECT = func.get_config_from_file(filename) # load values from config file to config object
func.check_config(g.CONFIG_OBJECT) # check the values and display warnings if values are out of bounds
config.CONFIG_FILE = filename # set global config file to the new file
config.CONFIG_OBJECT = config.get_config_from_file(filename) # load from config file to config object
config.check_config(
config.CONFIG_OBJECT) # check the values and display warnings if values are out of bounds
func.setup_all() # reinitialize devices and program with new values
self.update_fields() # update entry fields to show new values
elif filename == '': # this happens when file selection window is closed without selecting a file
@@ -487,24 +489,23 @@ class Configuration(Frame):
func.ui_print("Selected file", filename, "does not exist, could not load config.")
def save_config_as(self): # save current configuration to a new config file
directory = os.path.dirname(os.path.abspath(g.CONFIG_FILE)) # get directory of current config file
directory = os.path.dirname(os.path.abspath(config.CONFIG_FILE)) # get directory of current config file
# open file selection dialogue and save path of selected file
filename = filedialog.asksaveasfilename(initialdir=directory, title="Save config to file",
filetypes=([("Config File", "*.ini*")]),
defaultextension=[("Config File", "*.ini*")])
if exists(filename): # does the file exist?
g.CONFIG_FILE = filename # set global config file to the new file
self.write_values() # write current entry field values to the config object
func.write_config_to_file(g.CONFIG_OBJECT) # write contents of config object to file
self.update_fields() # update entry fields to show values as they are in the config
elif filename == '': # this happens when file selection window is closed without selecting a file
if filename == '': # this happens when file selection window is closed without selecting a file
func.ui_print("No file selected, could not save config.")
else:
func.ui_print("Selected file", filename, "does not exist, could not save config.")
else: # a file name was entered
config.CONFIG_FILE = filename # set global config file to the new file
self.write_values() # write current entry field values to the config object
config.write_config_to_file(config.CONFIG_OBJECT) # write contents of config object to file
self.update_fields() # update entry fields to show values as they are in the config
def save_config(self): # same as save_config_as() but with the current config file
self.write_values()
func.write_config_to_file(g.CONFIG_OBJECT)
config.write_config_to_file(config.CONFIG_OBJECT)
self.update_fields()
@@ -541,18 +542,19 @@ class ExecuteCSVMode(Frame):
# Create and place buttons
self.select_file_button = Button(self.file_select_frame, text="Select csv file...", command=self.load_csv,
pady=5, padx=5, font=SMALL_BUTTON_FONT)
pady=5, padx=5, font=SMALL_BUTTON_FONT)
self.select_file_button.grid(row=0, column=0, padx=5)
self.execute_button = Button(self.file_select_frame, text="Run Sequence", command=self.run_sequence,
pady=5, padx=5, font=SMALL_BUTTON_FONT, state="disabled")
pady=5, padx=5, font=SMALL_BUTTON_FONT, state="disabled")
self.execute_button.grid(row=0, column=1, padx=5)
self.stop_button = Button(self.file_select_frame, text="Stop Run", command=self.stop_run,
pady=5, padx=5, font=SMALL_BUTTON_FONT, state="disabled")
pady=5, padx=5, font=SMALL_BUTTON_FONT, state="disabled")
self.stop_button.grid(row=0, column=2, padx=5)
row_counter += 1
def page_switch(self): # every class in the UI needs this, even if it doesn't do anything
def page_switch(self): # function that is called when switching to this window
# every class in the UI needs this, even if it doesn't do anything
pass
def load_csv(self): # load in csv file to be executed