uses
  xIOADDA1,
  Crt,
  NumToStr;

const
  FlDif  : Boolean=false;
  OldInL : array[0..1] of Integer=(0,0);
  OldInH : array[0..1] of Integer=(0,0);
  DifIn  : array[0..1] of Integer=(0,0);
var
  IOADDA : pIOADDA1;

procedure WriteLnADDA;
var
  i,j : Integer;
begin
  with IOADDA^ do
  begin
    Write  ('o0='+WordStrHex(Word(AD_Out[0]))+' '+
            'o1='+WordStrHex(Word(AD_Out[1]))+' '+
            'i0='+WordStrHex(Word(AD_In[0]))+' '+
            'i1='+WordStrHex(Word(AD_In[1]))+' ');
    Write  ('o0=',AD_Out[0]:4,' '+
            'o1=',AD_Out[1]:4,' '+
            'i0=',AD_In[0]:4,' '+
            'i1=',AD_In[1]:4,' ');
    if FlDif then
    begin
      FlDif:=false;
      OldInL[0]:=AD_In[0];
      OldInL[1]:=AD_In[1];
      OldInH[0]:=AD_In[0];
      OldInH[1]:=AD_In[1];
      DifIn[0]:=0;
      DifIn[1]:=0;
    end;
    for i:=0 to 1 do
    begin
      if AD_In[i]<OldInL[i] then OldInL[i]:=AD_In[i];
      if AD_In[i]>OldInH[i] then OldInH[i]:=AD_In[i];
      DifIn[i]:=OldInH[i]-OldInL[i];
    end;
    WriteLn('d1=',DifIn[0]:2,' '+
            'd2=',DifIn[1]:2);
  end;
end;

procedure WSinUni(v1,v2: Integer);
begin
  with IOADDA^ do
  begin
{
    WriteDA(0,0);
    WriteDA(1,0);
}
    WriteDA(0,v1);
    WriteDA(1,v2);
{
    Delay(200);
}
    AD_In[0]:=ReadAD(0,false,false);
    AD_In[1]:=ReadAD(1,false,false);
    WriteLnADDA;
  end;
end;

procedure WSinBip(v1,v2: Integer);
var
  i : Integer;
begin
  with IOADDA^ do
  begin
{
    WriteDA(0,0);
    WriteDA(1,0);
}
    WriteDA(0,v1);
    WriteDA(1,v2);
{
    Delay(200);
}
    i:=ReadAD(0,false,true);
    i:=ReadAD(1,false,true);
    WriteLnADDA;
  end;
end;


const
  o : Integer=$800;

function TstKbd: Boolean;
var
  i,j : Integer;
  Str : String;
begin
  TstKbd:=false;
  if KeyPressed then
  begin
    Str:=' ';
    ReadLn(Str);
    case Str[1] of
      'x','X' : TstKbd:=true;
           else begin
                  Val(Str,i,j);
                  if j=0 then
                    o:=i;
                  FlDif:=true;
                end;
    end;
  end;
end;


begin
  WriteLn('Test desky IOADDA1.');
  IOADDA:=New(pIOADDA1,Init($300));
  if IOADDA=nil then RunError(203);

  with IOADDA^ do
  begin
    WriteLn('Test jednoduchych unipolarnich vstupu.');
    repeat
      WSinUni(o,o);
{
      Delay(100);
}
    until TstKbd;
    WriteLn('Test jednoduchych bipolarnich vstupu.');
    repeat
      WSinBip(o,o);
{
      Delay(100);
}
    until TstKbd;
  end;
end.