I have been working on a tk script in python3 for handling GPIO with gpiod and sys along with time.
Has anyone attempted Python scripting with tkinter for gpiod?
I am using the beagley-ai and it seems every route I take with the driver in question is dictating nothing. My commands seem to not work. I can make the motor move in other scripts without tkinter but not with it so far.
It seems most of the internet is filled with ways of using it but with BCM or RPi libs. (something that does not help currently with the beagley-ai. Oh! Here is the source in case you see error for concern.
#!/usr/bin/python3
import tkinter as tk
import gpiod
import sys
from time import sleep
# --- GPIO Setup ---
# Define the chip and line number for your Motor Driver
CHIP_NAME1 = 'gpiochip1'
LED_LINE_OFFSET1 = 38
CHIP_NAME2 = 'gpiochip2'
LED_LINE_OFFSET2 = 14
try:
chip1 = gpiod.Chip(CHIP_NAME1)
left_line1 = chip1.get_line(LED_LINE_OFFSET1)
left_line1.request(consumer="Left_Control", type=gpiod.LINE_REQ_DIR_OUT)
chip2 = gpiod.Chip(CHIP_NAME2)
right_line2 = chip2.get_line(LED_LINE_OFFSET2)
right_line2.request(consumer="Right_Control", type=gpiod.LINE_REQ_DIR_OUT)
except gpiod.FileNotFoundError:
print(f"Error: GPIO chip '{CHIP_NAME1}' '{CHIP_NAME2}' not found. Ensure gpiod is installed and you are running on a compatible device.")
sys.exit(1)
except Exception as e:
print(f"Error initializing GPIO: {e}")
sys.exit(1)
# --- Tkinter GUI Setup ---
def toggle_motor():
val1 = left_line1.get_value()
val2 = right_line2.get_value()
new_val1 = 1 if val1 == 0 else 0
left_line1.set_value(val1)
right_line2.set_value(val2)
if val1 == 1:
left_line1.set_value(0) # Turn Left on
right_line2.set_value(1)
toggle_button.config(text="Turn Motor Left")
else:
left_line1.set_value(1) # Turn Right on
right_line2.set_value(1)
toggle_button.config(text="Turn Motor Right")
def cleanup_gpio():
left_line1.release()
right_line2.release()
chip1.close()
chip2.close()
root.destroy()
root = tk.Tk()
root.title("Motor Control")
toggle_button = tk.Button(root, text="Turn Motor Left", command=toggle_motor)
toggle_button.pack(pady=20)
exit_button = tk.Button(root, text="Exit", command=cleanup_gpio)
exit_button.pack(pady=10)
# Ensure GPIO is cleaned up when the window is closed
root.protocol("WM_DELETE_WINDOW", cleanup_gpio)
root.mainloop()
That is as far as I have gotten and it is filthy with bugs. I understand. I am building around it still…