{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
unit UOutFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, TeEngine, Series, TeeProcs, Chart, ExtCtrls, StdCtrls, ComCtrls,
  Math;

type
  TOutFrame = class(TFrame)
    Shape1: TShape;
    Panel1: TPanel;
    NomCanalLabel: TLabel;
    Shape2: TShape;
    Panel2: TPanel;
    Image2: TImage;
    Image1: TImage;
    Chart: TChart;
    Series1: TFastLineSeries;
    AnalogTB: TTrackBar;
    ONOFFPanel: TPanel;
    OFFImage: TImage;
    ONImage: TImage;
    Panel3: TPanel;
    ValueLabel: TLabel;
    procedure Image1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure OFFImageClick(Sender: TObject);
    procedure ONImageClick(Sender: TObject);
    procedure AnalogTBChange(Sender: TObject);
  private
    { Déclarations privées }
		c1 : int64;
    c2 : int64;
    freq : int64;
    time : double;
    firstTime : boolean;
    numInDocDOCOMETRE : integer;
		currentValue : double;
		currentValue2 : integer;
  public
    { Déclarations publiques }
    isInt : boolean;
		NumCanalCarteES : integer;
		BitNumCanalCarteES : int64;
    scale : double;
		cst : double;
		ONOFFChanged : boolean;
		analogChanged : boolean;
    procedure setNumInDocDOCOMETRE(n:integer);
		procedure maj_Chart;
		procedure majONOFFChange;
		procedure majAnalogChange;
    procedure setMinMaxGraphe(unite:boolean);
    constructor CreateOutFrame(AOwner : TComponent; Parent : TWinControl; Name : string;
                       isInt : boolean; numCanal : integer; amp : string);
  end;

implementation

uses UCalibMonitor, UDocometre;

{$R *.dfm}

constructor TOutFrame.CreateOutFrame(AOwner : TComponent; Parent : TWinControl; Name : string;
                       isInt : boolean; numCanal : integer; amp : string);
begin
  inherited Create(AOwner);
  firstTime := true;

	ONOFFChanged := false;
	analogChanged := false;

	Self.Parent := Parent;
  Self.Name := 'GrapheOut' + Name;
  NomCanalLabel.Caption := Name + ' - n°' + inttostr(numCanal);
  Align := alTop;
  Self.isInt := isInt;

  if not Self.isInt then
	begin
    NumCanalCarteES := numCanal;
    ONOFFPanel.Visible := false;
    AnalogTB.Visible := true;
    ValueLabel.Caption := '0.0000';
    if amp ='-10-10V' then
    begin
      cst := 10.0;
      scale := 20.0;
    end;
    if amp ='-5-5V' then
    begin
      cst := 5.0;
      scale := 10.0;
    end;
    if amp ='0-10V' then
    begin
      cst := 0.0;
      scale := 10.0;
    end;
  end else
  begin
		BitNumCanalCarteES := trunc(power(2,numCanal-1));
		NumCanalCarteES := numCanal;
    ONOFFPanel.Visible := true;
    AnalogTB.Visible := false;
    ValueLabel.Caption := '0';
    cst := 0.0;
    scale := 1.0;
  end;
  
  c1 := 0;
  c2 := 0;
  time := 0.0;
  firstTime := true;
  QueryPerformanceFrequency(Freq);
end;

procedure TOutFrame.setNumInDocDOCOMETRE(n:integer);
begin
  numInDocDOCOMETRE := n;
	setMinMaxGraphe(false);
end;

procedure TOutFrame.setMinMaxGraphe(unite:boolean);
var AMAX,AMIN : double;
begin
  if not isInt then
  begin

    AMAX := 10.0;
    AMIN := -10.0;
    if DocDOCOMETRE.CTRLSAN.AMPLITUDE = '-10-10V' then
    begin
      AMAX := 10.0;
      AMIN := -10.0;
    end;

    if DocDOCOMETRE.CTRLSAN.AMPLITUDE = '-5-5V' then
    begin
      AMAX := 5.0;
      AMIN := -5.0;
    end;

    if DocDOCOMETRE.CTRLSAN.AMPLITUDE = '0-10V' then
    begin
      AMAX := 10.0;
      AMIN := 0.0;
    end;

    chart.LeftAxis.Maximum := AMAX + 1;
    chart.LeftAxis.Minimum := AMIN - 1;

  end else
  begin
    chart.LeftAxis.Maximum := 2;
    chart.LeftAxis.Minimum := -1;
  end;
end;

procedure TOutFrame.Image1Click(Sender: TObject);
begin
  Chart.Visible := true;
  image2.Visible := true;
  image1.Visible := false;
  Panel2.Left := 1000;
end;

procedure TOutFrame.Image2Click(Sender: TObject);
begin
  Chart.Visible := false;
  image1.Visible := true;
  image2.Visible := false;
  Panel2.Left := 1000;
end;

procedure TOutFrame.maj_Chart;
var initialDigState : integer;
begin
  QueryPerformanceCounter(c2);
  if firstTime then
  begin
   if isInt then
   begin
     initialDigState := CalibMonitorForm.ADwin.Get_Par(13);
		 initialDigState := initialDigState and BitNumCanalCarteES;
     if initialDigState = 0
     then currentValue := 0
		 else currentValue := 1;
   end;
   firstTime := false
  end else time := time + (c2-c1)/Freq;
  c1 := c2;
  if isInt
  then ValueLabel.Caption := formatfloat('0',currentValue)
  else ValueLabel.Caption := formatfloat('#0.0000',currentValue);

  if time >= 10.0 then Chart.Series[0].Delete(0);
	chart.Series[0].AddXY(time,currentValue);
	chart.Repaint;
end;

procedure TOutFrame.majONOFFChange;
begin
  CalibMonitorForm.ADwin.Set_Par(8,trunc(currentValue));
	CalibMonitorForm.ADwin.Set_Par(7,NumCanalCarteES - 1);
  CalibMonitorForm.StatusBar.SimpleText := 'DIO N°' + inttostr(NumCanalCarteES) +
  ' mis à ' +  inttostr(trunc(currentValue)) ;
	ONOFFChanged := false;
end;

procedure TOutFrame.OFFImageClick(Sender: TObject);
begin
	ONOFFChanged := true;
	currentValue := 0;
	//CalibMonitorForm.ADwin.Set_Par(8,trunc(currentValue));
	//CalibMonitorForm.ADwin.Set_Par(7,NumCanalCarteES - 1);
end;

procedure TOutFrame.ONImageClick(Sender: TObject);
begin
	ONOFFChanged := true;
	currentValue := 1;
	//CalibMonitorForm.ADwin.Set_Par(8,trunc(currentValue));
	//CalibMonitorForm.ADwin.Set_Par(7,NumCanalCarteES - 1);
end;

procedure TOutFrame.majAnalogChange;
begin
  CalibMonitorForm.ADwin.Set_Par(6,currentValue2);
	CalibMonitorForm.ADwin.Set_Par(5,NumCanalCarteES);
	analogChanged := false;
end;

procedure TOutFrame.AnalogTBChange(Sender: TObject);
var AMAX,AMIN : double;
    pmin,pmax,p : integer;
begin
  AMAX := chart.LeftAxis.Maximum - 1 ;
  AMIN := chart.LeftAxis.Minimum + 1;
  p := AnalogTB.Position;
  pmin := AnalogTB.Min;
  pmax := AnalogTB.Max;
  currentValue :=(AMAX-AMIN)*(p-pmin)/(pmax-pmin) + AMIN;
	currentValue2 := trunc((currentValue - AMIN)/(AMAX - AMIN)*65535);
	analogChanged := true;
	//CalibMonitorForm.ADwin.Set_Par(6,s);
	//CalibMonitorForm.ADwin.Set_Par(5,NumCanalCarteES);
end;

end.
