library ADCx148D;

//{$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);
{$IFNDEF DEBUG}
  Device := TADCDevice.Create;
{$ENDIF}
end;

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

{$IFNDEF DEBUG}
  Device.Free;
{$ENDIF}

end;

procedure DiveceList(var Text: PChar); export; stdcall;
var
  List : TStringList;
begin
  List := TStringList.create;
  try
{$IFDEF DEBUG}
    List.Add('ADCS14-8D Test1');
{$ELSE}
    Device.DiveceList(List);
{$ENDIF}
  finally
    Text := List.GetText;
    List.Free;
  end;
end;

function ConnectTo(const Inf: PChar):BOOL;export; stdcall;
begin
{$IFDEF DEBUG}
  Result := true;
{$ELSE}
  Result := Device.DeviceSearch(Inf);
{$ENDIF}
end;

procedure Disconnect;export; stdcall;
begin
  Calibration.CalibName := CALIB_DEFAULT;
{$IFNDEF DEBUG}
  Device.Disconnect;
{$ENDIF}
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
{$IFDEF DEBUG}
  Result := true;
{$ELSE}
  Result := Device.GetConnectInf(Inf);
{$ENDIF}

  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
{$IFDEF DEBUG}
  for i := 0 to Count - 1 do
     ArrayValues[i] := i;

  for i := 0 to Count - 1 do
     ArrayStates[i] := true;

  for i := 0 to Count - 1 do
     ArrayGains[i] := 1;
{$ELSE}
  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;
{$ENDIF}

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.
