Advertisement
j0h

sketchy

j0h
Feb 8th, 2024 (edited)
911
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.58 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import RPi.GPIO as GPIO
  3. import turtle
  4. import time
  5. import os
  6. # Encoder positions
  7. oldX = 0
  8. oldY = 0
  9. x_coord = 0
  10. y_coord = 0
  11. penState = False  # False for pen down, True for pen up
  12. i = 0             # index of current color
  13.  
  14. # GPIO pins for the encoders
  15. encoder2_pins = (23, 24)  # A and B pins for encoder 2 (y-axis)
  16. encoder1_pins = (15, 14)  # A and B pins for encoder 1 (x-axis)
  17.  
  18. # Setup GPIO
  19. clearBtn = 12
  20. liftBtn = 16
  21. saveSvgBtn = 21
  22. ColorBtn = 20
  23.  
  24. GPIO.setmode(GPIO.BCM)
  25. GPIO.setup(clearBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  26. GPIO.setup(liftBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  27. GPIO.setup(saveSvgBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  28. GPIO.setup(ColorBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  29. GPIO.setup(encoder1_pins[0], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  30. GPIO.setup(encoder1_pins[1], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  31. GPIO.setup(encoder2_pins[0], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  32. GPIO.setup(encoder2_pins[1], GPIO.IN, pull_up_down=GPIO.PUD_UP)
  33.  
  34. # Variables to store encoder state
  35. encoder1_last_state = (GPIO.input(encoder1_pins[0]) << 1) | GPIO.input(encoder1_pins[1])
  36. encoder2_last_state = (GPIO.input(encoder2_pins[0]) << 1) | GPIO.input(encoder2_pins[1])
  37.  
  38. # Create a turtle object
  39. #t = turtle.Turtle()
  40.  
  41. # Set up the turtle screen
  42. screen = turtle.Screen()
  43. screen.setup(width=1.0, height=1.0)  # Set the window to full-screen mode
  44. canvas = screen.getcanvas()
  45.  
  46. # Create a RawTurtle object with the canvas
  47. raw_turtle = turtle.RawTurtle(canvas)
  48. raw_turtle.shape('circle')
  49. raw_turtle.shapesize(0.5)  # Cursor size: 0.5 is half of normal
  50. root = canvas.winfo_toplevel()
  51. root.overrideredirect(1)
  52. ##BUTTONS
  53. #toggle though colors
  54. def Change_Color(channel):
  55.     global i
  56.     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
  57.     raw_turtle.pencolor(colors[i % len(colors)])  # Set pen color based on the current index modulo the length of the color list
  58.     i += 1
  59.  
  60. # Define a callback function to run when the button is pressed
  61. def button_callback(channel):
  62.     global x_coord, y_coord
  63.     #print("Clear Screen")
  64.     raw_turtle.reset()  # Reset the turtle, resets everything including point size
  65.     raw_turtle.shapesize(0.5)  # Cursor size: 0.5 is half of normal
  66.     x_coord = 0  # Reset the x coordinate to 0
  67.     y_coord = 0  # Reset the y coordinate to 0
  68.  
  69. # Callback function to lift or lower the pen
  70. def lift_pen(channel):
  71.     global penState
  72.     penState = not penState
  73.     if penState:
  74.         raw_turtle.penup()
  75.     else:
  76.         raw_turtle.pendown()
  77.  
  78. def saveSVG(channel):
  79.     # Get the current Unix time
  80.     current_time = int(time.time())
  81.     #print(current_time)
  82.     raw_turtle.hideturtle()
  83.     canvas_data = turtle.getcanvas().postscript()
  84.     # Construct the file name with the current time and the .ps extension
  85.     ps_file_name = f"{current_time}.ps"
  86.     svg_file_name = f"{current_time}.svg"
  87.     #print(ps_file_name)
  88.     #print(svg_file_name)
  89.     with open(ps_file_name, 'w') as f:
  90.         f.write(canvas_data)
  91.     # Show the turtle again
  92.     raw_turtle.showturtle()
  93.     os.system('eps2svg ' + ps_file_name + ' ' + svg_file_name)
  94.     os.system('rm ' + ps_file_name)
  95.  
  96. # Callback function for encoder 1 (x-axis)
  97. def encoder1_callback(channel):
  98.     global encoder1_last_state, x_coord
  99.     a = GPIO.input(encoder1_pins[0])
  100.     b = GPIO.input(encoder1_pins[1])
  101.     new_state = (a << 1) | b
  102.     if (encoder1_last_state == 0b00 and new_state == 0b10) or (encoder1_last_state == 0b11 and new_state == 0b01):
  103.         x_coord += 1
  104.     elif (encoder1_last_state == 0b10 and new_state == 0b00) or (encoder1_last_state == 0b01 and new_state == 0b11):
  105.         x_coord -= 1
  106.     encoder1_last_state = new_state
  107.  
  108. # Callback function for encoder 2 (y-axis)
  109. def encoder2_callback(channel):
  110.     global encoder2_last_state, y_coord
  111.     a = GPIO.input(encoder2_pins[0])
  112.     b = GPIO.input(encoder2_pins[1])
  113.     new_state = (a << 1) | b
  114.     if (encoder2_last_state == 0b00 and new_state == 0b10) or (encoder2_last_state == 0b11 and new_state == 0b01):
  115.         y_coord -= 1
  116.     elif (encoder2_last_state == 0b10 and new_state == 0b00) or (encoder2_last_state == 0b01 and new_state == 0b11):
  117.         y_coord += 1
  118.     encoder2_last_state = new_state
  119.  
  120. # Add event detection for encoder 1 (x-axis)
  121. GPIO.add_event_detect(encoder1_pins[0], GPIO.BOTH, callback=encoder1_callback)
  122. GPIO.add_event_detect(encoder1_pins[1], GPIO.BOTH, callback=encoder1_callback)
  123.  
  124. # Add event detection for encoder 2 (y-axis)
  125. GPIO.add_event_detect(encoder2_pins[0], GPIO.BOTH, callback=encoder2_callback)
  126. GPIO.add_event_detect(encoder2_pins[1], GPIO.BOTH, callback=encoder2_callback)
  127.  
  128. # Add event listener to the button pins
  129. GPIO.add_event_detect(clearBtn, GPIO.FALLING, callback=button_callback, bouncetime=300)
  130. GPIO.add_event_detect(liftBtn, GPIO.FALLING, callback=lift_pen, bouncetime=300)
  131. GPIO.add_event_detect(saveSvgBtn, GPIO.FALLING, callback=saveSVG, bouncetime=300)
  132. GPIO.add_event_detect(ColorBtn, GPIO.FALLING, callback=Change_Color, bouncetime=300)
  133.  
  134.  
  135. # Function to update the turtle's position based on encoder callbacks
  136. def update_position():
  137.     global x_coord, y_coord, oldX, oldY
  138.     if (x_coord != oldX or y_coord != oldY):
  139.         #print(f"X: {x_coord}, Y: {y_coord}")
  140.         raw_turtle.goto(x_coord, y_coord)
  141.     oldX = x_coord
  142.     oldY = y_coord
  143.     screen.ontimer(update_position, 100)  # Schedule the next update
  144.  
  145. # Start updating the position
  146. update_position()
  147. # Start the turtle main loop
  148. turtle.mainloop()
  149.  
  150. # Clean up GPIO on keyboard interrupt
  151. GPIO.cleanup()
  152. turtle.done()
  153.  
  154.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement