unit kconcomp;
{------------------------------------------------------------------------------}
{
  KComComp - This file include Kave 2000 Control components:
    TcInverter - inverter control
    TcEncoder  - encoder control

   Author:  Vesa Lappalainen
   Date:    09.09.1996
   Changes: 02.04.1997
             + simulointi otettu pois anturista.  Simulointi tapahtuu
               nykyisin K9CIO:n kautta, joka osaa simuloida.  

}
{------------------------------------------------------------------------------}

interface

uses
  Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, kComp,KSimComp,KYasPar,KGlobal,k9cio;

type
  TNotifyInvHz = procedure(hz:double) of object;

type
  {----------------------------------------------------------------------------}
  TcInverter = class(TCounter)
  private
    FsInverter : TsInverter;
    FParams : TYasParams;
    FNotifyHz : TNotifyInvHz;
    FInError : boolean;
    FErrorClr : TColor;
    FNormalClr : TColor;
  protected
    procedure SetFreq(f:double); virtual;
//    function GetFreq:double;     virtual;
    procedure SetParams(p:TYasParams); virtual;
    function GetParams:TYasParams;     virtual;
    function GetOK:Boolean;     virtual;
    function GetInUse:Boolean;     virtual;
    procedure SetInError(b:boolean);    virtual;
  public
    force : boolean;
    constructor Create(AOwner:TComponent); override;
    function AccAHz:double; virtual;
    function DecAHz:double; virtual;
    function MaxHz:double; virtual;
    function MinHz:double; virtual;
    property NotifyHz : TNotifyInvHz read FNotifyHz write FNotifyHz;
    property InError : boolean read FInError write SetInError;
    property OK : boolean read GetOK;
    property InUse : boolean read GetInUse;
    procedure ShowColor;
  published
    property Freq:double read FValue write SetFreq;
    property sInverter:TsInverter read FsInverter write FsInverter;
    property Params : TYasParams read GetParams write SetParams;
    property ErrorClr : TColor read FErrorClr write FErrorClr;
  end;

  {----------------------------------------------------------------------------}
  TcEncoder = class(TCounter)
  private
    FsEncoder : TsEncoder;
    FRatio : Double;
    Fcnt   : integer;
  protected
    procedure SetCount(c:Double);
    procedure SetRatio(r:Double);
    function GetCount:double;
  public
    constructor Create(AOwner:TComponent); override;
    Function Speed:double;
  published
    property Count:double read GetCount write SetCount;
    property Ratio:Double read FRatio write SetRatio;
    property sEncoder:TsEncoder read FsEncoder write FsEncoder;
    property cnt:integer read Fcnt write Fcnt;
  end;

procedure Register;

implementation

{------------------------------------------------------------------------------}
{ TcInverter ==================================================================}
{------------------------------------------------------------------------------}

function TcInverter.GetOK:Boolean;
begin
  Result := false;
  if ( InError ) then exit;
  if ( not Assigned(FParams) ) then exit;
  if ( FParams.Slave <= 0 ) then exit;
  Result := true;
end;

function TcInverter.GetInUse:Boolean;
begin
  Result := false;
  if ( not Assigned(FParams) ) then exit;
  if ( FParams.Slave = NotInUseSlave ) then exit;
  Result := true;
end;

procedure TcInverter.SetInError(b:boolean);
begin
  if ( b = InError ) then exit;
  if ( not InError ) then FNormalClr := Color;
  FInError := b;
  ShowColor;
end;

procedure TcInverter.ShowColor;
begin
  if ( Assigned(FParams) ) then begin
    if ( Params.Slave = NotInUseSlave ) then begin
      Color := clBlack;
      Exit;
    end;
    if ( Params.Slave < 0 ) then begin
      Color := clGray;
      Exit;
    end;
  end;
  if ( InError ) then Color := ErrorClr else Color := FNormalClr;
end;


function TcInverter.AccAHz:double;
begin
  Result := 1;
  if ( Assigned(FParams) ) then
    Result := FParams.AccAHz;
end;

{------------------------------------------------------------------------------}
function TcInverter.DecAHz:double;
begin
  Result := 1;
  if ( Assigned(FParams) ) then
    Result := FParams.DecAHz;
end;

{------------------------------------------------------------------------------}
function TcInverter.MaxHz:double;
begin
  Result := 1;
  if ( Assigned(FParams) ) then
    Result := FParams.ReadMaxHz;
end;

{------------------------------------------------------------------------------}
function TcInverter.MinHz:double;
begin
  Result := 1;
  if ( Assigned(FParams) ) then
    Result := FParams.ReadMinHz;
end;

{------------------------------------------------------------------------------}
procedure TcInverter.SetParams(p:TYasParams);
begin
  FParams := p;
end;

{------------------------------------------------------------------------------}
function TcInverter.GetParams:TYasParams;
begin
  Result := FParams;
end;

{------------------------------------------------------------------------------}
procedure TcInverter.SetFreq(f:double);
begin
  Value := f;
  if ( simulation and Assigned(FsInverter) ) then FsInverter.Freq := f;
  if ( not Simulation ) and Assigned(FParams)  then
    FParams.WriteRunHz(f);
  if ( assigned(FNotifyHz) ) then FNotifyHz(f);
end;

{------------------------------------------------------------------------------}
{
function TcInverter.GetFreq:double;
begin
  Result := Value;
end;
}

{------------------------------------------------------------------------------}
constructor TcInverter.Create(AOwner:TComponent);
begin
  force := false;
  FParams := NIL;
  FsInverter := NIL;
  FNotifyHz := NIL;
  FErrorClr := clRed;
  FNormalClr := clYellow;
  FInError := false;
  inherited Create(AOwner);
  FNormalClr := Color;
end;

{------------------------------------------------------------------------------}
{ TcEncoder ===================================================================}
{------------------------------------------------------------------------------}

{------------------------------------------------------------------------------}
constructor TcEncoder.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  FRatio := 1;
  Fcnt := 0;
  FsEncoder := NIL;
end;

{------------------------------------------------------------------------------}
procedure TcEncoder.SetRatio(r:Double);
begin
  FRatio := r;
  if ( r = 0 ) then FRatio := 1;
end;

{------------------------------------------------------------------------------}
procedure TcEncoder.SetCount(c:Double);
begin
  if ( cnt > 0 ) then begin SetCounter(cnt,c); end;
  value := c;
//  if ( ( not simulation ) and ( cnt > 0 ) ) then begin SetCounter(cnt,c); value := c; end
//  else if ( Assigned(sEncoder) ) then sEncoder.Count := c/Ratio;
end;

{------------------------------------------------------------------------------}
function TcEncoder.GetCount:double;
begin
  Result := Value;
  if ( ( cnt > 0 ) ) then Result := ReadCounter(cnt);
//  if ( ( not simulation ) and ( cnt > 0 ) ) then Result := ReadCounter(cnt)
//  else if ( Assigned(FsEncoder) ) then Result := FsEncoder.Count*Ratio;
  Value := Result;
end;

{------------------------------------------------------------------------------}
function TcEncoder.Speed:double;
begin
//  Result := 0;
  if ( ( cnt > 0 ) ) then Result := ReadCntSpeed(cnt)
//  if ( ( not simulation ) and ( cnt > 0 ) ) then Result := ReadCntSpeed(cnt)
  else Result := 0;  // Korjaa tämä simulointiin
end;

procedure Register;
begin
  RegisterComponents('Kave2000', [TcInverter,TcEncoder]);
end;

end.
