// =======================================
// USB Device Demo Project for nrComm Lib
// Copyright(C) 2007 Roman Novgorodov,
// http://www.DeepSoftware.com
//---------------------------------------------------------------------------

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    nrUSB1: TnrUSB;
    nrDeviceBox1: TnrDeviceBox;
    Label1: TLabel;
    bEject: TButton;
    lvProperties: TListView;
    bDescriptors: TButton;
    bRestart: TButton;
    bEnable: TButton;
    Button1: TButton;
    nrLogFile1: TnrLogFile;
    procedure bEjectClick(Sender: TObject);
    procedure nrDeviceBox1Change(Sender: TObject);
    procedure bDescriptorsClick(Sender: TObject);
    procedure bRestartClick(Sender: TObject);
    procedure bEnableClick(Sender: TObject);
    procedure Button1Click(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.bEjectClick(Sender: TObject);
begin
  nrUSB1.Eject;
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('Manufacturer',Dev.Manufacturer);
    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);
    AddInfo('S/N',Dev.SerialNumber);
    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.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.bDescriptorsClick(Sender: TObject);
begin
  if nrUsb1.UsbDevice <> nil then begin
    Screen.Cursor := crHourGlass;
    nrUsb1.UsbDevice.UpdateDescriptors;
    nrDeviceBox1Change(Self);
    Screen.Cursor := crDefault;
  end;  
end;

procedure TForm1.bRestartClick(Sender: TObject);
begin
  nrUSB1.Restart;
end;

procedure TForm1.bEnableClick(Sender: TObject);
begin
  nrUSB1.Enable;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  nrUSB1.Disable;
end;

end.
