feat: add pen (HI-Z and PD) and serial com
This commit is contained in:
28
code.py
28
code.py
@@ -4,11 +4,23 @@ import usb_cdc
|
|||||||
import pwmio
|
import pwmio
|
||||||
import digitalio
|
import digitalio
|
||||||
|
|
||||||
pwm_X = pwmio.PWMOut(board.GP17, frequency=1000, duty_cycle=0)
|
settling_time = 0.01 # Measured on the scope
|
||||||
pwm_Y = pwmio.PWMOut(board.GP18, frequency=1000, duty_cycle=0)
|
pen_time = 0.1
|
||||||
pen = digitalio.DigitalInOut(board.GP16)
|
|
||||||
|
# GP20 is purple
|
||||||
|
pwm_Y = pwmio.PWMOut(board.GP20, frequency=1000, duty_cycle=0)
|
||||||
|
# GP10 is blue
|
||||||
|
pwm_X = pwmio.PWMOut(board.GP10, frequency=1000, duty_cycle=0)
|
||||||
|
pen = digitalio.DigitalInOut(board.GP8)
|
||||||
|
|
||||||
|
def set_pen_state(enable_low):
|
||||||
|
if enable_low:
|
||||||
pen.direction = digitalio.Direction.OUTPUT
|
pen.direction = digitalio.Direction.OUTPUT
|
||||||
pen.value = False # Set pen to up position
|
pen.value = False # Set to LOW
|
||||||
|
else:
|
||||||
|
pen.direction = digitalio.Direction.INPUT # Set to HI-Z
|
||||||
|
time.sleep(pen_time)
|
||||||
|
|
||||||
|
|
||||||
def message_handler(msg):
|
def message_handler(msg):
|
||||||
parts = msg.split(',')
|
parts = msg.split(',')
|
||||||
@@ -16,9 +28,11 @@ def message_handler(msg):
|
|||||||
print("Invalid message format")
|
print("Invalid message format")
|
||||||
return
|
return
|
||||||
print(f"X: {parts[0]}, Y: {parts[1]}, Pen: {parts[2]}")
|
print(f"X: {parts[0]}, Y: {parts[1]}, Pen: {parts[2]}")
|
||||||
|
pen_state = True if int(parts[2]) == 1 else False
|
||||||
|
set_pen_state(pen_state)
|
||||||
|
# Set pen before setting the PWM values to not get weird lines.
|
||||||
pwm_X.duty_cycle = int(parts[0])
|
pwm_X.duty_cycle = int(parts[0])
|
||||||
pwm_Y.duty_cycle = int(parts[1])
|
pwm_Y.duty_cycle = int(parts[1])
|
||||||
pen.value = True if int(parts[2]) == 1 else False
|
|
||||||
|
|
||||||
print("Booted")
|
print("Booted")
|
||||||
while True:
|
while True:
|
||||||
@@ -26,6 +40,6 @@ while True:
|
|||||||
data = usb_cdc.data.read(usb_cdc.data.in_waiting)
|
data = usb_cdc.data.read(usb_cdc.data.in_waiting)
|
||||||
message = data.decode('utf-8').strip()
|
message = data.decode('utf-8').strip()
|
||||||
message_handler(message)
|
message_handler(message)
|
||||||
time.sleep(0.5)
|
time.sleep(settling_time)
|
||||||
usb_cdc.data.write(b"OK\n")
|
usb_cdc.data.write(b"OK\n")
|
||||||
time.sleep(0.1)
|
time.sleep(0.01)
|
||||||
Reference in New Issue
Block a user