Created
October 25, 2020 17:06
-
-
Save pringithub/d148b9e34908a5047131039608874a7d to your computer and use it in GitHub Desktop.
PS1 Controller Interface
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
import os | |
import serial | |
import numpy as np | |
btns = np.zeros(19) | |
# data sent in two-byte chunks | |
# first 10 bits map to shapes, sel/start, and trigger buttons | |
# next 4 bit map to D-Pad, but not 1:1; can press up-left, down-right, etc | |
# - 9 total combinations for the D-Pad | |
# next 2 bits are unused | |
dev = os.open("/dev/hidraw2", os.O_RDWR) | |
while True: | |
read_val = os.read(dev, 2) | |
fbyte = ord(read_val[0]) | |
sbyte = ord(read_val[1]) | |
#print(hex(fbyte)+' '+hex(sbyte)) | |
btns = np.zeros(19) | |
if (fbyte & 0x01): btns[0] = 1 # U | |
if (fbyte & 0x02): btns[1] = 1 # R | |
if (fbyte & 0x04): btns[2] = 1 # D | |
if (fbyte & 0x08): btns[3] = 1 # L | |
if (fbyte & 0x10): btns[4] = 1 # LT | |
if (fbyte & 0x20): btns[5] = 1 # RT | |
if (fbyte & 0x40): btns[6] = 1 # LB | |
if (fbyte & 0x80): btns[7] = 1 # RB | |
# | |
if (sbyte & 0x01): btns[8] = 1 # SEL | |
if (sbyte & 0x02): btns[9] = 1 # START | |
udlr = sbyte & 0xfc | |
btns[10] = udlr is 0x14 # center | |
btns[11] = udlr is 0x04 # up | |
btns[12] = udlr is 0x24 # down | |
btns[13] = udlr is 0x10 # left | |
btns[14] = udlr is 0x18 # right | |
btns[15] = udlr is 0x00 # up-left | |
btns[16] = udlr is 0x20 # down-left | |
btns[17] = udlr is 0x08 # up-right | |
btns[18] = udlr is 0x28 # down-right | |
#print(btns) | |
print("ST: {} SEL: {} LT: {} RT: {} LB: {} RB: {} U: {} D: {} L: {} R: {} C: {} U: {} D: {} L: {} R:{} UL: {} DL: {} UR: {} DR: {} ".format( | |
btns[9], btns[8], btns[4], btns[5], btns[6], btns[7], btns[0], btns[2], btns[3], btns[1], | |
btns[10], btns[11], btns[12], btns[13], btns[14], btns[15], btns[16], btns[17], btns[18] )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment