53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
import tkinter as tk
|
|
from tkinter import ttk
|
|
from matplotlib.figure import Figure
|
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
|
import time
|
|
|
|
ADC_PLOT_UPDATE_INTERVAL = 50 # 20 FPS, smooth and efficient
|
|
NOT_ACTIVE_CHECK_INTERVAL = 2000
|
|
MAX_HISTORY = 100
|
|
|
|
updating_enabled = False
|
|
adc_channels = list(range(8))
|
|
data = {ch: [0] * MAX_HISTORY for ch in adc_channels} # Preallocate lists
|
|
time_data = list(range(-MAX_HISTORY, 0)) # Simulated time axis
|
|
|
|
def create_adc_plot_tab(notebook, adc):
|
|
frame = ttk.Frame(notebook)
|
|
notebook.add(frame, text="ADC Plot")
|
|
|
|
figure = Figure(figsize=(8, 5), dpi=100)
|
|
ax = figure.add_subplot(1, 1, 1)
|
|
ax.set_ylim(0, 12)
|
|
ax.set_xlim(-MAX_HISTORY, 0) # Keep time axis fixed
|
|
canvas = FigureCanvasTkAgg(figure, master=frame)
|
|
canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
|
|
|
|
# Initialize lines for fast updates
|
|
lines = {ch: ax.plot(time_data, data[ch], label=f"ADC {ch+1}")[0] for ch in adc_channels}
|
|
|
|
def update_plot():
|
|
if not updating_enabled:
|
|
frame.after(NOT_ACTIVE_CHECK_INTERVAL, update_plot)
|
|
return
|
|
|
|
# Shift existing data left
|
|
for ch in adc_channels:
|
|
data[ch].pop(0)
|
|
data[ch].append(round(adc.read(ch) * 12 / 4095, 2))
|
|
|
|
# Update only the y-data for efficiency
|
|
for ch in adc_channels:
|
|
lines[ch].set_ydata(data[ch])
|
|
|
|
canvas.draw_idle() # Efficient redraw
|
|
frame.after(ADC_PLOT_UPDATE_INTERVAL, update_plot)
|
|
|
|
update_plot()
|
|
|
|
def set_updating_enabled(is_active):
|
|
global updating_enabled
|
|
updating_enabled = is_active
|
|
print(f"adc_plot tab: set updating_enabled to {updating_enabled}")
|