From d242e20811f3798cddf4fb8e8d5e882fa15f9b52 Mon Sep 17 00:00:00 2001 From: Pagadarai Date: Tue, 3 Sep 2024 18:52:58 -0500 Subject: [PATCH] Moved library loading functions to helpers class Signed-off-by: Pagadarai --- +adi/+libiio/attribute.m | 88 ++-------- +adi/+libiio/block.m | 235 ++++++++++++++++++++++++++ +adi/+libiio/buffer.m | 255 ++++++++++++++++++++++++++++ +adi/+libiio/channel.m | 350 +++++++++++++++++++++++++++++++++++---- +adi/+libiio/context.m | 106 +++--------- +adi/+libiio/device.m | 104 +++--------- +adi/+libiio/helpers.m | 64 +++++++ +adi/+libiio/scan.m | 80 +-------- +adi/+libiio/toplevel.m | 76 +-------- 9 files changed, 938 insertions(+), 420 deletions(-) create mode 100644 +adi/+libiio/block.m create mode 100644 +adi/+libiio/buffer.m create mode 100644 +adi/+libiio/helpers.m diff --git a/+adi/+libiio/attribute.m b/+adi/+libiio/attribute.m index 7ab35356..e083ed01 100644 --- a/+adi/+libiio/attribute.m +++ b/+adi/+libiio/attribute.m @@ -20,7 +20,7 @@ 'nonnegative', 'integer'}); if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_read_raw', attrPtr, dstPtr, len); + status = adi.libiio.helpers.calllibADI('iio_attr_read_raw', attrPtr, dstPtr, len); else status = coder.ceval('iio_attr_read_raw', attrPtr, dstPtr, len); end @@ -44,7 +44,7 @@ 'nonnegative', 'integer'}); if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_write_raw', attrPtr, srcPtr, len); + status = adi.libiio.helpers.calllibADI('iio_attr_write_raw', attrPtr, srcPtr, len); else status = coder.ceval('iio_attr_write_raw', attrPtr, srcPtr, len); end @@ -62,7 +62,7 @@ % libiio function: iio_attr_get_name if coder.target('MATLAB') - name = adi.libiio.attribute.calllibADI('iio_attr_get_name', attrPtr); + name = adi.libiio.helpers.calllibADI('iio_attr_get_name', attrPtr); else name = coder.ceval('iio_attr_get_name', attrPtr); end @@ -80,7 +80,7 @@ % libiio function: iio_attr_get_filename if coder.target('MATLAB') - name = adi.libiio.attribute.calllibADI('iio_attr_get_filename', attrPtr); + name = adi.libiio.helpers.calllibADI('iio_attr_get_filename', attrPtr); else name = coder.ceval('iio_attr_get_filename', attrPtr); end @@ -99,7 +99,7 @@ % libiio function: iio_attr_get_filename if coder.target('MATLAB') - name = adi.libiio.attribute.calllibADI('iio_attr_get_static_value', attrPtr); + name = adi.libiio.helpers.calllibADI('iio_attr_get_static_value', attrPtr); else name = coder.ceval('iio_attr_get_static_value', attrPtr); end @@ -108,7 +108,7 @@ function [status, value] = iio_attr_read_bool(attrPtr) valPtr = libpointer('bool', 0); if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_read_bool', attrPtr, valPtr); + status = adi.libiio.helpers.calllibADI('iio_attr_read_bool', attrPtr, valPtr); else status = coder.ceval('iio_attr_read_bool', attrPtr, valPtr); end @@ -123,7 +123,7 @@ function [status, value] = iio_attr_read_longlong(attrPtr) valPtr = libpointer('int64Ptr', 0); if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_read_longlong', attrPtr, valPtr); + status = adi.libiio.helpers.calllibADI('iio_attr_read_longlong', attrPtr, valPtr); else status = coder.ceval('iio_attr_read_longlong', attrPtr, valPtr); end @@ -138,7 +138,7 @@ function [status, value] = iio_attr_read_double(attrPtr) valPtr = libpointer('double', 0); if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_read_double', attrPtr, valPtr); + status = adi.libiio.helpers.calllibADI('iio_attr_read_double', attrPtr, valPtr); else status = coder.ceval('iio_attr_read_double', attrPtr, valPtr); end @@ -152,7 +152,7 @@ function status = iio_attr_write_string(attrPtr, value) if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_write_string', attrPtr, value); + status = adi.libiio.helpers.calllibADI('iio_attr_write_string', attrPtr, value); else status = coder.ceval('iio_attr_write_string', attrPtr, value); end @@ -160,7 +160,7 @@ function status = iio_attr_write_bool(attrPtr, value) if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_write_bool', attrPtr, value); + status = adi.libiio.helpers.calllibADI('iio_attr_write_bool', attrPtr, value); else status = coder.ceval('iio_attr_write_bool', attrPtr, value); end @@ -168,7 +168,7 @@ function status = iio_attr_write_longlong(attrPtr, value) if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_write_longlong', attrPtr, value); + status = adi.libiio.helpers.calllibADI('iio_attr_write_longlong', attrPtr, value); else status = coder.ceval('iio_attr_write_longlong', attrPtr, value); end @@ -176,74 +176,10 @@ function status = iio_attr_write_double(attrPtr, value) if coder.target('MATLAB') - status = adi.libiio.attribute.calllibADI('iio_attr_write_double', attrPtr, value); + status = adi.libiio.helpers.calllibADI('iio_attr_write_double', attrPtr, value); else status = coder.ceval('iio_attr_write_double', attrPtr, value); end end end - - %%Helpers - methods (Hidden, Access = private, Static) - function libName = getIIOLibName() - libName = 'libiio1'; - end - - function headername = getIIOHeaderName() - headername = 'iio.h'; - end - - function [notfound, warnings] = loadLibIIO() - notfound = []; - warnings = []; - libName = adi.libiio.attribute.getIIOLibName(); - headername = adi.libiio.attribute.getIIOHeaderName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % [notfound, warnings] = loadlibrary(libName,headername); - % if ~isempty(notfound) - % % error - % end - % IsLibiioLoaded = libisloaded(libName); - % end - - if ~libisloaded(libName) - [notfound, warnings] = loadlibrary(libName,headername); - if ~isempty(notfound) - % error - end - end - end - - function unloadLibIIO() - libName = adi.libiio.attribute.getIIOLibName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % IsLibiioLoaded = libisloaded(libName); - % end - % - % if IsLibiioLoaded - % unloadlibrary(libName); - % end - - if libisloaded(libName) - unloadlibrary(libName); - end - end - - function varargout = calllibADI(fn, varargin) - [notfound, warnings] = adi.libiio.attribute.loadLibIIO(); - varargout = cell(1, nargout); - varargoutLocal = calllib(adi.libiio.attribute.getIIOLibName(), fn, varargin{:}); - % adi.libiio.attribute.unloadLibIIO(); - [varargout{:}] = varargoutLocal; - end - - function strout = ntstr(strin) - % Appends a null character to terminate the string. - % This is needed for code generation since MATLAB character - % arrays are not null terminated in code generation. - strout = [uint8(strin) uint8(0)]; - end - end end \ No newline at end of file diff --git a/+adi/+libiio/block.m b/+adi/+libiio/block.m new file mode 100644 index 00000000..32f825ea --- /dev/null +++ b/+adi/+libiio/block.m @@ -0,0 +1,235 @@ +classdef block < handle + %% block methods + methods (Static) + function blockPtr = iio_buffer_create_block(buffPtr, size) + % Create a data block for the given buffer + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % size: The size of the block to create, in bytes + % + % Returns: + % On success, a pointer to an iio_block structure + % On failure, a pointer-encoded error is returned + % + % libiio function: iio_buffer_create_block + + if coder.target('MATLAB') + blockPtr = adi.libiio.helpers.calllibADI('iio_buffer_create_block', buffPtr, size); + else + blockPtr = coder.opaque('const struct iio_block*', 'NULL'); + blockPtr = coder.ceval('iio_buffer_create_block', buffPtr, size); + end + end + + function iio_block_destroy(blockPtr) + % Destroy the given block + % + % Args: + % blockPtr: A pointer to an iio_block structure + % + % libiio function: iio_block_destroy + + if coder.target('MATLAB') + adi.libiio.helpers.calllibADI('iio_block_destroy', blockPtr); + else + coder.ceval('iio_block_destroy', blockPtr); + end + end + + function startAddr = iio_block_start(blockPtr) + % Get the start address of the block + % + % Args: + % blockPtr: A pointer to an iio_block structure + % + % Returns: + % A pointer corresponding to the start address of the block + % + % libiio function: iio_block_start + + if coder.target('MATLAB') + startAddr = adi.libiio.helpers.calllibADI('iio_block_start', blockPtr); + else + startAddr = coder.opaque('void*', 'NULL'); + startAddr = coder.ceval('iio_block_start', blockPtr); + end + end + + function addr = iio_block_first(blockPtr, chnPtr) + % Find the first sample of a channel in a block + % + % Args: + % blockPtr: A pointer to an iio_block structure + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % A pointer to the first sample found, or to the end of the + % block if no sample for the given channel is present in the + % block. + % + % NOTE: + % This function, coupled with iio_block_end, can be used to + % iterate on all the samples of a given channel present in + % the block, doing the following: + % + % for (void *ptr = iio_block_first(block, chn); + % ptr < iio_block_end(block); + % ptr += iio_device_get_sample_size(dev, mask)) { + % .... + % } + % + % The iio_channel passed as argument must be from the + % iio_device that was used to create the iio_buffer and then + % the iio_block, otherwise the result is undefined. + % + % libiio function: iio_block_first + + if coder.target('MATLAB') + addr = adi.libiio.helpers.calllibADI('iio_block_first', blockPtr, chnPtr); + else + addr = coder.opaque('void*', 'NULL'); + addr = coder.ceval('iio_block_first', blockPtr, chnPtr); + end + end + + function endAddr = iio_block_end(blockPtr) + % Get the address after the last sample in a block + % + % Args: + % blockPtr: A pointer to an iio_block structure + % + % Returns: + % A pointer corresponding to the address that follows the + % last sample present in the buffer + % + % libiio function: iio_block_end + + if coder.target('MATLAB') + endAddr = adi.libiio.helpers.calllibADI('iio_block_end', blockPtr); + else + endAddr = coder.opaque('void*', 'NULL'); + endAddr = coder.ceval('iio_block_end', blockPtr); + end + end + %{ + function endAddr = iio_block_foreach_sample(blockPtr, maskPtr) + % Call the supplied callback for each sample found in a block + % + % Args: + % blockPtr: A pointer to an iio_block structure + % maskPtr: A pointer to the iio_channels_mask structure that + % represents the list of channels for which we want samples + % callback: A pointer to a function to call for each sample found + % dataPtr: A user-specified pointer that will be passed to the callback + % + % Returns: + % Number of bytes processed + % + % NOTE: + % The callback receives four arguments: + % A pointer to the iio_channel structure corresponding to the sample, + % A pointer to the sample itself, + % The length of the sample in bytes, + % The user-specified pointer passed to iio_block_foreach_sample. + % + % libiio function: iio_block_foreach_sample + + if coder.target('MATLAB') + endAddr = adi.libiio.helpers.calllibADI('iio_block_foreach_sample', blockPtr, maskPtr); + else + endAddr = coder.ceval('iio_block_foreach_sample', blockPtr, maskPtr); + end + end + + % /** @brief + % * @param block A pointer to an iio_block structure + % * @param mask A pointer to the iio_channels_mask structure that represents + % * the list of channels for which we want samples + % * @param callback A pointer to a function to call for each sample found + % * @param data A user-specified pointer that will be passed to the callback + % * @return number of bytes processed. + % * + % * NOTE: The callback receives four arguments: + % * * A pointer to the iio_channel structure corresponding to the sample, + % * * A pointer to the sample itself, + % * * The length of the sample in bytes, + % * * The user-specified pointer passed to iio_block_foreach_sample. */ + % __api __check_ret ssize_t + % iio_block_foreach_sample(const struct iio_block *block, + % const struct iio_channels_mask *mask, + % ssize_t (*callback)(const struct iio_channel *chn, + % void *src, size_t bytes, void *d), + % void *data); + %} + + function status = iio_block_enqueue(blockPtr, bytesUsed, cyclic) + % Enqueue the given iio_block to the buffer's queue + % + % Args: + % blockPtr: A pointer to an iio_block structure + % bytesUsed: The amount of data in bytes to be transferred + % (either transmitted or received). If zero, the size of + % the block is used. + % cyclic: If True, enable cyclic mode. The block's content + % will be repeated on the hardware's output until the + % buffer is cancelled or destroyed. + % + % Returns: + % On success, 0 is returned. + % On error, a negative error code is returned. + % + % NOTE: After iio_block_enqueue is called, the block's data + % must not be accessed until iio_block_dequeue successfully + % returns. + % + % libiio function: iio_block_enqueue + + if coder.target('MATLAB') + status = adi.libiio.helpers.calllibADI('iio_block_enqueue', blockPtr, bytesUsed, cyclic); + else + status = coder.ceval('iio_block_enqueue', blockPtr, bytesUsed, cyclic); + end + end + + function status = iio_block_dequeue(blockPtr, nonblock) + % Dequeue the given iio_block from the buffer's queue + % + % Args: + % blockPtr: A pointer to an iio_block structure + % nonblock: if True, the operation won't block and return + % -EBUSY if the block is not ready for dequeue. + % + % Returns: + % On success, 0 is returned. + % On error, a negative error code is returned. + % + % libiio function: iio_block_dequeue + + if coder.target('MATLAB') + status = adi.libiio.helpers.calllibADI('iio_block_dequeue', blockPtr, nonblock); + else + status = coder.ceval('iio_block_dequeue', blockPtr, nonblock); + end + end + + function buffPtr = iio_block_get_buffer(blockPtr) + % Retrieve a pointer to the iio_buffer structure + % + % Args: + % blockPtr: A pointer to an iio_block structure + % + % Returns: + % A pointer to an iio_buffer structure + % + % libiio function: iio_block_get_buffer + + if coder.target('MATLAB') + buffPtr = adi.libiio.helpers.calllibADI('iio_block_get_buffer', blockPtr); + else + buffPtr = coder.opaque('struct iio_buffer*', 'NULL'); + buffPtr = coder.ceval('iio_block_get_buffer', blockPtr); + end + end + end +end \ No newline at end of file diff --git a/+adi/+libiio/buffer.m b/+adi/+libiio/buffer.m new file mode 100644 index 00000000..279a1719 --- /dev/null +++ b/+adi/+libiio/buffer.m @@ -0,0 +1,255 @@ +classdef buffer < handle + %% buffer methods + methods (Static) + function devPtr = iio_buffer_get_device(buffPtr) + % Retrieve a pointer to the iio_device structure + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % Returns: + % A pointer to an iio_device structure + % + % libiio function: iio_buffer_get_device + + if coder.target('MATLAB') + devPtr = adi.libiio.helpers.calllibADI('iio_buffer_get_device', buffPtr); + else + devPtr = coder.opaque('const struct iio_device*', 'NULL'); + devPtr = coder.ceval('iio_buffer_get_device', buffPtr); + end + end + + function count = iio_buffer_get_attrs_count(buffPtr) + % Enumerate the attributes of the given buffer + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % Returns: + % The number of buffer-specific attributes found + % + % libiio function: iio_buffer_get_attrs_count + + if coder.target('MATLAB') + count = adi.libiio.helpers.calllibADI('iio_buffer_get_attrs_count', buffPtr); + else + count = coder.ceval('iio_buffer_get_attrs_count', buffPtr); + end + end + + function attrPtr = iio_buffer_get_attr(buffPtr, index) + % Get the buffer-specific attribute present at the given index + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % index: The index corresponding to the attribute + % + % Returns: + % On success, a pointer to an iio_attr structure. + % If the index is invalid, NULL is returned. + % + % libiio function: iio_buffer_get_attr + + if coder.target('MATLAB') + attrPtr = adi.libiio.helpers.calllibADI('iio_buffer_get_attr', buffPtr, index); + else + attrPtr = coder.opaque('const struct iio_attr*', 'NULL'); + attrPtr = coder.ceval('iio_buffer_get_attr', buffPtr, index); + end + end + + function attrPtr = iio_buffer_find_attr(buffPtr, name) + % Try to find a buffer-specific attribute by its name + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % name: A NULL-terminated string corresponding to the name + % of the attribute + % + % Returns: + % On success, a pointer to a static NULL-terminated string. + % If the name does not correspond to any known attribute of the given + % channel, NULL is returned + % + % libiio function: iio_buffer_find_attr + + if coder.target('MATLAB') + attrPtr = adi.libiio.helpers.calllibADI('iio_buffer_find_attr', buffPtr, name); + else + attrPtr = coder.opaque('const struct iio_attr*', 'NULL'); + attrPtr = coder.ceval('iio_buffer_find_attr', buffPtr, adi.libiio.helpers.ntstr(name)); + end + end + + function buffPtr = iio_device_create_buffer(devPtr, idx, maskPtr) + % Create an input or output buffer associated to the given device + % + % Args: + % devPtr: A pointer to an iio_device structure + % idx: The index of the hardware buffer. Should be 0 in + % most cases. + % maskPtr: A pointer to an iio_channels_mask structure + % + % Returns: + % On success, a pointer to an iio_buffer structure. + % On failure, a pointer-encoded error is returned. + % + % libiio function: iio_device_create_buffer + + if coder.target('MATLAB') + buffPtr = adi.libiio.helpers.calllibADI('iio_device_create_buffer', devPtr, idx, maskPtr); + else + buffPtr = coder.opaque('struct iio_buffer*', 'NULL'); + buffPtr = coder.ceval('iio_device_create_buffer', devPtr, idx, maskPtr); + end + end + + function iio_buffer_set_data(buffPtr, dataPtr) + % Associate a pointer to an iio_buffer structure + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % dataPtr: The pointer to be associated + % + % libiio function: iio_buffer_set_data + + if coder.target('MATLAB') + adi.libiio.helpers.calllibADI('iio_buffer_set_data', buffPtr, dataPtr); + else + coder.ceval('iio_buffer_set_data', buffPtr, dataPtr); + end + end + + function dataPtr = iio_buffer_get_data(buffPtr) + % Retrieve a previously associated pointer of an iio_channel structure + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % Returns: + % The pointer previously associated if present, or NULL + % + % libiio function: iio_buffer_get_data + + if coder.target('MATLAB') + dataPtr = adi.libiio.helpers.calllibADI('iio_buffer_get_data', buffPtr); + else + dataPtr = coder.opaque('void*', 'NULL'); + dataPtr = coder.ceval('iio_buffer_get_data', buffPtr); + end + end + + function iio_buffer_destroy(buffPtr) + % Destroy the given buffer + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % libiio function: iio_buffer_destroy + + if coder.target('MATLAB') + adi.libiio.helpers.calllibADI('iio_buffer_destroy', buffPtr); + else + coder.ceval('iio_buffer_destroy', buffPtr); + end + end + + function iio_buffer_cancel(buffPtr) + % This function cancels all outstanding buffer operations + % previously scheduled. This means that any pending + % iio_block_enqueue() or iio_block_dequeue() operation will + % abort and return immediately, any further invocation of these + % functions on the same buffer will return immediately with an + % error. + % + % Usually iio_block_dequeue() will block until all data has + % been transferred or a timeout occurs. This can depending on + % the configuration take a significant amount of time. + % iio_buffer_cancel() is useful to bypass these conditions if + % the buffer operation is supposed to be stopped in response to + % an external event (e.g. user input). + % + % To be able to transfer additional data after calling this + % function the buffer should be destroyed and then re-created. + % + % This function can be called multiple times for the same + % buffer, but all but the first invocation will be without + % additional effect. + % + % This function is thread-safe, but not signal-safe, i.e. it + % must not be called from a signal handler. + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % libiio function: iio_buffer_cancel + + if coder.target('MATLAB') + adi.libiio.helpers.calllibADI('iio_buffer_cancel', buffPtr); + else + coder.ceval('iio_buffer_cancel', buffPtr); + end + end + + function status = iio_buffer_enable(buffPtr) + % Enable the buffer + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % Returns: + % On success, 0 + % On error, a negative error code is returned + % + % libiio function: iio_buffer_enable + + if coder.target('MATLAB') + status = adi.libiio.helpers.calllibADI('iio_buffer_enable', buffPtr); + else + status = coder.ceval('iio_buffer_enable', buffPtr); + end + end + + function status = iio_buffer_disable(chnPtr, maskPtr) + % Disable the buffer + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % Returns: + % On success, 0 + % On error, a negative error code is returned + % + % libiio function: iio_buffer_disable + + if coder.target('MATLAB') + status = adi.libiio.helpers.calllibADI('iio_buffer_disable', chnPtr, maskPtr); + else + status = coder.ceval('iio_buffer_disable', chnPtr, maskPtr); + end + end + + function channelsMaskPtr = iio_buffer_get_channels_mask(chnPtr, maskPtr) + % Retrieve a mask of the channels enabled for the given buffer + % + % Args: + % buffPtr: A pointer to an iio_buffer structure + % + % Returns: + % A pointer to an iio_channels_mask structure + % + % NOTE: The mask returned may contain more enabled channels + % than the mask used for creating the buffer. + % + % libiio function: iio_buffer_get_channels_mask + + if coder.target('MATLAB') + channelsMaskPtr = adi.libiio.helpers.calllibADI('iio_buffer_get_channels_mask', chnPtr, maskPtr); + else + channelsMaskPtr = coder.opaque('const struct iio_channels_mask*', 'NULL'); + channelsMaskPtr = coder.ceval('iio_buffer_get_channels_mask', chnPtr, maskPtr); + end + end + end +end \ No newline at end of file diff --git a/+adi/+libiio/channel.m b/+adi/+libiio/channel.m index 5d3e7ce5..fb060cb4 100644 --- a/+adi/+libiio/channel.m +++ b/+adi/+libiio/channel.m @@ -1,51 +1,335 @@ classdef channel < handle - methods - function obj = channel() - % CHANNEL constructor method for matlabshared.libiio.context + %% channel methods + methods (Static) + function devPtr = iio_channel_get_device(chnPtr) + % Retrieve a pointer to the iio_device structure + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % A pointer to an iio_device structure % - % Returns the matlabshared.libiio.context object - coder.allowpcode('plain'); + % libiio function: iio_channel_get_device + + if coder.target('MATLAB') + devPtr = adi.libiio.helpers.calllibADI('iio_channel_get_device', chnPtr); + else + devPtr = coder.opaque('const struct iio_device*', 'NULL'); + devPtr = coder.ceval('iio_channel_get_device', chnPtr); + end end - end - - %% Internal Helper Functions - methods (Static) - function attrPtr = iio_channel_find_attr(chanPtr,attr) - attrPtr = calllib(adi.libiio.channel.getIIOLibName(), 'iio_channel_find_attr', chanPtr, attr); + + function id = iio_channel_get_id(chnPtr) + % Retrieve the channel ID (e.g. voltage0) + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % A pointer to a static NULL-terminated string + % + % libiio function: iio_channel_get_id + + if coder.target('MATLAB') + id = adi.libiio.helpers.calllibADI('iio_channel_get_id', chnPtr); + else + id = coder.ceval('iio_channel_get_id', chnPtr); + end end - function [status, value] = iio_channel_attr_read_bool(chanPtr,attr) - attrPtr = adi.libiio.channel.iio_channel_find_attr(chanPtr,attr); - % cstatus(status,['Attribute: ' attr ' not found']); - [status, value] = adi.libiio.attribute.iio_attr_read_bool(attrPtr); + function name = iio_channel_get_name(chnPtr) + % Retrieve the channel name (e.g. vccint) + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % A pointer to a static NULL-terminated string + % + % NOTE: + % If the channel has no name, NULL is returned. + % + % libiio function: iio_channel_get_name + + if coder.target('MATLAB') + name = adi.libiio.helpers.calllibADI('iio_channel_get_name', chnPtr); + else + name = coder.ceval('iio_channel_get_name', chnPtr); + end end - function [status, value] = iio_channel_attr_read_longlong(chanPtr,attr) - attrPtr = adi.libiio.channel.iio_channel_find_attr(chanPtr,attr); - % cstatus(status,['Attribute: ' attr ' not found']); - [status, value] = adi.libiio.attribute.iio_attr_read_longlong(attrPtr); + function status = iio_channel_is_output(chnPtr) + % Return True if the given channel is an output channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % True if the channel is an output channel, False otherwise + % + % libiio function: iio_channel_is_output + + if coder.target('MATLAB') + status = adi.libiio.helpers.calllibADI('iio_channel_is_output', chnPtr); + else + status = coder.ceval('iio_channel_is_output', chnPtr); + end end - function status = iio_channel_attr_write_bool(chanPtr,attr,value) - attrPtr = adi.libiio.channel.iio_channel_find_attr(chanPtr,attr); - % cstatus(status,['Attribute: ' attr ' not found']); - status = adi.libiio.attribute.iio_attr_write_bool(attrPtr, value); + function status = iio_channel_is_scan_element(chnPtr) + % Return True if the given channel is a scan element + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % True if the channel is a scan element, False otherwise + % + % NOTE: + % A channel that is a scan element is a channel that can + % generate samples (for an input channel) or receive samples + % (for an output channel) after being enabled. + % + % libiio function: iio_channel_is_scan_element + + if coder.target('MATLAB') + status = adi.libiio.helpers.calllibADI('iio_channel_is_scan_element', chnPtr); + else + status = coder.ceval('iio_channel_is_scan_element', chnPtr); + end end - function status = iio_channel_attr_write_longlong(chanPtr,attr,value) - attrPtr = adi.libiio.channel.iio_channel_find_attr(chanPtr,attr); - % cstatus(status,['Attribute: ' attr ' not found']); - status = adi.libiio.attribute.iio_attr_write_longlong(attrPtr, value); + function count = iio_channel_get_attrs_count(chnPtr) + % Enumerate the channel-specific attributes of the given channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % The number of channel-specific attributes found + % + % libiio function: iio_channel_get_attrs_count + + if coder.target('MATLAB') + count = adi.libiio.helpers.calllibADI('iio_channel_get_attrs_count', chnPtr); + else + count = coder.ceval('iio_channel_get_attrs_count', chnPtr); + end end - function nBytes = iio_channel_attr_write(chanPtr, attr, src) - attrPtr = adi.libiio.channel.iio_channel_find_attr(chanPtr,attr); - % cstatus(status,['Attribute: ' attr ' not found']); - nBytes = adi.libiio.attribute.iio_attr_write_string(attrPtr, src); + function attrPtr = iio_channel_get_attr(chnPtr, index) + % Get the channel-specific attribute present at the given index + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % index: The index corresponding to the attribute + % + % Returns: + % On success, a pointer to a static NULL-terminated string. + % If the index is invalid, NULL is returned + % + % libiio function: iio_channel_get_attr + + if coder.target('MATLAB') + attrPtr = adi.libiio.helpers.calllibADI('iio_channel_get_attr', chnPtr, index); + else + attrPtr = coder.opaque('const struct iio_attr*', 'NULL'); + attrPtr = coder.ceval('iio_channel_get_attr', chnPtr, index); + end end - function libName = getIIOLibName() - libName = 'libiio1'; + function attrPtr = iio_channel_find_attr(chnPtr, name) + % Try to find a channel-specific attribute by its name + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % name: A NULL-terminated string corresponding to the name + % of the attribute + % + % Returns: + % On success, a pointer to a static NULL-terminated string. + % If the name does not correspond to any known attribute of the given + % channel, NULL is returned + % + % libiio function: iio_channel_find_attr + + if coder.target('MATLAB') + attrPtr = adi.libiio.helpers.calllibADI('iio_channel_find_attr', chnPtr, name); + else + attrPtr = coder.opaque('const struct iio_attr*', 'NULL'); + attrPtr = coder.ceval('iio_channel_find_attr', chnPtr, adi.libiio.helpers.ntstr(name)); + end + end + + function iio_channel_enable(chnPtr, maskPtr) + % Enable the given channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % maskPtr: The channels mask to manipulate + % + % libiio function: iio_channel_enable + + if coder.target('MATLAB') + adi.libiio.helpers.calllibADI('iio_channel_enable', chnPtr, maskPtr); + else + coder.ceval('iio_channel_enable', chnPtr, maskPtr); + end + end + + function iio_channel_disable(chnPtr, maskPtr) + % Disable the given channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % maskPtr: The channels mask to manipulate + % + % libiio function: iio_channel_disable + + if coder.target('MATLAB') + adi.libiio.helpers.calllibADI('iio_channel_disable', chnPtr, maskPtr); + else + coder.ceval('iio_channel_disable', chnPtr, maskPtr); + end + end + + function status = iio_channel_is_enabled(chnPtr, maskPtr) + % Returns True if the channel is enabled + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % maskPtr: The channels mask to manipulate + % + % Returns: + % True if the channel is enabled, False otherwise + % + % libiio function: iio_channel_is_enabled + + if coder.target('MATLAB') + status = adi.libiio.helpers.calllibADI('iio_channel_is_enabled', chnPtr, maskPtr); + else + status = coder.ceval('iio_channel_is_enabled', chnPtr, maskPtr); + end + end + + function size = iio_channel_read(chnPtr, blockPtr, dstPtr, len, raw) + % Demultiplex and convert the samples of a given channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % blockPtr: A pointer to an iio_block structure + % dstPtr: A pointer to the memory area where the converted + % data will be stored + % len: The available length of the memory area, in bytes + % raw: True to read samples in the hardware format, false to read + % converted samples + % + % Returns: + % The size of the converted data, in bytes + % + % libiio function: iio_channel_read + + if coder.target('MATLAB') + size = adi.libiio.helpers.calllibADI('iio_channel_read', chnPtr, blockPtr, dstPtr, len, raw); + else + size = coder.ceval('iio_channel_read', chnPtr, blockPtr, dstPtr, len, raw); + end + end + + function size = iio_channel_write(chnPtr, blockPtr, dstPtr, len, raw) + % Convert and multiplex the samples of a given channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % blockPtr: A pointer to an iio_block structure + % srcPtr: A pointer to the memory area where the sequential data will + % be read from + % len: The available length of the memory area, in bytes + % raw: True if the samples are already in hardware format, false if they + % need to be converted + % + % Returns: + % The number of bytes actually converted and multiplexed + % + % libiio function: iio_channel_write + + if coder.target('MATLAB') + size = adi.libiio.helpers.calllibADI('iio_channel_write', chnPtr, blockPtr, dstPtr, len, raw); + else + size = coder.ceval('iio_channel_write', chnPtr, blockPtr, dstPtr, len, raw); + end + end + + function iio_channel_set_data(chnPtr, dataPtr) + % Associate a pointer to an iio_channel structure + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % dataPtr: The pointer to be associated + % + % libiio function: iio_channel_set_data + + if coder.target('MATLAB') + adi.libiio.helpers.calllibADI('iio_channel_set_data', chnPtr, dataPtr); + else + coder.ceval('iio_channel_set_data', chnPtr, dataPtr); + end + end + + function dataPtr = iio_channel_get_data(chnPtr) + % Retrieve a previously associated pointer of an iio_channel structure + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % The pointer previously associated if present, or NULL + % + % libiio function: iio_channel_get_data + + if coder.target('MATLAB') + dataPtr = adi.libiio.helpers.calllibADI('iio_channel_get_data', chnPtr); + else + dataPtr = coder.opaque('void*', 'NULL'); + dataPtr = coder.ceval('iio_channel_get_data', chnPtr); + end + end + + function chnType = iio_channel_get_type(chnPtr) + % Get the type of the given channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % The type of the channel + % + % libiio function: iio_channel_get_type + + if coder.target('MATLAB') + chnType = adi.libiio.helpers.calllibADI('iio_channel_get_type', chnPtr); + else + chnType = coder.ceval('iio_channel_get_type', chnPtr); + end + end + + function modType = iio_channel_get_modifier(chnPtr) + % Get the modifier type of the given channel + % + % Args: + % chnPtr: A pointer to an iio_channel structure + % + % Returns: + % The modifier type of the channel + % + % libiio function: iio_channel_get_modifier + + if coder.target('MATLAB') + modType = adi.libiio.helpers.calllibADI('iio_channel_get_modifier', chnPtr); + else + modType = coder.ceval('iio_channel_get_modifier', chnPtr); + end end end end \ No newline at end of file diff --git a/+adi/+libiio/context.m b/+adi/+libiio/context.m index fffdb382..75faa16d 100644 --- a/+adi/+libiio/context.m +++ b/+adi/+libiio/context.m @@ -63,10 +63,10 @@ % libiio function: iio_create_context if coder.target('MATLAB') - ctxPtr = adi.libiio.context.calllibADI('iio_create_context', ctxParamsPtr, uri); + ctxPtr = adi.libiio.helpers.calllibADI('iio_create_context', ctxParamsPtr, uri); else ctxPtr = coder.opaque('struct iio_context*', 'NULL'); - ctxPtr = coder.ceval('iio_create_context', ctxParamsPtr, adi.libiio.context.ntstr(uri)); + ctxPtr = coder.ceval('iio_create_context', ctxParamsPtr, adi.libiio.helpers.ntstr(uri)); end end @@ -79,7 +79,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_destroy if coder.target('MATLAB') - adi.libiio.context.calllibADI('iio_context_destroy', ctxPtr); + adi.libiio.helpers.calllibADI('iio_context_destroy', ctxPtr); else coder.ceval('iio_context_destroy', ctxPtr); end @@ -101,7 +101,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_version_major if coder.target('MATLAB') - major = adi.libiio.context.calllibADI('iio_context_get_version_major', ctxPtr); + major = adi.libiio.helpers.calllibADI('iio_context_get_version_major', ctxPtr); else major = coder.ceval('iio_context_get_version_major', ctxPtr); end @@ -123,7 +123,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_version_minor if coder.target('MATLAB') - minor = adi.libiio.context.calllibADI('iio_context_get_version_minor', ctxPtr); + minor = adi.libiio.helpers.calllibADI('iio_context_get_version_minor', ctxPtr); else minor = coder.ceval('iio_context_get_version_minor', ctxPtr); end @@ -145,7 +145,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_version_tag if coder.target('MATLAB') - vtag = adi.libiio.context.calllibADI('iio_context_get_version_tag', ctxPtr); + vtag = adi.libiio.helpers.calllibADI('iio_context_get_version_tag', ctxPtr); else vtag = coder.nullcopy(adi.libiio.scan.ntstr('')); vtag = coder.ceval('iio_context_get_version_tag', ctxPtr); @@ -165,7 +165,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_xml if coder.target('MATLAB') - xml = adi.libiio.context.calllibADI('iio_context_get_xml', ctxPtr); + xml = adi.libiio.helpers.calllibADI('iio_context_get_xml', ctxPtr); else xml = coder.nullcopy(adi.libiio.scan.ntstr('')); xml = coder.ceval('iio_context_get_xml', ctxPtr); @@ -189,7 +189,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_name if coder.target('MATLAB') - name = adi.libiio.context.calllibADI('iio_context_get_name', ctxPtr); + name = adi.libiio.helpers.calllibADI('iio_context_get_name', ctxPtr); else name = coder.nullcopy(adi.libiio.scan.ntstr('')); name = coder.ceval('iio_context_get_name', ctxPtr); @@ -212,7 +212,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_description if coder.target('MATLAB') - descr = adi.libiio.context.calllibADI('iio_context_get_description', ctxPtr); + descr = adi.libiio.helpers.calllibADI('iio_context_get_description', ctxPtr); else descr = coder.nullcopy(adi.libiio.scan.ntstr('')); descr = coder.ceval('iio_context_get_description', ctxPtr); @@ -231,7 +231,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_device_get_channels_count if coder.target('MATLAB') - count = adi.libiio.context.calllibADI('iio_device_get_channels_count', ctxPtr); + count = adi.libiio.helpers.calllibADI('iio_device_get_channels_count', ctxPtr); else count = coder.ceval('iio_device_get_channels_count', ctxPtr); end @@ -251,7 +251,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_find_device if coder.target('MATLAB') - attrPtr = adi.libiio.context.calllibADI('iio_context_get_attr', ctxPtr, idx); + attrPtr = adi.libiio.helpers.calllibADI('iio_context_get_attr', ctxPtr, idx); else attrPtr = coder.opaque('struct iio_attr*', 'NULL'); attrPtr = coder.ceval('iio_context_get_attr', ctxPtr, idx); @@ -274,10 +274,10 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_find_attr if coder.target('MATLAB') - attrPtr = adi.libiio.context.calllibADI('iio_context_find_attr', ctxPtr, name); + attrPtr = adi.libiio.helpers.calllibADI('iio_context_find_attr', ctxPtr, name); else attrPtr = coder.opaque('struct iio_attr*', 'NULL'); - attrPtr = coder.ceval('iio_context_find_attr', ctxPtr, adi.libiio.context.ntstr(name)); + attrPtr = coder.ceval('iio_context_find_attr', ctxPtr, adi.libiio.helpers.ntstr(name)); end end @@ -293,7 +293,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_devices_count if coder.target('MATLAB') - count = adi.libiio.context.calllibADI('iio_context_get_devices_count', ctxPtr); + count = adi.libiio.helpers.calllibADI('iio_context_get_devices_count', ctxPtr); else count = coder.ceval('iio_context_get_devices_count', ctxPtr); end @@ -313,7 +313,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_device if coder.target('MATLAB') - devPtr = adi.libiio.context.calllibADI('iio_context_get_device', ctxPtr, idx); + devPtr = adi.libiio.helpers.calllibADI('iio_context_get_device', ctxPtr, idx); else devPtr = coder.opaque('struct iio_device*', 'NULL'); devPtr = coder.ceval('iio_context_get_device', ctxPtr, idx); @@ -336,10 +336,10 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_find_device if coder.target('MATLAB') - devPtr = adi.libiio.context.calllibADI('iio_context_find_device', ctxPtr, name); + devPtr = adi.libiio.helpers.calllibADI('iio_context_find_device', ctxPtr, name); else devPtr = coder.opaque('struct iio_device*', 'NULL'); - devPtr = coder.ceval('iio_context_find_device', ctxPtr, adi.libiio.context.ntstr(name)); + devPtr = coder.ceval('iio_context_find_device', ctxPtr, adi.libiio.helpers.ntstr(name)); end end @@ -359,7 +359,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_set_timeout if coder.target('MATLAB') - status = adi.libiio.context.calllibADI('iio_context_set_timeout', ctxPtr, timeout_ms); + status = adi.libiio.helpers.calllibADI('iio_context_set_timeout', ctxPtr, timeout_ms); else status = coder.opaque('struct iio_device*', 'NULL'); status = coder.ceval('iio_context_set_timeout', ctxPtr, timeout_ms); @@ -378,7 +378,7 @@ function iio_context_destroy(ctxPtr) % libiio function: iio_context_get_params if coder.target('MATLAB') - ctxParamsPtr = adi.libiio.context.calllibADI('iio_context_get_params', ctxPtr); + ctxParamsPtr = adi.libiio.helpers.calllibADI('iio_context_get_params', ctxPtr); else ctxParamsPtr = coder.opaque('struct iio_context_params*', 'NULL'); ctxParamsPtr = coder.ceval('iio_context_get_params', ctxPtr); @@ -398,7 +398,7 @@ function iio_context_set_data(ctxPtr, dataPtr) % libiio function: iio_context_set_data if coder.target('MATLAB') - adi.libiio.context.calllibADI('iio_context_set_data', ctxPtr, dataPtr); + adi.libiio.helpers.calllibADI('iio_context_set_data', ctxPtr, dataPtr); else coder.ceval('iio_context_set_data', ctxPtr, dataPtr); end @@ -416,75 +416,11 @@ function iio_context_set_data(ctxPtr, dataPtr) % libiio function: iio_context_get_data if coder.target('MATLAB') - dataPtr = adi.libiio.context.calllibADI('iio_context_set_data', ctxPtr); + dataPtr = adi.libiio.helpers.calllibADI('iio_context_set_data', ctxPtr); else dataPtr = coder.opaque('void*', 'NULL'); dataPtr = coder.ceval('iio_context_set_data', ctxPtr); end end end - - %%Helpers - methods (Hidden, Access = private, Static) - function libName = getIIOLibName() - libName = 'libiio1'; - end - - function headername = getIIOHeaderName() - headername = 'iio.h'; - end - - function [notfound, warnings] = loadLibIIO() - notfound = []; - warnings = []; - libName = adi.libiio.context.getIIOLibName(); - headername = adi.libiio.context.getIIOHeaderName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % [notfound, warnings] = loadlibrary(libName,headername); - % if ~isempty(notfound) - % % error - % end - % IsLibiioLoaded = libisloaded(libName); - % end - - if ~libisloaded(libName) - [notfound, warnings] = loadlibrary(libName,headername); - if ~isempty(notfound) - % error - end - end - end - - function unloadLibIIO() - libName = adi.libiio.context.getIIOLibName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % IsLibiioLoaded = libisloaded(libName); - % end - % - % if IsLibiioLoaded - % unloadlibrary(libName); - % end - - if libisloaded(libName) - unloadlibrary(libName); - end - end - - function varargout = calllibADI(fn, varargin) - [notfound, warnings] = adi.libiio.context.loadLibIIO(); - varargout = cell(1, nargout); - varargoutLocal = calllib(adi.libiio.context.getIIOLibName(), fn, varargin{:}); - % adi.libiio.context.unloadLibIIO(); - [varargout{:}] = varargoutLocal; - end - - function strout = ntstr(strin) - % Appends a null character to terminate the string. - % This is needed for code generation since MATLAB character - % arrays are not null terminated in code generation. - strout = [uint8(strin) uint8(0)]; - end - end end \ No newline at end of file diff --git a/+adi/+libiio/device.m b/+adi/+libiio/device.m index bfc21739..9f70f46b 100644 --- a/+adi/+libiio/device.m +++ b/+adi/+libiio/device.m @@ -13,7 +13,7 @@ % libiio function: iio_device_get_context if coder.target('MATLAB') - ctxPtr = adi.libiio.device.calllibADI('iio_device_get_context', devPtr); + ctxPtr = adi.libiio.helpers.calllibADI('iio_device_get_context', devPtr); else ctxPtr = coder.opaque('struct iio_context*', 'NULL'); ctxPtr = coder.ceval('iio_device_get_context', devPtr); @@ -32,7 +32,7 @@ % libiio function: iio_device_get_id if coder.target('MATLAB') - id = adi.libiio.device.calllibADI('iio_device_get_id', devPtr); + id = adi.libiio.helpers.calllibADI('iio_device_get_id', devPtr); else id = coder.ceval('iio_device_get_id', devPtr); end @@ -53,7 +53,7 @@ % libiio function: iio_device_get_name if coder.target('MATLAB') - name = adi.libiio.device.calllibADI('iio_device_get_name', devPtr); + name = adi.libiio.helpers.calllibADI('iio_device_get_name', devPtr); else name = coder.ceval('iio_device_get_name', devPtr); end @@ -74,7 +74,7 @@ % libiio function: iio_device_get_label if coder.target('MATLAB') - label = adi.libiio.device.calllibADI('iio_device_get_label', devPtr); + label = adi.libiio.helpers.calllibADI('iio_device_get_label', devPtr); else label = coder.ceval('iio_device_get_label', devPtr); end @@ -92,7 +92,7 @@ % libiio function: iio_device_get_channels_count if coder.target('MATLAB') - count = adi.libiio.device.calllibADI('iio_device_get_channels_count', devPtr); + count = adi.libiio.helpers.calllibADI('iio_device_get_channels_count', devPtr); else count = coder.ceval('iio_device_get_channels_count', devPtr); end @@ -110,7 +110,7 @@ % libiio function: iio_device_get_attrs_count if coder.target('MATLAB') - count = adi.libiio.device.calllibADI('iio_device_get_attrs_count', devPtr); + count = adi.libiio.helpers.calllibADI('iio_device_get_attrs_count', devPtr); else count = coder.ceval('iio_device_get_attrs_count', devPtr); end @@ -130,7 +130,7 @@ % libiio function: iio_device_get_channel if coder.target('MATLAB') - chanPtr = adi.libiio.device.calllibADI('iio_device_get_channel', devPtr, index); + chanPtr = adi.libiio.helpers.calllibADI('iio_device_get_channel', devPtr, index); else chanPtr = coder.opaque('struct iio_channel*', 'NULL'); chanPtr = coder.ceval('iio_device_get_channel', devPtr, index); @@ -151,7 +151,7 @@ % libiio function: iio_device_get_attr if coder.target('MATLAB') - attrPtr = adi.libiio.device.calllibADI('iio_device_get_attr', devPtr, index); + attrPtr = adi.libiio.helpers.calllibADI('iio_device_get_attr', devPtr, index); else attrPtr = coder.opaque('struct iio_attr*', 'NULL'); attrPtr = coder.ceval('iio_device_get_attr', devPtr, index); @@ -176,7 +176,7 @@ % libiio function: iio_device_find_channel if coder.target('MATLAB') - chanPtr = adi.libiio.device.calllibADI('iio_device_find_channel', devPtr, id, output); + chanPtr = adi.libiio.helpers.calllibADI('iio_device_find_channel', devPtr, id, output); else chanPtr = coder.opaque('struct iio_channel*', 'NULL'); chanPtr = coder.ceval('iio_device_find_channel', devPtr, id, output); @@ -205,7 +205,7 @@ % libiio function: iio_device_find_attr if coder.target('MATLAB') - attrPtr = adi.libiio.device.calllibADI('iio_device_find_attr', devPtr, name); + attrPtr = adi.libiio.helpers.calllibADI('iio_device_find_attr', devPtr, name); else attrPtr = coder.opaque('struct iio_attr*', 'NULL'); attrPtr = coder.ceval('iio_device_find_attr', devPtr, name); @@ -222,13 +222,13 @@ function iio_device_set_data(devPtr, dataPtr) % libiio function: iio_device_set_data if coder.target('MATLAB') - adi.libiio.device.calllibADI('iio_device_set_data', devPtr, dataPtr); + adi.libiio.helpers.calllibADI('iio_device_set_data', devPtr, dataPtr); else coder.ceval('iio_device_set_data', devPtr, dataPtr); end end - function valPtr = iio_device_get_data(devPtr) + function dataPtr = iio_device_get_data(devPtr) % Retrieve a previously associated pointer of an iio_device structure % % Args: @@ -240,10 +240,10 @@ function iio_device_set_data(devPtr, dataPtr) % libiio function: iio_device_get_data if coder.target('MATLAB') - valPtr = adi.libiio.device.calllibADI('iio_device_get_data', devPtr); + dataPtr = adi.libiio.helpers.calllibADI('iio_device_get_data', devPtr); else - valPtr = coder.opaque('void*', 'NULL'); - valPtr = coder.ceval('iio_device_get_data', devPtr); + dataPtr = coder.opaque('void*', 'NULL'); + dataPtr = coder.ceval('iio_device_get_data', devPtr); end end @@ -264,7 +264,7 @@ function iio_device_set_data(devPtr, dataPtr) % libiio function: iio_device_get_trigger if coder.target('MATLAB') - triggerPtr = adi.libiio.device.calllibADI('iio_device_get_trigger', devPtr, dataPtr); + triggerPtr = adi.libiio.helpers.calllibADI('iio_device_get_trigger', devPtr, dataPtr); else triggerPtr = coder.opaque('const struct iio_device*', 'NULL'); triggerPtr = coder.ceval('iio_device_get_trigger', devPtr, dataPtr); @@ -286,7 +286,7 @@ function iio_device_set_data(devPtr, dataPtr) % libiio function: iio_device_set_trigger if coder.target('MATLAB') - status = adi.libiio.device.calllibADI('iio_device_set_trigger', devPtr, triggerPtr); + status = adi.libiio.helpers.calllibADI('iio_device_set_trigger', devPtr, triggerPtr); else status = coder.ceval('iio_device_set_trigger', devPtr, triggerPtr); end @@ -304,7 +304,7 @@ function iio_device_set_data(devPtr, dataPtr) % libiio function: iio_device_is_trigger if coder.target('MATLAB') - status = adi.libiio.device.calllibADI('iio_device_is_trigger', devPtr); + status = adi.libiio.helpers.calllibADI('iio_device_is_trigger', devPtr); else status = coder.ceval('iio_device_is_trigger', devPtr); end @@ -314,73 +314,9 @@ function iio_device_set_data(devPtr, dataPtr) % Wrappers to maintain backwards-compatibility methods (Static) function nBytes = iio_device_attr_write(devPtr,attr,src) - [status, attrPtr] = adi.libiio.device.iio_device_find_attr(devPtr, attr); + [status, attrPtr] = adi.libiio.helpers.iio_device_find_attr(devPtr, attr); % cstatus(obj,status,['Attribute: ' attr ' not found']); - nBytes = adi.libiio.device.iio_attr_write_string(attrPtr, src); - end - end - - %%Helpers - methods (Hidden, Access = private, Static) - function libName = getIIOLibName() - libName = 'libiio1'; - end - - function headername = getIIOHeaderName() - headername = 'iio.h'; - end - - function [notfound, warnings] = loadLibIIO() - notfound = []; - warnings = []; - libName = adi.libiio.device.getIIOLibName(); - headername = adi.libiio.device.getIIOHeaderName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % [notfound, warnings] = loadlibrary(libName,headername); - % if ~isempty(notfound) - % % error - % end - % IsLibiioLoaded = libisloaded(libName); - % end - - if ~libisloaded(libName) - [notfound, warnings] = loadlibrary(libName,headername); - if ~isempty(notfound) - % error - end - end - end - - function unloadLibIIO() - libName = adi.libiio.device.getIIOLibName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % IsLibiioLoaded = libisloaded(libName); - % end - % - % if IsLibiioLoaded - % unloadlibrary(libName); - % end - - if libisloaded(libName) - unloadlibrary(libName); - end - end - - function varargout = calllibADI(fn, varargin) - [notfound, warnings] = adi.libiio.device.loadLibIIO(); - varargout = cell(1, nargout); - varargoutLocal = calllib(adi.libiio.device.getIIOLibName(), fn, varargin{:}); - % adi.libiio.device.unloadLibIIO(); - [varargout{:}] = varargoutLocal; - end - - function strout = ntstr(strin) - % Appends a null character to terminate the string. - % This is needed for code generation since MATLAB character - % arrays are not null terminated in code generation. - strout = [uint8(strin) uint8(0)]; + nBytes = adi.libiio.helpers.iio_attr_write_string(attrPtr, src); end end end \ No newline at end of file diff --git a/+adi/+libiio/helpers.m b/+adi/+libiio/helpers.m new file mode 100644 index 00000000..e3acb7d5 --- /dev/null +++ b/+adi/+libiio/helpers.m @@ -0,0 +1,64 @@ +classdef helpers < handle + %%helpers + methods (Hidden, Access = private, Static) + function libName = getIIOLibName() + libName = 'libiio1'; + end + + function headername = getIIOHeaderName() + headername = 'iio.h'; + end + + function [notfound, warnings] = loadLibIIO() + notfound = []; + warnings = []; + libName = adi.libiio.helpers.getIIOLibName(); + headername = adi.libiio.helpers.getIIOHeaderName(); + % persistent IsLibiioLoaded + % if isempty(IsLibiioLoaded) + % [notfound, warnings] = loadlibrary(libName,headername); + % if ~isempty(notfound) + % % error + % end + % IsLibiioLoaded = libisloaded(libName); + % end + + if ~libisloaded(libName) + [notfound, warnings] = loadlibrary(libName,headername); + if ~isempty(notfound) + % error + end + end + end + + function unloadLibIIO() + libName = adi.libiio.helpers.getIIOLibName(); + % persistent IsLibiioLoaded + % if isempty(IsLibiioLoaded) + % IsLibiioLoaded = libisloaded(libName); + % end + % + % if IsLibiioLoaded + % unloadlibrary(libName); + % end + + if libisloaded(libName) + unloadlibrary(libName); + end + end + + function varargout = calllibADI(fn, varargin) + [notfound, warnings] = adi.libiio.helpers.loadLibIIO(); + varargout = cell(1, nargout); + varargoutLocal = calllib(adi.libiio.helpers.getIIOLibName(), fn, varargin{:}); + [varargout{:}] = varargoutLocal; + end + + function strout = ntstr(strin) + % Appends a null character to terminate the string. + % This is needed for code generation since MATLAB character + % arrays are not null terminated in code generation. + strout = [uint8(strin) uint8(0)]; + end + end +end \ No newline at end of file diff --git a/+adi/+libiio/scan.m b/+adi/+libiio/scan.m index bfff179a..e20fdc22 100644 --- a/+adi/+libiio/scan.m +++ b/+adi/+libiio/scan.m @@ -25,10 +25,10 @@ % libiio function: iio_scan if coder.target('MATLAB') - scanPtr = adi.libiio.scan.calllibADI('iio_scan', ctxParamsPtr, backends); + scanPtr = adi.libiio.helpers.calllibADI('iio_scan', ctxParamsPtr, backends); else scanPtr = coder.opaque('struct iio_context*', 'NULL'); - scanPtr = coder.ceval('iio_scan', ctxParamsPtr, adi.libiio.scan.ntstr(backends)); + scanPtr = coder.ceval('iio_scan', ctxParamsPtr, adi.libiio.helpers.ntstr(backends)); end end @@ -43,7 +43,7 @@ function iio_scan_destroy(scanPtr) % libiio function: iio_scan_destroy if coder.target('MATLAB') - adi.libiio.scan.calllibADI('iio_scan_destroy', scanPtr); + adi.libiio.helpers.calllibADI('iio_scan_destroy', scanPtr); else coder.ceval('iio_scan_destroy', scanPtr); end @@ -61,7 +61,7 @@ function iio_scan_destroy(scanPtr) % libiio function: iio_scan_get_results_count if coder.target('MATLAB') - count = adi.libiio.scan.calllibADI('iio_scan_get_results_count', scanPtr); + count = adi.libiio.helpers.calllibADI('iio_scan_get_results_count', scanPtr); else count = coder.ceval('iio_scan_get_results_count', scanPtr); end @@ -81,9 +81,9 @@ function iio_scan_destroy(scanPtr) % libiio function: iio_scan_get_description if coder.target('MATLAB') - desc = adi.libiio.scan.calllibADI(obj.libName, 'iio_scan_get_description', scanPtr, idx); + desc = adi.libiio.helpers.calllibADI(obj.libName, 'iio_scan_get_description', scanPtr, idx); else - desc = coder.nullcopy(adi.libiio.scan.ntstr('')); + desc = coder.nullcopy(adi.libiio.helpers.ntstr('')); desc = coder.ceval('iio_scan_get_description', scanPtr, idx); end end @@ -102,75 +102,11 @@ function iio_scan_destroy(scanPtr) % libiio function: iio_scan_get_uri if coder.target('MATLAB') - uri = adi.libiio.scan.calllibADI(obj.libName, 'iio_scan_get_uri', scanPtr, idx); + uri = adi.libiio.helpers.calllibADI(obj.libName, 'iio_scan_get_uri', scanPtr, idx); else - uri = coder.nullcopy(adi.libiio.scan.ntstr('')); + uri = coder.nullcopy(adi.libiio.helpers.ntstr('')); uri = coder.ceval('iio_scan_get_uri', scanPtr, idx); end end end - - %%Helpers - methods (Hidden, Access = private, Static) - function libName = getIIOLibName() - libName = 'libiio1'; - end - - function headername = getIIOHeaderName() - headername = 'iio.h'; - end - - function [notfound, warnings] = loadLibIIO() - notfound = []; - warnings = []; - libName = adi.libiio.scan.getIIOLibName(); - headername = adi.libiio.scan.getIIOHeaderName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % [notfound, warnings] = loadlibrary(libName,headername); - % if ~isempty(notfound) - % % error - % end - % IsLibiioLoaded = libisloaded(libName); - % end - - if ~libisloaded(libName) - [notfound, warnings] = loadlibrary(libName,headername); - if ~isempty(notfound) - % error - end - end - end - - function unloadLibIIO() - libName = adi.libiio.scan.getIIOLibName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % IsLibiioLoaded = libisloaded(libName); - % end - % - % if IsLibiioLoaded - % unloadlibrary(libName); - % end - - if libisloaded(libName) - unloadlibrary(libName); - end - end - - function varargout = calllibADI(fn, varargin) - [notfound, warnings] = adi.libiio.scan.loadLibIIO(); - varargout = cell(1, nargout); - varargoutLocal = calllib(adi.libiio.scan.getIIOLibName(), fn, varargin{:}); - % adi.libiio.scan.unloadLibIIO(); - [varargout{:}] = varargoutLocal; - end - - function strout = ntstr(strin) - % Appends a null character to terminate the string. - % This is needed for code generation since MATLAB character - % arrays are not null terminated in code generation. - strout = [uint8(strin) uint8(0)]; - end - end end \ No newline at end of file diff --git a/+adi/+libiio/toplevel.m b/+adi/+libiio/toplevel.m index b538401b..56efb8c3 100644 --- a/+adi/+libiio/toplevel.m +++ b/+adi/+libiio/toplevel.m @@ -13,7 +13,7 @@ function iio_strerror(err, dstPtr, len) % libiio function: iio_strerror if coder.target('MATLAB') - adi.libiio.toplevel.calllibADI('iio_strerror', err, dstPtr, len); + adi.libiio.helpers.calllibADI('iio_strerror', err, dstPtr, len); else coder.ceval('iio_strerror', err, dstPtr, len); end @@ -33,9 +33,9 @@ function iio_strerror(err, dstPtr, len) % libiio function: iio_has_backend if coder.target('MATLAB') - hasBackend = adi.libiio.toplevel.calllibADI('iio_has_backend', ctxParamsPtr, backend); + hasBackend = adi.libiio.helpers.calllibADI('iio_has_backend', ctxParamsPtr, backend); else - hasBackend = coder.ceval('iio_has_backend', ctxParamsPtr, adi.libiio.toplevel.ntstr(backend)); + hasBackend = coder.ceval('iio_has_backend', ctxParamsPtr, adi.libiio.helpers.ntstr(backend)); end end @@ -48,7 +48,7 @@ function iio_strerror(err, dstPtr, len) % libiio function: iio_has_backend if coder.target('MATLAB') - count = adi.libiio.toplevel.calllibADI('iio_get_builtin_backends_count'); + count = adi.libiio.helpers.calllibADI('iio_get_builtin_backends_count'); else count = coder.ceval('iio_get_builtin_backends_count'); end @@ -67,75 +67,11 @@ function iio_strerror(err, dstPtr, len) % libiio function: iio_get_builtin_backend if coder.target('MATLAB') - name = adi.libiio.toplevel.calllibADI(obj.libName, 'iio_get_builtin_backend', idx); + name = adi.libiio.helpers.calllibADI(obj.libName, 'iio_get_builtin_backend', idx); else - name = coder.nullcopy(adi.libiio.toplevel.ntstr('')); + name = coder.nullcopy(adi.libiio.helpers.ntstr('')); name = coder.ceval('iio_get_builtin_backend', idx); end end end - - %%Helpers - methods (Hidden, Access = private, Static) - function libName = getIIOLibName() - libName = 'libiio1'; - end - - function headername = getIIOHeaderName() - headername = 'iio.h'; - end - - function [notfound, warnings] = loadLibIIO() - notfound = []; - warnings = []; - libName = adi.libiio.toplevel.getIIOLibName(); - headername = adi.libiio.toplevel.getIIOHeaderName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % [notfound, warnings] = loadlibrary(libName,headername); - % if ~isempty(notfound) - % % error - % end - % IsLibiioLoaded = libisloaded(libName); - % end - - if ~libisloaded(libName) - [notfound, warnings] = loadlibrary(libName,headername); - if ~isempty(notfound) - % error - end - end - end - - function unloadLibIIO() - libName = adi.libiio.toplevel.getIIOLibName(); - % persistent IsLibiioLoaded - % if isempty(IsLibiioLoaded) - % IsLibiioLoaded = libisloaded(libName); - % end - % - % if IsLibiioLoaded - % unloadlibrary(libName); - % end - - if libisloaded(libName) - unloadlibrary(libName); - end - end - - function varargout = calllibADI(fn, varargin) - [notfound, warnings] = adi.libiio.toplevel.loadLibIIO(); - varargout = cell(1, nargout); - varargoutLocal = calllib(adi.libiio.toplevel.getIIOLibName(), fn, varargin{:}); - % adi.libiio.toplevel.unloadLibIIO(); - [varargout{:}] = varargoutLocal; - end - - function strout = ntstr(strin) - % Appends a null character to terminate the string. - % This is needed for code generation since MATLAB character - % arrays are not null terminated in code generation. - strout = [uint8(strin) uint8(0)]; - end - end end \ No newline at end of file