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


ps3joy
Author(s): Blaise Gassend, pascal@pabr.org, Melonee Wise
autogenerated on Fri Jun 7 2019 22:01:31