int kSeries :: sendRequest(int reqType, int respSize, int respInd) { long Val=-255; int cmdTimeout =0; while(this->_Serial->available() == 0) //send read command in a loop until we see a response { switch(reqType) { case 0: this->_Serial->write(cmd_read_CO2,7); break; case 1: this->_Serial->write(cmd_read_Temp,7); break; case 2: this->_Serial->write(cmd_read_RH,7); break; case 3: this->_Serial->write(cmd_init,8); break; default: return -256; break; } cmdTimeout++; if(cmdTimeout > 20) return -203; wait(130); //give some time after each request for a response } int timeOut=0; //initialize a timeout counter while(this->_Serial->available() < 7) //loop through until we have are 7-byte response { if(timeOut > 40); //after 40 loops, break out and try again break; timeOut++; delay(5); } if(this->_Serial->available() == 7) //if we have our 7-byte response get value Val = getResp(respSize,respInd); else //if we dont i.e. our request timed out { Val = -300; while(this->_Serial->available() > 0) //loop through and flush any bytes we did receiver so they dont throw the next packet off track { Val++; this->_Serial->read(); } } return Val; }
// issue a SD card command and wait normal response void cmd(unsigned char cmd, unsigned long adr) { unsigned char res; do { writeByte(0xff); writeByte(0x40+cmd); writeByte(adr>>24); writeByte((adr>>16)&0xff); writeByte((adr>>8)&0xff); writeByte(adr&0xff); writeByte(0x95); writeByte(0xff); } while (((res=getResp())!=0) && (res!=0xff)); }
int main(int argc, char** argv){ char* outPl[NB_PLAYER] = {NULL, NULL}; // Les sorties pour chaque joueur linkedlist_t listResp = NULL; int pipeW; int J1 = 0; int J2 = 1; int i; int jGagne = -1; int jPerdu; int plein = 0; int choix; char caract[NB_PLAYER]; caract[0] = 'X'; caract[1] = 'O'; char plateau[HAUTEUR][LARGEUR]; if(argc >= 1){ pipeW = atoi(argv[0]); } else{ perror("Erreur, arguments incorrects.\n"); exit(EXIT_FAILURE); } init_plateau(plateau); while(jGagne == -1 && !plein){ for(i=0; i < NB_PLAYER; i++){ systemc(&outPl[J1], "clear"); outc(&outPl[J1], "%s", "======= Morpion =======\n\n"); dessinerPlateau(&outPl[J1], plateau); outc(&outPl[J1], "%s", "Entrez un numero de case (1, 2, ...) : "); inc(&outPl[J1], "%d"); systemc(&outPl[J2], "clear"); outc(&outPl[J2], "%s", "======= Morpion =======\n\n"); dessinerPlateau(&outPl[J2], plateau); outc(&outPl[J2], "%s", "C'est a votre adversaire de jouer.\n"); flushc(pipeW, outPl, NB_PLAYER); choix = (int)getResp(&listResp, INT); if(jouer(plateau, choix, caract[J1])){ if(gagner(plateau, caract[J1])){ jGagne = J1; break; } if(estPlein(plateau)){ plein = 1; break; } } reversePlayer(&J1, &J2); } } systemc(&outPl[J1], "clear"); systemc(&outPl[J2], "clear"); if(!plein && jGagne != -1){ jPerdu = nextPlayer(jGagne); outc(&outPl[jGagne], "%s", "======= Morpion =======\n\n"); outc(&outPl[jGagne], "%s", "Vous avez gagne, felicitation !!\n"); outc(&outPl[jPerdu], "%s", "======= Morpion =======\n\n"); outc(&outPl[jPerdu], "%s", "Vous avez perdu, votre adversaire etait peut etre plus intelligent...\n"); } else{ outc(&outPl[J1], "%s", "======= Morpion =======\n\n"); outc(&outPl[J1], "%s", "Vous etes aussi fort (ou mauvais) que votre adversaire...\n"); outc(&outPl[J2], "%s", "======= Morpion =======\n\n"); outc(&outPl[J2], "%s", "Vous etes aussi fort (ou mauvais) que votre adversaire...\n"); } flushc(pipeW, outPl, NB_PLAYER); // On attend un peut avant de quitter waitc(&outPl[J1], 3); waitc(&outPl[J2], 3); quitc(&outPl[J1]); // Fermera le jeu pour le client quitc(&outPl[J2]); flushc(pipeW, outPl, NB_PLAYER); quit(pipeW); close(pipeW); close_out_players(outPl, NB_PLAYER); return EXIT_SUCCESS; }
int main(int argc, char** argv){ char* outPl[NB_PLAYER] = {NULL, NULL}; // Les sorties pour chaque joueur linkedlist_t listResp = NULL; int pipeW; int J1 = 0; int J2 = 1; int i; int nbTry = 0; int jGagne = -1; // Le numéro du joueur qui a gagné la partie int jPerdu; int nbLettre; char* word[NB_PLAYER]; char* hiddenWord[NB_PLAYER]; char* choice; // Le mot que le joueur a essayer if(argc >= 1){ pipeW = atoi(argv[0]); } else{ perror("Erreur, arguments incorrects.\n"); exit(EXIT_FAILURE); } for(i=0; i < NB_PLAYER; i++){ systemc(&outPl[J1], "clear"); systemc(&outPl[J2], "clear"); outc(&outPl[J1], "%s", "======= Jeu du pendu =======\n\n"); outc(&outPl[J1], "%s", "Entrez un mot(10 carct max): "); inc(&outPl[J1], "%s"); outc(&outPl[J2], "%s", "======= Jeu du pendu =======\n\n"); outc(&outPl[J2], "%s", "Attendez que l'autre joueur choisisse un mot...\n"); flushc(pipeW, outPl, NB_PLAYER); word[J1] = (char*)getResp(&listResp, STRING); upperWord(word[J1]); hiddenWord[J1] = initHiddenWord(word[J1]); //printf("Les mot choisi est : %s\n", word[J1]); reversePlayer(&J1, &J2); fflush(stdout); } systemc(&outPl[J2], "clear"); outc(&outPl[J2], "%s", "======= Jeu du pendu =======\n\n"); while(jGagne == -1){ for(i=0; i < NB_PLAYER && jGagne == -1; i++){ systemc(&outPl[J1], "clear"); outc(&outPl[J1], "%s", "======= Jeu du pendu =======\n\n"); outc(&outPl[J1], "%s %d %s", "Nombre d'essais : ", nbTry, "\n"); outc(&outPl[J1], "%s %s %s", "Le mot a trouver : ", hiddenWord[J2], "\n"); outc(&outPl[J1], "%s", "C'est a vous, entrez une lettre ou un mot : "); inc(&outPl[J1], "%s"); outc(&outPl[J2], "%s", "\nC'est a votre adversaire de jouer...\n"); flushc(pipeW, outPl, NB_PLAYER); choice = (char*)getResp(&listResp, STRING); if(strlen(choice) > 1){ upperWord(choice); if(strcmp(word[J2], choice) == 0){ outc(&outPl[J1], "%s", "Votre mot est le bon.\n"); jGagne = J1; } else{ outc(&outPl[J1], "%s", "Votre mot ne correspond pas.\n"); } } else{ // Si le joueur a entré un seul caractère nbLettre = searchInHidden(hiddenWord[J2], word[J2], choice[0]); if(nbLettre == 0){ outc(&outPl[J1], "%s", "Cette lettre n'est pas presente dans le mot.\n"); } else{ outc(&outPl[J1], "%s %d %s", "Cette lettre a ete trouve ", nbLettre, " fois dans le mot.\n"); } if(strcmp(word[J2], hiddenWord[J2]) == 0){ jGagne = J1; } } free(choice); reversePlayer(&J1, &J2); } nbTry++; } //printf("Pendu : Le joueur %d a gagne.\n", jGagne+1); jPerdu = nextPlayer(jGagne); systemc(&outPl[J1], "clear"); systemc(&outPl[J2], "clear"); outc(&outPl[jGagne], "%s", "======= Jeu du pendu =======\n\n"); outc(&outPl[jGagne], "%s %s %s", "Le mot etait bien : ", word[jPerdu], "\n"); outc(&outPl[jGagne], "%s %d %s", "Vous avez gagne en ", nbTry, " essais, felicitation !!\n"); outc(&outPl[jPerdu], "%s", "======= Jeu du pendu =======\n\n"); outc(&outPl[jPerdu], "%s", "Vous avez perdu, votre adversaire a ete plus rapide..\n"); outc(&outPl[jPerdu], "%s %s %s", "Le mot etait : ", word[jGagne],"\n"); flushc(pipeW, outPl, NB_PLAYER); for(i=0; i < NB_PLAYER; i++){ free(hiddenWord[i]); free(word[i]); } // On attend un peut avant de quitter waitc(&outPl[J1], 2); waitc(&outPl[J2], 2); quitc(&outPl[J1]); // Fermera le jeu pour le client quitc(&outPl[J2]); flushc(pipeW, outPl, NB_PLAYER); quit(pipeW); close(pipeW); close_out_players(outPl, NB_PLAYER); return EXIT_SUCCESS; }