50 lines
1.4 KiB
Python
Executable File

import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import time
def create_adc_plot(frame, adc):
"""
Creates a live-updating ADC plot in the given Tkinter frame.
"""
figure = Figure(figsize=(6, 4), dpi=100)
ax = figure.add_subplot(1, 1, 1)
ax.set_title("ADC Readings Over Time")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Voltage (V)")
ax.set_ylim(0, 12)
canvas = FigureCanvasTkAgg(figure, master=frame)
canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
adc_channels = list(range(8))
data = {ch: [] for ch in adc_channels}
time_data = []
def update_plot():
current_time = time.time()
if len(time_data) > 50:
for ch in adc_channels:
data[ch].pop(0)
time_data.pop(0)
time_data.append(current_time)
for ch in adc_channels:
voltage = round(adc.read(ch) * 12 / 4095, 2)
data[ch].append(voltage)
ax.clear()
ax.set_title("ADC Readings Over Time")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Voltage (V)")
ax.set_ylim(0, 12)
for ch in adc_channels:
ax.plot(time_data, data[ch], label=f"ADC {ch+1}")
ax.legend(loc="upper right")
canvas.draw()
frame.after(1000, update_plot)
update_plot()