Advertisement
j0h

sketchyEtchTesting.py

j0h
Mar 1st, 2024
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import RPi.GPIO as GPIO
  3. import turtle
  4. """
  5. Ought to set up limits in screen size and x,y boundaries.
  6. explore tracer function
  7. """
  8. # Setup GPIO
  9. clearBtn = 12
  10. liftBtn  = 16
  11. ColorBtn = 20
  12.  
  13. # GPIO pins for the encoders
  14. encoder1_pins = (24, 23)  # A and B pins for encoder 1 (x-axis)
  15. encoder2_pins = (14, 15)  # A and B pins for encoder 2 (y-axis)
  16.  
  17. GPIO.setmode(GPIO.BCM)
  18. GPIO.setup(clearBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  19. GPIO.setup(liftBtn, GPIO.IN) #, pull_up_down=GPIO.PUD_UP #removal of this means you must hold the button down
  20. GPIO.setup(ColorBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  21. GPIO.setup(encoder1_pins[0], GPIO.IN) #removed PUD_UP as encoders are not PWM, and resistors are included.
  22. GPIO.setup(encoder1_pins[1], GPIO.IN)
  23. GPIO.setup(encoder2_pins[0], GPIO.IN)
  24. GPIO.setup(encoder2_pins[1], GPIO.IN)
  25.  
  26. # Variables to store encoder state
  27. encoder1_last_state = (GPIO.input(encoder1_pins[0]) << 1) | GPIO.input(encoder1_pins[1])
  28. encoder2_last_state = (GPIO.input(encoder2_pins[0]) << 1) | GPIO.input(encoder2_pins[1])
  29.  
  30. # Encoder positions
  31. x_coord  = 0
  32. y_coord  = 0
  33.  
  34. penState = False  # False for pen down, True for pen up
  35. i = 0             # index of current color
  36.  
  37. # Set up the turtle screen
  38. screen = turtle.Screen()
  39. screen.tracer(0)
  40. screen.setup(width=1.0, height=1.0)  # Set the window to full-screen mode
  41. canvas = screen.getcanvas()
  42.  
  43. # Create a RawTurtle object with the canvas
  44. raw_turtle = turtle.RawTurtle(canvas)
  45. raw_turtle.shape('circle')
  46. raw_turtle.shapesize(0.5)  # Cursor size: 0.5 is half of normal
  47. raw_turtle.width(6)        # Line width
  48. raw_turtle.speed(0)
  49.  
  50. #BUTTON FUNCTIONS
  51.    
  52. #toggle though colors
  53. def Change_Color(channel):
  54.     global i
  55.     colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 1, 1), (1, 0, 1), (1, 1, 0), (1, 1, 1), (0, 0, 0)]  # List of RGB colors
  56.     raw_turtle.pencolor(colors[i % len(colors)])  # Set pen color based on the current index of the length of the color list
  57.     i += 1
  58.  
  59. def ClearScrnBtn(channel):
  60.     global x_coord, y_coord #,oldX, oldY
  61.     raw_turtle.reset()  # Reset the turtle, resets everything including point size
  62.     raw_turtle.shapesize(0.5)  # Cursor size: 0.5 is half of normal
  63.     raw_turtle.width(6)
  64.     x_coord = 0
  65.     y_coord = 0
  66.     screen.tracer(0)
  67.  
  68. # Callback function to lift or lower the pen
  69. def lift_pen(channel):
  70.     global penState
  71.     penState = not penState
  72.     if penState:
  73.         raw_turtle.penup()
  74.     else:
  75.         raw_turtle.pendown()
  76.  
  77.  
  78. # Callback function for encoder 1 (x-axis)
  79. def encoder1_callback(channel):
  80.     global encoder1_last_state, x_coord
  81.     a = GPIO.input(encoder1_pins[0])
  82.     b = GPIO.input(encoder1_pins[1])
  83.     new_state = (a << 1) | b
  84.     if (encoder1_last_state == 0b00 and new_state == 0b10) or (encoder1_last_state == 0b11 and new_state == 0b01):
  85.         x_coord += 6        
  86.     elif (encoder1_last_state == 0b10 and new_state == 0b00) or (encoder1_last_state == 0b01 and new_state == 0b11):
  87.         x_coord -= 6
  88.     encoder1_last_state = new_state
  89.  
  90. # Callback function for encoder 2 (y-axis)
  91. def encoder2_callback(channel):
  92.     global encoder2_last_state, y_coord
  93.     a = GPIO.input(encoder2_pins[0])
  94.     b = GPIO.input(encoder2_pins[1])
  95.     new_state = (a << 1) | b
  96.     if (encoder2_last_state == 0b00 and new_state == 0b10) or (encoder2_last_state == 0b11 and new_state == 0b01):
  97.         y_coord -= 6
  98.     elif (encoder2_last_state == 0b10 and new_state == 0b00) or (encoder2_last_state == 0b01 and new_state == 0b11):
  99.         y_coord += 6
  100.     encoder2_last_state = new_state
  101.  
  102.  
  103.  
  104. # Add event detection for encoder 1 (x-axis)
  105. GPIO.add_event_detect(encoder1_pins[0], GPIO.BOTH, callback=encoder1_callback,bouncetime=15)
  106. GPIO.add_event_detect(encoder1_pins[1], GPIO.BOTH, callback=encoder1_callback,bouncetime=15)
  107.  
  108. # Add event detection for encoder 2 (y-axis)
  109. GPIO.add_event_detect(encoder2_pins[0], GPIO.BOTH, callback=encoder2_callback,bouncetime=15)
  110. GPIO.add_event_detect(encoder2_pins[1], GPIO.BOTH, callback=encoder2_callback,bouncetime=15)
  111.  
  112. # Add event listener to the button pins
  113. GPIO.add_event_detect(clearBtn, GPIO.FALLING, callback=ClearScrnBtn, bouncetime=300)
  114. GPIO.add_event_detect(liftBtn,  GPIO.FALLING, callback=lift_pen,     bouncetime=300)
  115. GPIO.add_event_detect(ColorBtn, GPIO.FALLING, callback=Change_Color, bouncetime=300)
  116.  
  117. def update_position():
  118.     global x_coord, y_coord
  119.     screen.title(f"X: {x_coord}, Y: {y_coord}")
  120.     #raw_turtle.goto(x_coord, y_coord)     
  121.     raw_turtle.setx(x_coord)
  122.     raw_turtle.sety(y_coord)
  123.  
  124.     if(GPIO.event_detected(encoder1_pins[0]) or GPIO.event_detected(encoder1_pins[1]) or GPIO.event_detected(encoder2_pins[0]) or GPIO.event_detected(encoder1_pins[1])):
  125.         screen.ontimer(update_position, 100)
  126.     else:
  127.         screen.ontimer(update_position, 100)
  128.  
  129. # Start updating the position
  130. update_position()
  131.  
  132. # Start the turtle main loop
  133. #turtle.mainloop()
  134. screen.mainloop()
  135.  
  136. # Clean up GPIO on keyboard interrupt
  137. GPIO.cleanup()
  138. #turtle.done()   #not mentioned in turtle documentation for raw
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement