Last versionnnn

Dependencies:   BLE_API HCSR04 X_NUCLEO_IDB0XA1 mbed

Fork of contest_IOT3 by Contest IOT GSE5

Committer:
Alex_Hochart
Date:
Tue Nov 03 11:55:50 2015 +0000
Revision:
5:9437083564d0
Parent:
4:ed21ec4a79ad
Child:
6:55319ed07a08
Pong3

Who changed what in which revision?

UserRevisionLine numberNew 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 3:6bcdaa2636ec 5 #define ECHO_1 PA_8
Alex_Hochart 3:6bcdaa2636ec 6 #define ECHO_2 PB_4
Alex_Hochart 3:6bcdaa2636ec 7 #define TRIG_1 PB_10
Alex_Hochart 3:6bcdaa2636ec 8 #define TRIG_2 PB_5
Mickado 2:35466dfc81fe 9 #define PUSH USER_BUTTON //PC_13
Mickado 2:35466dfc81fe 10 #define LED_1 LED1
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 5:9437083564d0 20 DigitalIn bp(USER_BUTTON);
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
Alex_Hochart 5:9437083564d0 109 if(*posBallY <= 2){
Alex_Hochart 5:9437083564d0 110 *posBallY = -*posBallY+4;
Alex_Hochart 5:9437083564d0 111 *angBall = -*angBall;
Alex_Hochart 5:9437083564d0 112 }
Alex_Hochart 5:9437083564d0 113 else if(*posBallY >= 31){
Alex_Hochart 5:9437083564d0 114 *posBallY = 62 - *posBallY;
Alex_Hochart 5:9437083564d0 115 *angBall = -*angBall;
Alex_Hochart 5:9437083564d0 116 }
Alex_Hochart 5:9437083564d0 117 pc.printf("\033[%d;%dH",(int)*posBallY, (int)*posBallX); //Place le curseur à 0:0
Alex_Hochart 5:9437083564d0 118 pc.printf("o"); //Place le curseur à 0:0
Alex_Hochart 5:9437083564d0 119 return 3;
Alex_Hochart 5:9437083564d0 120 }
Alex_Hochart 4:ed21ec4a79ad 121
bcostm 0:028fac66239d 122 int main() {
Alex_Hochart 3:6bcdaa2636ec 123 int pos1prec[30];
Alex_Hochart 3:6bcdaa2636ec 124 int pos2prec[30];
Alex_Hochart 3:6bcdaa2636ec 125 int pos1[30];
Alex_Hochart 3:6bcdaa2636ec 126 int pos2[30];
Alex_Hochart 4:ed21ec4a79ad 127 double posBallX;
Alex_Hochart 4:ed21ec4a79ad 128 double posBallY;
Alex_Hochart 4:ed21ec4a79ad 129 double angBall;
Alex_Hochart 4:ed21ec4a79ad 130 int distance1;
Alex_Hochart 4:ed21ec4a79ad 131 int distance2;
Alex_Hochart 5:9437083564d0 132
Alex_Hochart 5:9437083564d0 133 int state;
Alex_Hochart 5:9437083564d0 134 int p1_score;
Alex_Hochart 5:9437083564d0 135 int p2_score;
Alex_Hochart 3:6bcdaa2636ec 136
Alex_Hochart 3:6bcdaa2636ec 137 pc.baud(115200);
Alex_Hochart 4:ed21ec4a79ad 138 pong_init_ttempro();
Alex_Hochart 5:9437083564d0 139 posBallX = 5.0;
Alex_Hochart 5:9437083564d0 140 posBallY = 19.0;
Alex_Hochart 5:9437083564d0 141 angBall = 25.0;
Alex_Hochart 5:9437083564d0 142 state = 0;
Alex_Hochart 5:9437083564d0 143 p1_score = 0;
Alex_Hochart 5:9437083564d0 144 p2_score = 0;
Alex_Hochart 3:6bcdaa2636ec 145 wait(1);
Alex_Hochart 5:9437083564d0 146 led=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 5:9437083564d0 153 case 0 : if(bp.read() != 1){
Alex_Hochart 5:9437083564d0 154 state = 3;
Alex_Hochart 5:9437083564d0 155 pong_init_ttempro();
Alex_Hochart 5:9437083564d0 156 }
Alex_Hochart 5:9437083564d0 157 break;
Alex_Hochart 5:9437083564d0 158
Alex_Hochart 5:9437083564d0 159 case 3 : for(int i=0; i<=29; i++){
Alex_Hochart 5:9437083564d0 160 pos1prec[i]= pos1[i];
Alex_Hochart 5:9437083564d0 161 pos2prec[i]= pos2[i];
Alex_Hochart 5:9437083564d0 162 pos1[i]= 0;
Alex_Hochart 5:9437083564d0 163 pos2[i]= 0;
Alex_Hochart 5:9437083564d0 164 }
Alex_Hochart 5:9437083564d0 165 distance1 = 22;//sensor1.distance(1);
Alex_Hochart 5:9437083564d0 166 distance2 = 10;//sensor2.distance(1);
Alex_Hochart 5:9437083564d0 167 set_pos_vector(pos1,distance1);
Alex_Hochart 5:9437083564d0 168 set_pos_vector(pos2,distance2);
Alex_Hochart 5:9437083564d0 169 print_cursor_ttempro(pos1,pos1prec,pos2,pos2prec);
Alex_Hochart 5:9437083564d0 170 state = set_print_ball(pos1,pos2,&posBallX,&posBallY,&angBall);
Alex_Hochart 5:9437083564d0 171 break;
Alex_Hochart 5:9437083564d0 172
Alex_Hochart 5:9437083564d0 173 case 1 : p2_score++;
Alex_Hochart 5:9437083564d0 174 if( p2_score >= 3){
Alex_Hochart 5:9437083564d0 175 pc.printf("\033[2J"); //Efface la console
Alex_Hochart 5:9437083564d0 176 pc.printf("\033[16;60H");
Alex_Hochart 5:9437083564d0 177 pc.printf("P2 WIN ");
Alex_Hochart 5:9437083564d0 178 p1_score = 0;
Alex_Hochart 5:9437083564d0 179 p2_score = 0;
Alex_Hochart 5:9437083564d0 180 posBallX = 5.0;
Alex_Hochart 5:9437083564d0 181 posBallY = 19.0;
Alex_Hochart 5:9437083564d0 182 angBall = 25.0;
Alex_Hochart 5:9437083564d0 183 }
Alex_Hochart 5:9437083564d0 184 else{
Alex_Hochart 5:9437083564d0 185 pc.printf("\033[2J"); //Efface la console
Alex_Hochart 5:9437083564d0 186 pc.printf("\033[10;60H");
Alex_Hochart 5:9437083564d0 187 pc.printf("P1 : %d // P2 : %d",p1_score,p2_score);
Alex_Hochart 5:9437083564d0 188 posBallX = 5.0;
Alex_Hochart 5:9437083564d0 189 posBallY = 19.0;
Alex_Hochart 5:9437083564d0 190 angBall = 25.0;
Alex_Hochart 5:9437083564d0 191
Alex_Hochart 5:9437083564d0 192 }
Alex_Hochart 5:9437083564d0 193 state = 0;
Alex_Hochart 5:9437083564d0 194 break;
Alex_Hochart 5:9437083564d0 195
Alex_Hochart 5:9437083564d0 196 case 2 : p1_score++;
Alex_Hochart 5:9437083564d0 197 if( p1_score >= 3){
Alex_Hochart 5:9437083564d0 198 pc.printf("\033[2J"); //Efface la console
Alex_Hochart 5:9437083564d0 199 pc.printf("\033[16;60H");
Alex_Hochart 5:9437083564d0 200 pc.printf("P1 WIN ");
Alex_Hochart 5:9437083564d0 201 p1_score = 0;
Alex_Hochart 5:9437083564d0 202 p2_score = 0;
Alex_Hochart 5:9437083564d0 203 posBallX = 124.0;
Alex_Hochart 5:9437083564d0 204 posBallY = 19.0;
Alex_Hochart 5:9437083564d0 205 angBall = 145.0;
Alex_Hochart 5:9437083564d0 206 }
Alex_Hochart 5:9437083564d0 207 else{
Alex_Hochart 5:9437083564d0 208 pc.printf("\033[2J"); //Efface la console
Alex_Hochart 5:9437083564d0 209 pc.printf("\033[10;60H");
Alex_Hochart 5:9437083564d0 210 pc.printf("P1 : %d // P2 : %d",p1_score,p2_score);
Alex_Hochart 5:9437083564d0 211 posBallX = 124.0;
Alex_Hochart 5:9437083564d0 212 posBallY = 19.0;
Alex_Hochart 5:9437083564d0 213 angBall = 145.0;
Alex_Hochart 5:9437083564d0 214 }
Alex_Hochart 5:9437083564d0 215 state = 0;
Alex_Hochart 5:9437083564d0 216 break;
Alex_Hochart 4:ed21ec4a79ad 217 }
Alex_Hochart 5:9437083564d0 218 wait(0.1);
Mickado 2:35466dfc81fe 219 }
bcostm 0:028fac66239d 220 }
Alex_Hochart 3:6bcdaa2636ec 221
bcostm 0:028fac66239d 222