unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  OleCtrls,  StdCtrls, FTROCKEYCONTROLLib_TLB;

type
  TForm1 = class(TForm)
    RockeyCtrl1: TFtRockeyControl;
    Find: TButton;
    FindNext: TButton;
    Open: TButton;
    Write: TButton;
    Read: TButton;
    Random: TButton;
    Seed: TButton;
    UserId: TButton;
    ReadUserId: TButton;
    Demo: TButton;
    SetModule: TButton;
    CheckModule: TButton;
    cal1: TButton;
    Cal2: TButton;
    Cal3: TButton;
    List1: TListBox;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FindClick(Sender: TObject);
    procedure OpenClick(Sender: TObject);
    procedure FindNextClick(Sender:TObject);
    procedure WriteClick(Sender:TObject) ;
    procedure ReadClick(Sender:TObject) ;
    procedure RandomClick(Sender:TObject) ;
    procedure SeedClick(Sender:TObject) ;
    procedure UserIdClick(Sender:TObject) ;
    procedure ReadUserIdClick(Sender:TObject) ;
    procedure SetModuleClick(Sender:TObject) ;
    procedure CheckModuleClick(Sender:TObject) ;
    procedure cal1Click(Sender:TObject) ;
    procedure Cal2Click(Sender:TObject) ;
    procedure Cal3Click(Sender:TObject) ;
    procedure WriteArithmetic(cmd:String;idx:Integer);
    Procedure ExecCalculate(idx : Integer);
    procedure DemoClick(Sender: TObject);
    procedure CloseRockey;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
Const RY_FIND = 1                      ;    // Find Dongle
Const RY_FIND_NEXT = 2                 ;    // Find Next Dongle
Const RY_OPEN = 3                      ;    // Open Dongle
Const RY_CLOSE = 4                     ;    // Close Dongle
Const RY_READ = 5                      ;    // Read Dongle
Const RY_WRITE = 6                     ;    // Write Dongle
Const RY_RANDOM = 7                    ;    // Generate Random Number
Const RY_SEED = 8                      ;    // Generate Seed Code
Const RY_WRITE_USERID = 9              ;    // Write User ID
Const RY_READ_USERID = 10              ;    // Read User ID
Const RY_SET_MODULE = 11               ;    // Set Module
Const RY_CHECK_MODULE = 12             ;    // Check Module
Const RY_WRITE_ARITHMETIC = 13         ;    // Write Arithmetic
Const RY_CALCULATE1 = 14               ;    // Calculate 1
Const RY_CALCULATE2 = 15               ;    // Calculate 2
Const RY_CALCULATE3 = 16               ;    // Calculate 3
Const RY_DECREASE = 17                 ;    // Decrease Module UnitConst
const cmd0 = 'H=H^H, A=A*23, F=B*17, A=A+F, A=A+G, A=A<C, A=A^D, B=B^B, C=C^C, D=D^D';
Const cmd1 = 'A=A+B, A=A+C, A=A+D, A=A+E, A=A+F, A=A+G, A=A+H';
Const cmd2 = 'A=E|E, B=F|F, C=G|G, D=H|H';
var
    err : integer;
    sd1 : integer;
    sd2 : integer;
    sd3 : integer;
    sd4 : integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
    RockeyCtrl1.P1 := smallInt($C44C);
    RockeyCtrl1.P2 := smallInt($C8F8);
    RockeyCtrl1.P3 := smallInt($799);
    RockeyCtrl1.P4 := smallInt($C43B);
end;

procedure TForm1.FindClick(Sender: TObject);
begin
     err := RockeyCtrl1.Rockey(RY_FIND);
     List1.Items.add('Find');
     List1.Items.Add(RockeyCtrl1.ErrToStr(err));
     List1.Items.Add('Find Rockey : ' + IntToStr(RockeyCtrl1.Lp1));
end;

procedure TForm1.OpenClick(Sender: TObject);
begin
    err := RockeyCtrl1.Rockey(RY_OPEN);
    List1.Items.Add('Open');
    List1.Items.Add(RockeyCtrl1.ErrToStr(err));
end;
procedure  TForm1.FindNextClick(Sender:TObject);
begin
    err := RockeyCtrl1.Rockey(RY_FIND_NEXT);
    List1.Items.Add( 'Find Next');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
end;

