unit UDemoTCUSB16IO;

interface

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

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;
    RadioGroup1: TRadioGroup;
    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;
    Output5: TCheckBox;
    Output6: TCheckBox;
    Counter1: TStaticText;
    Counter2: TStaticText;
    Counter3: TStaticText;
    Counter4: TStaticText;
    Counter5: TStaticText;
    Counter6: TStaticText;
    Counter7: TStaticText;
    Counter8: TStaticText;
    PosCodeur: TStaticText;
    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 RadioGroup1Click(Sender: TObject);
    procedure RAZPosBtnClick(Sender: TObject);
    procedure Output1Click(Sender: TObject);
    procedure Output2Click(Sender: TObject);
    procedure Output3Click(Sender: TObject);
    procedure Output4Click(Sender: TObject);
    procedure Output5Click(Sender: TObject);
    procedure Output6Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmTest: TFrmTest;

{$INCLUDE TCUSB16IO.INC}

implementation

uses uSetup;

var
 AffCompteur : array [0..7] of TStaticText;
 EtatOut : array [0..5] of TCheckBox;
 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
      TCUSB16IO_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
          TCUSB16IO_Counter(1,i+1,Compteur);
          AffCompteur[i].Caption:=IntToStr(Compteur);
        end;
      TCUSB16IO_Pos(1,Codeur);
      PosCodeur.Caption:=IntToStr(Codeur);
    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:= TCUSB16IO_Open(1,NumSer);
            i:= TCUSB16IO_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 5 do EtatOut[i]:=TCheckBox(FindComponent('Output'+IntToStr(i+1)));
                TCUSB16IO_Refresh(1);
                TCUSB16IO_Version(1,vers1,vers2);
                Serial.Caption:=Serial.Caption+SerialNumber;
                FirmVer.Caption:=FirmVer.Caption+IntToStr(vers1);
                DllVer.Caption:=DllVer.Caption+IntToStr(vers2);
                TCUSB16IO_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:='Sequenceur OFF';
      for i:=0 to 5 do EtatOut[i].Enabled:=False;
      EtatOut[0].Checked:=True;
    end
  else
    begin
      AutoBtn.Caption:='Sequenceur ON';
      for i:=0 to 5 do EtatOut[i].Enabled:=True;
    end;
end;

procedure TFrmTest.Timer1Timer(Sender: TObject);
var i:integer;
begin
  i:=TCUSB16IO_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 5 do
    if EtatOut[i].Checked then
      begin
        EtatOut[i].Checked:=False;
        if i<5 then EtatOut[i+1].Checked:=True else EtatOut[0].Checked:=True;
        break;
      end;
  TraiteData;
end;

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

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

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

procedure TFrmTest.Output1Click(Sender: TObject);
begin
  if TCUSB16IO_BitReadBack(1,1)=0 then TCUSB16IO_BitOutPut(1,1,1) else TCUSB16IO_BitOutPut(1,1,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB16IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output2Click(Sender: TObject);
begin
  if TCUSB16IO_BitReadBack(1,2)=0 then TCUSB16IO_BitOutPut(1,2,1) else TCUSB16IO_BitOutPut(1,2,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB16IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output3Click(Sender: TObject);
begin
  if TCUSB16IO_BitReadBack(1,3)=0 then TCUSB16IO_BitOutPut(1,3,1) else TCUSB16IO_BitOutPut(1,3,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB16IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output4Click(Sender: TObject);
begin
  if TCUSB16IO_BitReadBack(1,4)=0 then TCUSB16IO_BitOutPut(1,4,1) else TCUSB16IO_BitOutPut(1,4,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB16IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output5Click(Sender: TObject);
begin
  if TCUSB16IO_BitReadBack(1,5)=0 then TCUSB16IO_BitOutPut(1,5,1) else TCUSB16IO_BitOutPut(1,5,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB16IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output6Click(Sender: TObject);
begin
  if TCUSB16IO_BitReadBack(1,6)=0 then TCUSB16IO_BitOutPut(1,6,1) else TCUSB16IO_BitOutPut(1,6,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB16IO_Refresh(1);
      TraiteData;
    end;
end;

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

procedure TFrmTest.FormCreate(Sender: TObject);
begin
  SerialNumber:='';
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.RadioGroup1Click(Sender: TObject);
begin
  if (TopZCheck.State=CBChecked) then TCUSB16IO_Config(1,8+(6-Radiogroup1.Itemindex))
                       else TCUSB16IO_Config(1,(6-Radiogroup1.Itemindex));
  TCUSB16IO_Refresh(1);
end;

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

end.
