def get_ip_address(ifname): """ Returns the IPv4 address of the requested interface (thanks Martin Konecny, https://stackoverflow.com/a/24196955) :param string interface: The interface to get the IPv4 address of. :returns: The interface's IPv4 address. """ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa( fcntl.ioctl(s.fileno(), 0x8915, struct.pack("256s", bytes(ifname[:15], "utf-8")))[20:24]) # SIOCGIFADDR
def event_listener(): X = 0 Z = 0 # Iterate over the joystick devices. print('Available devices:') for fn in os.listdir('/dev/input'): if fn.startswith('js'): print(' /dev/input/%s' % (fn)) # We'll store the states here. axis_states = {} button_states = {} # These constants were borrowed from linux/input.h axis_map = [] button_map = [] # Open the joystick device. # fn = '/dev/input/js0' fn = '/dev/input/js1' # fn = '/dev/input/js2' print('Opening %s...' % fn) jsdev = open(fn, 'rb') # Get the device name. #buf = bytearray(63) buf = array.array('c', ['\0'] * 64) ioctl(jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len) js_name = buf.tostring() print('Device name: %s' % js_name) # Get number of axes and buttons. buf = array.array('B', [0]) ioctl(jsdev, 0x80016a11, buf) # JSIOCGAXES num_axes = buf[0] buf = array.array('B', [0]) ioctl(jsdev, 0x80016a12, buf) # JSIOCGBUTTONS num_buttons = buf[0] # Get the axis map. buf = array.array('B', [0] * 0x40) ioctl(jsdev, 0x80406a32, buf) # JSIOCGAXMAP for axis in buf[:num_axes]: axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis) axis_map.append(axis_name) axis_states[axis_name] = 0.0 # Get the button map. buf = array.array('H', [0] * 200) ioctl(jsdev, 0x80406a34, buf) # JSIOCGBTNMAP for btn in buf[:num_buttons]: btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn) button_map.append(btn_name) button_states[btn_name] = 0 print('%d axes found: %s' % (num_axes, ', '.join(axis_map))) print('%d buttons found: %s' % (num_buttons, ', '.join(button_map))) # Main event loop while True: print("\n") evbuf = jsdev.read(8) if evbuf: time, value, type, number = struct.unpack('IhBB', evbuf) if type & 0x80: print("(initial)", ) if type & 0x01: button = button_map[number] if button: button_states[button] = value if value: print("%s pressed" % (button)) else: print("%s released" % (button)) if ((("%s" % button) == 'base2') & (value == 0) & (not (type & 0x80))): s.close() print("Connection Closed!") exit() if type & 0x02: axis = axis_map[number] if axis: fvalue = value / 32767.0 axis_states[axis] = fvalue # print "%s: %.3f " % (axis, fvalue) if (axis == 'y') or (axis == 'rz'): X = 1000 * fvalue elif axis == 'z': Z = 1000 * fvalue T = "*%s| %.0f #" % (axis, 1000 * fvalue) # print X,Z motor_speed(X, Z)