<reponame>utkarshchauhan021/Joystick-PC-Controller
import serial
import pydirectinput

arduino = serial.Serial('COM3', 115200, timeout=.1)     #serial input from arduino. change COM port to wherever your arduino is connected

pydirectinput.PAUSE = 0

keysDown = {}   #list of currently pressed keys


def keyDown(key):               #what to do if key pressed. takes value from handleJoyStickAsArrowKeys
    keysDown[key] = True        #adds key to KeysDown listi
    pydirectinput.keyDown(key)  #runs pydirectinput using kiey from (argument)
    #print('Down: ', key)       #remove '#' from print to teist data streami

def keyUp(key):                     #what to do if key released. takes value from handleJoyStickAsArrowKeys
    if key in keysDown:
        del (keysDown[key])         #remove key from KeysDown
        pydirectinput.keyUp(key)    #runs pydirectinput using key from (argument)
        #print('Up: ', key)         #remove '#' from print to test data stream


def handleJoyStickAsArrowKeys(x, y, z):      #note that the x and y directions are swapped due to the way I orient my thumbstick
    if x == 0:          #0 is up on joystick
        keyDown('up')   #add up key to keyDown (argument)
        keyUp('down')   #add down key to keyUp (argument), as you can't press up and down together
    elif x == 2:        #2 is down on joystick
        keyDown('down')
        keyUp('up')
    else:               #1 is neutral on joystick
        keyUp('up')
        keyUp('down')

    if y == 2:          #2 is right on joystick
        keyDown('right')
        keyUp('left')
    elif y == 0:        #0 is left on joystick
        keyDown('left')
        keyUp('right')
    else:               #1 is neutral on joystick
        keyUp('left')
        keyUp('right')

    if z == 1:          #z argument is JSButton in this case. 1 is button pressed
        keyDown('d')    #key to be pressed with Joystick button. Change to any key
    else:
        keyUp('d')      #0 is button not pressed

while True:
    rawdata = arduino.readline()            #read serial data from arduino one line at a time
    data = str(rawdata.decode('utf-8'))     #decode the raw byte data into UTF-8
    if data.startswith("S"):                #make sure the read starts in the correct place
        dx = int(data[1])                   #X direction is second digit in data (data[0] is 'S')
        dy = int(data[3])                   #Y direction is fourth digit in data
        JSButton = int(data[5])             #JSButton is sixth digit in data
        #print(dx, dy, JSButton)            #remove '#' from print to test data stream
        handleJoyStickAsArrowKeys(dx, dy, JSButton)     #run body of code using dx, dy and JSButton as inputs

import serial
import pydirectinput

arduino = serial.Serial('COM3', 115200, timeout=.1)     #serial input from arduino. change COM port to wherever your arduino is connected

pydirectinput.PAUSE = 0

keysDown = {}   #list of currently pressed keys


def keyDown(key):               #what to do if key pressed. takes value from handleJoyStickAsArrowKeys
    keysDown[key] = True        #adds key to KeysDown listi
    pydirectinput.keyDown(key)  #runs pydirectinput using kiey from (argument)
    #print('Down: ', key)       #remove '#' from print to teist data streami

def keyUp(key):                     #what to do if key released. takes value from handleJoyStickAsArrowKeys
    if key in keysDown:
        del (keysDown[key])         #remove key from KeysDown
        pydirectinput.keyUp(key)    #runs pydirectinput using key from (argument)
        #print('Up: ', key)         #remove '#' from print to test data stream


def handleJoyStickAsArrowKeys(x, y, z):      #note that the x and y directions are swapped due to the way I orient my thumbstick
    if x == 0:          #0 is up on joystick
        keyDown('up')   #add up key to keyDown (argument)
        keyUp('down')   #add down key to keyUp (argument), as you can't press up and down together
    elif x == 2:        #2 is down on joystick
        keyDown('down')
        keyUp('up')
    else:               #1 is neutral on joystick
        keyUp('up')
        keyUp('down')

    if y == 2:          #2 is right on joystick
        keyDown('right')
        keyUp('left')
    elif y == 0:        #0 is left on joystick
        keyDown('left')
        keyUp('right')
    else:               #1 is neutral on joystick
        keyUp('left')
        keyUp('right')

    if z == 1:          #z argument is JSButton in this case. 1 is button pressed
        keyDown('d')    #key to be pressed with Joystick button. Change to any key
    else:
        keyUp('d')      #0 is button not pressed

while True:
    rawdata = arduino.readline()            #read serial data from arduino one line at a time
    data = str(rawdata.decode('utf-8'))     #decode the raw byte data into UTF-8
    if data.startswith("S"):                #make sure the read starts in the correct place
        dx = int(data[1])                   #X direction is second digit in data (data[0] is 'S')
        dy = int(data[3])                   #Y direction is fourth digit in data
        JSButton = int(data[5])             #JSButton is sixth digit in data
        #print(dx, dy, JSButton)            #remove '#' from print to test data stream
        handleJoyStickAsArrowKeys(dx, dy, JSButton)     #run body of code using dx, dy and JSButton as inputs
#serial input from arduino. change COM port to wherever your arduino is connected #list of currently pressed keys #what to do if key pressed. takes value from handleJoyStickAsArrowKeys #adds key to KeysDown listi #runs pydirectinput using kiey from (argument) #print('Down: ', key)       #remove '#' from print to teist data streami #what to do if key released. takes value from handleJoyStickAsArrowKeys #remove key from KeysDown #runs pydirectinput using key from (argument) #print('Up: ', key)         #remove '#' from print to test data stream #note that the x and y directions are swapped due to the way I orient my thumbstick #0 is up on joystick #add up key to keyDown (argument) #add down key to keyUp (argument), as you can't press up and down together #2 is down on joystick #1 is neutral on joystick #2 is right on joystick #0 is left on joystick #1 is neutral on joystick #z argument is JSButton in this case. 1 is button pressed #key to be pressed with Joystick button. Change to any key #0 is button not pressed #read serial data from arduino one line at a time #decode the raw byte data into UTF-8 #make sure the read starts in the correct place #X direction is second digit in data (data[0] is 'S') #Y direction is fourth digit in data #JSButton is sixth digit in data #print(dx, dy, JSButton)            #remove '#' from print to test data stream #run body of code using dx, dy and JSButton as inputs