Skip to content

Commit

Permalink
Moved library loading functions to helpers class
Browse files Browse the repository at this point in the history
Signed-off-by: Pagadarai <[email protected]>
  • Loading branch information
SrikanthPagadarai committed Sep 3, 2024
1 parent c52a091 commit d242e20
Show file tree
Hide file tree
Showing 9 changed files with 938 additions and 420 deletions.
88 changes: 12 additions & 76 deletions +adi/+libiio/attribute.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -152,98 +152,34 @@

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
end

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
end

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
end

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
Loading

0 comments on commit d242e20

Please sign in to comment.