init workflow
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||
|
||||
@@ -7,7 +6,7 @@ import asyncio
|
||||
|
||||
from .filter import *
|
||||
from .gui.device import Device
|
||||
from .gui.slider import Slider
|
||||
from .gui.toolbar import RecordForm, FilterForm, DataStats
|
||||
|
||||
class FilterDevApp(tk.Tk):
|
||||
def __init__(self, loop: asyncio.EventLoop):
|
||||
@@ -15,9 +14,7 @@ class FilterDevApp(tk.Tk):
|
||||
self.loop = loop
|
||||
self.protocol("WM_DELETE_WINDOW", self.close)
|
||||
self.tasks = []
|
||||
self.tasks.append(loop.create_task(self.updater(1./2)))
|
||||
self.tasks.append(loop.create_task(self.update_plot(1./20)))
|
||||
self.tasks.append(loop.create_task(self.read_values(1./100)))
|
||||
self.tasks.append(loop.create_task(self.updater(1./100)))
|
||||
|
||||
self.filter = None
|
||||
|
||||
@@ -29,6 +26,7 @@ class FilterDevApp(tk.Tk):
|
||||
|
||||
# Create a figure for plotting
|
||||
self.fig, self.ax = plt.subplots()
|
||||
self.ax2 = self.ax.twinx()
|
||||
self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame)
|
||||
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH)
|
||||
|
||||
@@ -37,91 +35,92 @@ class FilterDevApp(tk.Tk):
|
||||
self.toolbar.pack(side=tk.RIGHT, fill=tk.Y)
|
||||
|
||||
# Device Settings
|
||||
self.connect_button = ttk.Button(self.toolbar, text="Connect", command=self.connect_disconnect)
|
||||
self.connect_button.pack(pady=10)
|
||||
self.device_label = ttk.Label(self.toolbar, text="Device Name:")
|
||||
self.device_label.pack(pady=10)
|
||||
self.device_name = ttk.Entry(self.toolbar)
|
||||
self.device_name.insert(0, "Smaage") # Set default value
|
||||
self.device_name.pack(pady=10)
|
||||
self.record_form = RecordForm(self.toolbar, self.record_data)
|
||||
self.record_form.pack(pady=10)
|
||||
|
||||
self.device = Device(self.device_name)
|
||||
self.device = Device(self.record_form.device_name)
|
||||
|
||||
# Filter Settings
|
||||
self.filter_type_label = ttk.Label(self.toolbar, text="Filter:")
|
||||
self.filter_type_label.pack(pady=10)
|
||||
self.filter_type_combobox = ttk.Combobox(self.toolbar, values=["MovAvg"])
|
||||
self.filter_type_combobox.set("MovAvg") # Set default value
|
||||
self.filter_type_combobox.pack(pady=10)
|
||||
self.change_filter = ttk.Button(self.toolbar, text="Change Filter", command=self.update_filter)
|
||||
self.change_filter.pack(pady=10)
|
||||
|
||||
# Objects
|
||||
self.filter = MovAvg(self.device, self.toolbar, lambda: None)
|
||||
self.filter.pack()
|
||||
self.filter_form = FilterForm(self.toolbar, self.update_filter)
|
||||
self.filter = MovAvg(self.device, self.toolbar, self.update_plot)
|
||||
|
||||
async def update_plot(self, interval):
|
||||
while await asyncio.sleep(interval, True):
|
||||
if self.filter is None:
|
||||
continue
|
||||
|
||||
# Clear the current plot
|
||||
self.ax.clear()
|
||||
self.data_stats = DataStats(self.toolbar, self.reset)
|
||||
|
||||
# Get current values from sliders
|
||||
df = self.filter()
|
||||
def update_plot(self):
|
||||
if self.filter is None:
|
||||
return
|
||||
|
||||
# Clear the current plot
|
||||
self.ax.clear()
|
||||
self.ax2.clear()
|
||||
|
||||
# Generate data
|
||||
x = df['timestamps']
|
||||
y1 = df['weights']
|
||||
y2 = df['filtered']
|
||||
# Get current values from sliders
|
||||
df = self.filter()
|
||||
self.data_stats.update_stats(df)
|
||||
|
||||
# Plot the data
|
||||
self.ax.plot(x, y1)
|
||||
self.ax.plot(x, y2)
|
||||
self.ax.set_xlabel("Time in ms")
|
||||
self.ax.set_ylabel("Weight")
|
||||
self.ax.grid()
|
||||
# Generate data
|
||||
x = df['timestamps']
|
||||
y1 = df['weights']
|
||||
# y1_g = df['calib_weights']
|
||||
# y2 = df['filtered']
|
||||
y2_g = df['filtered_calib']
|
||||
|
||||
# Draw the updated plot
|
||||
self.canvas.draw()
|
||||
# Plot the data
|
||||
self.ax.plot(x, y1)
|
||||
# self.ax.plot(x, y2)
|
||||
self.ax.set_xlabel("Time in ms")
|
||||
self.ax.set_ylabel("Raw Weight")
|
||||
self.ax.grid()
|
||||
|
||||
# self.ax2.plot(x, y1_g)
|
||||
self.ax2.plot(x, y2_g, color="orange")
|
||||
|
||||
# Draw the updated plot
|
||||
self.canvas.draw()
|
||||
|
||||
def update_filter(self):
|
||||
option = self.filter_type_combobox.get()
|
||||
option = self.filter_form.filter_type_combobox.get()
|
||||
if option == 'MovAvg' and not isinstance(self.filter, MovAvg):
|
||||
self.filter = MovAvg(self.device, self.toolbar, self.update_plot)
|
||||
|
||||
self.update_plot()
|
||||
|
||||
def connect_disconnect(self):
|
||||
def record_data(self):
|
||||
if self.device.is_connected:
|
||||
self.device_label.pack(pady=10)
|
||||
self.device_name.pack(pady=10)
|
||||
self.connect_button.config(text="Connect")
|
||||
self.connect_button.pack(pady=10)
|
||||
self.record_form.pack(pady=10)
|
||||
|
||||
self.device.disconnect()
|
||||
|
||||
else:
|
||||
task = self.loop.create_task(self.device.connect())
|
||||
task.add_done_callback(self.connected)
|
||||
record_duration = self.record_form.record_time.get_value()
|
||||
task = self.loop.create_task(self.device.read_values(record_duration))
|
||||
task.add_done_callback(self.data_recorded)
|
||||
|
||||
|
||||
def connected(self, *args):
|
||||
def data_recorded(self, *args):
|
||||
if self.device.is_connected:
|
||||
self.device_label.pack_forget()
|
||||
self.device_name.pack_forget()
|
||||
self.connect_button.config(text="Disconnect")
|
||||
self.record_form.pack_forget()
|
||||
|
||||
self.filter_form.pack(pady=10)
|
||||
self.filter.pack(pady=10)
|
||||
self.data_stats.pack(pady=10)
|
||||
|
||||
self.filter.pack()
|
||||
|
||||
async def read_values(self, interval):
|
||||
await self.device.read_values(interval)
|
||||
self.update_filter()
|
||||
|
||||
async def updater(self, interval):
|
||||
while await asyncio.sleep(interval, True):
|
||||
self.update()
|
||||
|
||||
|
||||
def reset(self):
|
||||
self.device.disconnect()
|
||||
self.device.clear_data()
|
||||
|
||||
self.filter_form.pack_forget()
|
||||
self.filter.pack_forget()
|
||||
self.data_stats.pack_forget()
|
||||
|
||||
self.record_form.pack()
|
||||
self.update_plot()
|
||||
|
||||
def close(self):
|
||||
for task in self.tasks:
|
||||
|
||||
Reference in New Issue
Block a user