procedure  TForm1.WriteClick(Sender:TObject);
begin
    RockeyCtrl1.P1 := 3;
    RockeyCtrl1.P2 := 5;
    RockeyCtrl1.Buffer := 'Hello';
    err := RockeyCtrl1.Rockey(RY_WRITE);
    List1.Items.Add( 'Write');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
end;

procedure  TForm1.ReadClick(Sender:TObject);
begin
    RockeyCtrl1.P1 := 3;
    RockeyCtrl1.P2 := 5;
    err := RockeyCtrl1.Rockey(RY_READ);
    List1.Items.Add( 'Read');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add( RockeyCtrl1.Buffer);
end;

procedure  TForm1.RandomClick(Sender:TObject);
begin
    err := RockeyCtrl1.Rockey(RY_RANDOM);
    List1.Items.Add( 'Random');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add( IntToStr(RockeyCtrl1.P1));
end;

procedure  TForm1.SeedClick(Sender:TObject);
begin
    RockeyCtrl1.Lp2 := $12334578;
    err := RockeyCtrl1.Rockey(RY_SEED);
    List1.Items.Add( 'Seed');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add(IntToStr(RockeyCtrl1.P1));
    List1.Items.Add(IntToStr(RockeyCtrl1.P2));
    List1.Items.Add(IntToStr(RockeyCtrl1.P3));
    List1.Items.Add(IntToStr(RockeyCtrl1.P4));
    sd1 := RockeyCtrl1.P1;
    sd2 := RockeyCtrl1.P2;
    sd3 := RockeyCtrl1.P3;
    sd4 := RockeyCtrl1.P4;
end;
procedure  TForm1.UserIdClick(Sender:TObject);
begin
    RockeyCtrl1.Lp1 := integer($88888888);
    err := RockeyCtrl1.Rockey(RY_WRITE_USERID);
    List1.Items.Add( 'Write UserId');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add(IntToStr(RockeyCtrl1.Lp1));
end;

procedure  TForm1.ReadUserIdClick(Sender:TObject);begin
    err := RockeyCtrl1.Rockey(RY_READ_USERID);
    List1.Items.Add( 'Read UserId');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add( IntToStr(RockeyCtrl1.Lp1));
end;

procedure  TForm1.SetModuleClick(Sender:TObject);
begin
    RockeyCtrl1.P1 := 7;
    RockeyCtrl1.P2 := $2121;
    RockeyCtrl1.P3 := 0;
    err := RockeyCtrl1.Rockey(RY_SET_MODULE);
    List1.Items.Add( 'Set Module');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add( IntToStr(RockeyCtrl1.P3));
end;
procedure  TForm1.CheckModuleClick(Sender:TObject);
begin
    RockeyCtrl1.P1 := 7;
    err := RockeyCtrl1.Rockey(RY_CHECK_MODULE);
    List1.Items.Add( 'Check Module');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add( IntToStr(RockeyCtrl1.P2));
    List1.Items.Add( IntToStr(RockeyCtrl1.P3));
end;
procedure  TForm1.WriteArithmetic(cmd:String;idx : Integer);
begin
    RockeyCtrl1.Buffer := cmd;
    err := RockeyCtrl1.Rockey(RY_WRITE_ARITHMETIC);
    List1.Items.Add( 'Write Arithmetic');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add( 'Write Arithmetic ' + IntToStr(idx));
end;
Procedure TForm1.ExecCalculate(idx : Integer);
begin
    List1.Items.Add( 'Calculate Input: p1:=' + IntToStr(RockeyCtrl1.P1) + ' p2:=' + IntToStr(RockeyCtrl1.P2) + ' p3:=' + InttoStr(RockeyCtrl1.P3) + ' p4:=' + IntToStr(RockeyCtrl1.P4));
    err := RockeyCtrl1.Rockey(idx);
    List1.Items.Add( 'Exec Calculate');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    List1.Items.Add( 'Exec Calculate ' + IntToStr(idx));
end;

