void sensorFeedback(CPipe motorDone, CPipe motor, MotorData ST400, CSerial serial){ while(motorDone.TestForData() == 0){ //Check if a sensor detects something, otherwise wait until //motor indicates it has completed current instructions /*Insert sensor logic here (maybe a separate thread or new class?)*/ //Make sure ST400.stop is set to 1 if a sensor interrupt occurs //and write to ST400Control if (serial.Open(4, 9600)) { char* lpBuffer = new char[100]; int nBytesRead = serial.ReadData(lpBuffer, 100); strtok(lpBuffer,"\n"); if(atoi(lpBuffer) == 1){ ST400.stop=1; cout<<"HIT"<<endl; //cout<<"YAY I READ FROM ARDRUINO!"<<endl; motor.Write(&ST400, sizeof(ST400)); ST400.stop=0; ST400.leftWheel=0; //Prevent rover from executing remaining steps ST400.rightWheel=0; break; } else{ ST400.stop=0; cout<<"MISS"<<endl; } } } };
BOOL CRKey::Set(LPCTSTR pName, CRKey *pRk) {_STTEX(); if ( !pRk || !pRk->Size() ) { // Clear data Set( pName, "" ); return TRUE; } // end if // Serialize variables CPipe pipe; pRk->EncodeUrl( &pipe ); // Write a NULL character pipe.Write( "", 1 ); // Save the serialized variables Set( pName, (LPCTSTR)pipe.GetBuffer() ); return TRUE; }
UINT __stdcall elevator_console(void *args) { char str[3]; int val[2]; // save str as int int priority = 0; Sleep(2000); while (1) { // gets request entry sema_io_console.Wait(); MOVE_CURSOR(0, 2); printf("Please enter command:\n"); sema_io_console.Signal(); str[0] = _getch(); // Clears out the enter message sema_io_console.Wait(); MOVE_CURSOR(0, 3); printf("\t\t\t\t\t"); MOVE_CURSOR(0, 3); printf("Servicing: %c", str[0]); sema_io_console.Signal(); str[1] = _getch(); str[2] = '\0'; sema_io_console.Wait(); MOVE_CURSOR(0, 3); printf("\t\t\t\t\t"); MOVE_CURSOR(0, 3); printf("Entered: %s\n", str); // convert input to integers if valid if (atoi(&str[1]) && str[1] != '0') { val[1] = atoi(&str[1]); } else if (str[1] == '0') { val[1] = 0; } MOVE_CURSOR(0, 4); printf("\t\t\t\t\t\t\t\t\t\t"); MOVE_CURSOR(0, 4); if (!strcmp(str, "ee")) { // exit case printf("Shutting down elevators\n"); // Write request and priority type to pipe mypip1 = { 99, 99 }; // exit code pipe.Write(&mypip1, sizeof(mypip1)); // redezvous type here before the return 0 return 0; } else if ((str[0] == 'u' && (0 <= val[1] && val[1] < 9)) || (str[0] == 'd' && (0 < val[1] && val[1] < 10))) { // calling the elevator case printf("Please wait, going %c from floor %c\n", str[0], str[1]); // Write request and priority type to pipe // "3-" is an up request and "4-" is a down request if (str[0] == 'u') { mypip1 = { 30 + val[1],priority }; // exit code } else if (str[0] == 'd') { mypip1 = { 40 + val[1],priority }; // exit code } pipe.Write(&mypip1, sizeof(mypip1)); priority++; } else if ((str[0] == '1' || str[0] == '2') && (0 <= val[1] && val[1] < 10)) { // inside the elevator case printf("Closing door, moving elevator %c to floor %c \n", str[0], str[1]); if (str[0] == '1') { mypip1 = { 10 + val[1],priority }; // exit code } else if (str[0] == '2') { mypip1 = { 20 + val[1],priority }; // exit code } pipe.Write(&mypip1, sizeof(mypip1)); priority++; } else if ((str[0] == '+' || str[0] == '-') && (val[1] == 1 || val[1] == 2)) // servicing elevators and faults { if (!strcmp(str, "+1")) { printf("elevator 1 is in service \n"); } else if (!strcmp(str, "-1")) { printf("elevator 1 is out of service \n"); } else if (!strcmp(str, "+2")) { printf("elevator 2 is in service \n"); } else if (!strcmp(str, "-2")) { printf("elevator 2 is out of service \n"); } // Write request and priority type to pipe // "5-" is an up request and "6-" is a down request if (str[0] == '+') { mypip1 = { 50 + val[1],0 }; // exit code } else if (str[0] == '-') { mypip1 = { 60 + val[1],0 }; // exit code } pipe.Write(&mypip1, sizeof(mypip1)); } else { printf("INVALID ENTRY!\n"); } sema_io_console.Signal(); val[0] = -1; //if val[0] isn't what we want then set it to -1 val[1] = -1; //if val[1] isn't what we want then set it to -1 } }
//@ MAIN int main(){ char KeyData; char KeyData1; int iKeyData; int preflag = 1; int termination_code = 99; //@ SEQ: STARTUP RENDEZVOUS printf("Pretending to rendezvous by sleeping 3 seconds...\n"); Sleep(3000); r1.Wait(); printf("let's go!"); Sleep(1000); MOVE_CURSOR(0,0); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,4); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,8); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,12); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,16); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,20); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,24); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,28); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,32); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,36); printf("----------+---+---------------------+---+--------------------"); MOVE_CURSOR(0,40); printf("----------+---+---------------------+---+--------------------"); fflush(stdout); //@ INIT: THREADS CThread t1(IOToElevator1, ACTIVE, NULL) ; CThread t2(IOToElevator2, ACTIVE, NULL) ; //printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); //MOVE_CURSOR(0,0); //printf("Press x at any time to exit... "); //fflush(stdout); while(flag) { // Master Termination. if (preflag == 0) { pipe3.Write(&termination_code, sizeof(int)); pipe4.Write(&termination_code, sizeof(int)); flag = 0; Sleep(50); pipe1.Write(&termination_code, sizeof(int)); break; } if (TEST_FOR_KEYBOARD() != 0) { KeyData = getch() ; // read next character from keyboard m1->Wait(); MOVE_CURSOR(0,50); printf("first character = %c \n", KeyData); printf("second character? \n"); printf(" \n"); printf(" \n"); fflush(stdout); m1->Signal(); while (TEST_FOR_KEYBOARD() == 0) {} KeyData1 = getch(); m1->Wait(); MOVE_CURSOR(0,51); printf("second character = %c \n", KeyData1); printf("command entered is: %c%c \n", KeyData, KeyData1); //Command Organization. if (KeyData == 'e' && KeyData1 == 'e'){ preflag = 0; printf("terminating. (2 seconds) \n "); Sleep(2000); } else if (KeyData == '-' && KeyData1 == '1') { iKeyData = 70; pipe1.Write(&iKeyData, sizeof(int)); printf("FREEZING Elevator 1! "); } else if (KeyData == '-' && KeyData1 == '2') { iKeyData = 71; pipe1.Write(&iKeyData, sizeof(int)); printf("FREEZING Elevator 2! "); } else if (KeyData == '+' && KeyData1 == '1') { iKeyData = 75; pipe1.Write(&iKeyData, sizeof(int)); printf("FAULT FIXED for Elevator 1! "); } else if (KeyData == '+' && KeyData1 == '2') { iKeyData = 76; pipe1.Write(&iKeyData, sizeof(int)); printf("FAULT FIXED for Elevator 2! "); } else if (KeyData == 'u'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 ) ) { iKeyData = 10 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Outside Floor %d wants to go UP. ", (KeyData1 - '0') ); } else if (KeyData == 'd'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 )) { iKeyData = 20 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Outside Floor %d wants to go DOWN. ", (KeyData1 - '0') ); } else if (KeyData == '1'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 )) { iKeyData = 50 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Inside Elevator 1 Pressed Floor %d. ", (KeyData1 - '0') ); } else if (KeyData == '2'&& ( (KeyData1-'0') >= 0 && (KeyData1-'0') < 10 )) { iKeyData = 60 + KeyData1 - '0'; pipe1.Write(&iKeyData, sizeof(int)); printf("Someone Inside Elevator 2 Pressed Floor %d. ", (KeyData1 - '0') ); } else{ printf("ERROR: illegible code. "); } fflush(stdout); m1->Signal(); KeyData = '0'; KeyData1 = '0'; iKeyData = 0; } m1->Wait(); // 9 MOVE_CURSOR(20,2); printf(" |"); if (Ele1Status.updir[9] == 1 || Ele2Status.updir[9] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[9] == 1 || Ele2Status.downdir[9] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //8 MOVE_CURSOR(20,6); printf(" |"); if (Ele1Status.updir[8] == 1 || Ele2Status.updir[8] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[8] == 1 || Ele2Status.downdir[8] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //7 MOVE_CURSOR(20,10); printf(" |"); if (Ele1Status.updir[7] == 1 || Ele2Status.updir[7] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[7] == 1 || Ele2Status.downdir[7] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //6 MOVE_CURSOR(20,14); printf(" |"); if (Ele1Status.updir[6] == 1 || Ele2Status.updir[6] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[6] == 1 || Ele2Status.downdir[6] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //5 MOVE_CURSOR(20,18); printf(" |"); if (Ele1Status.updir[5] == 1 || Ele2Status.updir[5] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[5] == 1 || Ele2Status.downdir[5] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //4 MOVE_CURSOR(20,22); printf(" |"); if (Ele1Status.updir[4] == 1 || Ele2Status.updir[4] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[4] == 1 || Ele2Status.downdir[4] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //3 MOVE_CURSOR(20,26); printf(" |"); if (Ele1Status.updir[3] == 1 || Ele2Status.updir[3] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[3] == 1 || Ele2Status.downdir[3] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //2 MOVE_CURSOR(20,30); printf(" |"); if (Ele1Status.updir[2] == 1 || Ele2Status.updir[2] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[2] == 1 || Ele2Status.downdir[2] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //1 MOVE_CURSOR(20,34); printf(" |"); if (Ele1Status.updir[1] == 1 || Ele2Status.updir[1] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[1] == 1 || Ele2Status.downdir[1] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); //0 MOVE_CURSOR(20,38); printf(" |"); if (Ele1Status.updir[0] == 1 || Ele2Status.updir[0] == 1) TEXT_COLOUR(14); printf("UP "); TEXT_COLOUR(7); if (Ele1Status.downdir[0] == 1 || Ele2Status.downdir[0] == 1) TEXT_COLOUR(14); printf("DOWN"); TEXT_COLOUR(7); printf("| "); fflush(stdout); MOVE_CURSOR(0,50); fflush(stdout); m1->Signal(); Sleep(1); } t1.WaitForThread() ; t2.WaitForThread() ; printf("\nExiting... (waiting on other processes)"); r2.Wait(); return 0; }