From d6d310fdd13c11382f37faca6a0c20b361ae9c40 Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Wed, 4 Apr 2018 10:42:51 -0600 Subject: Added python binding --- bindings/python/Makefile | 7 ++++ bindings/python/example.py | 13 +++++++ bindings/python/libsurvive.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 bindings/python/Makefile create mode 100644 bindings/python/example.py create mode 100644 bindings/python/libsurvive.py (limited to 'bindings/python') diff --git a/bindings/python/Makefile b/bindings/python/Makefile new file mode 100644 index 0000000..d3f846e --- /dev/null +++ b/bindings/python/Makefile @@ -0,0 +1,7 @@ +_libsurvive.so: ../../lib/libsurvive.so + cp ../../lib/libsurvive.so _libsurvive.so + +../../lib/libsurvive.so: .always + cd ../.. && make clean all + +.always: diff --git a/bindings/python/example.py b/bindings/python/example.py new file mode 100644 index 0000000..5454fbc --- /dev/null +++ b/bindings/python/example.py @@ -0,0 +1,13 @@ +import libsurvive +import sys + +actx = libsurvive.AsyncContext(sys.argv) + +for obj in actx.Objects(): + print(obj.Name()) + +while actx.Running(): + updated = actx.NextUpdated() + if updated: + print(updated.Name(), updated.Pose()) + diff --git a/bindings/python/libsurvive.py b/bindings/python/libsurvive.py new file mode 100644 index 0000000..1cf9b67 --- /dev/null +++ b/bindings/python/libsurvive.py @@ -0,0 +1,83 @@ +import ctypes + +_libsurvive = ctypes.CDLL('./_libsurvive.so') +LP_c_char = ctypes.POINTER(ctypes.c_char) +LP_LP_c_char = ctypes.POINTER(LP_c_char) + +_libsurvive.survive_async_init.argtypes = (ctypes.c_int, LP_LP_c_char) # argc, argv +_libsurvive.survive_async_init.restype = ctypes.c_void_p + +_libsurvive.survive_async_get_next_tracked.argtypes = [ctypes.c_void_p, ctypes.c_void_p] +_libsurvive.survive_async_get_next_tracked.restype = ctypes.c_void_p + +_libsurvive.survive_async_get_first_tracked.argtypes = [ctypes.c_void_p] +_libsurvive.survive_async_get_first_tracked.restype = ctypes.c_void_p + +_libsurvive.survive_async_get_next_updated.argtypes = [ctypes.c_void_p] +_libsurvive.survive_async_get_next_updated.restype = ctypes.c_void_p + +_libsurvive.survive_async_object_name.argtypes = [ ctypes.c_void_p ] +_libsurvive.survive_async_object_name.restype = ctypes.c_char_p + +_libsurvive.survive_async_is_running.argtypes = [ctypes.c_void_p] +_libsurvive.survive_async_is_running.restype = ctypes.c_bool + +_libsurvive.survive_async_start_thread.argtypes = [ctypes.c_void_p] +_libsurvive.survive_async_start_thread.restype = None + +class SurvivePose(ctypes.Structure): + _fields_ = [ + ('Pos', ctypes.c_double * 3), + ('Rot', ctypes.c_double * 4) + ] + def __repr__(self): + return '[{0} {1} {2}], [{3} {4} {5} {6}]'.format(self.Pos[0],self.Pos[1],self.Pos[2],self.Rot[0],self.Rot[1],self.Rot[2],self.Rot[3]) + + +_libsurvive.survive_async_object_get_latest_pose.argtypes = [ctypes.c_void_p, ctypes.POINTER(SurvivePose)] +_libsurvive.survive_async_object_get_latest_pose.restype = ctypes.c_uint + +class AsyncObject: + ptr = 0 + def __init__(self, ptr): + self.ptr = ptr + + def Name(self): + return _libsurvive.survive_async_object_name(self.ptr) + + def Pose(self): + pose = SurvivePose() + time = _libsurvive.survive_async_object_get_latest_pose(self.ptr, pose) + return (pose, time) + +class AsyncContext: + ptr = 0 + objects = [] + + def __init__(self, args): + argc = len(args) + argv = (LP_c_char * (argc + 1))() + for i, arg in enumerate(args): + enc_arg = arg.encode('utf-8') + argv[i] = ctypes.create_string_buffer(enc_arg) + self.ptr = _libsurvive.survive_async_init(argc, argv) + + self.objects = [] + curr = _libsurvive.survive_async_get_first_tracked(self.ptr); + while curr: + self.objects.append(AsyncObject(curr)) + curr = _libsurvive.survive_async_get_next_tracked(self.ptr, curr); + _libsurvive.survive_async_start_thread(self.ptr) + + def Objects(self): + return self.objects + + def Running(self): + return _libsurvive.survive_async_is_running(self.ptr) + + def NextUpdated(self): + ptr = _libsurvive.survive_async_get_next_updated(self.ptr) + if ptr: + return AsyncObject(ptr) + return None + -- cgit v1.2.3 From 0653df1da7364e75953eea85e92d23f2b41480be Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Thu, 5 Apr 2018 08:39:25 -0600 Subject: Fixed naming in python binding --- bindings/python/libsurvive.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'bindings/python') diff --git a/bindings/python/libsurvive.py b/bindings/python/libsurvive.py index 1cf9b67..e88657a 100644 --- a/bindings/python/libsurvive.py +++ b/bindings/python/libsurvive.py @@ -7,11 +7,11 @@ LP_LP_c_char = ctypes.POINTER(LP_c_char) _libsurvive.survive_async_init.argtypes = (ctypes.c_int, LP_LP_c_char) # argc, argv _libsurvive.survive_async_init.restype = ctypes.c_void_p -_libsurvive.survive_async_get_next_tracked.argtypes = [ctypes.c_void_p, ctypes.c_void_p] -_libsurvive.survive_async_get_next_tracked.restype = ctypes.c_void_p +_libsurvive.survive_async_get_next_object.argtypes = [ctypes.c_void_p, ctypes.c_void_p] +_libsurvive.survive_async_get_next_object.restype = ctypes.c_void_p -_libsurvive.survive_async_get_first_tracked.argtypes = [ctypes.c_void_p] -_libsurvive.survive_async_get_first_tracked.restype = ctypes.c_void_p +_libsurvive.survive_async_get_first_object.argtypes = [ctypes.c_void_p] +_libsurvive.survive_async_get_first_object.restype = ctypes.c_void_p _libsurvive.survive_async_get_next_updated.argtypes = [ctypes.c_void_p] _libsurvive.survive_async_get_next_updated.restype = ctypes.c_void_p @@ -63,10 +63,10 @@ class AsyncContext: self.ptr = _libsurvive.survive_async_init(argc, argv) self.objects = [] - curr = _libsurvive.survive_async_get_first_tracked(self.ptr); + curr = _libsurvive.survive_async_get_first_object(self.ptr); while curr: self.objects.append(AsyncObject(curr)) - curr = _libsurvive.survive_async_get_next_tracked(self.ptr, curr); + curr = _libsurvive.survive_async_get_next_object(self.ptr, curr); _libsurvive.survive_async_start_thread(self.ptr) def Objects(self): -- cgit v1.2.3 From 5f429662488533ff4f4384b678ae738244972cc6 Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Tue, 10 Apr 2018 23:48:36 -0600 Subject: Updated naming of python binding --- bindings/python/example.py | 2 +- bindings/python/libsurvive.py | 56 +++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 29 deletions(-) (limited to 'bindings/python') diff --git a/bindings/python/example.py b/bindings/python/example.py index 5454fbc..32d58de 100644 --- a/bindings/python/example.py +++ b/bindings/python/example.py @@ -1,7 +1,7 @@ import libsurvive import sys -actx = libsurvive.AsyncContext(sys.argv) +actx = libsurvive.SimpleContext(sys.argv) for obj in actx.Objects(): print(obj.Name()) diff --git a/bindings/python/libsurvive.py b/bindings/python/libsurvive.py index e88657a..653eb60 100644 --- a/bindings/python/libsurvive.py +++ b/bindings/python/libsurvive.py @@ -4,26 +4,26 @@ _libsurvive = ctypes.CDLL('./_libsurvive.so') LP_c_char = ctypes.POINTER(ctypes.c_char) LP_LP_c_char = ctypes.POINTER(LP_c_char) -_libsurvive.survive_async_init.argtypes = (ctypes.c_int, LP_LP_c_char) # argc, argv -_libsurvive.survive_async_init.restype = ctypes.c_void_p +_libsurvive.survive_simple_init.argtypes = (ctypes.c_int, LP_LP_c_char) # argc, argv +_libsurvive.survive_simple_init.restype = ctypes.c_void_p -_libsurvive.survive_async_get_next_object.argtypes = [ctypes.c_void_p, ctypes.c_void_p] -_libsurvive.survive_async_get_next_object.restype = ctypes.c_void_p +_libsurvive.survive_simple_get_next_object.argtypes = [ctypes.c_void_p, ctypes.c_void_p] +_libsurvive.survive_simple_get_next_object.restype = ctypes.c_void_p -_libsurvive.survive_async_get_first_object.argtypes = [ctypes.c_void_p] -_libsurvive.survive_async_get_first_object.restype = ctypes.c_void_p +_libsurvive.survive_simple_get_first_object.argtypes = [ctypes.c_void_p] +_libsurvive.survive_simple_get_first_object.restype = ctypes.c_void_p -_libsurvive.survive_async_get_next_updated.argtypes = [ctypes.c_void_p] -_libsurvive.survive_async_get_next_updated.restype = ctypes.c_void_p +_libsurvive.survive_simple_get_next_updated.argtypes = [ctypes.c_void_p] +_libsurvive.survive_simple_get_next_updated.restype = ctypes.c_void_p -_libsurvive.survive_async_object_name.argtypes = [ ctypes.c_void_p ] -_libsurvive.survive_async_object_name.restype = ctypes.c_char_p +_libsurvive.survive_simple_object_name.argtypes = [ ctypes.c_void_p ] +_libsurvive.survive_simple_object_name.restype = ctypes.c_char_p -_libsurvive.survive_async_is_running.argtypes = [ctypes.c_void_p] -_libsurvive.survive_async_is_running.restype = ctypes.c_bool +_libsurvive.survive_simple_is_running.argtypes = [ctypes.c_void_p] +_libsurvive.survive_simple_is_running.restype = ctypes.c_bool -_libsurvive.survive_async_start_thread.argtypes = [ctypes.c_void_p] -_libsurvive.survive_async_start_thread.restype = None +_libsurvive.survive_simple_start_thread.argtypes = [ctypes.c_void_p] +_libsurvive.survive_simple_start_thread.restype = None class SurvivePose(ctypes.Structure): _fields_ = [ @@ -34,23 +34,23 @@ class SurvivePose(ctypes.Structure): return '[{0} {1} {2}], [{3} {4} {5} {6}]'.format(self.Pos[0],self.Pos[1],self.Pos[2],self.Rot[0],self.Rot[1],self.Rot[2],self.Rot[3]) -_libsurvive.survive_async_object_get_latest_pose.argtypes = [ctypes.c_void_p, ctypes.POINTER(SurvivePose)] -_libsurvive.survive_async_object_get_latest_pose.restype = ctypes.c_uint +_libsurvive.survive_simple_object_get_latest_pose.argtypes = [ctypes.c_void_p, ctypes.POINTER(SurvivePose)] +_libsurvive.survive_simple_object_get_latest_pose.restype = ctypes.c_uint -class AsyncObject: +class SimpleObject: ptr = 0 def __init__(self, ptr): self.ptr = ptr def Name(self): - return _libsurvive.survive_async_object_name(self.ptr) + return _libsurvive.survive_simple_object_name(self.ptr) def Pose(self): pose = SurvivePose() - time = _libsurvive.survive_async_object_get_latest_pose(self.ptr, pose) + time = _libsurvive.survive_simple_object_get_latest_pose(self.ptr, pose) return (pose, time) -class AsyncContext: +class SimpleContext: ptr = 0 objects = [] @@ -60,24 +60,24 @@ class AsyncContext: for i, arg in enumerate(args): enc_arg = arg.encode('utf-8') argv[i] = ctypes.create_string_buffer(enc_arg) - self.ptr = _libsurvive.survive_async_init(argc, argv) + self.ptr = _libsurvive.survive_simple_init(argc, argv) self.objects = [] - curr = _libsurvive.survive_async_get_first_object(self.ptr); + curr = _libsurvive.survive_simple_get_first_object(self.ptr); while curr: - self.objects.append(AsyncObject(curr)) - curr = _libsurvive.survive_async_get_next_object(self.ptr, curr); - _libsurvive.survive_async_start_thread(self.ptr) + self.objects.append(SimpleObject(curr)) + curr = _libsurvive.survive_simple_get_next_object(self.ptr, curr); + _libsurvive.survive_simple_start_thread(self.ptr) def Objects(self): return self.objects def Running(self): - return _libsurvive.survive_async_is_running(self.ptr) + return _libsurvive.survive_simple_is_running(self.ptr) def NextUpdated(self): - ptr = _libsurvive.survive_async_get_next_updated(self.ptr) + ptr = _libsurvive.survive_simple_get_next_updated(self.ptr) if ptr: - return AsyncObject(ptr) + return SimpleObject(ptr) return None -- cgit v1.2.3