procedure  TForm1.Cal1Click(Sender:TObject);
begin
    RockeyCtrl1.P1 := 0;
    WriteArithmetic(cmd0,1);

    RockeyCtrl1.Lp1 := 0;
    RockeyCtrl1.Lp2 := 7;
    RockeyCtrl1.P1 := 5;
    RockeyCtrl1.P2 := 3;
    RockeyCtrl1.P3 := 1;
    RockeyCtrl1.P4 := smallInt($FFFF);
    ExecCalculate(RY_CALCULATE1);
    List1.Items.Add( 'Result := 5*23 + 3*17 + 0x2121 < 1 ^ 0xffff := BC71');
    List1.Items.Add( 'Calculate Output: p1:=' + IntToStr(RockeyCtrl1.P1) + ' p2:=' + IntToStr(RockeyCtrl1.P2) + ' p3:=' + IntToStr(RockeyCtrl1.P3) + ' p4:=' + IntToStr(RockeyCtrl1.P4));
end;

procedure  TForm1.Cal2Click(Sender:TObject);
begin
    RockeyCtrl1.P1 := 10;
    WriteArithmetic(cmd1, 2);

    RockeyCtrl1.Lp1 := 10;
    RockeyCtrl1.Lp2 := $12345678;
    RockeyCtrl1.P1 := 1;
    RockeyCtrl1.P2 := 2;
    RockeyCtrl1.P3 := 3;
    RockeyCtrl1.P4 := 4;
    ExecCalculate(RY_CALCULATE2);
    List1.Items.Add( 'Result :=sd1 + sd2 + sd3 + sd4 + 1 + 2 + 3 + 4 := ' + IntToStr(1 + 2 + 3 + 4 + 10));
    List1.Items.Add( 'Calculate Output: p1:=' + IntToStr(RockeyCtrl1.P1) + ' p2:=' + IntToStr(RockeyCtrl1.P2) + ' p3:=' + IntToStr(RockeyCtrl1.P3) + ' p4:=' + IntToStr(RockeyCtrl1.P4));
end;

procedure  TForm1.Cal3Click(Sender:TObject);
begin
    RockeyCtrl1.P1 := 9;
    RockeyCtrl1.P2 := 5;
    RockeyCtrl1.P3 := 1;
    err := RockeyCtrl1.Rockey(RY_SET_MODULE);
    List1.Items.Add( 'Set Module 9');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));

    RockeyCtrl1.P1 := 17;
    WriteArithmetic(cmd2, 3);
    RockeyCtrl1.Lp1 := 17;
    RockeyCtrl1.Lp2 := 6;
    RockeyCtrl1.P1 := 1;
    RockeyCtrl1.P2 := 2;
    RockeyCtrl1.P3 := 3;
    RockeyCtrl1.P4 := 4;
    ExecCalculate (RY_CALCULATE3);
    List1.Items.Add( 'Calculate Output: p1:=' + IntToStr(RockeyCtrl1.P1) + ' p2:=' + IntToStr(RockeyCtrl1.P2) + ' p3:=' + IntToStr(RockeyCtrl1.P3) + ' p4:=' + IntToStr(RockeyCtrl1.P4));

    RockeyCtrl1.P1 := 9;
    err := RockeyCtrl1.Rockey(RY_DECREASE);
    List1.Items.Add( 'Decrease Module 9');
    List1.Items.Add( RockeyCtrl1.ErrToStr(err));
    RockeyCtrl1.Lp1 := 17;
    RockeyCtrl1.Lp2 := 6;
    RockeyCtrl1.P1 := 1;
    RockeyCtrl1.P2 := 2;
    RockeyCtrl1.P3 := 3;
    RockeyCtrl1.P4 := 4;
    ExecCalculate (RY_CALCULATE3);
    List1.Items.Add( 'Calculate Output: p1:=' + IntToStr(RockeyCtrl1.P1) + ' p2:=' + IntToStr(RockeyCtrl1.P2) + ' p3:=' + IntToStr(RockeyCtrl1.P3) + ' p4:=' + IntToStr(RockeyCtrl1.P4));
end;
procedure TForm1.CloseRockey();
begin
     err := RockeyCtrl1.Rockey(RY_CLOSE);
     List1.Items.Add( 'Close');
     List1.Items.Add( RockeyCtrl1.ErrToStr(err));
end;

procedure TForm1.DemoClick(Sender: TObject);
begin
    Find.Click();
    Open.Click();
    Write.Click();
    Read.Click();
    Random.Click();
    Seed.Click();
    UserId.Click();
    ReadUserId.Click();
    SetModule.Click();
    CheckModule.Click();
    Cal1.Click();
    Cal2.Click();
    Cal3.Click();
    CloseRockey();
end;

end.
