% FFM CW reconfiguration example.

% Optional compiler setup:
% setenv('MW_MINGW64_LOC','C:\msys64\mingw64');
% mex -setup

clear;
clc;

% Load h2_api.dll.
% Use DLL and header from .\h2_api.
if ~libisloaded('h2_api')
    loadlibrary('.\h2_api\h2_api.dll', '.\h2_api\h2_api_matlab.h', ...
        'alias', 'h2_api', 'includepath', '.\h2_api');
end

libfunctions('h2_api');

% Open device.
Device = libpointer('voidPtrPtr');
DevNum = uint8(0);
Status = int32(0);
chn = libpointer('uint16Ptr', uint16(0));

% Allocate channel buffer.
% Use a byte buffer; empty channel structs can fail in calllib.
CHANNEL_COUNT = 8;
CHANNEL_STRUCT_BYTES = 16;
ch_buf = zeros(1, CHANNEL_COUNT * CHANNEL_STRUCT_BYTES, 'uint8');
ch = libpointer('uint8Ptr', ch_buf);

% Create device_info.
% Fields must match h2_api_matlab.h.
deviceinfo.model = uint32(0);
deviceinfo.uid_l64 = uint64(0);
deviceinfo.uid_h32 = uint32(0);
deviceinfo.hardware_version = uint32(0);
deviceinfo.mfw_version = uint32(0);
deviceinfo.ffw_version = uint32(0);
deviceinfo.pmu_version = uint32(0);
deviceinfo.agu_version = uint32(0);
deviceinfo.bus_version = uint32(0);
deviceinfo.eio_version = uint32(0);
deviceinfo.bus_bandwidth = uint32(0);
deviceinfo_p = libstruct('device_info', deviceinfo);

USE_ETH = false;

if USE_ETH
    ETH_STANDARD = uint8(1);

    ethsettings.eth_interface = ETH_STANDARD;
    ethsettings.eth_is_ipv6 = uint8(0);
    ethsettings.eth_ip_address = zeros(1, 16, 'uint8');
    ethsettings.eth_ip_address(1:4) = uint8([192 168 1 100]);
    ethsettings.eth_remote_port = uint16(5000);
    ethsettings.eth_read_timeout = int32(5000);

    ethsettings_p = libstruct('eth_setting', ethsettings);
    Status = calllib('h2_api', 'device_open_eth', Device, ethsettings_p, ch, chn, deviceinfo_p);
    fprintf('device_open_eth: Status = %d\n', Status);
else
    Status = calllib('h2_api', 'device_open_usb', Device, DevNum, ch, chn, deviceinfo_p);
    fprintf('device_open_usb: Status = %d\n', Status);
end

if Status ~= 0
    fprintf('device_open failed, Status = %d\n', Status);
    try
        calllib('h2_api', 'device_close', Device);
    catch
    end
    if libisloaded('h2_api')
        unloadlibrary('h2_api');
    end
    return;
end

fprintf('channel count = %d\n', chn.Value);
get(deviceinfo_p);

% Configure FFM RF.
fc = 1.0e9;
level = single(0.0);

Status = calllib('h2_api', 'tx_config_ffm', ch, fc, level);
fprintf('tx_config_ffm: Status = %d\n', Status);

% Configure trigger.
% Build tx_trigger directly from h2_api_matlab.h.
txtrigger.source = uint8(0);  % TRIGGER_SOURCE_BUS
txtrigger.edge   = uint8(0);  % TRIGGER_EDGE_RISING
txtrigger.action = uint8(1);  % TRIGGER_ACTION_SWEEP
txtrigger.count  = int32(-1);
txtrigger_p = libstruct('tx_trigger', txtrigger);

Status = calllib('h2_api', 'channel_config_trigger', ch, txtrigger_p);
fprintf('channel_config_trigger: Status = %d\n', Status);

% Configure CW.
Status = calllib('h2_api', 'tx_config_cw', ch);
fprintf('tx_config_cw: Status = %d\n', Status);

STATE_ON = uint8(1);
STATE_OFF = uint8(0);

% Configure output.
Status = calllib('h2_api', 'tx_config_output', ch, STATE_ON, STATE_OFF);
fprintf('tx_config_output: Status = %d\n', Status);

% Start channel.
Status = calllib('h2_api', 'channel_start', ch);
fprintf('channel_start: Status = %d\n', Status);

% Send bus trigger.
Status = calllib('h2_api', 'channel_trigger_bus', ch);
fprintf('channel_trigger_bus: Status = %d\n', Status);

% Run for 2 seconds.
pause(2);

% Reconfigure and trigger again.
fc = 1.2e9;

Status = calllib('h2_api', 'tx_config_ffm', ch, fc, level);
fprintf('tx_config_ffm: Status = %d\n', Status);

Status = calllib('h2_api', 'tx_config_output', ch, STATE_ON, STATE_OFF);
fprintf('tx_config_output: Status = %d\n', Status);

Status = calllib('h2_api', 'channel_start', ch);
fprintf('channel_start: Status = %d\n', Status);

Status = calllib('h2_api', 'channel_trigger_bus', ch);
fprintf('channel_trigger_bus: Status = %d\n', Status);

% Run for 10 seconds.
pause(10);

% Close device.
Status = calllib('h2_api', 'device_close', Device);
fprintf('device_close: Status = %d\n', Status);

clear Device ch chn deviceinfo_p txtrigger_p;

if libisloaded('h2_api')
    unloadlibrary('h2_api');
end

disp('Uninstall complete');
