X-Git-Url: https://scm.cri.ensmp.fr/git/minwii.git/blobdiff_plain/b1d5972142e87c4cc58e08e44cbd025cf5f6b26c..7e08f1784451775c1712a0584299e8f4fda00ae6:/src/pywiiuse/PyWiiUse.py diff --git a/src/pywiiuse/PyWiiUse.py b/src/pywiiuse/PyWiiUse.py index 55a1c98..c86d69b 100755 --- a/src/pywiiuse/PyWiiUse.py +++ b/src/pywiiuse/PyWiiUse.py @@ -12,8 +12,9 @@ hacked for new API and data June 2009 import os import ctypes -from ctypes import c_char_p, c_int, c_byte, c_uint, c_uint16, c_float, c_short, c_void_p, c_char +from ctypes import c_char_p, c_int, c_byte, c_uint, c_uint16, c_float, c_short, c_void_p, c_char, c_ubyte, c_ushort from ctypes import CFUNCTYPE, Structure, POINTER, Union, byref, cdll +from ctypes.util import find_library import sys # duplicate the wiiuse data structures @@ -37,6 +38,12 @@ class vec3b(_Structure): ('z', c_byte), ] +class vec3w(_Structure): + _fields_ = [('x', c_ushort), + ('y', c_ushort), + ('z', c_ushort), + ] + class vec3f(_Structure): _fields_ = [('x', c_float), ('y', c_float), @@ -56,8 +63,8 @@ class orient(_Structure): self.roll, self.pitch, self.yaw, self.a_roll, self.a_pitch) class accel(_Structure): - _fields_ = [('cal_zero', vec3b), - ('cal_g', vec3b), + _fields_ = [('cal_zero', vec3w), + ('cal_g', vec3w), ('st_roll', c_float), ('st_pitch', c_float), ('st_alpha', c_float), @@ -89,6 +96,16 @@ class ir(_Structure): ('z', c_float), ] + def __str__(self) : + l = [] + pr = l.append + for name, typ in self._fields_ : + try : + pr('(%s, %s)' % (name, getattr(self, name))) + except : + pass + return '\n'.join(l) + class joystick(_Structure): _fields_ = [('max', vec2b), ('min', vec2b), @@ -102,17 +119,19 @@ class nunchuk(_Structure): ('js', joystick), ('flags', POINTER(c_int)), ('btns', c_byte), + ('btns_last', c_byte), ('btns_held', c_byte), ('btns_released', c_byte), ('orient_threshold', c_float), - ('accel_threshold', c_int), - ('accel', vec3b), + ('accel_threshold', c_float), + ('accel', vec3w), ('orient', orient), ('gforce', vec3f), ] class classic_ctrl(_Structure): _fields_ = [('btns', c_short), + ('btns_last', c_short), ('btns_held', c_short), ('btns_released', c_short), ('r_shoulder', c_float), @@ -123,16 +142,26 @@ class classic_ctrl(_Structure): class guitar_hero_3(_Structure): _fields_ = [('btns', c_short), + ('btns_last', c_short), ('btns_held', c_short), ('btns_released', c_short), ('whammy_bar', c_float), ('js', joystick), ] +class motion_plus(_Structure): + _fields_ = [('rx', c_short), + ('ry', c_short), + ('rz', c_short), + ('status', c_ubyte), + ('ext', c_ubyte) + ] + class expansion_union(Union): _fields_ = [('nunchuk', nunchuk), ('classic', classic_ctrl), ('gh3', guitar_hero_3), + ('mp', motion_plus) ] class expansion(_Structure): @@ -145,17 +174,21 @@ class wiimote_state(_Structure): ('exp_rjs_ang', c_float), ('exp_ljs_mag', c_float), ('exp_rjs_mag', c_float), - ('exp_btns', c_uint16), + ('exp_btns', c_ushort), ('exp_orient', orient), - ('exp_accel', vec3b), + ('exp_accel', vec3w), ('exp_r_shoulder', c_float), ('exp_l_shoulder', c_float), + ('drx', c_short), + ('dry', c_short), + ('drz', c_short), ('ir_ax', c_int), ('ir_ay', c_int), ('ir_distance', c_float), ('orient', orient), - ('btns', c_uint16), + ('btns', c_ushort), ('accel', vec3b), + ('exp', expansion) ] if os.name == 'nt': @@ -166,6 +199,12 @@ if os.name == 'nt': ('normal_timeout', c_byte), ('exp_timeout', c_byte), ] + +elif sys.platform == 'darwin' : + JunkSkip = [('device', c_void_p), + ('bdaddr_str', c_char*18) + ] + else: JunkSkip = [('bdaddr', c_void_p), ('bdaddr_str', c_char*18), @@ -192,23 +231,37 @@ class wiimote(_Structure): ('state', c_int), ('leds', c_byte), ('battery_level', c_float), + ('flags', c_int), + ('handshake_state', c_byte), + ('expansion_state', c_ubyte), ('read_req', c_void_p), + ('data_req', c_void_p), + + ('cmd_head', c_void_p), + ('cmd_tail', c_void_p), ('accel_calib', accel), ('exp', expansion), - ('accel', vec3b), + + ('accel', vec3w), ('orient', orient), ('gforce', vec3f), + ('ir', ir), - ('btns', c_uint16), - ('btns_held', c_uint16), - ('btns_released', c_uint16), + + ('btns', c_ushort), + ('btns_last', c_ushort), + ('btns_held', c_ushort), + ('btns_released', c_ushort), ('orient_threshold', c_float), ('accel_threshold', c_int), + ('lstate', wiimote_state), + ('event', c_int), ('event_buf', c_byte*32), + ('motion_plus_id', c_ubyte*6) ] wiimote_p = POINTER(wiimote) @@ -239,10 +292,13 @@ min_val,max_val,min_loc,max_loc = cvMinMaxLoc(img) return CFUNCTYPE(result, *atypes)((name, dll), tuple(aflags)) # get the shared library -if os.name == 'nt': - dll = cdll.LoadLibrary('wiiuse.dll') -else: - dll = cdll.LoadLibrary('libwiiuse.so') +lib = find_library('wiiuse') or find_library('libwiiuse') +dll = cdll.LoadLibrary(lib) + +#if os.name == 'nt': +# dll = cdll.LoadLibrary('wiiuse.dll') +#else: +# dll = cdll.LoadLibrary('libwiiuse.so') # access the functions init = cfunc('wiiuse_init', dll, wiimote_pp, @@ -266,6 +322,9 @@ set_accel_threshold = dll.wiiuse_set_accel_threshold set_orient_threshold = dll.wiiuse_set_orient_threshold set_orient_threshold.argtypes = [wiimote_p, c_float] set_timeout = dll.wiiuse_set_timeout +set_ir = dll.wiiuse_set_ir +set_ir_position = dll.wiiuse_set_ir_position +set_ir_vres = dll.wiiuse_set_ir_vres def is_pressed(dev, button): return dev.btns & button @@ -332,7 +391,8 @@ nunchuk_button = { 'Z':0x01, if __name__ == '__main__': def handle_event(wm): print 'EVENT', wm.unid, wm.btns - print wm.gforce.x, wm.gforce.y, wm.gforce.z + #print wm.gforce.x, wm.gforce.y, wm.gforce.z + print wm.ir nmotes = 1 wiimotes = init(nmotes) @@ -349,13 +409,18 @@ if __name__ == '__main__': print 'failed to connect to any wiimote.' sys.exit(1) - set_leds(wiimotes[0], 0x50) + set_leds(wiimotes[0], 0x20) motion_sensing(wiimotes[0], 1) + set_ir(wiimotes[0], 1) while True: - if poll(wiimotes, nmotes): - for i in range(nmotes): - m = wiimotes[i][0] - if wiimotes[i][0].event == EVENT: - handle_event(wiimotes[i][0]) + try : + if poll(wiimotes, nmotes): + print '.' + for i in range(nmotes): + m = wiimotes[i][0] + if wiimotes[i][0].event == EVENT: + handle_event(wiimotes[i][0]) + except KeyboardInterrupt : + break