Contest IOT GSE5
/
contest_IOT4
Pong Game Tea Term
Fork of contest_IOT3 by
main.cpp@6:89a007f6508f, 2015-11-04 (annotated)
- Committer:
- Alex_Hochart
- Date:
- Wed Nov 04 11:57:48 2015 +0000
- Revision:
- 6:89a007f6508f
- Parent:
- 5:9437083564d0
pong3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:028fac66239d | 1 | #include "mbed.h" |
Alex_Hochart | 3:6bcdaa2636ec | 2 | #include "HCSR04.h" |
Alex_Hochart | 4:ed21ec4a79ad | 3 | #include <math.h> |
Mickado | 2:35466dfc81fe | 4 | |
Alex_Hochart | 6:89a007f6508f | 5 | #define ECHO_1 PA_4 |
Alex_Hochart | 6:89a007f6508f | 6 | #define ECHO_2 PB_3 |
Alex_Hochart | 6:89a007f6508f | 7 | #define TRIG_1 PB_5 |
Alex_Hochart | 6:89a007f6508f | 8 | #define TRIG_2 PA_10 |
Alex_Hochart | 6:89a007f6508f | 9 | #define PUSH PA_9 //PC_13 |
Alex_Hochart | 6:89a007f6508f | 10 | #define LED_1 PA_8 |
bcostm | 1:e9d1c42a73ae | 11 | |
bcostm | 1:e9d1c42a73ae | 12 | //------------------------------------ |
bcostm | 1:e9d1c42a73ae | 13 | // Hyperterminal configuration |
Alex_Hochart | 4:ed21ec4a79ad | 14 | // 115200 bauds, 8-bit data, no parity |
bcostm | 1:e9d1c42a73ae | 15 | //------------------------------------ |
bcostm | 1:e9d1c42a73ae | 16 | |
Mickado | 2:35466dfc81fe | 17 | |
Alex_Hochart | 3:6bcdaa2636ec | 18 | Serial pc(USBTX, USBRX); //UART |
Mickado | 2:35466dfc81fe | 19 | DigitalOut led(LED_1); //Led d'état |
Alex_Hochart | 6:89a007f6508f | 20 | DigitalIn bp(PUSH); |
Mickado | 2:35466dfc81fe | 21 | |
Alex_Hochart | 3:6bcdaa2636ec | 22 | HCSR04 sensor1(TRIG_1, ECHO_1); |
Alex_Hochart | 3:6bcdaa2636ec | 23 | HCSR04 sensor2(TRIG_2, ECHO_2); |
Alex_Hochart | 3:6bcdaa2636ec | 24 | |
Alex_Hochart | 4:ed21ec4a79ad | 25 | void pong_init_ttempro(){ |
Mickado | 2:35466dfc81fe | 26 | |
Alex_Hochart | 3:6bcdaa2636ec | 27 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 3:6bcdaa2636ec | 28 | pc.printf("\033[?25l"); //Cache le curseur |
Alex_Hochart | 3:6bcdaa2636ec | 29 | |
Alex_Hochart | 3:6bcdaa2636ec | 30 | for(int i=0; i <= 128 ; i++){ |
Alex_Hochart | 3:6bcdaa2636ec | 31 | pc.printf("\033[0;%dH",i); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 32 | pc.printf("X"); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 33 | pc.printf("\033[32;%dH",i); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 34 | pc.printf("X"); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 35 | } |
Alex_Hochart | 3:6bcdaa2636ec | 36 | |
Alex_Hochart | 3:6bcdaa2636ec | 37 | for(int i=0; i <= 32 ; i++){ |
Alex_Hochart | 3:6bcdaa2636ec | 38 | pc.printf("\033[%d;0H",i); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 39 | pc.printf("X"); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 40 | pc.printf("\033[%d;128H",i); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 41 | pc.printf("X"); //Place le curseur à 0:0 |
Alex_Hochart | 3:6bcdaa2636ec | 42 | } |
Alex_Hochart | 3:6bcdaa2636ec | 43 | |
Mickado | 2:35466dfc81fe | 44 | } |
Mickado | 2:35466dfc81fe | 45 | |
Alex_Hochart | 4:ed21ec4a79ad | 46 | void print_cursor_ttempro(int *pos1, int *pos1prec, int *pos2, int *pos2prec){ |
Alex_Hochart | 4:ed21ec4a79ad | 47 | for(int i=0;i<=29;i++){ |
Alex_Hochart | 4:ed21ec4a79ad | 48 | if( pos1[i] != pos1prec[i] ){ |
Alex_Hochart | 4:ed21ec4a79ad | 49 | if(pos1[i] == 1){ |
Alex_Hochart | 4:ed21ec4a79ad | 50 | pc.printf("\033[%d;3H",i+2); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 51 | pc.printf("X"); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 52 | } |
Alex_Hochart | 4:ed21ec4a79ad | 53 | else{ |
Alex_Hochart | 4:ed21ec4a79ad | 54 | pc.printf("\033[%d;3H",i+2); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 55 | pc.printf(" "); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 56 | } |
Alex_Hochart | 4:ed21ec4a79ad | 57 | } |
Alex_Hochart | 4:ed21ec4a79ad | 58 | |
Alex_Hochart | 4:ed21ec4a79ad | 59 | if( pos2[i] != pos2prec[i] ){ |
Alex_Hochart | 4:ed21ec4a79ad | 60 | if(pos2[i] == 1){ |
Alex_Hochart | 4:ed21ec4a79ad | 61 | pc.printf("\033[%d;126H",i+2); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 62 | pc.printf("X"); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 63 | } |
Alex_Hochart | 4:ed21ec4a79ad | 64 | else{ |
Alex_Hochart | 4:ed21ec4a79ad | 65 | pc.printf("\033[%d;126H",i+2); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 66 | pc.printf(" "); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 67 | } |
Alex_Hochart | 4:ed21ec4a79ad | 68 | } |
Alex_Hochart | 4:ed21ec4a79ad | 69 | } |
Alex_Hochart | 4:ed21ec4a79ad | 70 | } |
Alex_Hochart | 4:ed21ec4a79ad | 71 | |
Alex_Hochart | 4:ed21ec4a79ad | 72 | void set_pos_vector(int *pos, int distance){ |
Alex_Hochart | 4:ed21ec4a79ad | 73 | |
Alex_Hochart | 4:ed21ec4a79ad | 74 | if(distance <= 10){ |
Alex_Hochart | 4:ed21ec4a79ad | 75 | for(int i=0;i<=5;i++){ pos[i] = 1; } |
Alex_Hochart | 4:ed21ec4a79ad | 76 | } |
Alex_Hochart | 4:ed21ec4a79ad | 77 | else if(distance >= 34){ |
Alex_Hochart | 4:ed21ec4a79ad | 78 | for(int i=24;i<=29;i++){ pos[i] = 1; } |
Alex_Hochart | 4:ed21ec4a79ad | 79 | } |
Alex_Hochart | 4:ed21ec4a79ad | 80 | else{ |
Alex_Hochart | 4:ed21ec4a79ad | 81 | for(int i=(distance-10);i<=(distance-4);i++){ pos[i] = 1; } |
Alex_Hochart | 4:ed21ec4a79ad | 82 | } |
Alex_Hochart | 4:ed21ec4a79ad | 83 | } |
Alex_Hochart | 4:ed21ec4a79ad | 84 | |
Alex_Hochart | 5:9437083564d0 | 85 | int set_print_ball(int *pos1,int *pos2,double *posBallX, double *posBallY, double *angBall){ |
Alex_Hochart | 5:9437083564d0 | 86 | |
Alex_Hochart | 5:9437083564d0 | 87 | pc.printf("\033[%d;%dH",(int)*posBallY, (int)*posBallX); //Place le curseur à 0:0 |
Alex_Hochart | 5:9437083564d0 | 88 | pc.printf(" "); //Place le curseur à 0:0 |
Alex_Hochart | 5:9437083564d0 | 89 | *posBallX = *posBallX + 2.0*cos((double)*angBall*3.1415/180.0); |
Alex_Hochart | 5:9437083564d0 | 90 | *posBallY = *posBallY - 2.0*sin((double)*angBall*3.1415/180.0); |
Alex_Hochart | 5:9437083564d0 | 91 | |
Alex_Hochart | 5:9437083564d0 | 92 | if(*posBallX <= 4){ |
Alex_Hochart | 5:9437083564d0 | 93 | if(pos1[(int)*posBallY] == 1){ |
Alex_Hochart | 5:9437083564d0 | 94 | *posBallX = 8 - *posBallX; |
Alex_Hochart | 5:9437083564d0 | 95 | *angBall = 180 - *angBall; |
Alex_Hochart | 5:9437083564d0 | 96 | } |
Alex_Hochart | 5:9437083564d0 | 97 | else{ return 1; } |
Alex_Hochart | 5:9437083564d0 | 98 | } |
Alex_Hochart | 5:9437083564d0 | 99 | else if(*posBallX >= 125){ |
Alex_Hochart | 5:9437083564d0 | 100 | if(pos2[(int)*posBallY] == 1){ |
Alex_Hochart | 5:9437083564d0 | 101 | *posBallX = 250 - *posBallX; |
Alex_Hochart | 5:9437083564d0 | 102 | *angBall = 180 - *angBall; |
Alex_Hochart | 5:9437083564d0 | 103 | } |
Alex_Hochart | 5:9437083564d0 | 104 | else{ return 2; } |
Alex_Hochart | 5:9437083564d0 | 105 | } |
Alex_Hochart | 5:9437083564d0 | 106 | |
Alex_Hochart | 5:9437083564d0 | 107 | |
Alex_Hochart | 5:9437083564d0 | 108 | if(*posBallY <= 2){ |
Alex_Hochart | 5:9437083564d0 | 109 | *posBallY = -*posBallY+4; |
Alex_Hochart | 5:9437083564d0 | 110 | *angBall = -*angBall; |
Alex_Hochart | 5:9437083564d0 | 111 | } |
Alex_Hochart | 5:9437083564d0 | 112 | else if(*posBallY >= 31){ |
Alex_Hochart | 5:9437083564d0 | 113 | *posBallY = 62 - *posBallY; |
Alex_Hochart | 5:9437083564d0 | 114 | *angBall = -*angBall; |
Alex_Hochart | 5:9437083564d0 | 115 | } |
Alex_Hochart | 5:9437083564d0 | 116 | pc.printf("\033[%d;%dH",(int)*posBallY, (int)*posBallX); //Place le curseur à 0:0 |
Alex_Hochart | 5:9437083564d0 | 117 | pc.printf("o"); //Place le curseur à 0:0 |
Alex_Hochart | 5:9437083564d0 | 118 | return 3; |
Alex_Hochart | 5:9437083564d0 | 119 | } |
Alex_Hochart | 4:ed21ec4a79ad | 120 | |
bcostm | 0:028fac66239d | 121 | int main() { |
Alex_Hochart | 3:6bcdaa2636ec | 122 | int pos1prec[30]; |
Alex_Hochart | 3:6bcdaa2636ec | 123 | int pos2prec[30]; |
Alex_Hochart | 3:6bcdaa2636ec | 124 | int pos1[30]; |
Alex_Hochart | 3:6bcdaa2636ec | 125 | int pos2[30]; |
Alex_Hochart | 4:ed21ec4a79ad | 126 | double posBallX; |
Alex_Hochart | 4:ed21ec4a79ad | 127 | double posBallY; |
Alex_Hochart | 4:ed21ec4a79ad | 128 | double angBall; |
Alex_Hochart | 4:ed21ec4a79ad | 129 | int distance1; |
Alex_Hochart | 4:ed21ec4a79ad | 130 | int distance2; |
Alex_Hochart | 5:9437083564d0 | 131 | |
Alex_Hochart | 5:9437083564d0 | 132 | int state; |
Alex_Hochart | 5:9437083564d0 | 133 | int p1_score; |
Alex_Hochart | 5:9437083564d0 | 134 | int p2_score; |
Alex_Hochart | 3:6bcdaa2636ec | 135 | |
Alex_Hochart | 3:6bcdaa2636ec | 136 | pc.baud(115200); |
Alex_Hochart | 4:ed21ec4a79ad | 137 | pong_init_ttempro(); |
Alex_Hochart | 5:9437083564d0 | 138 | posBallX = 5.0; |
Alex_Hochart | 5:9437083564d0 | 139 | posBallY = 19.0; |
Alex_Hochart | 5:9437083564d0 | 140 | angBall = 25.0; |
Alex_Hochart | 5:9437083564d0 | 141 | state = 0; |
Alex_Hochart | 5:9437083564d0 | 142 | p1_score = 0; |
Alex_Hochart | 5:9437083564d0 | 143 | p2_score = 0; |
Alex_Hochart | 3:6bcdaa2636ec | 144 | wait(1); |
Alex_Hochart | 5:9437083564d0 | 145 | led=1; |
Alex_Hochart | 6:89a007f6508f | 146 | wait(1); |
Alex_Hochart | 3:6bcdaa2636ec | 147 | //Initialisation de l'interruption : en appuyant sur le bouton bleu de la carte, le programme change d'état |
Mickado | 2:35466dfc81fe | 148 | |
Mickado | 2:35466dfc81fe | 149 | |
Mickado | 2:35466dfc81fe | 150 | //Boucle d'exécution du programme |
Mickado | 2:35466dfc81fe | 151 | while(1) { |
Alex_Hochart | 5:9437083564d0 | 152 | switch(state){ |
Alex_Hochart | 6:89a007f6508f | 153 | case 0 : if(bp.read() == 1){ |
Alex_Hochart | 5:9437083564d0 | 154 | state = 3; |
Alex_Hochart | 5:9437083564d0 | 155 | pong_init_ttempro(); |
Alex_Hochart | 6:89a007f6508f | 156 | led = 1; |
Alex_Hochart | 5:9437083564d0 | 157 | } |
Alex_Hochart | 6:89a007f6508f | 158 | else led = 0; |
Alex_Hochart | 5:9437083564d0 | 159 | break; |
Alex_Hochart | 5:9437083564d0 | 160 | |
Alex_Hochart | 5:9437083564d0 | 161 | case 3 : for(int i=0; i<=29; i++){ |
Alex_Hochart | 5:9437083564d0 | 162 | pos1prec[i]= pos1[i]; |
Alex_Hochart | 5:9437083564d0 | 163 | pos2prec[i]= pos2[i]; |
Alex_Hochart | 5:9437083564d0 | 164 | pos1[i]= 0; |
Alex_Hochart | 5:9437083564d0 | 165 | pos2[i]= 0; |
Alex_Hochart | 5:9437083564d0 | 166 | } |
Alex_Hochart | 6:89a007f6508f | 167 | distance1 = sensor1.distance(1); |
Alex_Hochart | 6:89a007f6508f | 168 | distance2 = sensor2.distance(1); |
Alex_Hochart | 5:9437083564d0 | 169 | set_pos_vector(pos1,distance1); |
Alex_Hochart | 5:9437083564d0 | 170 | set_pos_vector(pos2,distance2); |
Alex_Hochart | 5:9437083564d0 | 171 | print_cursor_ttempro(pos1,pos1prec,pos2,pos2prec); |
Alex_Hochart | 5:9437083564d0 | 172 | state = set_print_ball(pos1,pos2,&posBallX,&posBallY,&angBall); |
Alex_Hochart | 5:9437083564d0 | 173 | break; |
Alex_Hochart | 5:9437083564d0 | 174 | |
Alex_Hochart | 5:9437083564d0 | 175 | case 1 : p2_score++; |
Alex_Hochart | 5:9437083564d0 | 176 | if( p2_score >= 3){ |
Alex_Hochart | 5:9437083564d0 | 177 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 5:9437083564d0 | 178 | pc.printf("\033[16;60H"); |
Alex_Hochart | 5:9437083564d0 | 179 | pc.printf("P2 WIN "); |
Alex_Hochart | 5:9437083564d0 | 180 | p1_score = 0; |
Alex_Hochart | 5:9437083564d0 | 181 | p2_score = 0; |
Alex_Hochart | 5:9437083564d0 | 182 | posBallX = 5.0; |
Alex_Hochart | 5:9437083564d0 | 183 | posBallY = 19.0; |
Alex_Hochart | 5:9437083564d0 | 184 | angBall = 25.0; |
Alex_Hochart | 5:9437083564d0 | 185 | } |
Alex_Hochart | 5:9437083564d0 | 186 | else{ |
Alex_Hochart | 5:9437083564d0 | 187 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 5:9437083564d0 | 188 | pc.printf("\033[10;60H"); |
Alex_Hochart | 5:9437083564d0 | 189 | pc.printf("P1 : %d // P2 : %d",p1_score,p2_score); |
Alex_Hochart | 5:9437083564d0 | 190 | posBallX = 5.0; |
Alex_Hochart | 5:9437083564d0 | 191 | posBallY = 19.0; |
Alex_Hochart | 5:9437083564d0 | 192 | angBall = 25.0; |
Alex_Hochart | 5:9437083564d0 | 193 | |
Alex_Hochart | 5:9437083564d0 | 194 | } |
Alex_Hochart | 5:9437083564d0 | 195 | state = 0; |
Alex_Hochart | 5:9437083564d0 | 196 | break; |
Alex_Hochart | 5:9437083564d0 | 197 | |
Alex_Hochart | 5:9437083564d0 | 198 | case 2 : p1_score++; |
Alex_Hochart | 5:9437083564d0 | 199 | if( p1_score >= 3){ |
Alex_Hochart | 5:9437083564d0 | 200 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 5:9437083564d0 | 201 | pc.printf("\033[16;60H"); |
Alex_Hochart | 5:9437083564d0 | 202 | pc.printf("P1 WIN "); |
Alex_Hochart | 5:9437083564d0 | 203 | p1_score = 0; |
Alex_Hochart | 5:9437083564d0 | 204 | p2_score = 0; |
Alex_Hochart | 5:9437083564d0 | 205 | posBallX = 124.0; |
Alex_Hochart | 5:9437083564d0 | 206 | posBallY = 19.0; |
Alex_Hochart | 5:9437083564d0 | 207 | angBall = 145.0; |
Alex_Hochart | 5:9437083564d0 | 208 | } |
Alex_Hochart | 5:9437083564d0 | 209 | else{ |
Alex_Hochart | 5:9437083564d0 | 210 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 5:9437083564d0 | 211 | pc.printf("\033[10;60H"); |
Alex_Hochart | 5:9437083564d0 | 212 | pc.printf("P1 : %d // P2 : %d",p1_score,p2_score); |
Alex_Hochart | 5:9437083564d0 | 213 | posBallX = 124.0; |
Alex_Hochart | 5:9437083564d0 | 214 | posBallY = 19.0; |
Alex_Hochart | 5:9437083564d0 | 215 | angBall = 145.0; |
Alex_Hochart | 5:9437083564d0 | 216 | } |
Alex_Hochart | 5:9437083564d0 | 217 | state = 0; |
Alex_Hochart | 5:9437083564d0 | 218 | break; |
Alex_Hochart | 4:ed21ec4a79ad | 219 | } |
Alex_Hochart | 5:9437083564d0 | 220 | wait(0.1); |
Mickado | 2:35466dfc81fe | 221 | } |
bcostm | 0:028fac66239d | 222 | } |
Alex_Hochart | 3:6bcdaa2636ec | 223 | |
bcostm | 0:028fac66239d | 224 |