unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, nrusbdriver, nrclasses, nrusb, nrcommbox, ComCtrls,
  Mask;

type
  TForm1 = class(TForm)
    nrDeviceBox1: TnrDeviceBox;
    nrUSB1: TnrUSB;
    nrUsbDriver1: TnrUsbDriver;
    Label1: TLabel;
    lvProperties: TListView;
    GroupBox1: TGroupBox;
    Button1: TButton;
    eDate: TDateTimePicker;
    eCaption: TEdit;
    eVersion: TMaskEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure nrDeviceBox1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure AddInfo(sCapt, sVal: string);
    { Private declarations }
  public
    { Public declarations }
    procedure WMDeviceChange(var Msg:TMessage); message WM_DEVICECHANGE;
  end;

var
  Form1: TForm1;

implementation
uses nrusbman;

{$R *.dfm}


procedure TForm1.WMDeviceChange(var Msg: TMessage);
var i:integer;
begin
  Screen.Cursor := crHourGlass;
  i := nrUsb1.DeviceIndex;
  nrUsb1.WMDeviceChange(Msg);
  nrUsb1.DeviceIndex := i;
//  nrDeviceBox1Change(Self);
  Screen.Cursor := crDefault;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  nrUsbDriver1.DriverCaption := eCaption.Text;
  nrUsbDriver1.DriverVersion := eVersion.EditText;
  nrUsbDriver1.DriverDate := eDate.Date;

  if nrUSB1.UsbDevice = nil then begin
    ShowMessage('Please select device.');
    exit;
  end;

  if AnsiCompareText(nrUSB1.UsbDevice.Service,'WinUSB') = 0 then begin
    ShowMessage('WinUSB driver is already installed.');
    exit;
  end;

  if Application.MessageBox('Do you want continue?','Warning',MB_YESNO) <> IDYES
    then exit;


  Screen.Cursor := crHourGlass;
  try
    nrUsbDriver1.Install;
  finally
    Screen.Cursor := crDefault;
  end;

end;

procedure TForm1.AddInfo(sCapt, sVal: string);
var Itm:TListItem;
begin
  if Trim(sVal) = '' then exit;
  with lvProperties do begin
    Itm := Items.Add;
    Itm.Caption := sCapt;
    Itm.SubItems.Add(sVal);
  end;
end;

procedure TForm1.nrDeviceBox1Change(Sender: TObject);
var i:integer;
    Dev:TnrUsbDevice;
begin
  Dev := nrUSB1.UsbDevice;

//  bEject.Enabled := Dev <> nil;
//  bDescriptors.Enabled := Dev <> nil;

  lvProperties.Items.BeginUpdate;
  lvProperties.Clear;

  if Dev <> nil then begin
    AddInfo('Description',Dev.Description);
    AddInfo('Max Power',IntToStr(Dev.MaxPower)+' mA');
    AddInfo('Max Packet Size',IntToStr(Dev.MaxPacketSize));
    AddInfo('Vendor ID',IntToHex(Dev.VendorID,4)+'h');
    AddInfo('Product ID',IntToHex(Dev.ProductID,4)+'h');
    AddInfo('Is Wake Up',BoolToStr(Dev.IsWakeUp,True));
    AddInfo('Is Self Power',BoolToStr(Dev.IsSelfPower,True));
    AddInfo('Is Bus Power',BoolToStr(Dev.IsBusPower,True));
    AddInfo('USB Version',IntToStr(Dev.VerHi) + '.' + IntToStr(Dev.VerLo));
    AddInfo('Speed (Low,Full,High)',strUSBSpeeds[Dev.Speed]+' ');
    AddInfo('Pipe Count',IntToStr(Dev.PipeCount));
    AddInfo('Service',Dev.Service);
    AddInfo('Disk''s',Dev.DriverLetters);
    for i := 0 to Dev.Descriptors.Count - 1
      do AddInfo('String Descriptor ['+IntToStr(i+1) + ']', Dev.Descriptors[i]);
  end;

  lvProperties.Items.EndUpdate;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  eCaption.Text := nrUsbDriver1.DriverCaption;
  eVersion.EditText := nrUsbDriver1.DriverVersion;
  eDate.Date := nrUsbDriver1.DriverDate;
end;

end.
