unit Vezerles;

interface

uses Esemeny;

procedure Varakozas(dt:TDateTime);

function  GetSensorBoolean(Sensor:byte):longint;
function  GetSensorRaw(Sensor:byte):longint;

procedure MotorVezerlesTime(Motor:string; Ido:TDateTime; IsCheckKalib:boolean);
procedure MotorVezerlesSensor(Motor:string; Sensor:byte; var dt:TDateTime);
procedure ResetHead;
procedure FejBeallit(fej:TFejTipus; Ido:TDateTime);
procedure Kalibralas;

implementation

uses SysUtils, Forms,
     Main;

function IsFuttatas:boolean;
begin
   Application.ProcessMessages;
   IsFuttatas := IsKalibralas or IsResetHead;
end;

procedure Varakozas(dt:TDateTime);
var ndt   :TDateTime;
begin
   ndt := now;
   while now<ndt+dt do;
end;

function GetSensorBoolean(Sensor:byte):longint;
begin
{   try}
      GetSensorBoolean := MainForm.SP.Poll(13,Sensor);
{Sensor pufferenek kiolvasasa miatt kellett a varakozas}
{      Varakozas(EgySec/100);}
{   except
      GetSensorBoolean := 0;
      Varakozas(2*EgySec/100);
   end;}
end;

function GetSensorRaw(Sensor:byte):longint;
begin
{   try}
      GetSensorRaw := MainForm.SP.Poll(12,Sensor);
{Sensor pufferenek kiolvasasa miatt kellett a varakozas}
{      Varakozas(EgySec/100);}
{   except
      GetSensorRaw := 0;
      Varakozas(2*EgySec/100);
   end;}
end;

procedure MotorVezerlesTime(Motor:string; Ido:TDateTime; IsCheckKalib:boolean);
begin
   if IsOKTesztKalib or not IsCheckKalib then begin
      MainForm.SP.On_(Motor);
      Varakozas(Ido);
      MainForm.SP.Off(Motor);
   end;
end;

procedure MotorVezerlesSensor(Motor:string; Sensor:byte; var dt:TDateTime);
const MaxTime = 2*EgySec;
var IsMotor    :boolean;
begin
   if IsOKTesztInit then begin
      IsMotor := true;
      dt      := now;
      MainForm.SP.On_(Motor);
      while (GetSensorBoolean(Sensor)<>0) and (now<dt+MaxTime) and IsFuttatas do;
      while IsMotor and IsFuttatas do begin
         if GetSensorBoolean(Sensor)<>0 then begin
            dt := now-dt;
            MainForm.SP.Off(Motor);
            IsMotor := false;
         end;
      end;
      MainForm.SP.Off(Motor);  {Biztos, ami biztos.}
   end;
end;

procedure ResetHead;
{Bal felsõ sarokba, (0,0)-ra mozgatás.}
var dt    :TDateTime;
begin
   if IsOKTesztInit then begin
      MainForm.SP.SetPower(MotorX+MotorY,scConst,spScanner);
      MainForm.SP.SetRwd(MotorX+MotorY);
      IsResetHead := true;
      MotorVezerlesSensor(MotorX,SensorX,dt);
      MotorVezerlesSensor(MotorY,SensorY,dt);
      IsResetHead := false;
   end;
end;

procedure FejBeallit(fej:TFejTipus; Ido:TDateTime);
begin
   if IsOKTesztInit then begin
      MainForm.SP.SetPower(MotorS,scConst,spFej);
      if (fej=fejIras) then MainForm.SP.SetFwd(MotorS)
                       else MainForm.SP.SetRwd(MotorS);
      MotorVezerlesTime(MotorS,Ido,false);
   end;
end;

procedure Kalibralas;
begin
   FejBeallit(fejNemIras,idoFejMozgas);
   if IsKalibralas then ResetHead;

   {Pozitiv irany tesztje}
   MainForm.SP.SetPower(MotorX+MotorY,scConst,spScanner);
   MainForm.SP.SetFwd(MotorX+MotorY);
   if IsKalibralas then MotorVezerlesSensor(MotorX,SensorX,dtxp);
   if IsKalibralas then MotorVezerlesSensor(MotorY,SensorY,dtyp);

   {Negativ irany tesztje}
   MainForm.SP.SetRwd(MotorX+MotorY);
   if IsKalibralas then MotorVezerlesSensor(MotorX,SensorX,dtxn);
   if IsKalibralas then MotorVezerlesSensor(MotorY,SensorY,dtyn);

   AktX := 0;
   AktY := 0;
end;

end.
