Last active
July 19, 2021 12:55
-
-
Save VLTNOgithub/00e23fe444ee18ce82f6f08ecc5fee72 to your computer and use it in GitHub Desktop.
Thumbstick code for pc
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 serial | |
import pydirectinput | |
arduino = serial.Serial('COM13', 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 handlethumbstickAsArrowKeys | |
keysDown[key] = True # Adds key to KeysDown list | |
pydirectinput.keyDown(key) # Runs pydirectinput using key from (argument) | |
def keyUp(key): # What to do if key released. takes value from handlethumbstickAsArrowKeys | |
if key in keysDown: | |
del (keysDown[key]) # Remove key from KeysDown | |
pydirectinput.keyUp(key) # Runs pydirectinput using key from (argument) | |
def handleThumbstickAsArrowKeys(x, y): # Note that the x and y directions are swapped due to the way I orient my thumbstick | |
if x == 0: #0 is up on thumbstick | |
keyDown('w') # Add up key to keyDown (argument) | |
keyUp('s') # Add down key to keyUp (argument), as you can't press up and down together | |
elif x == 2: #2 is down on thumbstick | |
keyDown('s') | |
keyUp('w') | |
else: #1 is neutral on thumbstick | |
keyUp('w') | |
keyUp('s') | |
if y == 2: #2 is right on thumbstick | |
keyDown('d') | |
keyUp('a') | |
elif y == 0: #0 is left on thumbstick | |
keyDown('a') | |
keyUp('d') | |
else: #1 is neutral on thumbstick | |
keyUp('a') | |
keyUp('d') | |
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 | |
handleThumbstickAsArrowKeys(dx, dy) # Run body of code using dx, dy and JSButton as inputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment