ps3joysim.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2009, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of the Willow Garage nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 from __future__ import print_function
35 from bluetooth import *
36 import select
37 import fcntl
38 import os
39 import struct
40 import time
41 import sys
42 import traceback
43 import threading
44 import ps3joy
45 import socket
46 import signal
47 
48 
50  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
51  sock.bind(("127.0.0.1", 0))
52  sock.listen(1)
53  return sock, sock.getsockname()[1]
54 
55 
56 # Class to spawn the ps3joy.py infrastructure in its own thread
57 class driversim(threading.Thread):
58  def __init__(self, intr, ctrl):
59  threading.Thread.__init__(self)
60  self.intr = intr
61  self.ctrl = ctrl
62  self.start()
63 
64  def run(self):
66  self.cm.listen(self.intr, self.ctrl)
67  print("driversim exiting")
68 
69  def shutdown(self):
70  self.cm.shutdown = True
71 
72 
73 class joysim(threading.Thread):
74  def __init__(self, intr, ctrl):
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))
79  if self.intr == -1:
80  raise "Error creating interrput socket."
81  self.ctrl = socket.socket()
82  self.ctrl.connect(("127.0.0.1", ctrl))
83  if self.ctrl == -1:
84  raise "Error creating control socket."
85  self.active = False
86  self.shutdown = False
87  self.start()
88 
89  def run(self):
90  while not self.active and not self.shutdown:
91  (rd, wr, err) = select.select([self.ctrl], [], [], 1)
92  if len(rd) == 1:
93  cmd = self.ctrl.recv(128)
94  if cmd == "\x53\xf4\x42\x03\x00\x00":
95  self.active = True
96  print("Got activate command")
97  else:
98  print("Got unknown command (len=%i)" % len(cmd), end=' ')
99  time.sleep(1)
100  for c in cmd:
101  print("%x" % ord(c), end=' ')
102  print()
103  print("joyactivate exiting")
104 
105  def publishstate(self, ax, butt):
106  if self.active:
107  ranges = [255] * 17 + [8191] * 20
108  axval = [int((v + 1) * s / 2) for (v, s) in zip(ax, ranges)]
109  buttout = []
110  for i in range(0, 2):
111  newval = 0
112  for j in range(0, 8):
113  newval = (newval << 1)
114  if butt[i * 8 + j]:
115  newval = newval + 1
116  buttout.append(newval)
117  joy_coding = "!1B2x3B1x4B4x12B15x4H"
118  self.intr.send(struct.pack(joy_coding, 161, *(buttout + [0] + axval)))
119  else:
120  print("Tried to publish while inactive")
121 
122 
123 if __name__ == "__main__":
124  def stop_all_threads(a, b):
125  exit(0)
126 
127  signal.signal(signal.SIGINT, stop_all_threads)
128 
129  # Create sockets for the driver side and pass them to the driver
130  (intr_in, intr_port) = mk_in_socket()
131  (ctrl_in, ctrl_port) = mk_in_socket()
132 
133  ds = driversim(intr_in, ctrl_in)
134 
135  # Give the simulator a chance to get going
136  time.sleep(2)
137 
138  # Call up the simulator telling it which ports to connect to.
139  js = joysim(intr_port, ctrl_port)
140  buttons1 = [True] * 16
141  axes1 = [1, 0, -1, .5] * 5
142  buttons2 = [False] * 16
143  axes2 = [-1] * 20
144  buttons3 = [False] * 16
145  axes3 = [0] * 20
146  shutdown = False
147  while not js.active and not shutdown:
148  time.sleep(0.2)
149  time.sleep(0.01)
150  time.sleep(0.01)
151  while not shutdown:
152  js.publishstate(axes1, buttons2)
153  time.sleep(0.01)
154  axes1[0] = -axes1[0]
155  js.publishstate(axes2, buttons2)
156  time.sleep(0.01)
157 
158  print("main exiting")
ps3joysim.driversim.__init__
def __init__(self, intr, ctrl)
Definition: ps3joysim.py:58
ps3joysim.joysim.run
def run(self)
Definition: ps3joysim.py:89
ps3joysim.joysim.__init__
def __init__(self, intr, ctrl)
Definition: ps3joysim.py:74
ps3joysim.joysim.publishstate
def publishstate(self, ax, butt)
Definition: ps3joysim.py:105
ps3joy.decoder
Definition: ps3joy.py:141
ps3joysim.driversim.ctrl
ctrl
Definition: ps3joysim.py:61
ps3joysim.joysim
Definition: ps3joysim.py:73
ps3joysim.driversim.run
def run(self)
Definition: ps3joysim.py:64
ps3joysim.joysim.ctrl
ctrl
Definition: ps3joysim.py:81
ps3joysim.joysim.shutdown
shutdown
Definition: ps3joysim.py:86
ps3joysim.joysim.active
active
Definition: ps3joysim.py:85
ps3joysim.stop_all_threads
def stop_all_threads(a, b)
Definition: ps3joysim.py:124
ps3joysim.driversim.shutdown
def shutdown(self)
Definition: ps3joysim.py:69
ps3joy.connection_manager
Definition: ps3joy.py:272
ps3joysim.driversim
Definition: ps3joysim.py:57
ps3joysim.driversim.cm
cm
Definition: ps3joysim.py:65
ps3joysim.driversim.intr
intr
Definition: ps3joysim.py:60
ps3joysim.mk_in_socket
def mk_in_socket()
Definition: ps3joysim.py:49
ps3joysim.joysim.intr
intr
Definition: ps3joysim.py:77


ps3joy
Author(s): Blaise Gassend, pascal@pabr.org, Melonee Wise
autogenerated on Sun May 2 2021 02:17:55