% 配置编译环境
% setenv('MW_MINGW64_LOC','D:\msys64\ucrt64');
% mex -setup C++

filePath = fullfile(pwd, 'htra_api_mat');  

% 检查文件夹是否存在，如果不存在，创建文件夹
if ~exist(filePath, 'dir')
    run('htra_api.m');
    filePath = fullfile(pwd, 'htra_api_mat');  
end

%加载htra_api.dll
if not(libisloaded('htra_api.dll'))   
   %.dll和.h的文件路径一定要注意
    loadlibrary('.\htra_api\htra_api.dll','.\htra_api\htra_api.h'); 
end

%查看API中的所有函数
libfunctions('htra_api'); 

%打开设备
Device = libpointer; %创建Device指针
DevNum = 0;
Status = 0;

%打开设备
Device_1 = libpointer; %创建Device指针
DevNum_1 = 1;
Status_1 = 0;

% 直接加载BootProfile_TypeDef结构体
load(fullfile(filePath, 'BootProfile.mat'));  
% 直接加载BootInfo_TypeDef结构体
load(fullfile(filePath, 'BootInfo.mat'));  

IS_USB = 1;  % 默认为 USB 设备，如果使用网口型设备则设置为 0

if IS_USB == 1
    % 配置 USB 接口
    BootProfile.PhysicalInterface = 0;
else
    % 配置 ETH 接口
    BootProfile.PhysicalInterface = 3;
    BootProfile.ETH_IPVersion = 0;
    BootProfile.ETH_RemotePort = 5000;
    BootProfile.ETH_ReadTimeOut = 5000;
    BootProfile.ETH_IPAddress = [192, 168, 1, 100];
end

BootProfile_1=BootProfile;
BootProfile_2=BootProfile;

BootInfo_1 = BootInfo ;
BootInfo_2 = BootInfo ;

%调用Device_Open函数
Status = calllib('htra_api', 'Device_Open', Device, DevNum, BootProfile_1, BootInfo_1);

if Status == 0
    get(BootProfile);%打印BootInfo的值
    get(BootInfo);%打印BootInfo的值    
    fprintf('Device successfully\n');
else
    % 设备打开失败，根据不同的错误状态进行处理
    switch Status
        case -1
            fprintf('Error - Check the power supply of the device, the connection of the data cable, and check that the driver is installed correctly\n');
        case -3
            fprintf('Error - RF calibration file is missing, please copy the RF calibration file to the CalFile folder\n');
        case -4
            fprintf('Error - IF calibration file is missing, please copy the IF calibration file to the CalFile folder\n');
        case -5
            fprintf('Error - Profile is missing, please copy the profile to the CalFile folder\n');
        case -6
            fprintf('Error - The device spec file is missing, please copy the device spec file to the CalFile folder\n');
        otherwise
            fprintf('Device turn on failed: Returns another error Status = %d', Status);
    end

    % 返回错误状态值
    return ;
end

%调用Device_Open函数
Status_1 = calllib('htra_api', 'Device_Open', Device_1, DevNum_1, BootProfile_2, BootInfo_2);

if Status_1 == 0
    fprintf('Device successfully\n');
    get(BootProfile_2);%打印BootInfo_p的值
    get(BootInfo_2);%打印BootInfo_p的值
else
    % 设备打开失败，根据不同的错误状态进行处理
    switch Status_1
        case -1
            fprintf('Error - Check the power supply of the device, the connection of the data cable, and check that the driver is installed correctly\n');
        case -3
            fprintf('Error - RF calibration file is missing, please copy the RF calibration file to the CalFile folder\n');
        case -4
            fprintf('Error - IF calibration file is missing, please copy the IF calibration file to the CalFile folder\n');
        case -5
            fprintf('Error - Profile is missing, please copy the profile to the CalFile folder\n');
        case -6
            fprintf('Error - The device spec file is missing, please copy the device spec file to the CalFile folder\n');
        otherwise
            fprintf('Device turn on failed: Returns another error Status = %d', Status);
    end

    % 返回错误状态值
    return ;
end

% 直接加载 IQS_Profile_TypeDef结构体
load(fullfile(filePath, 'IQS_Profile.mat')); 

%定义IQS_ProfileIn指针和IQS_ProfileOut_0指针
IQS_ProfileIn = IQS_Profile ;
IQS_ProfileOut_0 = IQS_Profile ;

%IQS_ProfileOut_1指针
IQS_ProfileOut_1 = IQS_Profile;

%调用IQS_ProfileDeInit函数
Status = calllib('htra_api', 'IQS_ProfileDeInit', Device, IQS_ProfileIn);
get(IQS_ProfileIn);  %打印IQS_ProfileIn的值

%调用IQS_ProfileDeInit函数
Status_1 = calllib('htra_api', 'IQS_ProfileDeInit', Device_1, IQS_ProfileIn);
get(IQS_ProfileIn); %打印IQS_ProfileIn的值

%修改IQS_ProfileIn结构体的部分参数
IQS_ProfileIn.CenterFreq_Hz = 1e9;              %配置中心频率
IQS_ProfileIn.RefLevel_dBm = 0;	                %配置参考电平
IQS_ProfileIn.DecimateFactor = 2;               %配置抽取倍数
IQS_ProfileIn.TriggerSource = 1;                %配置触发源
IQS_ProfileIn.TriggerLength = 16242;            %配置单次触发采集的点数
% IQS_ProfileIn.ReferenceClockSource = 1;       %配置参考时钟源
% IQS_ProfileIn.ReferenceClockFrequency = 10e9; %配置参考时钟频率

 %直接加载IQS_StreamInfo_TypeDef结构体
