# Main file of the program. Run this file to start the application. # ToDo: improve performance, communication to PSUs is way too slow for dynamic field changes # import packages: from os.path import exists import traceback from tkinter import messagebox # import other project files: from src.helmholtz_cage_device import HelmholtzCageDevice import src.globals as g import src.config_handling as config import src.csv_logging as log from src.magnetometer import MagnetometerProxy from src.user_interface import HelmholtzGUI, ExecuteCSVMode from src.socket_control import SocketInterfaceThread from src.utility import ui_print def program_end(): # called on exception or when user closes application # safely shuts everything down and saves any unsaved data g.exitFlag = True # tell everything else the application has been closed if g.app is not None: # the main Tkinter app object has been initialized before if g.app.pages[ExecuteCSVMode].csv_thread is not None: # check if a thread for executing CSVs exists g.app.pages[ExecuteCSVMode].csv_thread.stop() # stop the thread g.CAGE_DEVICE.destroy() # shut down devices and end all threads. if log.unsaved_data: # Check if there is logged data that has not been saved yet # open pop-up to ask user if he wants to save the data: save_log = messagebox.askquestion("Save log data?", "There seems to be unsaved logging data. " "Do you wish to write it to a file now?") if save_log == 'yes': # user has chosen yes filepath = log.select_file() # let user select a file to write to log.write_to_file(log.log_data, filepath) # write the data to the chosen file if g.app is not None: g.app.destroy() # close application try: # start normal operations config.CONFIG_FILE = 'config.ini' # set the config file path # ToDo: remember what the last config file was if not exists(config.CONFIG_FILE): # config file does not exist yet print("Config file not found, creating new from defaults.") config.reset_config_to_default() # create configuration object from defaults config.write_config_to_file(config.CONFIG_OBJECT) # write the configuration object to a new file config.CONFIG_OBJECT = config.get_config_from_file(config.CONFIG_FILE) # read configuration data from config file print("Starting setup...") # initiate communication with devices and initialize all major program objects g.CAGE_DEVICE = HelmholtzCageDevice() # Mostly a data structure to hold field data broadcast by connected tcp client with HW access. g.MAGNETOMETER = MagnetometerProxy() print("\nOpening User Interface...") g.app = HelmholtzGUI() # initialize user interface g.exitFlag = False # tell all functions that the user interface is now running # g.app.state('zoomed') # open UI in maximized window # g.app.StatusDisplay.continuous_label_update(g.app, 1000) # initiate regular Status Display updates (ms) # ToDo: label update is very slow, commented out to save performance but should be implemented # ToDo!: csv thread + continuous label update seems to exceed capacity of PSU communication ui_print("Program Initialized") config.check_config(config.CONFIG_OBJECT) # check config for values exceeding limits # Create TCP/Socket listener socket_controller = SocketInterfaceThread() socket_controller.start() g.app.protocol("WM_DELETE_WINDOW", program_end) # call program_end function if user closes the application g.app.mainloop() # start main program loop except Exception as e: # An error has occurred somewhere in the program print("\nAn error occurred, Shutting down.") # shop pup-up error message: message = "%s.\nSee python console traceback for more details. " \ "\nShutting down devices, check equipment to confirm." % e messagebox.showerror("Error!", message) print(traceback.print_exc()) # print error traceback in the python console program_end() # safely close everything and shut down devices