unit UDemoTCUSB18A;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, ExtCtrls, ComCtrls;

type
  TFrmTest = class(TForm)
    Panel1: TPanel;
    Timer1: TTimer;
    Timer2: TTimer;
    RefreshBtn: TButton;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label3: TLabel;
    Label7: TLabel;
    Label5: TLabel;
    AutoBtn: TButton;
    WdgOnBtn: TButton;
    WdgOffBtn: TButton;
    WdgRearmBtn: TButton;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label2: TLabel;
    RAZPosBtn: TButton;
    TopZCheck: TCheckBox;
    GroupBox3: TGroupBox;
    Serial: TLabel;
    FirmVer: TLabel;
    DllVer: TLabel;
    Input1: TShape;
    Input2: TShape;
    Input3: TShape;
    Input4: TShape;
    Input5: TShape;
    Input6: TShape;
    Input7: TShape;
    Input8: TShape;
    Output1: TCheckBox;
    Input9: TShape;
    Input10: TShape;
    Input11: TShape;
    Output2: TCheckBox;
    Output3: TCheckBox;
    Output4: TCheckBox;
    Counter1: TStaticText;
    Counter2: TStaticText;
    Counter3: TStaticText;
    Counter4: TStaticText;
    Counter5: TStaticText;
    Counter6: TStaticText;
    Counter7: TStaticText;
    Counter8: TStaticText;
    PosCodeur: TStaticText;
    GroupBox4: TGroupBox;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Edit_vout1: TEdit;
    Edit_vout2: TEdit;
    Edit_vin1: TEdit;
    Edit_vin2: TEdit;
    Edit_vin3: TEdit;
    Edit_vin4: TEdit;
    UpDown_VOUT1: TUpDown;
    UpDown_VOUT2: TUpDown;
    procedure AutoBtnClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure WdgOnBtnClick(Sender: TObject);
    procedure WdgOffBtnClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure RefreshBtnClick(Sender: TObject);
    procedure WdgRearmBtnClick(Sender: TObject);
    procedure RAZPosBtnClick(Sender: TObject);
    procedure Output1Click(Sender: TObject);
    procedure Output2Click(Sender: TObject);
    procedure Output3Click(Sender: TObject);
    procedure Output4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure UpDown_VOUT1Click(Sender: TObject; Button: TUDBtnType);
    procedure UpDown_VOUT2Click(Sender: TObject; Button: TUDBtnType);
    procedure TopZCheckClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmTest: TFrmTest;

{$INCLUDE TCUSB18A.INC}

implementation

uses uSetup;

var
 AffCompteur :array [0..7] of TStaticText;
 EtatOut :    array [0..3] of TCheckBox;
 VoutValue:   array [1..2] of Double;
 VinValue:    array [1..4]  of Double;
 SerialNumber: string;
 NumSer: PChar;
 SerialNumberN: DWord;

{$R *.DFM}

procedure TraiteData;
var i: integer;
    mask,Entree: longword;
    Compteur: byte;
    Codeur: Integer;
begin
  with FrmTest do
    begin
      TCUSB18A_Input(1,Entree);
      mask:=1;
      for i:=0 to 7 do
        begin
          if (Entree and (mask shl i))<>0 then Tshape(FindComponent('Input'+IntToStr(i+1))).Brush.Color:=ClLime else Tshape(FindComponent('Input'+IntToStr(i+1))).Brush.Color:=ClGreen;
        end;
      for i:=0 to 2 do
        begin
          if ((Entree shr 8) and (mask shl i))<>0 then Tshape(FindComponent('Input'+IntToStr(i+9))).Brush.Color:=ClLime else Tshape(FindComponent('Input'+IntToStr(i+9))).Brush.Color:=ClGreen;
        end;
      for i:=0 to 7 do
        begin
          TCUSB18A_Counter(1,i+1,Compteur);
          AffCompteur[i].Caption:=IntToStr(Compteur);
        end;
      TCUSB18A_Pos(1,Codeur);
      PosCodeur.Caption:=IntToStr(Codeur);

      for i:=1 to 2 do TCUSB18A_AnaOutVolts(1,i,VoutValue[i]);
      for i:=1 to 4 do
          begin
               TCUSB18A_AnaInVolts (1,i,VinValue[i]);
               TEdit(FindComponent('Edit_vin'+IntToStr(i))).text:=floattostrf(VinValue[i],ffFixed,3,3) + ' V';
          end;

    end;
end;

procedure TFrmTest.FormActivate(Sender: TObject);
var  i,vers1,vers2 : Byte;
begin
  if length(SerialNumber)=0 then
    begin
      SerialNumber:=GetSerial;
      if SerialNumber='QUIT' then Application.Terminate
      else
        begin
          NumSer:=PChar(SerialNumber);
          SerialNumberN:=StrToInt64Def(SerialNumber,0);
          //i:= TCUSB18A_Open(1,NumSer);
          i:= TCUSB18A_OpenN(1,SerialNumberN);
          SerialNumber:=IntToStr(SerialNumberN);
          if i<>erNoError then
            begin
              MessageDlg('Erreur ouverture module (Erreur '+IntToStr(i)+')', MtError, [mbOk], 0);
              Application.Terminate;
            end
          else
            begin
              for i:=0 to 7 do AffCompteur[i]:=TStaticText(FindComponent('Counter'+IntToStr(i+1)));
              for i:=0 to 3 do EtatOut[i]:=TCheckBox(FindComponent('Output'+IntToStr(i+1)));
              TCUSB18A_Refresh(1);
              TCUSB18A_Version(1,vers1,vers2);
              Serial.Caption:=Serial.Caption+SerialNumber;
              FirmVer.Caption:=FirmVer.Caption+IntToStr(vers1);
              DllVer.Caption:=DllVer.Caption+IntToStr(vers2);
              TCUSB18A_WdgStop(1);
              AutoBtn.Enabled:=True;
              RefreshBtn.Enabled:=True;
              TraiteData;
              Timer1.Enabled:=True;
            end;
        end;
    end;
