unit UDemoTCUSB48IO;

interface

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

type
  TFrmTest = class(TForm)
    Panel1: TPanel;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label7: TLabel;
    Input1: TShape;
    Input2: TShape;
    Input3: TShape;
    Input4: TShape;
    Input5: TShape;
    Input6: TShape;
    Input7: TShape;
    Input8: TShape;
    Label3: TLabel;
    Input9: TShape;
    Input10: TShape;
    Input11: TShape;
    Input12: TShape;
    Input13: TShape;
    Input14: TShape;
    Input15: TShape;
    Input16: TShape;
    Label5: TLabel;
    Input17: TShape;
    Input18: TShape;
    Input19: TShape;
    Input20: TShape;
    Input21: TShape;
    Input22: TShape;
    Input23: TShape;
    Input24: TShape;
    Label6: TLabel;
    Input25: TShape;
    Input26: TShape;
    Input27: TShape;
    Input28: TShape;
    Input29: TShape;
    Input30: TShape;
    Input31: TShape;
    Input32: TShape;
    Label8: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    AutoBtn: TButton;
    WdgOnBtn: TButton;
    WdgOffBtn: TButton;
    WdgRearmBtn: TButton;
    Output1: TCheckBox;
    Output2: TCheckBox;
    Output3: TCheckBox;
    Output4: TCheckBox;
    Output5: TCheckBox;
    Output6: TCheckBox;
    Output7: TCheckBox;
    Output8: TCheckBox;
    Output9: TCheckBox;
    Output10: TCheckBox;
    Output11: TCheckBox;
    Output12: TCheckBox;
    Output13: TCheckBox;
    Output14: TCheckBox;
    Output15: TCheckBox;
    Output16: TCheckBox;
    RefreshBtn: TButton;
    Counter4: TStaticText;
    Counter16: TStaticText;
    Counter15: TStaticText;
    Counter14: TStaticText;
    Counter13: TStaticText;
    Counter12: TStaticText;
    Counter11: TStaticText;
    Counter10: TStaticText;
    Counter9: TStaticText;
    Counter8: TStaticText;
    Counter7: TStaticText;
    Counter6: TStaticText;
    Counter5: TStaticText;
    Counter3: TStaticText;
    Counter2: TStaticText;
    Counter1: TStaticText;
    Counter20: TStaticText;
    Counter32: TStaticText;
    Counter31: TStaticText;
    Counter30: TStaticText;
    Counter29: TStaticText;
    Counter28: TStaticText;
    Counter27: TStaticText;
    Counter26: TStaticText;
    Counter25: TStaticText;
    Counter24: TStaticText;
    Counter23: TStaticText;
    Counter22: TStaticText;
    Counter21: TStaticText;
    Counter19: TStaticText;
    Counter18: TStaticText;
    Counter17: TStaticText;
    GroupBox3: TGroupBox;
    Serial: TLabel;
    DllVer: TLabel;
    Timer1: TTimer;
    Timer2: TTimer;
    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 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);
    procedure Output7Click(Sender: TObject);
    procedure Output8Click(Sender: TObject);
    procedure Output9Click(Sender: TObject);
    procedure Output10Click(Sender: TObject);
    procedure Output11Click(Sender: TObject);
    procedure Output12Click(Sender: TObject);
    procedure Output13Click(Sender: TObject);
    procedure Output14Click(Sender: TObject);
    procedure Output15Click(Sender: TObject);
    procedure Output16Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmTest: TFrmTest;

{$INCLUDE TCUSB48IO.INC}

implementation

uses uSetup;

var

 EtatOut : array [0..15] of TCheckBox;
 SerialNumber: string;
 NumSer: PChar;
 SerialNumberN: DWord;

{$R *.DFM}

procedure TraiteData;
var i: integer;
    mask,Entree: longword;
    lu: byte;
begin
  with FrmTest do
    begin
      TCUSB48IO_Input(1,Entree);
      mask:=1;
      for i:=0 to 31 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;
          TCUSB48IO_Counter(1,i+1,lu);
          TStaticText(FindComponent('Counter'+IntToStr(i+1))).Caption:=IntToStr(lu);
        end;
    end;
end;

procedure TFrmTest.FormActivate(Sender: TObject);
var  i,vers1 : 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:= TCUSB48IO_Open(1,NumSer);
          i:= TCUSB48IO_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 15 do EtatOut[i]:=TCheckBox(FindComponent('Output'+IntToStr(i+1)));
              TCUSB48IO_Refresh(1);
              vers1:=TCUSB48IO_Version();
              DllVer.Caption:=DllVer.Caption+IntToStr(vers1);
              Serial.caption:=Serial.caption+IntToStr(SerialNumberN);
              TCUSB48IO_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 15 do EtatOut[i].Enabled:=False;
      EtatOut[0].Checked:=True;
    end
  else
    begin
      AutoBtn.Caption:='Sequencer ON';
      for i:=0 to 15 do EtatOut[i].Enabled:=True;
    end;
end;

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

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

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

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

procedure TFrmTest.Output1Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,1)=0 then TCUSB48IO_BitOutPut(1,1,1) else TCUSB48IO_BitOutPut(1,1,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

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

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

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

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

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

procedure TFrmTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Timer1.Enabled:=False;
  TCUSB48IO_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.RadioGroup1Click(Sender: TObject);
begin
  TCUSB48IO_Refresh(1);
end;

procedure TFrmTest.FormCreate(Sender: TObject);
begin
  SerialNumber:='';
end;

procedure TFrmTest.Output7Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,7)=0 then TCUSB48IO_BitOutPut(1,7,1) else TCUSB48IO_BitOutPut(1,7,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output8Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,8)=0 then TCUSB48IO_BitOutPut(1,8,1) else TCUSB48IO_BitOutPut(1,8,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output9Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,9)=0 then TCUSB48IO_BitOutPut(1,9,1) else TCUSB48IO_BitOutPut(1,9,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output10Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,10)=0 then TCUSB48IO_BitOutPut(1,10,1) else TCUSB48IO_BitOutPut(1,10,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output11Click(Sender: TObject);
begin
   if TCUSB48IO_BitReadBack(1,11)=0 then TCUSB48IO_BitOutPut(1,11,1) else TCUSB48IO_BitOutPut(1,11,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output12Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,12)=0 then TCUSB48IO_BitOutPut(1,12,1) else TCUSB48IO_BitOutPut(1,12,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output13Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,13)=0 then TCUSB48IO_BitOutPut(1,13,1) else TCUSB48IO_BitOutPut(1,13,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output14Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,14)=0 then TCUSB48IO_BitOutPut(1,14,1) else TCUSB48IO_BitOutPut(1,14,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output15Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,15)=0 then TCUSB48IO_BitOutPut(1,15,1) else TCUSB48IO_BitOutPut(1,15,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

procedure TFrmTest.Output16Click(Sender: TObject);
begin
  if TCUSB48IO_BitReadBack(1,16)=0 then TCUSB48IO_BitOutPut(1,16,1) else TCUSB48IO_BitOutPut(1,16,0);
  if not(Timer1.Enabled) then
    begin
      TCUSB48IO_Refresh(1);
      TraiteData;
    end;
end;

end.
