63 lines
2.7 KiB
Python

import sys
import os
import tkinter as tk
from tkinter import ttk
import RPi.GPIO as GPIO
# Add the parent directory to the module search path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from interface_board_pins import * # Import pin assignments
def create_control_tab(notebook, adc, shift_reg, pwm1, pwm2):
frame = ttk.Frame(notebook)
notebook.add(frame, text="Controls")
digital_input_states = [tk.StringVar(value="LOW") for _ in range(8)]
digital_output_states = [tk.BooleanVar(value=False) for _ in range(8)]
adc_values = [tk.StringVar(value="0.00V") for _ in range(8)]
pwm_values = [tk.IntVar(value=0), tk.IntVar(value=0)]
def update_inputs():
for i, pin in enumerate(GPIO_DIGITAL_INPUTS.values()):
digital_input_states[i].set("HIGH" if GPIO.input(pin) else "LOW")
frame.after(500, update_inputs)
def update_adc():
for i, adc_channel in enumerate(ADC_CHANNELS.values()):
value = adc.read(adc_channel)
adc_values[i].set(f"{round(value * 12 / 4095, 2)}V")
frame.after(1000, update_adc)
def toggle_output(index):
shift_reg.set_pin(index, digital_output_states[index].get())
def update_pwm(channel, value):
duty_cycle = int(float(value))
if channel == 0:
pwm1.ChangeDutyCycle(duty_cycle)
else:
pwm2.ChangeDutyCycle(duty_cycle)
# UI Layout
style = ttk.Style()
style.configure("TScale", thickness=30) # Increases slider thickness
control_frame = ttk.Frame(frame, padding=30)
control_frame.pack(expand=True, fill="both")
for i in range(8):
ttk.Label(control_frame, text=f"ADC {i+1}:", font=("Arial", 14)).grid(row=i, column=0, sticky="e")
ttk.Label(control_frame, textvariable=adc_values[i], width=10, font=("Arial", 14)).grid(row=i, column=1, sticky="w")
ttk.Label(control_frame, text=f"IN {i+1}:", font=("Arial", 14)).grid(row=i, column=2, sticky="e")
ttk.Label(control_frame, textvariable=digital_input_states[i], width=6, font=("Arial", 14)).grid(row=i, column=3, sticky="w")
btn = ttk.Checkbutton(control_frame, text=f"OUT {i+1}", variable=digital_output_states[i], command=lambda i=i: toggle_output(i))
btn.grid(row=i, column=4, sticky="w")
for i in range(2):
ttk.Label(control_frame, text=f"PWM{i+1}:", font=("Arial", 14)).grid(row=i, column=5, sticky="e")
slider = ttk.Scale(control_frame, from_=0, to=100, orient="horizontal", length=400, variable=pwm_values[i], command=lambda val, i=i: update_pwm(i, val), style="TScale")
slider.grid(row=i, column=6, sticky="w", pady=10) # Added spacing with `pady=10`
update_inputs()
update_adc()