end;

procedure TFrmTest.AutoBtnClick(Sender: TObject);
var i: integer;
begin
  Timer2.Enabled:=not(Timer2.Enabled);
  if Timer2.Enabled then
    begin
      AutoBtn.Caption:='Sequencer OFF';
      for i:=0 to 3 do EtatOut[i].Enabled:=False;
      EtatOut[0].Checked:=True;
    end
  else
    begin
      AutoBtn.Caption:='Sequencer ON';
      for i:=0 to 3 do EtatOut[i].Enabled:=True;
    end;
end;

procedure TFrmTest.Timer1Timer(Sender: TObject);
var i:integer;
begin
  i:=TCUSB18A_Refresh(1);
  if i>0 then MessageDlg('Erreur dialogue module', mtError, [mbOk], 0) else TraiteData;
end;

procedure TFrmTest.Timer2Timer(Sender: TObject);
var i: integer;
begin
  for i:=0 to 3 do
    if EtatOut[i].Checked then
      begin
        EtatOut[i].Checked:=False;
        if i<3 then EtatOut[i+1].Checked:=True else EtatOut[0].Checked:=True;
        break;
      end;
  TraiteData;
end;

procedure TFrmTest.TopZCheckClick(Sender: TObject);
begin
  if (TopZCheck.State=CBChecked) then TCUSB18A_Config(1,8)
                       else TCUSB18A_Config(1,0);
  TCUSB18A_Refresh(1);
end;

procedure TFrmTest.WdgOnBtnClick(Sender: TObject);
begin
  TCUSB18A_WdgRun(1);
end;

procedure TFrmTest.WdgOffBtnClick(Sender: TObject);
begin
  TCUSB18A_WdgStop(1);
end;

procedure TFrmTest.WdgRearmBtnClick(Sender: TObject);
begin
  TCUSB18A_WdgRearm(1);
end;

procedure TFrmTest.Output1Click(Sender: TObject);
var test:byte;
begin

  TCUSB18A_BitReadBack(1,1,test);
  if test=0 then TCUSB18A_BitOutPut(1,1,1) else TCUSB18A_BitOutPut(1,1,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB18A_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output2Click(Sender: TObject);
var test:byte;
begin
  TCUSB18A_BitReadBack(1,2,test);
  if test=0  then TCUSB18A_BitOutPut(1,2,1) else TCUSB18A_BitOutPut(1,2,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB18A_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output3Click(Sender: TObject);
var test:byte;
begin
  TCUSB18A_BitReadBack(1,3,test);
  if test=0  then TCUSB18A_BitOutPut(1,3,1) else TCUSB18A_BitOutPut(1,3,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB18A_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output4Click(Sender: TObject);
var test:byte;
begin
  TCUSB18A_BitReadBack(1,4,test);
  if test=0 then TCUSB18A_BitOutPut(1,4,1) else TCUSB18A_BitOutPut(1,4,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB18A_Refresh(1);
      TraiteData;
    end;
end;



procedure TFrmTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Timer1.Enabled:=False;
  TCUSB18A_Close(1);
end;

procedure TFrmTest.RefreshBtnClick(Sender: TObject);
begin
  Timer1.Enabled:=not(Timer1.Enabled);
  if Timer1.Enabled then
    begin
      WdgOnBtn.Enabled:=False;
      WdgOffBtn.Enabled:=False;
      WdgRearmBtn.Enabled:=False;
      RefreshBtn.Caption:='Stop AutoRefresh'
    end
  else
    begin
      WdgOnBtn.Enabled:=True;
      WdgOffBtn.Enabled:=True;
      WdgRearmBtn.Enabled:=True;
      RefreshBtn.Caption:='Start AutoRefresh';
    end;
end;

procedure TFrmTest.RAZPosBtnClick(Sender: TObject);
begin
  TCUSB18A_ResetPos(1);
end;

procedure TFrmTest.FormCreate(Sender: TObject);
var i:byte;
begin
  SerialNumber:='';
  Edit_vout1.text:='';Edit_vout2.text:='';
  Edit_vin1.text:=''; Edit_vin2.text:='';
  Edit_vin3.text:=''; Edit_vin4.text:='';
  for i:=1 to 4 do VinValue[i]:=0;
  VoutValue[1]:=0;
  VoutValue[2]:=0;
  Edit_vout1.text:=floattostrf(VoutValue[1],ffFixed,2,2)+ ' V';
  Edit_vout2.text:=floattostrf(VoutValue[2],ffFixed,2,2)+ ' V';
end;

procedure TFrmTest.UpDown_VOUT1Click(Sender: TObject; Button: TUDBtnType);
begin
VoutValue[1]:=UpDown_VOUT1.position/10;
Edit_vout1.text:=floattostrf(VoutValue[1],ffFixed,2,2)+ ' V';
end;

procedure TFrmTest.UpDown_VOUT2Click(Sender: TObject; Button: TUDBtnType);
begin
VoutValue[2]:=UpDown_VOUT2.position/10;
Edit_vout2.text:=floattostrf(VoutValue[2],ffFixed,2,2) + ' V';
end;

end.
