{-------------------------------------------------------------------------------
 LIBRARY     : LIB_ToolManager.IPS
 PROJECT     :
 DESCRIPTION : Communication between MCD TestManager and Toolmonitors via the
               virtual interface
 PROGRAMMER  : 2011-03-01 / gt /MCD
 UPDATE      :
-------------------------------------------------------------------------------}
library;

{-------------------------------------------------------------------------------
 PROCEDURE   : TM_SetValue
 DESCRIPTION : Set a value
 INPUT       : fsName   : Value name
               fsValue  : The value
               frFactor : A factor to adjust the value
               frDB     : Convert from DB (0=none, 1=dBV, 2=dBu)
 OUTPUT      :
 PROGRAMMER  : 2011-03-01 / gt /MCD
-------------------------------------------------------------------------------}
procedure TM_SetValue(fsName, fsValue : string; frFactor, frDB : real);
var
  frValue : real;
  frIsString : real;
  vsTool : string;
begin

  vsTool := CutFirstToken(fsName, '.');

  if ToolManager.IsActive(vsTool) then begin

    if (Copy(fsValue,1,1) = '#') then begin
      frIsString := true;
      fsValue := Delete(fsValue,1,1);
    end else if (Copy(fsValue,1,1) = '$') then begin
      frValue := HexVal(Delete(fsValue,1,1),0);
    end else begin
      frValue := Val(fsValue,0);
    end;
    
    if not frIsString then begin
  
      if frFactor <> 0 then begin
        frValue := frValue * frFactor;
      end;
    
      if frDB = 1 then begin
        frValue := Math.Power(10, frValue/20);
      end else if frDB = 2 then begin
        frValue := Math.Power(10, frValue/20) * 0.77459667;
      end;
  
      ToolManager.SetValue(vsTool, fsName, frValue);
  
    end else begin
  
      ToolManager.SetValue(vsTool, fsName, fsValue);
  
    end;
  end;

end;

{-------------------------------------------------------------------------------
 FUNCTION    : TM_GetValue
 DESCRIPTION : Get a value
 INPUT       : fsName   : Value name
               frFactor : A factor to adjust the value
               frDB     : Convert to DB (0=none, 1=dBV, 2=dBu)
 OUTPUT      :
 PROGRAMMER  : 2011-03-01 / gt /MCD
-------------------------------------------------------------------------------}
function TM_GetValue(fsName : string; frFactor, frDB : real) : real;
var
  vsTool : string;
  frValue : real;
begin

  vsTool := CutFirstToken(fsName, '.');

  if ToolManager.IsActive(vsTool) then begin

    frValue := ToolManager.GetValue(vsTool, fsName);

    if frFactor <> 0 then begin
      frValue := frValue * frFactor;
    end;
  
    if frDB = 1 then begin
      if (frValue > 0) then begin
        frValue := 20 * Math.Log10(Abs(frValue));
      end;
    end else if frDB = 2 then begin
      if (frValue > 0) then begin
        frValue := 20 * Math.Log10(Abs(frValue/0.77459667));
      end else begin
        frValue := -999999999;
      end;
    end;

  end;
  result := frValue;

end;

{-------------------------------------------------------------------------------
 FUNCTION    : TM_GetStream
 DESCRIPTION : Get a stream
 INPUT       : fsName   : Stream name
               frFactor : A factor to adjust the value
               frDB     : Convert to DB (0=none, 1=dBV, 2=dBu)
 OUTPUT      :
 PROGRAMMER  : 2011-03-01 / gt /MCD
-------------------------------------------------------------------------------}
function TM_GetStream(fsName : string; frFactor, frDB : real) : vector;
var
  vsTool : string;
  vrIndex : real;
  fvValue : vector;
begin

  vsTool := CutFirstToken(fsName, '.');

  if ToolManager.IsActive(vsTool) then begin

    fvValue := ToolManager.GetStream(vsTool, fsName);

    if frFactor <> 0 then begin
      for vrIndex := 1 to Len(fvValue) do begin
        fvValue[vrIndex] := fvValue[vrIndex] * frFactor;
      end;
    end;
  
    if frDB = 1 then begin
      for vrIndex := 1 to Len(fvValue) do begin
        if (fvValue[vrIndex] > 0) then begin
          fvValue[vrIndex] := 20 * Math.Log10(Abs(fvValue[vrIndex]));
        end else begin
          fvValue[vrIndex] := -999999999;
        end;
      end;
    end else if frDB = 2 then begin
      for vrIndex := 1 to Len(fvValue) do begin
        if (fvValue[vrIndex] > 0) then begin
          fvValue[vrIndex] := 20 * Math.Log10(Abs(fvValue[vrIndex]/0.77459667));
        end else begin
          fvValue[vrIndex] := -999999999;
        end;
      end;
    end;

  end;
  result := fvValue;

end;

{-------------------------------------------------------------------------------
 PROCEDURE   : TM_SetAttributes
 DESCRIPTION : Set attributes
 INPUT       : fsValue      : Value name
               fsAttributes : Attributes list
 OUTPUT      :
 PROGRAMMER  : 2011-03-01 / gt /MCD
-------------------------------------------------------------------------------}
procedure TM_SetAttributes(fsValue, fsAttributes : string);
var
  sAttributes : string;
  sAttribute  : string;
  sAttributeName : string;
  sAttributeValue : string;

begin

  sAttributes := fsAttributes;
  while sAttributes <> '' do begin

    sAttribute := CutFirstToken ( sAttributes, ';' );
    sAttributeName := Trim(CutFirstToken ( sAttribute, '=' ));
    sAttributeValue := Trim(sAttribute);
  
    if sAttributeName[1] = '.' then begin
      sAttributeName := fsValue + sAttributeName;
    end;
    TM_SetValue (sAttributeName, sAttributeValue, 1, 0);
  
  end;

end;

end.
