% FFM CW transmit example.
% Optional compiler setup:
% setenv('MW_MINGW64_LOC','C:\msys64\mingw64');
% mex -setup

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

% List loaded API functions.
libfunctions('h2_api');

% Prepare device handles.
Device = libpointer('voidPtrPtr');     % Device handle
DevNum = uint8(0);                     % USB device index
Status = 0;                            % API status
chn = libpointer('uint16Ptr', uint16(0));  % Channel count

% Allocate channel buffer.
% device_open_usb can return up to 8 channels.
% Use 16 bytes per channel on 64-bit Windows.
% Layout: void* + uint16 + uint8 + padding.
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 = 0;
deviceinfo.uid_l64 = 0;
deviceinfo.uid_h32 = 0;
deviceinfo.hardware_version = 0;
deviceinfo.mfw_version = 0;
deviceinfo.ffw_version = 0;
deviceinfo.pmu_version = 0;
deviceinfo.agu_version = 0;
deviceinfo.bus_version = 0;
deviceinfo.eio_version = 0;
deviceinfo.bus_bandwidth = 0;

% Convert MATLAB struct to C struct pointer.
deviceinfo_p = libstruct('device_info', deviceinfo);

USE_ETH = false;  % true for Ethernet, false for USB

% Open device.
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

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

% RF parameters.
fc = 1.0e9;           % Carrier frequency
level = single(0.0);  % Output level

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

% Create trigger settings.
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);  % Infinite trigger count

% Convert MATLAB struct to C struct pointer.
txtrigger_p = libstruct('tx_trigger', txtrigger);

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

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

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

% Enable RF 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 10 seconds.
pause(10);

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

% Clear variables and unload library.
clear all;
if libisloaded('h2_api')
    unloadlibrary('h2_api');
end

disp('Uninstall complete');
