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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "nrclasses"
#pragma link "nrcommbox"
#pragma link "nrusb"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::bEjectClick(TObject *Sender)
{
        nrUSB1->Eject();
}

void __fastcall TForm1::AddInfo(AnsiString sCapt, AnsiString sVal)
{
        TListItem *Itm;
        if(sVal.Trim() == "") return;
        Itm = lvProperties->Items->Add();
        Itm->Caption = sCapt;
        Itm->SubItems->Add(sVal);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::nrDeviceBox1Change(TObject *Sender)
{
        int i;
        TnrUsbDevice *Dev;
        Dev = nrUSB1->UsbDevice;

        bEject->Enabled = Dev != NULL;
        bDescriptors->Enabled = Dev != NULL;

        lvProperties->Items->BeginUpdate();
        lvProperties->Clear();

        if(Dev != NULL)
        {
                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 I",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(int i = 0; i < Dev->Descriptors->Count; i++)
                        AddInfo("String Descriptor ["+IntToStr(i+1) + "]", Dev->Descriptors->Strings[i]);
        }

        lvProperties->Items->EndUpdate();

}

//---------------------------------------------------------------------------
int __fastcall TForm1::WMDeviceChange(TMessage &Msg)
{
  int i = nrUSB1->DeviceIndex;
  nrUSB1->WMDeviceChange(Msg);
  nrUSB1->DeviceIndex = i;
  nrDeviceBox1Change(this);
  return 0;
}

//---------------------------------------------------------------------------
void __fastcall TForm1::bDescriptorsClick(TObject *Sender)
{
  if(nrUSB1->UsbDevice != NULL)
  {
    nrUSB1->UsbDevice->UpdateDescriptors();
    nrDeviceBox1Change(this);
  }
}
//---------------------------------------------------------------------------
