Programme carte strategie (disco)
Dependencies: mbed SerialHalfDuplex SDFileSystem DISCO-F469NI_portrait liaison_Bluetooth ident_crac
Instruction/Instruction.cpp@11:ed13a480ddca, 2016-05-02 (annotated)
- Committer:
- antbig
- Date:
- Mon May 02 19:40:59 2016 +0000
- Revision:
- 11:ed13a480ddca
- Parent:
- 4:88431b537477
- Child:
- 29:41e02746041d
Ajout de toutes les actions et de leurs sym?trique
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
antbig | 0:ad97421fb1fb | 1 | #include "Instruction.h" |
antbig | 0:ad97421fb1fb | 2 | |
antbig | 0:ad97421fb1fb | 3 | |
antbig | 0:ad97421fb1fb | 4 | char cheminFileStart[SIZE+8]="/local/"; |
antbig | 0:ad97421fb1fb | 5 | struct S_Instruction strat_instructions[SIZE_BUFFER_FILE];//Liste des instructions |
antbig | 0:ad97421fb1fb | 6 | unsigned char nb_instructions = 0;//Le nombre d'instruction dans le fichier de strategie |
antbig | 0:ad97421fb1fb | 7 | unsigned char actual_instruction = 0;//La ligne de l'instruction en cours d'execution |
antbig | 0:ad97421fb1fb | 8 | |
antbig | 0:ad97421fb1fb | 9 | LocalFileSystem local("local"); |
antbig | 0:ad97421fb1fb | 10 | |
antbig | 0:ad97421fb1fb | 11 | enum E_InstructionType charToInstructionType(char type) |
antbig | 0:ad97421fb1fb | 12 | { |
antbig | 0:ad97421fb1fb | 13 | switch(type) |
antbig | 0:ad97421fb1fb | 14 | { |
antbig | 0:ad97421fb1fb | 15 | case 'C': return MV_COURBURE; |
antbig | 0:ad97421fb1fb | 16 | case 'L': return MV_LINE; |
antbig | 0:ad97421fb1fb | 17 | case 'T': return MV_TURN; |
antbig | 0:ad97421fb1fb | 18 | case 'X': return MV_XYT; |
antbig | 0:ad97421fb1fb | 19 | case 'R': return MV_RECALAGE; |
antbig | 0:ad97421fb1fb | 20 | case 'A': return ACTION; |
antbig | 0:ad97421fb1fb | 21 | default: return UNKNOWN; |
antbig | 0:ad97421fb1fb | 22 | } |
antbig | 0:ad97421fb1fb | 23 | } |
antbig | 0:ad97421fb1fb | 24 | |
antbig | 0:ad97421fb1fb | 25 | enum E_InstructionDirection charToInstructionDirection(char type) |
antbig | 0:ad97421fb1fb | 26 | { |
antbig | 0:ad97421fb1fb | 27 | switch(type) |
antbig | 0:ad97421fb1fb | 28 | { |
antbig | 0:ad97421fb1fb | 29 | case 'B': return BACKWARD; |
antbig | 0:ad97421fb1fb | 30 | case 'F': return FORWARD; |
antbig | 0:ad97421fb1fb | 31 | case 'R': return RELATIVE; |
antbig | 0:ad97421fb1fb | 32 | case 'A': return ABSOLUTE; |
antbig | 11:ed13a480ddca | 33 | case 'L': return LEFT; |
antbig | 0:ad97421fb1fb | 34 | default: return NODIRECTION; |
antbig | 0:ad97421fb1fb | 35 | } |
antbig | 0:ad97421fb1fb | 36 | } |
antbig | 0:ad97421fb1fb | 37 | |
antbig | 0:ad97421fb1fb | 38 | enum E_InstructionPrecisionOuRecalage charToInstructionPrecisionOuRecalage(char type) |
antbig | 0:ad97421fb1fb | 39 | { |
antbig | 0:ad97421fb1fb | 40 | switch(type) |
antbig | 0:ad97421fb1fb | 41 | { |
antbig | 0:ad97421fb1fb | 42 | case 'P': return PRECISION; |
antbig | 0:ad97421fb1fb | 43 | case 'X': return RECALAGE_X; |
antbig | 0:ad97421fb1fb | 44 | case 'Y': return RECALAGE_Y; |
antbig | 0:ad97421fb1fb | 45 | default: return NOPRECISION; |
antbig | 0:ad97421fb1fb | 46 | } |
antbig | 0:ad97421fb1fb | 47 | } |
antbig | 0:ad97421fb1fb | 48 | |
antbig | 0:ad97421fb1fb | 49 | enum E_InstructionNextActionType charToInstructionNextActionType(char type) |
antbig | 0:ad97421fb1fb | 50 | { |
antbig | 0:ad97421fb1fb | 51 | switch(type) |
antbig | 0:ad97421fb1fb | 52 | { |
antbig | 0:ad97421fb1fb | 53 | case 'J': return JUMP; |
antbig | 0:ad97421fb1fb | 54 | case 'W': return WAIT; |
antbig | 0:ad97421fb1fb | 55 | case 'E': return ENCHAINEMENT; |
antbig | 0:ad97421fb1fb | 56 | default: return NONEXTACTION; |
antbig | 0:ad97421fb1fb | 57 | } |
antbig | 0:ad97421fb1fb | 58 | } |
antbig | 0:ad97421fb1fb | 59 | |
antbig | 0:ad97421fb1fb | 60 | enum E_InstructionNextActionJumpType charToInstructionNextActionJumpType(char type) |
antbig | 0:ad97421fb1fb | 61 | { |
antbig | 0:ad97421fb1fb | 62 | switch(type) |
antbig | 0:ad97421fb1fb | 63 | { |
antbig | 0:ad97421fb1fb | 64 | case 'T': return JUMP_TIME; |
antbig | 0:ad97421fb1fb | 65 | case 'P': return JUMP_POSITION; |
antbig | 0:ad97421fb1fb | 66 | default: return NONEXTACTIONJUMPTYPE; |
antbig | 0:ad97421fb1fb | 67 | } |
antbig | 0:ad97421fb1fb | 68 | } |
antbig | 0:ad97421fb1fb | 69 | |
antbig | 4:88431b537477 | 70 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 71 | /* FUNCTION NAME: stringToInstruction */ |
antbig | 4:88431b537477 | 72 | /* DESCRIPTION : Conversion d'une ligne du fichier de strat en instruction */ |
antbig | 4:88431b537477 | 73 | /****************************************************************************************/ |
antbig | 0:ad97421fb1fb | 74 | struct S_Instruction stringToInstruction(char line[]) { |
antbig | 0:ad97421fb1fb | 75 | struct S_Instruction instruction; |
antbig | 0:ad97421fb1fb | 76 | |
antbig | 0:ad97421fb1fb | 77 | char instructionOrder; |
antbig | 0:ad97421fb1fb | 78 | char instructionDirection; |
antbig | 0:ad97421fb1fb | 79 | char instructionPrecision; |
antbig | 0:ad97421fb1fb | 80 | char instructionNextActionType; |
antbig | 0:ad97421fb1fb | 81 | char instructionJumpAction; |
antbig | 1:116040d14164 | 82 | int errorCode = 0; |
antbig | 0:ad97421fb1fb | 83 | /* |
antbig | 0:ad97421fb1fb | 84 | Info sur la fonction sscanf |
antbig | 0:ad97421fb1fb | 85 | %d -> Entier signé |
antbig | 0:ad97421fb1fb | 86 | %u -> Entié non signé |
antbig | 0:ad97421fb1fb | 87 | %c -> char |
antbig | 0:ad97421fb1fb | 88 | */ |
antbig | 1:116040d14164 | 89 | errorCode = sscanf(line, "%d,%c,%c,%u,%u,%d,%c,%c,%c,%u,%u,%d,%d", |
antbig | 0:ad97421fb1fb | 90 | &instruction.lineNumber, |
antbig | 0:ad97421fb1fb | 91 | &instructionOrder, |
antbig | 0:ad97421fb1fb | 92 | &instructionDirection, |
antbig | 0:ad97421fb1fb | 93 | &instruction.arg1, |
antbig | 0:ad97421fb1fb | 94 | &instruction.arg2, |
antbig | 0:ad97421fb1fb | 95 | &instruction.arg3, |
antbig | 0:ad97421fb1fb | 96 | &instructionPrecision, |
antbig | 0:ad97421fb1fb | 97 | &instructionNextActionType, |
antbig | 0:ad97421fb1fb | 98 | &instructionJumpAction, |
antbig | 0:ad97421fb1fb | 99 | &instruction.JumpTimeOrX, |
antbig | 0:ad97421fb1fb | 100 | &instruction.JumpY, |
antbig | 0:ad97421fb1fb | 101 | &instruction.nextLineOK, |
antbig | 0:ad97421fb1fb | 102 | &instruction.nextLineError |
antbig | 0:ad97421fb1fb | 103 | ); |
antbig | 1:116040d14164 | 104 | /* |
antbig | 1:116040d14164 | 105 | if(errorCode != 13) { |
antbig | 1:116040d14164 | 106 | errorInstructionLoop();//L'instruction est pas bonne !! |
antbig | 1:116040d14164 | 107 | }*/ |
antbig | 0:ad97421fb1fb | 108 | |
antbig | 0:ad97421fb1fb | 109 | instruction.order = charToInstructionType(instructionOrder); |
antbig | 0:ad97421fb1fb | 110 | instruction.direction = charToInstructionDirection(instructionDirection); |
antbig | 0:ad97421fb1fb | 111 | instruction.precision = charToInstructionPrecisionOuRecalage(instructionPrecision); |
antbig | 0:ad97421fb1fb | 112 | instruction.nextActionType = charToInstructionNextActionType(instructionNextActionType); |
antbig | 0:ad97421fb1fb | 113 | instruction.jumpAction = charToInstructionNextActionJumpType(instructionJumpAction); |
antbig | 0:ad97421fb1fb | 114 | |
antbig | 0:ad97421fb1fb | 115 | |
antbig | 0:ad97421fb1fb | 116 | return instruction; |
antbig | 0:ad97421fb1fb | 117 | } |
antbig | 0:ad97421fb1fb | 118 | |
antbig | 4:88431b537477 | 119 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 120 | /* FUNCTION NAME: loadAllInstruction */ |
antbig | 4:88431b537477 | 121 | /* DESCRIPTION : Charger toutes les instructions du fichier de stratégie */ |
antbig | 4:88431b537477 | 122 | /* Il faut utiliser strcpy(cheminFileStart,"/local/strat.txt"); */ |
antbig | 4:88431b537477 | 123 | /* pour indiquer le fichier à utiliser */ |
antbig | 4:88431b537477 | 124 | /****************************************************************************************/ |
antbig | 0:ad97421fb1fb | 125 | void loadAllInstruction(void) { |
antbig | 0:ad97421fb1fb | 126 | |
antbig | 0:ad97421fb1fb | 127 | struct S_Instruction instruction; |
antbig | 0:ad97421fb1fb | 128 | char LineBuffer[SIZE]; |
antbig | 0:ad97421fb1fb | 129 | printf("Reading file : "); |
antbig | 0:ad97421fb1fb | 130 | printf(cheminFileStart); |
antbig | 0:ad97421fb1fb | 131 | printf("\n"); |
antbig | 0:ad97421fb1fb | 132 | FILE *testFile = fopen(cheminFileStart, "rt"); //Ouverture du fichier en mode lecture seul au format string |
antbig | 0:ad97421fb1fb | 133 | |
antbig | 0:ad97421fb1fb | 134 | nb_instructions = 0; |
antbig | 0:ad97421fb1fb | 135 | while (fgets(LineBuffer, SIZE, testFile) != NULL) { |
antbig | 0:ad97421fb1fb | 136 | instruction = stringToInstruction(LineBuffer); |
antbig | 0:ad97421fb1fb | 137 | strat_instructions[nb_instructions] = instruction; |
antbig | 1:116040d14164 | 138 | if(strat_instructions[nb_instructions].order == UNKNOWN) { |
antbig | 1:116040d14164 | 139 | errorInstructionLoop();//L'instruction est pas bonne !! |
antbig | 1:116040d14164 | 140 | } |
antbig | 0:ad97421fb1fb | 141 | //printf(LineBuffer); |
antbig | 0:ad97421fb1fb | 142 | //debug_Instruction(instruction); |
antbig | 0:ad97421fb1fb | 143 | nb_instructions++; |
antbig | 0:ad97421fb1fb | 144 | } |
antbig | 0:ad97421fb1fb | 145 | printf("nb instruction = %d\n",nb_instructions); |
antbig | 0:ad97421fb1fb | 146 | fclose(testFile); |
antbig | 0:ad97421fb1fb | 147 | |
antbig | 0:ad97421fb1fb | 148 | } |
antbig | 4:88431b537477 | 149 | |
antbig | 4:88431b537477 | 150 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 151 | /* FUNCTION NAME: FileExists */ |
antbig | 4:88431b537477 | 152 | /* DESCRIPTION : Permet de vérifier si un fichier existe */ |
antbig | 4:88431b537477 | 153 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 154 | int FileExists(const char *fname) |
antbig | 4:88431b537477 | 155 | { |
antbig | 4:88431b537477 | 156 | FILE *file; |
antbig | 4:88431b537477 | 157 | if (file = fopen(fname, "r")) |
antbig | 4:88431b537477 | 158 | { |
antbig | 4:88431b537477 | 159 | fclose(file); |
antbig | 4:88431b537477 | 160 | return 1; |
antbig | 4:88431b537477 | 161 | } |
antbig | 4:88431b537477 | 162 | return 0; |
antbig | 4:88431b537477 | 163 | } |