34 from __future__
import print_function
35 from bluetooth
import *
50 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
51 sock.bind((
"127.0.0.1", 0))
53 return sock, sock.getsockname()[1]
59 threading.Thread.__init__(self)
67 print(
"driversim exiting")
70 self.
cm.shutdown =
True
75 threading.Thread.__init__(self)
76 print(
"Starting joystick simulator on ports", intr,
"and", ctrl)
77 self.
intr = socket.socket()
78 self.
intr.connect((
"127.0.0.1", intr))
80 raise "Error creating interrput socket."
81 self.
ctrl = socket.socket()
82 self.
ctrl.connect((
"127.0.0.1", ctrl))
84 raise "Error creating control socket."
91 (rd, wr, err) = select.select([self.
ctrl], [], [], 1)
93 cmd = self.
ctrl.recv(128)
94 if cmd ==
"\x53\xf4\x42\x03\x00\x00":
96 print(
"Got activate command")
98 print(
"Got unknown command (len=%i)" % len(cmd), end=
' ')
101 print(
"%x" % ord(c), end=
' ')
103 print(
"joyactivate exiting")
107 ranges = [255] * 17 + [8191] * 20
108 axval = [int((v + 1) * s / 2)
for (v, s)
in zip(ax, ranges)]
110 for i
in range(0, 2):
112 for j
in range(0, 8):
113 newval = (newval << 1)
116 buttout.append(newval)
117 joy_coding =
"!1B2x3B1x4B4x12B15x4H"
118 self.
intr.send(struct.pack(joy_coding, 161, *(buttout + [0] + axval)))
120 print(
"Tried to publish while inactive")
123 if __name__ ==
"__main__":
127 signal.signal(signal.SIGINT, stop_all_threads)
140 buttons1 = [
True] * 16
141 axes1 = [1, 0, -1, .5] * 5
142 buttons2 = [
False] * 16
144 buttons3 = [
False] * 16
147 while not js.active
and not shutdown:
152 js.publishstate(axes1, buttons2)
155 js.publishstate(axes2, buttons2)
158 print(
"main exiting")