Advertisement
j0h

xyPlot

j0h
Jan 25th, 2024 (edited)
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import RPi.GPIO as GPIO
  3. import turtle
  4. import time
  5. oldX=0
  6. oldY=0
  7. x_coord = 0
  8. y_coord = 0
  9. # GPIO pins for the encoders
  10. encoder1_pins = (23, 24)  # A and B pins for encoder 2 (x-axis)
  11. encoder2_pins = (14, 15)  # A and B pins for encoder 1 (y-axis)
  12.  
  13.  
  14. # Set up GPIO
  15. button_pin = 12
  16. GPIO.setmode(GPIO.BCM)
  17. GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  18. GPIO.setup(encoder1_pins[0], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  19. GPIO.setup(encoder1_pins[1], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  20. GPIO.setup(encoder2_pins[0], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  21. GPIO.setup(encoder2_pins[1], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  22.  
  23. # Variables to store encoder state
  24. encoder1_last_state = (GPIO.input(encoder1_pins[0]) << 1) | GPIO.input(encoder1_pins[1])
  25. encoder2_last_state = (GPIO.input(encoder2_pins[0]) << 1) | GPIO.input(encoder2_pins[1])
  26.  
  27. # Create a turtle object
  28. t = turtle.Turtle()
  29.  
  30. # Set up the turtle screen
  31. screen = turtle.Screen()
  32. screen.setup(width=1.0, height=1.0)  # Set the window to full-screen mode
  33. canvas = screen.getcanvas()
  34.  
  35. # Create a RawTurtle object with the canvas
  36. raw_turtle = turtle.RawTurtle(canvas)
  37. root = canvas.winfo_toplevel()
  38. root.overrideredirect(1)
  39.    
  40. # Define a callback function to run when the button is pressed
  41. def button_callback(channel):
  42.     global x_coord, y_coord
  43.     print("Clear Screen")
  44.     raw_turtle.reset()  # Reset the turtle
  45.     x_coord = 0  # Reset the x coordinate to 0
  46.     y_coord = 0  # Reset the y coordinate to 0
  47.        
  48. # Callback function for encoder 1 (x-axis)
  49. def encoder1_callback(channel):
  50.     global encoder1_last_state, x_coord
  51.     a = GPIO.input(encoder1_pins[0])
  52.     b = GPIO.input(encoder1_pins[1])
  53.     new_state = (a << 1) | b
  54.     if (encoder1_last_state == 0b00 and new_state == 0b10) or (encoder1_last_state == 0b11 and new_state == 0b01):
  55.         x_coord += 1
  56.     elif (encoder1_last_state == 0b10 and new_state == 0b00) or (encoder1_last_state == 0b01 and new_state == 0b11):
  57.         x_coord -= 1
  58.     encoder1_last_state = new_state
  59.  
  60. # Callback function for encoder 2 (y-axis)
  61. def encoder2_callback(channel):
  62.     global encoder2_last_state, y_coord
  63.     a = GPIO.input(encoder2_pins[0])
  64.     b = GPIO.input(encoder2_pins[1])
  65.     new_state = (a << 1) | b
  66.     if (encoder2_last_state == 0b00 and new_state == 0b10) or (encoder2_last_state == 0b11 and new_state == 0b01):
  67.         y_coord -= 1
  68.     elif (encoder2_last_state == 0b10 and new_state == 0b00) or (encoder2_last_state == 0b01 and new_state == 0b11):
  69.         y_coord += 1
  70.     encoder2_last_state = new_state
  71.  
  72. # Add event detection for encoder 1 (x-axis)
  73. GPIO.add_event_detect(encoder1_pins[0], GPIO.BOTH, callback=encoder1_callback)
  74. GPIO.add_event_detect(encoder1_pins[1], GPIO.BOTH, callback=encoder1_callback)
  75.  
  76. # Add event detection for encoder 2 (y-axis)
  77. GPIO.add_event_detect(encoder2_pins[0], GPIO.BOTH, callback=encoder2_callback)
  78. GPIO.add_event_detect(encoder2_pins[1], GPIO.BOTH, callback=encoder2_callback)
  79.  
  80. # Add event listener to the button pin
  81. GPIO.add_event_detect(button_pin, GPIO.FALLING, callback=button_callback, bouncetime=300)
  82.  
  83. # Function to update the turtle's position based on encoder callbacks
  84. def update_position():
  85.     global x_coord, y_coord, oldX, oldY
  86.     if (x_coord != oldX or y_coord != oldY):
  87.         print(f"X: {x_coord}, Y: {y_coord}")
  88.         raw_turtle.goto(x_coord, y_coord)
  89.     oldX = x_coord
  90.     oldY = y_coord
  91.     screen.ontimer(update_position, 100)  # Schedule the next update
  92.  
  93.  
  94. # Main loop
  95. # Start updating the position
  96. update_position()
  97. # Start the turtle main loop
  98. turtle.mainloop()
  99.  
  100. # Clean up GPIO on keyboard interrupt
  101. GPIO.cleanup() #Press Ctrl+C to exit (terminal window... maybe add a screen listener for ctrl+c or Q)
  102. turtle.done()
  103.  
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement