Skip to content

Instantly share code, notes, and snippets.

@HavishNetla
Created July 26, 2019 21:23
Show Gist options
  • Save HavishNetla/7d7a50bc183a962a81c88356801e0d8e to your computer and use it in GitHub Desktop.
Save HavishNetla/7d7a50bc183a962a81c88356801e0d8e to your computer and use it in GitHub Desktop.
import socket
from contextlib import closing
import time
import threading
import numpy
from inputs import get_gamepad
HOST = "192.168.1.184" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
r1 = 0
r2 = 0
def main():
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP print(HOST + str(PORT))
print(sock.connect_ex(("192.168.1.253", 65432)))
while True:
global r1
global r2
print(r1)
sock.sendto(bytes(str(r1) + "," + str(r2)), (HOST, PORT))
time.sleep(0.11)
def gamepad_loop():
x1 = 0
x2 = 0
while True:
global r1
global r2
events = get_gamepad()
for event in events:
if event.code == 'ABS_Y':
x1 = -event.state / 32768.0
elif event.code == 'ABS_RX':
x2 = -event.state / 32768.0
r2 = str(
numpy.clip(
(round(x1, 2) * 100) + (round(x2, 2) * 100), -100, 100
)
)
r1 = str(
numpy.clip(
(round(x1, 2) * 100) - (round(x2, 2) * 100), -100, 100
)
)
if __name__ == "__main__":
x = threading.Thread(target=gamepad_loop)
x.start()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment