Read-Sensor-Resistances/test.py

112 lines
3.9 KiB
Python

from ui import Frame
from threading import *
from read_arduino import *
from serial_plotter import *
import os, json, wx, warnings
import serial.tools.list_ports
import numpy as np
warnings.filterwarnings("ignore", category=DeprecationWarning)
HEIGHT = 800
WIDTH = 800
global ani
def main():
#################### USER INPUTS ###########################
# TODO: if there is already a setting.json exist, loads all inputs from there as 'default'
resistors = [float(frame.r_ref_1.GetValue()), float(frame.r_ref_2.GetValue()), float(frame.r_ref_3.GetValue()),
float(frame.r_ref_4.GetValue()),
0] # resisters for each An port, where n is an integer from 0-3. Use 0 if none. in Ohms
input_voltage = float(frame.input_voltage.GetValue())
bit_rate = float(frame.adjusted_volt.GetValue())
port = frame.dev_list.GetValue()
# typical window size: 480
window_size = int(frame.m_textCtrl26.GetValue())
#################### 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")
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, bit_rate, port, filename, window_size, delay)
return filename
def gen_settings(resistors, input_voltage, bits, port, filename, window_size, delay):
"""
export all inputs from main() to a .json file
"""
name = "settings.json"
settings = {}
settings["refRes"] = resistors
settings["sensor_ports"] = np.where(np.array(resistors) > 0)[0].tolist()
settings["v_in"] = input_voltage
settings["port"] = port
settings["resolution"] = bits
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():
"""
get all available devices connected, put them in the UI's dropdown menu
"""
ports = [comport.device for comport in serial.tools.list_ports.comports()] # get all available ports
frame.dev_list.AppendItems(ports)
def runPlot(e):
global ani
plotter = SerialPlotter()
ani = plotter.plotting()
plotter.fig.show()
def run(e):
file = main()
t1 = Thread(target=read, args=())
t1.setDaemon(True)
t1.start()
runPlot(e)
frame.btLaunch.Enable(False)
frame.plot_but.Enable(True) # this might have some problem ... what happen if user spamming the plot button?
if not frame.show_msg.GetValue():
frame.Hide()
if __name__ == '__main__':
app = wx.App()
frame = Frame(None)
get_devices()
frame.SetTitle("Cease your resistance! - alpha 0.1.1")
frame.btLaunch.Bind(wx.EVT_BUTTON, run)
frame.plot_but.Bind(wx.EVT_BUTTON,
runPlot) # There is one problem with this approch: what happen if user spamming the plot button?
if os.path.isfile("settings.json"):
print("Found existing settings.json, auto-fill previous inputs!")
settings = json.load(open('settings.json', 'r'))
frame.r_ref_1.SetValue(str(settings["refRes"][0]))
frame.r_ref_2.SetValue(str(settings["refRes"][1]))
frame.r_ref_3.SetValue(str(settings["refRes"][2]))
frame.r_ref_4.SetValue(str(settings["refRes"][3]))
frame.input_voltage.SetValue(str(settings["v_in"]))
frame.adjusted_volt.SetValue(str(settings["resolution"]))
frame.m_textCtrl26.SetValue(str(settings["winSize"]))
frame.Show()
app.MainLoop()