184 lines
6.3 KiB
Python
184 lines
6.3 KiB
Python
"""
|
|
Top level script to prompt user for inputs, and then make those input as a .json file so that other modules can use it.
|
|
to run stuff. This module can have the frontend UI merged into, or can have separate one but should be the first things
|
|
to call from frontend before running other stuff.
|
|
"""
|
|
from datetime import datetime
|
|
from threading import *
|
|
import os, json, sys, subprocess
|
|
import serial.tools.list_ports
|
|
import numpy as np
|
|
import tkinter as tk
|
|
|
|
HEIGHT = 800
|
|
WIDTH = 800
|
|
|
|
|
|
def main():
|
|
#################### USER INPUTS ###########################
|
|
# TODO: if there is already a setting.json exist, loads all inputs from there as 'default'
|
|
# 9840
|
|
resistors = [int(text3.get()), int(text4.get()), 0, 0, 0] # resisters for each An port, where n is an integer from 0-3. Use 0 if none. in Ohms
|
|
input_voltage = 5
|
|
adjusted_volt = 5
|
|
port = get_devices()[0]
|
|
# typical window size: 480
|
|
window_size = 480
|
|
|
|
#################### END USER INPUTS ########################
|
|
|
|
# filename, disable customize name but should inform the user the current name of the data file on the front end
|
|
# alternatively, could be default input if no other input given. But if there is might have problem...
|
|
dat_folder = "RecordedData"
|
|
os.makedirs(dat_folder, exist_ok=True)
|
|
filename = os.path.join(os.getcwd(), dat_folder, f"{datetime.now().strftime('%Y-%m-%d_%H%M%S')}.csv")
|
|
resolution = 1023 # Arduino's analogRead()'s input resolution. We don't change this number usually.
|
|
delay = 1000 # millisec pe r data, defined in the firmware
|
|
if not (len(resistors) == 5):
|
|
raise ValueError(f"expecting 5 resistor values, but got {len(resistors)}!!!")
|
|
|
|
gen_settings(resistors, input_voltage, adjusted_volt, port, filename, resolution, window_size, delay)
|
|
return filename
|
|
|
|
|
|
def gen_settings(resistors, input_voltage, adjusted_volt, port, filename, resolution, window_size, delay):
|
|
name = "settings.json"
|
|
settings = {}
|
|
settings["refRes"] = resistors
|
|
settings["sensor_ports"] = np.where(np.array(resistors) > 0)[0].tolist()
|
|
settings["v_src"] = input_voltage
|
|
settings["v_in"] = adjusted_volt
|
|
settings["port"] = port
|
|
settings["resolution"] = resolution
|
|
settings["winSize"] = window_size
|
|
settings["file_name"] = filename
|
|
settings["delay"] = delay
|
|
open(name, 'w').write(json.dumps(settings, indent=4))
|
|
open(filename, "a", newline="", encoding="utf-8")
|
|
|
|
|
|
def get_devices():
|
|
ports = [comport.device for comport in serial.tools.list_ports.comports()] # get all available ports
|
|
if not ports:
|
|
raise ConnectionError("No devices detected!") # TODO: set to writerandomvalues.py thing instead raise error
|
|
return ports
|
|
|
|
|
|
# for scheduler maybe?
|
|
def task1():
|
|
print("run task 1")
|
|
# exec(open("read_arduino.py").read())
|
|
run_t1 = ["start", sys.executable, "read_arduino.pyw"]
|
|
out1 = subprocess.Popen(run_t1, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
running = True
|
|
init = 0
|
|
while running:
|
|
cline = out1.stdout.readline()
|
|
print(cline.decode())
|
|
if not cline and init > 0:
|
|
print(cline.decode())
|
|
running = False
|
|
init += 1
|
|
|
|
if out1.returncode:
|
|
print('Something went wrong:', out1.returncode)
|
|
|
|
|
|
def task2():
|
|
print("run task 2")
|
|
run_t2 = ["start", os.path.join(sys.exec_prefix, 'pythonw'), "serial_plot.pyw"]
|
|
out2 = subprocess.Popen(run_t2, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
running = True
|
|
init = 0
|
|
while running:
|
|
cline = out2.stdout.readline()
|
|
print(cline.decode())
|
|
if not cline and init > 0:
|
|
print(cline.decode())
|
|
running = False
|
|
init += 1
|
|
|
|
if out2.returncode:
|
|
print('Something went wrong:', out2.returncode)
|
|
|
|
|
|
def test_func():
|
|
file = main()
|
|
label['text'] = f"File has saved as {os.path.split(file)[1]} under {os.path.split(file)[0]} directory!\n"
|
|
t1 = Thread(target=task1, args=())
|
|
t1.setDaemon(True)
|
|
t1.start()
|
|
|
|
t2 = Thread(target=task2, args=())
|
|
t2.setDaemon(True)
|
|
t2.start()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# file = main()
|
|
# print(f"File has saved as {os.path.split(file)[1]} under {os.path.split(file)[0]} directory!")
|
|
# t1 = Thread(target=task1, args=())
|
|
# t1.setDaemon(True)
|
|
# t1.start()
|
|
#
|
|
# t2 = Thread(target=task2, args=())
|
|
# t2.setDaemon(True)
|
|
# t2.start()
|
|
# # while True:
|
|
# # pass
|
|
# t1.join()
|
|
# t2.join()
|
|
root = tk.Tk()
|
|
|
|
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
|
|
canvas.pack()
|
|
|
|
# backgroud_image = tk.PhotoImage(file='im1.gif')
|
|
backIm = tk.Label(root, bg='#80c1ff')
|
|
backIm.place(relwidth=1, relheight=1)
|
|
|
|
frame = tk.Frame(root, bg="#80c1ff", bd=5)
|
|
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
|
|
|
|
button1 = tk.Button(frame, text="update", font=50, fg='green', bg='grey', command=lambda: test_func())
|
|
button1.place(relx=0.8, relheight=0.5, relwidth=0.2)
|
|
|
|
button2 = tk.Button(frame, text="clear", font=50, fg='red', bg='grey')
|
|
button2.place(relx=0.8, rely=0.5, relheight=0.5, relwidth=0.2)
|
|
|
|
lb1 = tk.Label(frame, text="filename")
|
|
lb1.place(relx=0.1, relheight=0.4, relwidth=0.2)
|
|
|
|
text1 = tk.StringVar()
|
|
box1 = tk.Entry(frame, textvariable=text1)
|
|
box1.place(relx=0.1, rely=0.5, relheight=0.5, relwidth=0.2)
|
|
|
|
lb2 = tk.Label(frame, text="num of sensor")
|
|
lb2.place(relx=0.3, relheight=0.4, relwidth=0.2)
|
|
|
|
text2 = tk.StringVar()
|
|
box2 = tk.Entry(frame, textvariable=text2)
|
|
box2.place(relx=0.3, rely=0.5, relheight=0.5, relwidth=0.2)
|
|
|
|
lb3 = tk.Label(frame, text="res1")
|
|
lb3.place(relx=0.5, relheight=0.4, relwidth=0.1)
|
|
|
|
text3 = tk.StringVar()
|
|
box3 = tk.Entry(frame, textvariable=text3)
|
|
box3.place(relx=0.5, rely=0.5, relheight=0.5, relwidth=0.1)
|
|
|
|
lb4 = tk.Label(frame, text="res2")
|
|
lb4.place(relx=0.6, relheight=0.4, relwidth=0.1)
|
|
|
|
text4 = tk.StringVar()
|
|
box4 = tk.Entry(frame, textvariable=text4)
|
|
box4.place(relx=0.6, rely=0.5, relheight=0.5, relwidth=0.1)
|
|
|
|
lower_frame = tk.Frame(root, bg="#80c1ff", bd=10)
|
|
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
|
|
|
|
label = tk.Label(lower_frame, font=40)
|
|
label.place(relwidth=1, relheight=1)
|
|
|
|
root.mainloop()
|