Last active
March 2, 2022 10:32
-
-
Save jaames/a8649cfc4585d55ecd726dfb6d34162c to your computer and use it in GitHub Desktop.
Puts a USB-connected Playdate into disk mode - workaround for the Simulator not recognising that a Playdate is connected in macOS Monterey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# playdate-disk.py | |
# Puts a USB-connected Playdate into disk mode | |
# requires Python 3 and pyUSB (pip install pyusb) | |
# python3 playdate-disk.py | |
import usb.core | |
import usb.util | |
# Playdate USB vendor and product IDs | |
PLAYDATE_VID = 0x1331; | |
PLAYDATE_PID = 0x5740; | |
IN_SIZE = 64; | |
def usb_connect(): | |
# find our playdate device | |
device = usb.core.find(idVendor=PLAYDATE_VID, idProduct=PLAYDATE_PID) | |
if device is None: | |
raise ValueError('Device not found') | |
# set the active configuration. With no arguments, the first | |
# configuration will be the active one | |
device.set_configuration() | |
# get an endpoint instance | |
cfg = device.get_active_configuration() | |
intf = cfg[(1,0)] | |
epOut = usb.util.find_descriptor( | |
intf, | |
# match the first OUT endpoint | |
custom_match = \ | |
lambda e: \ | |
usb.util.endpoint_direction(e.bEndpointAddress) == \ | |
usb.util.ENDPOINT_OUT) | |
epIn = usb.util.find_descriptor( | |
intf, | |
# match the first IN endpoint | |
custom_match = \ | |
lambda e: \ | |
usb.util.endpoint_direction(e.bEndpointAddress) == \ | |
usb.util.ENDPOINT_IN) | |
assert epOut is not None | |
assert epIn is not None | |
device.reset() | |
return epOut, epIn | |
def usb_read_bytes(endPoint): | |
res = bytearray() | |
has_started = False | |
while True: | |
try: | |
b = bytearray(epIn.read(IN_SIZE)) | |
res += b | |
if b != b'': has_started = True | |
if has_started and b == b'': break | |
except usb.core.USBTimeoutError: | |
break | |
return res | |
# connect to playdate over usb | |
print('finding playdate connected to usb...') | |
epOut, epIn = usb_connect() | |
print('successfully connected to playdate!') | |
# set usb echo mode to off | |
print('setting usb echo to off') | |
epOut.write('echo off\n') | |
resp = usb_read_bytes(epIn) | |
# send datadisk command - this causes the Playdate to expose its storage as though it's a USB flash drive | |
print('putting playdate into USB disk mode -- eject it to put it back') | |
epOut.write('datadisk\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment