library ADCS12x;

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

{$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);
    Text := List.GetText;
  finally
    List.Free;
  end;
end;

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

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

  if Result then
     Calibration.CalibName := 'ADCS-12-x'
  else
     Calibration.CalibName := CALIB_DEFAULT;
end;

procedure Disconnect;export; stdcall;
begin
  Calibration.CalibName := CALIB_DEFAULT;
  Device.Disconnect;
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
  if Configuration.General^.ADCS = 0 then
  begin
    Device.ReadADC0(ArrayValues[0]);
    ArrayStates[0] := Configuration.General^.Channel[0];
    ArrayGains[0]  := 0;
  end
  else
      for i := 0 to CHANNEL_COUNT - 1 do
      begin
        Device.ReadADC1(i, ArrayValues[i]);
        ArrayStates[i] := Configuration.General^.Channel[i];
        ArrayGains[i]  := 0;
      end;

end;

procedure Start;export; stdcall;
begin
  if Assigned(Calibration) then
      Calibration.Load(Calibration.CalibName);
  Device.Init_ADC;
end;

procedure Stop;export; stdcall;
begin

end;

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

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

end.
