int main() { courses direction=FRONT; /* Direction of the character */ int currentPosition=START_POSITION, /* Position of the character */ step; /* The step number that athe character moves in a direction */ char cinAli[HEIGHT][WIDTH], /* the Character of the Game*/ action; /* Type of action that user enters */ /* Initialize Cin Ali */ initialize(currentPosition, &direction, cinAli); /* Play game until user exits */ do { /* Display menu and read the order user gave*/ action=menu(); /* Do the action */ switch (action) { case '>': /* Move Right */ setStepNumber(&step); /* Read and set the step number */ turnRight(currentPosition, &direction, cinAli); walk(step, direction, ¤tPosition, cinAli); break; case '<': /* Move left */ setStepNumber(&step); turnLeft(currentPosition, &direction, cinAli); walk(step, direction, ¤tPosition, cinAli); break; case '=': /* Turn Front */ turnFront(currentPosition, &direction, cinAli); break; case 'r': /* Turn Right */ turnRight(currentPosition, &direction, cinAli); break; case 'l': /* Turn Left */ turnLeft(currentPosition, &direction, cinAli); break; case 'x': /* Exit */ gameOver(); break; case 'j': setStepNumber(&step); turnFront(currentPosition, &direction, cinAli); jump(step, currentPosition, direction, cinAli); break; default: error(incorrectInput); /* Incorrect input from the user */ break; } flushInputBuffer(); /* Clean the input buffer */ }while(action!='x'); return 0; }
char _getCellSummary(struct status_t *status, int maxAttempts) { for (int attempt = 0; TRUE; attempt++) { if (attempt >= maxAttempts) { return 0; fprintf(stderr, "%d bus errors, exiting\n", attempt); chargercontrol_shutdown(); exit(1); } if (attempt > 0) { fprintf(stderr, "no response from %d (id %d) in %s, resetting\n", status->cellIndex, status->cellId, status->battery->name); buscontrol_setBus(FALSE); buscontrol_setBus(TRUE); } unsigned char buf[EVD5_SUMMARY_3_LENGTH]; struct timeval start, end; gettimeofday(&start, NULL); sendCommand(status, 's'); if (status->version == 3) { if (!readPacket(status, buf, EVD5_SUMMARY_3_LENGTH, &end)) { continue; } } else if (status->version == 4) { if (!readPacket(status, buf, EVD5_SUMMARY_4_LENGTH, &end)) { continue; } } unsigned short recievedCellId = bufToShortLE(buf + 1); if (status->cellId != recievedCellId) { fprintf(stderr, "\nSent message to %2d (id %2d) in %s but received response from 0x%x\n", status->cellIndex, status->cellId, status->battery->name, recievedCellId); dumpBuffer(buf, EVD5_SUMMARY_3_LENGTH); flushInputBuffer(); continue; } if (status->version == 3) { decodeSummary3(buf, status); } else if (status->version == 4) { decodeSummary4(buf, status); } status->latency = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); monitorCan_sendLatency(status->battery->batteryIndex, status->cellIndex, status->latency / 1000); break; } return 1; }