# -*- coding: utf-8 -*- """ Created on Thu Mar 11 15:39:17 2021 @author: alain $ cd ~/guitastro/gui/tp_audine_elec $ python3 tp_audine_elec.py """ import os import sys import yaml import subprocess import time from PIL import ImageTk, Image import tkinter as tk from tkinter import ttk import tkinter.font as tkFont import tkinter.scrolledtext as tkscrolled import platform path = os.path.abspath("../../src") if path not in sys.path: sys.path.insert(0,path) import guitastro # ##################################################################### # ##################################################################### # ##################################################################### # Main # ##################################################################### # ##################################################################### # ##################################################################### if __name__ == "__main__": # --- Get path_images for examples ima1 = guitastro.Ima() path_products = ima1.copy_data2products() # --- Global variable config for the YML file fconfig = os.path.join(path_products,"tp_audine_elec.yml") if not os.path.exists(fconfig): config = {} conf = {} conf["fits_path"] = "/home/user/Documents" config["visu1"] = conf else: with open(fconfig, 'r') as stream: try: config = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) # --- Global variable to select the method methods = ["full_frame", "zi_zh", "fast_line", "read_pel_fast", "read_pel_fast2", "fast_vidage"] #, "square_signal", "set_0", "set_255"] # --- Global variable to define info messages infos = {} infos['demarrer_acq'] = "Appuyez sur le bouton Acquisition pour envoyer des signaux à la caméra" infos['acq_en_cours'] = "Acquisition en cours. Attendre quelques secondes... Prendre des mesures à l'oscilloscope" infos['acq_impossible'] = "Acquisition impossible sur cet OS. Utiliser un Raspberry PI" infos['bad_method'] = f"Method not found amonsgt {methods}" # --- Global variable for Tk variables var = {} var['executable'] = "/home/user/iut/tp_ccd/bin/Debug/tp_ccd" var['c_code_filename'] = os.path.join(os.getcwd(), "main.cpp") var['c_code_filename_alt'] = "main.cpp" var['image_path'] = "/home/user/Documents" var['image_filename'] = "image.fit" # =================================================== # =================================================== # Function definitions # =================================================== # =================================================== def save_config(): global fconfig with open(fconfig, 'w') as yaml_file: yaml.dump(config, yaml_file, default_flow_style=False) def ima1_load(): global widgets path_cur = config["visu1"]["fits_path"] ima1.rootdir = path_cur fullfile = ima1.load() if fullfile != "": path_new = os.path.dirname(fullfile) if path_cur != path_new and path_new != "": config["visu1"]["fits_path"] = path_new save_config() visu1.autocuts() visu1.disp() widgets["button_saveima_1"].configure(state= tk.NORMAL) def ima1_save(): ima1.save() def acquisition(): global widgets global infos global var global methods widgets["button_loadima_1"].configure(state= tk.DISABLED) widgets["button_saveima_1"].configure(state= tk.DISABLED) widgets["button_acq"].configure(state= tk.DISABLED) var['info'].set(infos['acq_en_cours']) # --- method = var_method.get() if method in methods: meth = method else: var['info'].set(infos['bad_method']) tkroot.update() time.sleep(5) var['info'].set(infos['demarrer_acq']) tkroot.update() return # --- expo = var_exp.get() if expo < 0: expo = 0 if expo > 30: expo = 30 var_exp.set(expo) # --- binn = var_bin.get() if binn < 1: binn = 1 if binn > 100: binn = 100 var_bin.set(binn) # --- tkroot.update() # --- argus = [] argus.append(var['executable']) argus.append(f"{meth}") argus.append(f"{expo}") argus.append(f"{binn}") # --- Launch the acquisition process t0 = time.time() message = f'subprocess.run({argus},capture_output=True)' print(message) release = platform.release() image = False if "raspi" in release: # Launch the process only for Raspberry res = subprocess.run(argus, capture_output=True) stdo = res.stdout print(stdo) #lstdos = str(stdo).split("\\n") if meth == "full_frame": image = True else: time.sleep(1) else: var['info'].set(infos['acq_impossible']) tkroot.update() time.sleep(5) dt = time.time() - t0 message = f"Acquisition done in {dt:.3f} sec" print(message) # --- if meth=="full_frame" and image==True: time.sleep(0.5) fname = os.path.join(var['image_path'],var['image_filename']) fullfile = ima1.load(fname) path_new = os.path.dirname(fullfile) config["visu1"]["fits_path"] = path_new ima1.rootdir = path_new save_config() # --- visu1.autocuts() visu1.disp() # --- widgets["button_loadima_1"].configure(state= tk.NORMAL) widgets["button_saveima_1"].configure(state= tk.NORMAL) widgets["button_acq"].configure(state= tk.NORMAL) var['info'].set(infos['demarrer_acq']) # --- update the plot tkroot.update() # =================================================== # =================================================== # Start main code # =================================================== # =================================================== # --- Create a Tk interface tkroot = tk.Tk() dimx = 1600 dimy = 880 geom = f"{dimx}x{dimy}+80+0" tkroot.geometry(geom) tkroot.title("TP CCD Electronique - BUT S5") tkroot.minsize(height=dimy, width=dimx) tkroot.maxsize(height=dimy+50, width=dimx+50) fontStyle_1 = tkFont.Font(family="Arial", size=14, weight="bold") fontStyle_2 = tkFont.Font(family="Arial", size=12, weight="bold") fontStyle_3 = tkFont.Font(family="Arial", size=10, weight="bold") fontStyle_c3 = tkFont.Font(family="courier", size=10, weight="bold") # =================================================== # =================================================== # Frame1. Acquisition button and image visu # =================================================== # =================================================== frame1 = tk.Frame(tkroot) # --- frame101 = tk.Frame(frame1) frame101.pack(side = tk.TOP, fill=tk.X, expand=tk.NO) # --- widgets = {} # --- frame101m = tk.Frame(frame101) # --- photo_logo = ImageTk.PhotoImage(file='logo_iut_mp.png') label1 = tk.Label(frame101m, image=photo_logo) label1.image = photo_logo label1.pack(side = tk.LEFT, padx=2) # --- var_method = tk.StringVar() var_method.set(methods[0]) label_method = tk.Label(frame101m, text="Method") label_method.pack(side = tk.LEFT) entry_method = ttk.Combobox(frame101m, textvariable=var_method, width=12) entry_method['values'] = methods entry_method['state'] = 'readonly' entry_method.pack(side = tk.LEFT) #entry_method('<>', var_method) # --- var_exp = tk.DoubleVar() var_exp.set(0.0) label_exp = tk.Label(frame101m, text="Temps de pose (s)") label_exp.pack(side = tk.LEFT) entry_exp = tk.Entry(frame101m, width=4, textvariable=var_exp) entry_exp.pack(side = tk.LEFT) # --- var_bin = tk.IntVar() var_bin.set(1) label_bin = tk.Label(frame101m, text="Binning") label_bin.pack(side = tk.LEFT) entry_bin = tk.Entry(frame101m, width=4, textvariable=var_bin) entry_bin.pack(side = tk.LEFT) # --- widgets["button_acq"] = tk.Button(frame101m, text="Acquisition", command=acquisition, padx=20, pady=20, font='sans 16 bold') widgets["button_acq"].pack(side = tk.LEFT, pady=5, padx=5) # --- widgets["button_loadima_1"] = tk.Button(frame101m, text="Load FITS", command=ima1_load) widgets["button_loadima_1"].pack(side=tk.LEFT, padx = 2) widgets["button_saveima_1"] = tk.Button(frame101m, text="Save FITS", command=ima1_save, state= tk.DISABLED) widgets["button_saveima_1"].pack(side=tk.LEFT, padx = 2) frame101m.pack(side = tk.TOP, fill=tk.X, expand=tk.NO) # --- frame102 = tk.Frame(frame1) # --- var['info'] = tk.StringVar() var['info'].set(infos['demarrer_acq']) widgets["label_info"] = tk.Label(frame102, textvariable=var['info'], padx = 10) widgets["label_info"].pack(side = tk.LEFT) frame102.pack(side = tk.TOP, fill=tk.X, expand=tk.NO) # --- Create and use a visu try: frame101i = tk.Frame(frame1) visu1 = guitastro.Visu(frame101i) visu1.ima(ima1) frame101i.pack(side = tk.TOP, fill=tk.BOTH, expand=tk.YES) except: msg = "{}".format(sys.exc_info()) print(msg) frame1.pack(fill=tk.Y, side = tk.LEFT, anchor=tk.NW) # =================================================== # =================================================== # Frame2. Display C code # =================================================== # =================================================== frame2 = tk.Frame(tkroot) label_code = tk.Label(frame2, text="C code run by the button Acquisition") label_code.pack(side = tk.TOP) frame_display_code = tk.Frame(frame2) scrollbar = tk.Scrollbar(frame_display_code) text_code_txt = tk.Text(frame_display_code, height=15, wrap="word", yscrollcommand=scrollbar.set, font=fontStyle_3) scrollbar.config(command=text_code_txt.yview) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) text_code_txt.tag_config('red', foreground="red") text_code_txt.tag_config('blue', foreground="blue") text_code_txt.tag_config('black', foreground="black") text_code_txt.tag_config('green', foreground="green") lignes = "" try: with open(var['c_code_filename'], "rt") as fid: lignes = fid.readlines() except: with open(var['c_code_filename_alt'], "rt") as fid: lignes = fid.readlines() for ligne in lignes: text_code_txt.insert(tk.END, ligne, "blue") text_code_txt.yview_moveto(1.0) text_code_txt.pack(side = tk.TOP, padx=5, pady=1) frame_display_code.pack(side = tk.TOP, pady=5) notebook = ttk.Notebook(frame2) tab201 = ttk.Frame(notebook) tab202 = ttk.Frame(notebook) tab203 = ttk.Frame(notebook) tab204 = ttk.Frame(notebook) tab205 = ttk.Frame(notebook) tab206 = ttk.Frame(notebook) tab207 = ttk.Frame(notebook) # ===================================================== # Tab 201 == # ===================================================== frame_display_ccdphase = tk.Frame(tab201) image = Image.open('true2phase_ccd.png') zoom = 1.2 image_size = tuple([int(zoom * x) for x in image.size]) ccdphase_canvas = tk.Canvas(frame_display_ccdphase, width=image_size[0], height=image_size[1], relief=tk.RAISED, cursor="crosshair") ccdphase_image = ImageTk.PhotoImage(image.resize(image_size)) ccdphase_canvas.create_image(0, 0, anchor = tk.NW, image = ccdphase_image) ccdphase_canvas.pack() frame_display_ccdphase.pack() # ===================================================== # Tab 202 == # ===================================================== frame_display_ccdtransf = tk.Frame(tab202) image = Image.open('ccd_transferts1.png') zoom = 2 image_size = tuple([int(zoom * x) for x in image.size]) ccdtransf_canvas = tk.Canvas(frame_display_ccdtransf, width=image_size[0], height=image_size[1], relief=tk.RAISED, cursor="crosshair") ccdtransf_image = ImageTk.PhotoImage(image.resize(image_size)) ccdtransf_canvas.create_image(0, 0, anchor = tk.NW, image = ccdtransf_image) ccdtransf_canvas.pack() frame_display_ccdtransf.pack() # ===================================================== # Tab 203 == # ===================================================== frame_display_timing = tk.Frame(tab203) image = Image.open('timing_diagram1.png') zoom = 0.75 image_size = tuple([int(zoom * x) for x in image.size]) timing_canvas = tk.Canvas(frame_display_timing, width=image_size[0], height=image_size[1], relief=tk.RAISED, cursor="crosshair") timing_image = ImageTk.PhotoImage(image.resize(image_size)) timing_canvas.create_image(0, 0, anchor = tk.NW, image = timing_image) timing_canvas.pack() frame_display_timing.pack() # ===================================================== # Tab 204 == # ===================================================== frame_display_pins = tk.Frame(tab204) image = Image.open('pins_raspberry.png') zoom = 0.75 image_size = tuple([int(zoom * x) for x in image.size]) pins_canvas = tk.Canvas(frame_display_pins, width=image_size[0], height=image_size[1], relief=tk.RAISED, cursor="crosshair") pins_image = ImageTk.PhotoImage(image.resize(image_size)) pins_canvas.create_image(0, 0, anchor = tk.NW, image = pins_image) pins_canvas.pack() frame_display_pins.pack() # ===================================================== # Tab 205 == # ===================================================== frame_display_ccd = tk.Frame(tab205) image = Image.open('ccd.png') zoom = 0.65 image_size = tuple([int(zoom * x) for x in image.size]) ccd_canvas = tk.Canvas(frame_display_ccd, width=image_size[0], height=image_size[1], relief=tk.RAISED, cursor="crosshair") ccd_image = ImageTk.PhotoImage(image.resize(image_size)) ccd_canvas.create_image(0, 0, anchor = tk.NW, image = ccd_image) ccd_canvas.pack() frame_display_ccd.pack() # ===================================================== # Tab 206 == # ===================================================== frame_display_ampli = tk.Frame(tab206) image = Image.open('ampli.png') zoom = 0.60 image_size = tuple([int(zoom * x) for x in image.size]) ampli_canvas = tk.Canvas(frame_display_ampli, width=image_size[0], height=image_size[1], relief=tk.RAISED, cursor="crosshair") ampli_image = ImageTk.PhotoImage(image.resize(image_size)) ampli_canvas.create_image(0, 0, anchor = tk.NW, image = ampli_image) ampli_canvas.pack() frame_display_ampli.pack() # ===================================================== # Tab 207 == # ===================================================== frame_display_can = tk.Frame(tab207) image = Image.open('can.png') zoom = 0.65 image_size = tuple([int(zoom * x) for x in image.size]) can_canvas = tk.Canvas(frame_display_can, width=image_size[0], height=image_size[1], relief=tk.RAISED, cursor="crosshair") can_image = ImageTk.PhotoImage(image.resize(image_size)) can_canvas.create_image(0, 0, anchor = tk.NW, image = can_image) can_canvas.pack() frame_display_can.pack() # ===================================================== # Pack frame of tabs # ===================================================== notebook.add(tab201, text="CCD 2 phases") notebook.add(tab202, text="CCD transferts") notebook.add(tab203, text="Timings") notebook.add(tab204, text="Raspberry pins") notebook.add(tab205, text="CCD chip") notebook.add(tab206, text="Analogic output") notebook.add(tab207, text="CAN and multiplexer") notebook.pack(fill = tk.Y) frame2.pack(fill = tk.Y, side=tk.LEFT, anchor="nw") # =================================================== # --- Launch the event loop of the user interface # =================================================== tkroot.pack_propagate(0) tkroot.update() tkroot.mainloop()