{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 UinFrame;

interface

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

type
  TInFrame = class(TFrame)
    Panel1: TPanel;
    NomCanalLabel: TLabel;
    ValueLabel: TLabel;
    Shape1: TShape;
    Shape2: TShape;
    Panel2: TPanel;
    Image2: TImage;
    Image1: TImage;
    CalibPanel: TPanel;
    valBruteRB: TRadioButton;
    valCalibRB: TRadioButton;
    Chart: TChart;
    Panel3: TPanel;
    CalibImage: TImage;
    Series1: TFastLineSeries;
    CheckBox1: TCheckBox;
    procedure Image1Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure CalibImageClick(Sender: TObject);
    procedure valBruteRBClick(Sender: TObject);
    procedure valCalibRBClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    { Déclarations privées }
    numInDocDOCOMETRE : integer;
    c1 : int64;
    c2 : int64;
    freq : int64;
    time : double;
    firstTime : boolean;
  public
    { Déclarations publiques }
    isInt : boolean;
    NumCanalCarteES : integer;
    scale : double;
    cst : double;
    procedure setNumInDocDOCOMETRE(n:integer);
    procedure maj_Chart;
    procedure setMinMaxGraphe(unite:boolean);
    constructor CreateInFrame(AOwner : TComponent; Parent : TWinControl; Name : string;
                       isInt : boolean; numCanal : integer; amp : string);
  end;

implementation

uses UCalibMonitor, UDocometre, UCalib, UGlobal;

{$R *.dfm}

constructor TInFrame.CreateInFrame(AOwner : TComponent; Parent : TWinControl; Name : string;
                       isInt : boolean; numCanal : integer; amp : string);
begin
  inherited Create(AOwner);

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

  if not Self.isInt then
  begin
    NumCanalCarteES := numCanal;
    CalibPanel.Visible := true;
    CalibImage.Visible := true;
    ValueLabel.Caption := '0.0';
    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
    NumCanalCarteES := round(power(2,numCanal-1));
    CalibPanel.Visible := false;
    CalibImage.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 TInFrame.setNumInDocDOCOMETRE(n:integer);
begin
  numInDocDOCOMETRE := n;
  setMinMaxGraphe(false);
end;

procedure TInFrame.setMinMaxGraphe(unite : boolean);
var AMAX,AMIN,UMAX,UMIN,temp : double;
begin
  if not isInt then
  begin
    AMAX := DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMAX;
    AMIN := DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMIN;
    UMAX := DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMAX;
    UMIN := DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMIN;

    if AMAX<AMIN then
    begin
      temp := AMAX; AMAX := AMIN; AMIN := temp;
    end;

    if UMAX<UMIN then
    begin
      temp := UMAX; UMAX := UMIN; UMIN := temp;
    end;

    if unite then
    begin
      chart.LeftAxis.Maximum := UMAX + 1;
      chart.LeftAxis.Minimum := UMIN - 1;
    end else
    begin
      chart.LeftAxis.Maximum := AMAX + 1;
      chart.LeftAxis.Minimum := AMIN - 1;
    end;
  end else
  begin
    chart.LeftAxis.Maximum := 2;
    chart.LeftAxis.Minimum := -1;
  end;
end;

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

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

procedure TInFrame.maj_Chart;
var x : integer;
    y,AMAX,AMIN,UMAX,UMIN : double;
begin
  QueryPerformanceCounter(c2);
  if firstTime
  then firstTime := false
  else time := time + (c2-c1)/Freq;
  c1 := c2;
  
  if isInt then
  begin
    CalibMonitorForm.ADwin.Set_Par(3,NumCanalCarteES);
    repeat
    until (CalibMonitorForm.ADwin.Get_Par(3) = 0) or (CalibMonitorForm.ADwin.Process_Status(num_process) = 0);
    x := CalibMonitorForm.ADwin.Get_Par(4);
    if x = 0 then y := 0 else y := 1;
    ValueLabel.Caption := formatfloat('0',y);
  end else
  begin
    AMAX := DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMAX;
    AMIN := DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMIN;
    UMAX := DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMAX;
    UMIN := DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMIN;
    CalibMonitorForm.ADwin.Set_Par(14,DocDOCOMETRE.EAN[numInDocDOCOMETRE].GAIN);
    CalibMonitorForm.ADwin.Set_Par(1,NumCanalCarteES);
    repeat
    until (CalibMonitorForm.ADwin.Get_Par(1) = 0) or (CalibMonitorForm.ADwin.Process_Status(num_process) = 0);
    x := CalibMonitorForm.ADwin.Get_Par(2);
    y := scale*x/65535 - cst;
    if valCalibRB.Checked
    then y := (UMAX - UMIN)/(AMAX - AMIN)*(y - AMIN) + UMIN;
    ValueLabel.Caption := formatfloat('#0.0000',y);
  end;

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

end;

procedure TInFrame.CalibImageClick(Sender: TObject);
begin
  CalibForm.UnitLabel1.Caption := DocDOCOMETRE.EAN[numInDocDOCOMETRE].UNITE;
  CalibForm.UnitLabel2.Caption := DocDOCOMETRE.EAN[numInDocDOCOMETRE].UNITE;
  CalibForm.valMinVoltLE.Text := floattostr(DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMIN);
  CalibForm.valMaxVoltLE.Text := floattostr(DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMAX);
  CalibForm.valMinUnitLE.Text := floattostr(DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMIN);
  CalibForm.valMaxUnitLE.Text := floattostr(DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMAX);
  CalibForm.aInFrame := self;

  if CalibForm.ShowModal = mrOK then
  begin
    DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMIN := strtofloat(CalibForm.valMinVoltLE.Text);
    DocDOCOMETRE.EAN[numInDocDOCOMETRE].AMAX:= strtofloat(CalibForm.valMaxVoltLE.Text);
    DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMIN := strtofloat(CalibForm.valMinUnitLE.Text);
    DocDOCOMETRE.EAN[numInDocDOCOMETRE].UMAX := strtofloat(CalibForm.valMaxUnitLE.Text);
  end;

  setMinMaxGraphe(valCalibRB.Checked);
end;

procedure TInFrame.valBruteRBClick(Sender: TObject);
begin
  setMinMaxGraphe(false);
end;

procedure TInFrame.valCalibRBClick(Sender: TObject);
begin
  setMinMaxGraphe(true);
end;

procedure TInFrame.CheckBox1Click(Sender: TObject);
begin
  Chart.LeftAxis.Automatic := CheckBox1.Checked;
  setMinMaxGraphe(valCalibRB.Checked);
end;

end.
