From a06951ed16b745efd89c1f43ae1291490622eebc Mon Sep 17 00:00:00 2001 From: Arnaud Blanchard <arnaud.blanchard@ensea.fr> Date: Fri, 15 Oct 2021 01:50:17 +0200 Subject: [PATCH] Fix c linkage --- python/blc_array.py | 2 +- python/blc_channel.py | 106 ++++++++++-------------------------------- python/blc_network.py | 4 +- 3 files changed, 28 insertions(+), 84 deletions(-) diff --git a/python/blc_array.py b/python/blc_array.py index 8fb3a03..f6bf0c2 100644 --- a/python/blc_array.py +++ b/python/blc_array.py @@ -54,7 +54,7 @@ class BlcArray: def get_def(self): self.type=lib.blc_array_get_type(self.pointer).to_bytes(4, "big").decode() self.ctype = BlcArray.types[self.type] - self.format=lib.blc_array_get_format(self.pointer) + self.format=lib.blc_array_get_format(self.pointer).to_bytes(4, "big").decode() dims_nb=lib.blc_array_get_dims_nb(self.pointer) diff --git a/python/blc_channel.py b/python/blc_channel.py index 0ae6c5a..f0c2e75 100644 --- a/python/blc_channel.py +++ b/python/blc_channel.py @@ -2,12 +2,9 @@ import os import ctypes from ctypes import * from ctypes.util import find_library +from blc_array import BlcArray - -class BlcDim(Structure): - _fields_ = [("length", c_size_t), ("step", c_size_t)] - -class BlcChannel: +class BlcChannel(BlcArray): """ blc_channel wrapper. """ @@ -15,23 +12,16 @@ class BlcChannel: lib_path = find_library("blc") lib = CDLL(lib_path) lib.blc_channel_new.restype = c_void_p + lib.blc_channel_new_open.restype = c_void_p + lib.blc_channel_delete.argtypes = [ c_void_p ] - lib.blc_channel_get_data.argtypes = [ c_void_p ] - lib.blc_channel_get_data.restype = c_void_p - lib.blc_channel_get_type.argtypes = [ c_void_p ] - lib.blc_channel_get_type.restype = c_uint32 - lib.blc_channel_get_format.argtypes = [ c_void_p ] - lib.blc_channel_get_format.restype = c_uint32 - lib.blc_channel_get_dims_nb.argtypes= [ c_void_p ] - lib.blc_channel_get_dims_nb.restype = c_int - lib.blc_channel_get_dims.argtypes = [ c_void_p ] + lib.blc_channel_wait_new_data.argtypes = [ c_void_p ] lib.blc_channel_wait_ack_data.argtypes = [ c_void_p ] lib.blc_channel_post_new_data.argtypes = [ c_void_p ] lib.blc_channel_post_ack_data.argtypes = [ c_void_p ] lib.blc_channel_remove.argtypes = [ c_void_p ] - types={ 'UIN8': c_uint8, 'INT8': c_int8, 'UI16': c_uint16, 'INT16': c_int16, 'UI32': c_uint32, 'IN32': c_int32, 'UI64': c_uint64, 'IN64': c_int64, 'FL32': c_float, 'FL64': c_double} def __init__( @@ -42,95 +32,47 @@ class BlcChannel: :param name: Name of the blc_channel (with sync/async identifier). Example: "/data" :param mode: RW mode. Example: os.O_RDWR - TODO - """ - self.name = name - self.mode = mode - self.type = data_type - self.format = format_type - self.dims = dims + TODO """ if (dims): #Creates - - dim_types = [c_int] * len(dims) - - BlcChannel.lib.blc_channel_new.argtypes = [ - c_char_p, - c_int, - c_uint32, - c_uint32, - c_int, - *dim_types - ] + dim_types = [c_size_t] * len(dims) + BlcChannel.lib.blc_channel_new.argtypes = [ c_char_p, c_int, c_uint32, c_uint32, c_int, *dim_types ] + type_int = int.from_bytes(data_type.encode(), "big") + format_int = int.from_bytes(format_type.encode(), "big") # Create a new channel - self.channel = BlcChannel.lib.blc_channel_new( - name.encode(), - mode, - int.from_bytes(data_type.encode(), "big"), - int.from_bytes(format_type.encode(), "big"), - len(dims), - *dims - ) + self.pointer = BlcChannel.lib.blc_channel_new(name.encode(), mode, type_int, format_int, len(dims), *dims) else: #Open - BlcChannel.lib.blc_channel_new.argtypes = [ - c_char_p, - c_int, - c_uint32, - c_uint32, - c_int, - c_int - ] - self.channel = BlcChannel.lib.blc_channel_new( - name.encode(), - mode, - 0, - 0, - 0, - 0 - ) - self.type=BlcChannel.lib.blc_channel_get_type(self.channel).to_bytes(4, "big").decode() - self.format=BlcChannel.lib.blc_channel_get_format(self.channel).to_bytes(4, "big").decode() - - dims_nb=BlcChannel.lib.blc_channel_get_dims_nb(self.channel) - BlcChannel.lib.blc_channel_get_dims.restype = POINTER(BlcDim * dims_nb) - blc_dims=BlcChannel.lib.blc_channel_get_dims(self.channel) - dims=[] - for i in range(dims_nb): - dims.append(blc_dims[0][i].length) #dims[i]= + BlcChannel.lib.blc_channel_new_open.argtypes = [ c_char_p, c_int ] + self.pointer = BlcChannel.lib.blc_channel_new_open( name.encode(), mode ) - # Specify types for channel - self.total_length=1 - for length in dims: - self.total_length *= length + self.get_def() - self.ctype = BlcChannel.types[self.type] - self.array = cast(BlcChannel.lib.blc_channel_get_data(self.channel), POINTER(self.ctype)) def wait_new_data(self): - BlcChannel.lib.blc_channel_wait_new_data(self.channel) + BlcChannel.lib.blc_channel_wait_new_data(self.pointer) def wait_ack_data(self): - BlcChannel.lib.blc_channel_wait_ack_data(self.channel) + BlcChannel.lib.blc_channel_wait_ack_data(self.pointer) def post_new_data(self): - BlcChannel.lib.blc_channel_post_new_data(self.channel) + BlcChannel.lib.blc_channel_post_new_data(self.pointer) def post_ack_data(self): - BlcChannel.lib.blc_channel_post_ack_data(self.channel) + BlcChannel.lib.blc_channel_post_ack_data(self.pointer) def remove(self): - BlcChannel.lib.blc_channel_remove(self.channel) + BlcChannel.lib.blc_channel_remove(self.pointer) def __del__(self): - BlcChannel.lib.blc_channel_delete(self.channel) #Fre the local object but do not destroy the system shared memory + BlcChannel.lib.blc_channel_delete(self.pointer) #Free the local object but do not destroy the system shared memory @property def list(self): #cast it from - data_list = list(self.array[:self.total_length]) + data_list = list(self.data[:self.total_length]) return data_list if __name__ == "__main__": @@ -138,13 +80,15 @@ if __name__ == "__main__": print("EXEMPLE: blc_channel") print("We create a blc_channel of type unsigned char and size 3x2 on the POSIX system (shared memory)") channel = BlcChannel("/python_test", os.O_RDWR, 'UIN8', 'RGB3', [3, 2]) + print("type",channel.type, "format", channel.format, "length", channel.total_length) + print("We write 7 in column 2") - channel.array[2]=7 + channel.data[2]=7 print("We open the shared blc_channel to read it") channel_read = BlcChannel("/python_test", os.O_RDONLY) print("We check that we have the same values") print("type",channel_read.type, "format", channel_read.format, "length", channel_read.total_length) - print(channel_read.array[:channel.total_length]) + print(channel_read.data[:channel.total_length]) print("We remove our shared memory from the system") channel.remove() diff --git a/python/blc_network.py b/python/blc_network.py index 9d50936..30a9193 100644 --- a/python/blc_network.py +++ b/python/blc_network.py @@ -1,12 +1,12 @@ from ctypes import * from ctypes.util import find_library +import cv2 from blc_array import BlcArray path=find_library("blc") lib = CDLL(name=path) - class BlcArrayTCP4Server(BlcArray): lib.blc_array_get_type.argtypes = [ c_void_p ] lib.blc_array_get_type.restype = c_uint32 @@ -28,9 +28,9 @@ class BlcArrayTCP4Server(BlcArray): type_int = int.from_bytes(data_type.encode(), "big") format_int = int.from_bytes(format_type.encode(), "big") self.pointer = lib.blc_array_tcp4_server_new(port.encode(), type_int, format_int, len(dims), *dims ) - mtype=lib.blc_array_get_type(self.pointer) else: self.pointer = lib.blc_array_tcp4_server_new_void(port.encode()) + self.get_def() def send_data(self): -- GitLab