Skip to content

Instantly share code, notes, and snippets.

@HavishNetla
Created August 23, 2019 22:28
Show Gist options
  • Save HavishNetla/579ddc0e96080052f89adfa938d079b3 to your computer and use it in GitHub Desktop.
Save HavishNetla/579ddc0e96080052f89adfa938d079b3 to your computer and use it in GitHub Desktop.
#!usr/bin/env python
import socket
import time
import threading
import numpy
from inputs import devices
import math
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
r3 = 0
r4 = 0
r5 = 0
r6 = 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) + ',' + str(r3) + ',' + str(r4) + ',' + str(r5) + ',' + str(r6), 'utf-8'), (HOST, PORT))
time.sleep(0.11)
def gamepad_loop():
x1 = 0
x2 = 0
x3 = 0
x4 = 0
while True:
global r1
global r2
global r3
global r4
events = devices.gamepads[0].read()
for event in events:
if event.code == 'ABS_Y':
if abs(event.state / 32768) < -10000:
x1 = 0
else:
x1 = event.state / 32768.0
elif event.code == 'ABS_RX':
if abs(event.state / 32768) < -1000:
x2 = 0
else:
x2 = -event.state / 32768.0 * 0.5
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
)
)
def gamepad_loop1():
x1 = 0
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
while True:
global r1
global r2
global r3
global r4
global r5
global r6
events = devices.gamepads[1].read()
for event in events:
print(event.ev_type, event.code, event.state)
if event.code == 'ABS_Z':
x3 = event.state / 123
elif event.code == 'BTN_TL':
x4 = event.state
elif event.code == 'ABS_RZ':
x5 = event.state
elif event.code == 'BTN_TR':
x6 = event.state
r3 = str(numpy.clip(round(x3 * 100), -100, 100))
r4 = str(-numpy.clip(round(x4 * 100), -100, 100))
r5 = str(numpy.clip(round(x5 * 100), -100, 100))
r6 = str(-numpy.clip(round(x6 * 100), -100, 100))
if __name__ == "__main__":
x = threading.Thread(target=gamepad_loop)
y = threading.Thread(target=gamepad_loop1)
y.start()
x.start()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment