import tkinter as tk
from tkinter import messagebox
# Use gpiozero for GPIO simulation
from gpiozero import LED
import random
# Simulate GPIO pin for pump control
class MockGPIO:
def __init__(self, pin):
self.pin = pin
self.state = False
def on(self):
self.state = True
print(f"GPIO {self.pin} is ON")
def off(self):
self.state = False
print(f"GPIO {self.pin} is OFF")
def is_lit(self):
return self.state
PUMP_PIN = MockGPIO(17)
# Mock HX711 class for simulation
class MockHX711:
def tare(self):
print("Scale tared.")
def get_weight_mean(self, samples=10):
return random.uniform(0, 100) # Simulate weight reading
hx = MockHX711()
def start_dispensing():
try:
target_weight = float(entry_amount.get())
dispensed_weight = 0
hx.tare()
while dispensed_weight < target_weight:
PUMP_PIN.on()
dispensed_weight = hx.get_weight_mean(10)
root.update() # Update the GUI
PUMP_PIN.off()
messagebox.showinfo("Dispensing Complete", "The target amount has been dispensed.")
except ValueError:
messagebox.showerror("Input Error", "Please enter a valid number.")
def tare_scale():
hx.tare()
messagebox.showinfo("Tare Complete", "The scale has been tared.")
def cancel_dispensing():
PUMP_PIN.off()
messagebox.showinfo("Dispensing Canceled", "The dispensing has been canceled.")
root = tk.Tk()
root.title("Peristaltic Pump Control")
root.geometry("800x480")
label_amount = tk.Label(root, text="Enter amount to dispense (grams):")
label_amount.pack(pady=10)
entry_amount = tk.Entry(root)
entry_amount.pack(pady=10)
button_tare = tk.Button(root, text="Tare", command=tare_scale)
button_tare.pack(pady=10)
button_start = tk.Button(root, text="Start Dispensing", command=start_dispensing)
button_start.pack(pady=10)
button_cancel = tk.Button(root, text="Cancel Dispensing", command=cancel_dispensing)
button_cancel.pack(pady=10)
root.mainloop()