Skip to content

Instantly share code, notes, and snippets.

@hsk
Last active May 23, 2024 03:49
Show Gist options
  • Save hsk/97f8b1341064c36c71f9b32280e58380 to your computer and use it in GitHub Desktop.
Save hsk/97f8b1341064c36c71f9b32280e58380 to your computer and use it in GitHub Desktop.
MDMiniPadKeyMapper
# read https://qiita.com/wisteria/items/5c7c218bb0b3dfce984b
import usb.core, usb.util, pyautogui, time
class MDMiniPad:
def __init__(self):
self.dev = usb.core.find(idVendor=0x0ca3,idProduct=0x0024)
self.ep = usb.util.find_descriptor(
self.dev.get_active_configuration()[(0,0)],
custom_match = lambda e:
usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
interface = 0
if self.dev.is_kernel_driver_active(interface):
self.dev.detach_kernel_driver(interface)
usb.util.claim_interface(self.dev, interface)
def read(self):
data = self.dev.read(self.ep.bEndpointAddress,self.ep.wMaxPacketSize)
return {
"left": data[3]==0,
"right":data[3]==255,
"up": data[4]==0,
"down": data[4]==255,
"a": bool(data[5]&64),
"b": bool(data[5]&32),
"c": bool(data[6]&2),
"x": bool(data[5]&128),
"y": bool(data[5]&16),
"z": bool(data[6]&1),
"start": bool(data[6]&32),
"mode": bool(data[6]&16),
}
class MDMiniPadKeyMapper:
def __init__(self):
pyautogui.PAUSE = 0.0001
self.pad = MDMiniPad()
self.last = {"left":False,"right":False,"up":False,"down":False,"a":False,"b":False,"c":False,"x":False,"y":False,"z":False,"start":False,"mode":False}
#self.setting = {"left":"Left","right":"Right","up":"Up","down":"Down","a":"a","b":"b","c":"c","x":"x","y":"y","z":"z","start":"enter","mode":"shift"}
self.setting = {"left":"q","right":"e","up":"2","down":"w","a":"a","b":"b","c":"c","x":"x","y":"y","z":"z","start":"enter","mode":"shift"}
def loop(self):
now = self.pad.read()
for k,v in self.last.items():
if v != now[k]:
#print(f"k {k}")
if now[k]: pyautogui.keyDown(self.setting[k])
else: pyautogui.keyUp(self.setting[k])
self.last = now
if __name__=="__main__":
import psutil
pad = MDMiniPadKeyMapper()
while True:
pad.loop()
time.sleep(0.001)
psutil.Process().nice(-20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment