Exemplo n.º 1
0
    def __init__(self, dev_num=0, send_socket_port=63636, rec_socket_port=63637, socket_ip="localhost"):
        self.send_socket = TCPSendSocket(tcp_port=send_socket_port, tcp_ip=socket_ip, send_type=JSON)
        self.rec_socket = TCPReceiveSocket(tcp_port=rec_socket_port, tcp_ip=socket_ip, handler_function=self.on_receive)

        joysticks = XInputJoystick.enumerate_devices()
        device_numbers = list(map(attrgetter('device_number'), joysticks))

        print('found %d devices: %s' % (len(joysticks), device_numbers))

        if not joysticks:
            sys.exit(0)

        self.joystick = joysticks[dev_num]
        print('using %d' % self.joystick.device_number)

        @self.joystick.event
        def on_button(button, pressed):
            self.send_socket.send_data([button, pressed])

        @self.joystick.event
        def on_axis(axis, value):
            self.send_socket.send_data([axis, value])
Exemplo n.º 2
0
def receiving_function():
    num_messages_received = [0]

    # function to run when a new piece of data is received
    def print_value(data):
        print("value recieved: ", data)
        num_messages_received[0] = 1 + num_messages_received[0]

    rec_socket = TCPReceiveSocket(tcp_port=port, handler_function=print_value)
    rec_socket.start(blocking=True)

    while num_messages_received[0] < number_of_messages:
        # add delay so this loop does not unnecessarily tax the CPU
        time.sleep(0.25)

    print("closing receive socket.")
    rec_socket.stop()
Exemplo n.º 3
0

# define function to print the echo back from matlab
def print_data(data):
    global start_time
    now = time.time()
    print('length of returned array:',
          np.frombuffer(data, dtype='float32').shape[0], 'Time for full trip:',
          now - start_time)


# create a send and receive socket
send_socket = TCPSendSocket(tcp_port=send_port, tcp_ip='', send_type=RAW)
rec_socket = TCPReceiveSocket(tcp_port=receive_port,
                              handler_function=print_data,
                              receive_as_raw=True,
                              as_server=True,
                              receive_buffer_size=65536)

# start the sockets
send_socket.start()
rec_socket.start()

stop_flag = threading.Event()


def send_sig():
    global start_time
    while not stop_flag.is_set():
        data = np.random.random(
            (100, 100))  # create 100x100 array of random numbers
Exemplo n.º 4
0
import sys

send_port = 4242
rec_port = 4242
ip = '127.0.0.1'


# define function to print the echo back from matlab
def print_data(data):
    print(data, "unpacked:", struct.unpack('ff', data))


# create a send and receive socket
send_socket = TCPSendSocket(tcp_port=send_port, tcp_ip=ip, send_type=RAW)
receive_socket = TCPReceiveSocket(tcp_port=rec_port,
                                  tcp_ip=ip,
                                  receive_as_raw=True,
                                  handler_function=print_data)

# start the sockets
send_socket.start()
receive_socket.start()
stop_flag = threading.Event()


def send_sig():
    while not stop_flag.is_set():
        data = np.random.random((1, 2)).tolist()[0]
        data_as_bytes = struct.pack('ff', *data)
        send_socket.send_data(data_as_bytes)
        time.sleep(0.5)
Exemplo n.º 5
0
 def __init__(self, tcp_port, tcp_ip='localhost'):
     super().__init__()
     self.socket = TCPReceiveSocket(tcp_ip=tcp_ip,
                                    tcp_port=tcp_port,
                                    handler_function=self._data_received)
Exemplo n.º 6
0
import sys

send_port = 4242
receive_port = 4343
ip = '0.0.0.0'


# define function to print the echo back from matlab
def print_data(data):
    print(data)


# create a send and receive socket
send_socket = TCPSendSocket(tcp_port=send_port, tcp_ip='', send_type=JSON)
rec_socket = TCPReceiveSocket(tcp_port=receive_port,
                              handler_function=print_data,
                              receive_as_raw=False)

# start the sockets
send_socket.start()
rec_socket.start()

stop_flag = threading.Event()


def send_sig():
    while not stop_flag.is_set():
        send_socket.send_data({'data': np.random.random((4, 4)).tolist()})
        time.sleep(0.5)