# This file provide an interface between the incomming serial data # and a virtual joystick device # you will need to change the comm port to the number of the device that your # enumerates to #It some times crashes if this program is run while data is flowing so simply re-run PSB_SELECT = 0x0001 PSB_L3 = 0x0002 PSB_R3 = 0x0004 PSB_START = 0x0008 PSB_PAD_UP = 0x0010 PSB_PAD_RIGHT = 0x0020 PSB_PAD_DOWN = 0x0040 PSB_PAD_LEFT = 0x0080 PSB_L2 = 0x0100 PSB_R2 = 0x0200 PSB_L1 = 0x0400 PSB_R1 = 0x0800 PSB_GREEN = 0x1000 PSB_RED = 0x2000 PSB_BLUE = 0x4000 PSB_PINK = 0x8000 PSB_TRIANGLE = 0x1000 PSB_CIRCLE = 0x2000 PSB_CROSS = 0x4000 PSB_SQUARE = 0x8000 #These are analog buttons PSAB_PAD_RIGHT = 9 PSAB_PAD_UP = 11 PSAB_PAD_DOWN = 12 PSAB_PAD_LEFT = 10 PSAB_L2 = 19 PSAB_R2 = 20 PSAB_L1 = 17 PSAB_R1 = 18 PSAB_GREEN = 13 PSAB_RED = 14 PSAB_BLUE = 15 PSAB_PINK = 16 PSAB_TRIANGLE = 13 PSAB_CIRCLE = 14 PSAB_CROSS = 15 PSAB_SQUARE = 16 #These are stick values PSS_RX = 5 PSS_RY = 6 PSS_LX = 7 PSS_LY = 8 import pyvjoy from time import sleep import serial from textwrap import wrap ser = serial.Serial('COM8',500000,timeout=None) print(ser.name) j = pyvjoy.VJoyDevice(1) while True: GamePadData = ser.readline() #print GamePadData GamePadDataArr = (wrap(GamePadData, 2)) #print GamePadDataArr buttonData = (int(GamePadDataArr[4],16) * 256) + int(GamePadDataArr[3],16) Butts = 0 #used to map buttons recieved from PS2x to value required by the vJoy driver #not all buttons are mapped if ((buttonData & PSB_TRIANGLE) == 0): Butts = Butts + 1 if ((buttonData & PSB_CIRCLE) == 0): Butts = Butts + 2 if ((buttonData & PSB_CROSS) == 0): Butts = Butts + 4 if ((buttonData & PSB_SQUARE) == 0): Butts = Butts + 8 #print Butts # scale up the 0 - 255 PS2x values to 0 - 0x8000 j.data.wAxisX = 128 * int(GamePadDataArr[PSS_LX],16) j.data.wAxisY = 128 * int(GamePadDataArr[PSS_LY],16) j.data.wAxisZ= 0x2500 # static test value j.data.wAxisXRot = 128 * int(GamePadDataArr[PSS_RX],16) j.data.wAxisYRot = 128 * int(GamePadDataArr[PSS_RY],16) j.data.wAxisZRot = 0x5500 #static test value j.data.wSlider= 0x6500 #send data to vJoy devicewDial j.data.wDial= 0x7500 j.data.lButtons = Butts j.update()