//--------------------------------------------------------------------------- #include #pragma hdrstop #include "MainUnit.h" #include "DetectUnit.h" #include "aiousb.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; unsigned long DeviceIndex; TImage *InImage[4], *OutImage[4]; Graphics::TBitmap *RelayLow, *RelayHigh, *InputOff, *InputOn; unsigned DIOData; bool DoUpdate; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { TDetectForm *DetectForm; unsigned long Status; unsigned long PID; RelayLow = new Graphics::TBitmap; RelayHigh = new Graphics::TBitmap; InputOff = new Graphics::TBitmap; InputOn = new Graphics::TBitmap; DeviceIndex = diOnly; Status = QueryDeviceInfo(DeviceIndex, &PID, NULL, NULL, NULL, NULL); if ( (Status != ERROR_SUCCESS) || ! ((PID == 0x8030) || (PID == 0x8031)) ) { DetectForm = new TDetectForm(this); if (DetectForm->ShowModal() == mrOk) { DeviceIndex = DetectForm ->DeviceIndex; DetectForm->Free(); }else { DetectForm->Free(); Application->Terminate(); return; } } DIOData = 0xFFFF; DIO_WriteAll(DeviceIndex, &DIOData); InImage[0] = InImage0; InImage[1] = InImage1; InImage[2] = InImage2; InImage[3] = InImage3; OutImage[0] = OutImage0; OutImage[1] = OutImage1; OutImage[2] = OutImage2; OutImage[3] = OutImage3; LevelImages->GetBitmap(0, RelayLow ); LevelImages->GetBitmap(1, RelayHigh); LevelImages->GetBitmap(2, InputOff ); LevelImages->GetBitmap(3, InputOn ); DoUpdate = true; ReadTime->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ReadTimeTimer(TObject *Sender) { int I; unsigned oDIOData, Changes; oDIOData = DIOData; DIO_ReadAll(DeviceIndex, &DIOData); Changes = DIOData ^ oDIOData; if (DoUpdate) { Changes = 0xFFFF; DoUpdate = false; } //Read back relays for display for (I = 0; I < 4; I++) { if ((Changes & (1 << I)) != 0) { if ((DIOData & (1 << I)) == 0) OutImage[I]->Picture->Assign(RelayLow); else OutImage[I]->Picture->Assign(RelayHigh); } } //Read isolated inputs for (I = 0; I < 4; I++) { if ((Changes & (0x100 << I)) != 0 ) { if ((DIOData & (0x100 << I)) == 0) InImage[I]->Picture->Assign(InputOff); else InImage[I]->Picture->Assign(InputOn); } } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { RelayLow ->Free(); RelayHigh->Free(); InputOff->Free(); InputOn->Free(); } //--------------------------------------------------------------------------- void __fastcall TForm1::LevelImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { int I; bool NewValue; TImage *image; if (Button == mbLeft) { image = (TImage *) Sender; I = image->Tag; DIOData = DIOData ^ (1 << I); //Toggle bit NewValue = (DIOData & (1 << I)) != 0; //Get bit value DIO_Write1(DeviceIndex, I, NewValue); DoUpdate = True; } } //---------------------------------------------------------------------------