1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
unit RelayCtlU;
interface
uses
Windows, Messages, SysUtils, Variants,Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons,
Spin, ComCtrls, AntHIDDeviceController;
type
TRLButtons = record
Buttons: array [0..15] of TSpeedButton;
end;
THidRlyForm = class(TForm)
Panel2: TPanel;
Panel1: TPanel;
lbBoards: TListBox;
Panel3: TPanel;
Log: TMemo;
PC: TPageControl;
tsChannelButtons: TTabSheet;
tsAllOn: TTabSheet;
BtnPan: TPanel;
Label1: TLabel;
buAllOn: TButton;
buAllOff: TButton;
seRelNo: TSpinEdit;
buOn: TButton;
buOff: TButton;
Panel4: TPanel;
sb1On: TSpeedButton;
sb1Off: TSpeedButton;
sb2On: TSpeedButton;
sb2Off: TSpeedButton;
sb3On: TSpeedButton;
sb3Off: TSpeedButton;
sb4On: TSpeedButton;
sb4Off: TSpeedButton;
Sb5On: TSpeedButton;
sb5Off: TSpeedButton;
sb6On: TSpeedButton;
sb6Off: TSpeedButton;
sb7On: TSpeedButton;
sb7Off: TSpeedButton;
sb8On: TSpeedButton;
sb8Off: TSpeedButton;
Panel5: TPanel;
Panel6: TPanel;
HidCtl: TAntHidDeviceController;
procedure FormActivate(Sender: TObject);
function HidCtlEnumerate(HidDev: TAntHidDevice; const Idx: Integer): Boolean;
procedure HidCtlDeviceChange(Sender: TObject);
procedure buAllOnClick(Sender: TObject);
procedure sb1OnClick(Sender: TObject);
procedure PCChange(Sender: TObject);
private
RlTtl: TPanel;
function GetButtonStatus: Byte;
function SendCmd(Relay, Mode: Byte): String;
procedure InitButtons(var ButtonList: TRLButtons);
{ Private declarations }
public
{ Public declarations }
end;
var
HidRlyForm: THidRlyForm;
implementation
{$R *.dfm}
const
DEVICENAME = 'USBRelay'; // Base name for report ProductName
Cmds: array [0..3] of Byte = ({On} $FF, {Off} $FD, {All ON} $FE, {All OFF} $FC);
SubCmds: array [0..1] of Byte = ({Off} $FD, {On} $FF);
procedure THidRlyForm.InitButtons(var ButtonList: TRLButtons);
begin
ButtonList.Buttons[ 0] := sb1On;
ButtonList.Buttons[ 1] := sb1Off;
ButtonList.Buttons[ 2] := sb2On;
ButtonList.Buttons[ 3] := sb2Off;
ButtonList.Buttons[ 4] := sb3On;
ButtonList.Buttons[ 5] := sb3Off;
ButtonList.Buttons[ 6] := sb4On;
ButtonList.Buttons[ 7] := sb4Off;
ButtonList.Buttons[ 8] := sb5On;
ButtonList.Buttons[ 9] := sb5Off;
ButtonList.Buttons[10] := sb6On;
ButtonList.Buttons[11] := sb6Off;
ButtonList.Buttons[12] := sb7On;
ButtonList.Buttons[13] := sb7Off;
ButtonList.Buttons[14] := sb8On;
ButtonList.Buttons[15] := sb8Off;
end;
function THidRlyForm.GetButtonStatus: Byte;
var
Btns: TRLButtons;
Dev: TAntHidDevice;
pB: pByte;
Status: Boolean;
Sb, i: Byte;
begin
if lbBoards.Count > 0 then
begin
Dev := TAntHidDevice(lbBoards.Items.Objects[lbBoards.ItemIndex]);
try
pB := AllocMem(10);
pB^ := 0;
(pB + 1)^ := 0; :arf::arf::arf:
(pB + 2)^ := 0;
Status := Dev.GetFeature(pB^, 9);
if Status then
begin
Sb := (pB + 8)^;
InitButtons(Btns);
for i := 0 to 7 do
begin
Btns.Buttons[(i * 2) ].Down := Boolean(Sb and 1);
Btns.Buttons[(i * 2) + 1].Down := not Boolean(Sb and 1);
Sb := Sb shr 1;
end;
end;
finally
FreeMem(pB);
end;
end;
end;
procedure THidRlyForm.buAllOnClick(Sender: TObject);
begin
Log.Lines.Add(SendCmd(seRelNo.Value, Cmds[TWinControl(Sender).Tag - 1]));
end;
procedure THidRlyForm.FormActivate(Sender: TObject);
var
i: Integer;
begin
HidCtlDeviceChange(Self);
PC.TabIndex := 0;
end;
// free all board objects, clear board list and re-enumerate boards
procedure THidRlyForm.HidCtlDeviceChange(Sender: TObject);
var
i: Integer;
begin
for i := 0 to lbBoards.Items.Count - 1 do
TAntHidDevice(lbBoards.Items.Objects[i]).Free;
lbBoards.Clear;
Log.Clear;
Log.Lines.Add(Format('-- %s --', [DateTimeToStr(Now)]));
HidCtl.Enumerate;
// Select the first board
if lbBoards.Count = 0 then
Log.Lines.Add('No relay boards present')
else
lbBoards.ItemIndex := lbBoards.Count - 1;
BtnPan.Enabled := (lbBoards.Count > 0);
end;
function THidRlyForm.HidCtlEnumerate(HidDev: TAntHidDevice; const Idx: Integer): Boolean;
var
Bd: Integer;
begin
// Show all USB devices in the log
if HidDev.ProductName <> '' then
Log.Lines.Add(HidDev.ProductName)
else
Log.Lines.Add(Format('VID=%4.4x, PID=%4.4x', [HidDev.Attributes.VendorID, HidDev.Attributes.ProductID]));
// Select only the devices we are interested in
if Copy(HidDev.ProductName, 1, Length(DEVICENAME)) = DEVICENAME then
begin
Bd := lbBoards.Count;
Bd := lbBoards.Items.Add(Format('%d - %s', [Bd, HidDev.ProductName]));
HidCtl.CheckOutByIndex(HidDev, Idx);
lbBoards.Items.Objects[Bd] := HidDev;
end;
// Application.ProcessMessages;
Result := True;
end;
procedure THidRlyForm.PCChange(Sender: TObject);
var
Stat: Byte;
i, j: Integer;
begin
if PC.TabIndex = 1 then
GetButtonStatus;
end;
procedure THidRlyForm.sb1OnClick(Sender: TObject);
var
Ch: Integer;
On: Byte;
begin
Ch := ((TWinControl(Sender).Tag - 1) shr 1) + 1;
On := (TWinControl(Sender).Tag and 1);
Log.Lines.Add(SendCmd(Ch, SubCMds[On]));
end;
function THidRlyForm.SendCmd(Relay, Mode: Byte): String;
var
pB: pByte;
Dev: TAntHidDevice;
Status: Boolean;
begin
{ Dev := TAntHidDevice(lbBoards.Items.Objects[lbBoards.ItemIndex]);
try
pB := AllocMem(10);
pB^ := 0;
(pB + 1)^ := Mode; :arf:
(pB + 2)^ := Relay;
Status := Dev.SetFeature(pB^, 9);
if Status then
Result := Format('%2.2x %2.2x %2.2x', [(pB + 0)^, (pB + 1)^, (pB + 2)^])
else
Result := 'Command not sent';
Status := Dev.GetFeature(Pb^, 9);
if Status then
Result := Format('%s - %2.2x', [Result, (pB + 8)^]);
finally
FreeMem(pB);
end;}
end;
end. |
Partager