library ADCU1432;

//{$DEFINE DEBUG}

uses
  Windows,
  Forms,
  Classes,
  SysUtils,
  Settings in 'Settings.pas' {SettingsForm},
  ADCDevice in 'ADCDevice.pas',
  Config in 'Config.pas',
  Edit in 'Edit.pas' {EditForm},
  Calib in 'Calib.pas';

{$E dlp}

{$R *.RES} // VersionInfo

type
  TArrayValues = array [0..CHANNEL_COUNT - 1] of double;
  TArrayStates = array [0..CHANNEL_COUNT - 1] of boolean;
  TArrayGains  = array [0..CHANNEL_COUNT - 1] of integer;

procedure ShowSettings; export; stdcall;
begin
  TSettingsForm.Execute;
end;

procedure Init(Handle : HWND; Path : PChar); export; stdcall;
begin
  DecimalSeparator := '.';
  Application.Handle := Handle;
  Calibration.Path   := StrPas(Path);
  Device := TADCDevice.Create;
end;

procedure Final; export; stdcall;
begin
  Application.Handle := 0;

  Device.Free;
end;

procedure DiveceList(var Text: PChar); export; stdcall;
var
  List : TStringList;
begin
  List := TStringList.create;
  try
    Device.DiveceList(List);
  finally
    Text := List.GetText;
    List.Free;
  end;
end;

function ConnectTo(const Inf: PChar):BOOL;export; stdcall;
begin
  Result := Device.DeviceSearch(Inf);
end;

procedure Disconnect;export; stdcall;
begin
  Calibration.CalibName := CALIB_DEFAULT;
  Device.Disconnect;
end;

procedure Start;export; stdcall;
begin
  Calibration.Load(Calibration.CalibName);
end;

procedure Stop;export; stdcall;
begin

end;

function Connected(const Inf: PChar):BOOL;export; stdcall;
begin
  Result := Device.isConnect(Inf);

  if Result then
     Calibration.CalibName := StrPas(Inf)
  else
     Calibration.CalibName := CALIB_DEFAULT;
end;

function GetCount:UCHAR; export; stdcall;
begin
  Result := CHANNEL_COUNT;
end;

procedure GetValues(var ArrayValues: TArrayValues; var ArrayStates: TArrayStates; var ArrayGains: TArrayGains); export; stdcall;
var
  i : integer;
begin
  for i := 0 to CHANNEL_COUNT - 1 do
  begin
    ArrayStates[i] := Configuration.General^.Channel[i];
    if ArrayStates[i] then
       Device.ReadADC(i, Configuration.General^.Gain[i], ArrayValues[i])
    else
       ArrayValues[i] := 0;
    ArrayGains[i] := Device.IndexToGain(Configuration.General^.Gain[i]);
  end;

end;

procedure About; export; stdcall;
begin
  Application.MessageBox('www.spetsribor.com', 'About...');
end;

exports
  Init,
  Final,
  DiveceList,
  ShowSettings,
  ConnectTo,
  Disconnect,
  Connected,
  GetCount,
  GetValues,
  Start,
  Stop,
  About;


end.
