//=========================================================================== // EZIO Joystick and InfraRed Sensor Example //=========================================================================== // EZIO CONNECTIONS: // // RED Plus 5V for Sensors // BLACK Ground for Sensors // // YELLOW IR SENSOR A/D 1 (address 0) // YELLOW JOYSTICK A/D 2 (address 1) // GREEN JOYSTICK A/D 3 (address 2) //=========================================================================== //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DoubleBuffered = true; // THIS ELIMINATES FLICKER } //--------------------------------------------------------------------------- //=========================================================================== // EZIO VARIABLES //=========================================================================== HANDLE hComm = NULL; COMMTIMEOUTS ctmoNew = {0}, ctmoOld; DWORD dwBytesRead; int InBuff[100]; char InASCII[100]; int data; int note; //=========================================================================== // Open Port to EZIO // EVENT HANDLER - ON FORM CREATE // This opens COMM1, the RS-232 Serial Port // It remembers the values that existed before this program opened //=========================================================================== void __fastcall TForm1::FormCreate(TObject *Sender) { DCB dcbCommPort; // OPEN THE COMM PORT. hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); // IF THE PORT CANNOT BE OPENED, BAIL OUT. if(hComm == INVALID_HANDLE_VALUE) Application->Terminate(); // SET THE COMM TIMEOUTS IN OUR EXAMPLE. GetCommTimeouts(hComm,&ctmoOld); ctmoNew.ReadTotalTimeoutConstant = 10; ctmoNew.ReadTotalTimeoutMultiplier = 0; ctmoNew.WriteTotalTimeoutMultiplier = 0; ctmoNew.WriteTotalTimeoutConstant = 0; SetCommTimeouts(hComm, &ctmoNew); // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS. // THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST. dcbCommPort.DCBlength = sizeof(DCB); GetCommState(hComm, &dcbCommPort); BuildCommDCB("57600,N,8,1", &dcbCommPort); SetCommState(hComm, &dcbCommPort); } //=========================================================================== // Close Port to EZIO // EVENT HANDLER - ON FORM CLOSE // This closes COMM1 // It restores the values that existed before this program opened //=========================================================================== void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { // WAIT FOR THREAD TO TERMINATE, // PURGE THE INTERNAL COMM BUFFER, // RESTORE THE PREVIOUS TIMEOUT SETTINGS, // AND CLOSE THE COMM PORT. Sleep(250); PurgeComm(hComm, PURGE_RXABORT); SetCommTimeouts(hComm, &ctmoOld); CloseHandle(hComm); } //=========================================================================== // VARIABLES //=========================================================================== int distance; int yellowPot; int greenPot; int red, green, blue; int range, value; bool running = true; //=========================================================================== // FUNCTIONS //=========================================================================== //--------------------------------------------------------------------------- // COLOR RAMP FUNCTION // The color ramp function describes a path from black to white along // seven edges of the color cube. // Since each edge of the color cube is 256 pixels long, the length of // the entire path is 7 x 256 or 1792 pixels. //--------------------------------------------------------------------------- int colorRamp(int range, int value) { int pixelDistanceAlongPath = (value * 1792) / range; int red, green, blue; // Which edge of the color cube are we on? if (pixelDistanceAlongPath < 256) { // Edge 1 from BLACK to BLUE red=0; green=0; blue=pixelDistanceAlongPath; } else if (pixelDistanceAlongPath < 512) { // Edge 2 from BLUE to CYAN red =0; green=pixelDistanceAlongPath-256; blue=255; } else if (pixelDistanceAlongPath < 768) { // Edge 3 from CYAN to GREEN red =0; green =255; blue= 255-(pixelDistanceAlongPath-512); } else if (pixelDistanceAlongPath < 1024) { // Edge 4 from GREEN to YELLOW red= (pixelDistanceAlongPath-768); green =255; blue =0; } else if (pixelDistanceAlongPath <1280) { // Edge 5 from YELLOW to RED red =255; green=255-(pixelDistanceAlongPath-1024); blue =0; } else if (pixelDistanceAlongPath < 1536) { // Edge 6 from RED to MAGENTA red =255; green=0; blue=pixelDistanceAlongPath -1280; } else { // Edge 7 from MAGENTA to WHITE red =255; green=pixelDistanceAlongPath-1536; blue =255; } return (RGB(red, green, blue)); } //------------------------------------------------------------- Read Distance void readIRSensor (void) { Sleep(1); TransmitCommChar(hComm, 'A'); Sleep(1); TransmitCommChar(hComm, 0); Sleep(1); ReadFile(hComm, InBuff, 50, &dwBytesRead, NULL); distance = *InBuff; Sleep(1); Form1->EditDistance->Text = distance; Form1->ShapeRed->Brush->Color = static_cast(colorRamp(140, distance + 1)); Application->ProcessMessages(); } //----------------------------------------------------------- Read Yellow Pot void readYellowPot (void) { Sleep(1); TransmitCommChar(hComm, 'A'); Sleep(1); TransmitCommChar(hComm, 1); Sleep(1); ReadFile(hComm, InBuff, 50, &dwBytesRead, NULL); yellowPot = *InBuff; Sleep(1); Form1->EditYellowPot->Text = yellowPot; Form1->ShapeYellow->Left = 50 + yellowPot - 5; Application->ProcessMessages(); } //------------------------------------------------------------ Read Green Pot void readGreenPot (void) { Sleep(1); TransmitCommChar(hComm, 'A'); Sleep(1); TransmitCommChar(hComm, 2); Sleep(1); ReadFile(hComm, InBuff, 50, &dwBytesRead, NULL); greenPot = *InBuff; Sleep(1); Form1->EditGreenPot->Text = greenPot; Form1->ShapeGreen->Top = 50 + greenPot - 5; Application->ProcessMessages(); } //----------------------------------------------------------------------- Run void run (void) { while (running) { readIRSensor(); readYellowPot(); readGreenPot(); Application->ProcessMessages(); } Form1->EditDistance->Text = " Distance"; Form1->EditYellowPot->Text = " Yellow Pot"; Form1->ShapeYellow->Left = 177; Form1->EditGreenPot->Text = " Green Pot"; Form1->ShapeGreen->Top = 177; Application->ProcessMessages(); } //=========================================================================== // EVENT HANDLERS //=========================================================================== //---------------------------------------------------------------- Run Button void __fastcall TForm1::ButtonRunClick(TObject *Sender) { running = true; run(); } //--------------------------------------------------------------- Stop Button void __fastcall TForm1::ButtonStopClick(TObject *Sender) { running = false; } //------------------------------------------------------- That's All Folks...