load(fullfile(filePath, 'IQS_StreamInfo.mat')); 
IQS_StreamInfo_0 = IQS_StreamInfo;
IQS_StreamInfo_1 = IQS_StreamInfo;

%调用IQS_Configuration函数
Status = calllib('htra_api', 'IQS_Configuration', Device, IQS_ProfileIn, IQS_ProfileOut_0,IQS_StreamInfo_0);

% 如果配置下发成功（Status == 0）
if Status == 0
    fprintf('Configuration successfully\n');
    get(IQS_ProfileOut_0); %打印IQS_ProfileOut的值
    get(IQS_StreamInfo_0); %打印StreamInfo的值
else
    % 如果调用出错，打印错误信息并关闭设备
    fprintf('Configuration successfully\n');
    Device_Close(Device);
    
    % 返回 0，结束函数执行
    return;
end

%调用IQS_Configuration函数
Status_1 = calllib('htra_api', 'IQS_Configuration', Device_1, IQS_ProfileIn, IQS_ProfileOut_1,IQS_StreamInfo_1);

% 如果配置下发成功（Status_1 == 0）
if Status_1 == 0
    fprintf('Configuration delivery succeeded\n');
    get(IQS_ProfileOut_1); %打印IQS_ProfileOut的值
    get(IQS_StreamInfo_1); %打印StreamInfo的值
else
    % 如果调用出错，打印错误信息并关闭设备
    fprintf('IQS_Configuration Call error Status_1 = %d\n', Status_1);
    Device_Close(Device);  
    
    % 返回 0，结束函数执行
    return;
end

%直接加载 IQS_TriggerInfo_TypeDef 结构体 
load(fullfile(filePath, 'TriggerInfo.mat'));   
TriggerInfo_0 = TriggerInfo;
TriggerInfo_1 = TriggerInfo;

%直接加载 MeasAuxInfo_TypeDef 结构体 
load(fullfile(filePath, 'MeasAuxInfo.mat'));  
MeasAuxInfo_0 = MeasAuxInfo;
MeasAuxInfo_1 = MeasAuxInfo;

IQ_data = int16(1:IQS_StreamInfo_0.PacketSamples * 2);

% 交织分布的IQ时域数据，单路可能为i8 i16 i32 格式
AlternIQStream=libpointer('int16Ptr',IQ_data);

% int类型至电压绝对值（V）的系数
ScaleToV = single(0);                                           
ScaleToV_p=libpointer('singlePtr',ScaleToV);

IQ_data_1 = int16(1:IQS_StreamInfo_0.PacketSamples * 2);

% 交织分布的IQ时域数据，单路可能为i8 i16 i32 格式
AlternIQStream_1=libpointer('int16Ptr',IQ_data_1);

% int类型至电压绝对值（V）的系数
ScaleToV_1 = single(0);                                           
ScaleToV_p_1=libpointer('singlePtr',ScaleToV_1);

% 选择系统中的其中一台，进行触发信号的等待。该函数执行完说明系统已捕获到触发信号
Status = calllib('htra_api', 'IQS_MultiDevice_WaitExternalSync',Device,IQS_ProfileOut_0);

% 在系统捕获到触发信号后，使能多机同步运行
Status=calllib('htra_api', 'IQS_MultiDevice_Run',Device);
Status_1=calllib('htra_api', 'IQS_MultiDevice_Run',Device_1);

DET_data1 = 1:IQS_StreamInfo_0.StreamSamples;
DET_data2 = 1:IQS_StreamInfo_1.StreamSamples;

% 获取n次IQ数据
n = 10000;

for j = 1:n
    for i = 1:IQS_StreamInfo_0.PacketCount
        Status = calllib('htra_api', 'IQS_GetIQStream', Device, AlternIQStream, ScaleToV_p, TriggerInfo_0, MeasAuxInfo_0);
        Status_1 = calllib('htra_api', 'IQS_GetIQStream', Device_1, AlternIQStream_1, ScaleToV_p_1, TriggerInfo_1, MeasAuxInfo_1);
        if Status ~= 0
            % 如果调用出错，打印错误信息并关闭设备
            fprintf('IQS_GetIQStream Call error Status = %d\n', Status);
            return;
        elseif Status_1 ~= 0
            % 如果调用出错，打印错误信息并关闭设备
            fprintf('IQS_GetIQStream Call error Status_1 = %d\n', Status);

            % 返回 0，结束函数执行
            return;
        end
    end
    
    for t=1:IQS_StreamInfo_1.StreamSamples    
        DET_data1(t) = power(AlternIQStream.value(2 * t - 1), 2) + power(AlternIQStream.value(2 * t), 2);  
        DET_data2(t) = power(AlternIQStream_1.value(2 * t - 1), 2) + power(AlternIQStream_1.value(2 * t), 2);
        DET_data1(t) = 10*log10(DET_data1(t));
        DET_data2(t) = 10*log10(DET_data2(t));
    end
    plot(1:IQS_StreamInfo_1.StreamSamples,DET_data1,1:IQS_StreamInfo_1.StreamSamples,DET_data2);              
    axis([0, IQS_StreamInfo_1.StreamSamples - 1, 0, 50]);
    pause(0.001);

end

%调用Device_Close函数
Status = calllib('htra_api', 'Device_Close', Device);
%调用Device_Close函数
Status_1 = calllib('htra_api', 'Device_Close', Device_1);


clear all;
%卸载库文件
unloadlibrary('htra_api');   
disp('Uninstall complete')











