unit OfficeRocketMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, StdCtrls, JvHidControllerClass;
type
TRocketInitRec =
packed record
ReportID: Byte;
Payload:
array [0..7]
of Byte;
end;
TRocketCommandRec =
packed record
ReportID: Byte;
Payload:
array [0..63]
of Byte;
end;
TRocketCommand = (rcLeft, rcRight, rcUp, rcDown, rcFire);
TRocketCommands =
set of TRocketCommand;
TOfficeRocketForm =
class(TForm)
UpBtn: TSpeedButton;
LeftBtn: TSpeedButton;
RightBtn: TSpeedButton;
DownBtn: TSpeedButton;
ShootBtn: TSpeedButton;
HidCtl: TJvHidDeviceController;
procedure CommandClick(Sender: TObject);
procedure HidCtlDeviceChange(Sender: TObject);
procedure ShootBtnClick(Sender: TObject);
public
// both devices are needed to access Rocket Launcher
RocketCommand: TJvHidDevice;
RocketInit: TJvHidDevice;
Commands: TRocketCommands;
procedure SendRocketCommands(Cmds: TRocketCommands);
end;
var
OfficeRocketForm: TOfficeRocketForm;
implementation
{$R *.dfm}
function CheckCommandDevice(HidDev: TJvHidDevice): Boolean;
stdcall;
begin
Result := (HidDev.Attributes.VendorID = $1130)
and
(HidDev.Attributes.ProductID = $0202)
and
(HidDev.Caps.OutputReportByteLength = 65);
end;
function CheckInitDevice(HidDev: TJvHidDevice): Boolean;
stdcall;
begin
Result := (HidDev.Attributes.VendorID = $1130)
and
(HidDev.Attributes.ProductID = $0202)
and
(HidDev.Caps.OutputReportByteLength = 9);
end;
procedure TOfficeRocketForm.HidCtlDeviceChange(Sender: TObject);
var
Found: Boolean;
begin
// give back devices no matter which plug or unplug caused the event
HidCtl.CheckIn(RocketCommand);
HidCtl.CheckIn(RocketInit);
// try to get the two Rocket devices
if HidCtl.CheckOutByCallback(RocketCommand, CheckCommandDevice)
then
HidCtl.CheckOutByCallback(RocketInit, CheckInitDevice);
// enable/disable GUI
Found := Assigned(RocketCommand)
and Assigned(RocketInit);
DownBtn.Enabled := Found;
LeftBtn.Enabled := Found;
RightBtn.Enabled := Found;
ShootBtn.Enabled := Found;
UpBtn.Enabled := Found;
end;
procedure TOfficeRocketForm.SendRocketCommands(Cmds: TRocketCommands);
const
// init data to write to RocketInit device
Init1: TRocketInitRec = (ReportID: $00; Payload: ($55, $53, $42, $43, $00, $00, $04, $00));
Init2: TRocketInitRec = (ReportID: $00; Payload: ($55, $53, $42, $43, $00, $40, $02, $00));
// data template to write to RocketCommand device (causes stop if unmodified)
CmdEmpty: TRocketCommandRec =
(ReportID: $00; Payload:
($00, $00, $00, $00, $00, $00, $08, $08, $00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00));
var
Written: DWORD;
Init: TRocketInitRec;
Command: TRocketCommandRec;
I: TRocketCommand;
begin
// send both init commands
Init := Init1;
RocketInit.WriteFile(Init, SizeOf(Init), Written);
Init := Init2;
RocketInit.WriteFile(Init, SizeOf(Init), Written);
// collect commands into command record
Command := CmdEmpty;
for I := Low(TRocketCommand)
to High(TRocketCommand)
do
if I
in Cmds
then
Command.Payload[Ord(I) + 1] := $01;
// send commands
RocketCommand.WriteFile(Command, SizeOf(Command), Written);
end;
procedure TOfficeRocketForm.CommandClick(Sender: TObject);
begin
with Sender
as TSpeedButton
do
if Down
then
Commands := Commands + [TRocketCommand(Tag)]
else
Commands := Commands + [TRocketCommand(Tag)];
SendRocketCommands(Commands);
end;
procedure TOfficeRocketForm.ShootBtnClick(Sender: TObject);
begin
with Sender
as TSpeedButton
do
begin
Commands := Commands + [TRocketCommand(Tag)];
SendRocketCommands(Commands);
Commands := Commands + [TRocketCommand(Tag)];
end;
end;
end.