Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of contest_IOT2 by
main.cpp@4:ed21ec4a79ad, 2015-10-23 (annotated)
- Committer:
- Alex_Hochart
- Date:
- Fri Oct 23 14:53:46 2015 +0000
- Revision:
- 4:ed21ec4a79ad
- Parent:
- 3:6bcdaa2636ec
- Child:
- 5:9437083564d0
Pong1
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 | 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 |
Mickado | 2:35466dfc81fe | 20 | InterruptIn button(PUSH); //Bouton d'interruption |
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 | 4:ed21ec4a79ad | 85 | |
bcostm | 0:028fac66239d | 86 | int main() { |
Alex_Hochart | 3:6bcdaa2636ec | 87 | int pos1prec[30]; |
Alex_Hochart | 3:6bcdaa2636ec | 88 | int pos2prec[30]; |
Alex_Hochart | 3:6bcdaa2636ec | 89 | int pos1[30]; |
Alex_Hochart | 3:6bcdaa2636ec | 90 | int pos2[30]; |
Alex_Hochart | 4:ed21ec4a79ad | 91 | double posBallX; |
Alex_Hochart | 4:ed21ec4a79ad | 92 | double posBallY; |
Alex_Hochart | 4:ed21ec4a79ad | 93 | double angBall; |
Alex_Hochart | 4:ed21ec4a79ad | 94 | int distance1; |
Alex_Hochart | 4:ed21ec4a79ad | 95 | int distance2; |
Alex_Hochart | 3:6bcdaa2636ec | 96 | |
Alex_Hochart | 3:6bcdaa2636ec | 97 | pc.baud(115200); |
Alex_Hochart | 4:ed21ec4a79ad | 98 | pong_init_ttempro(); |
Alex_Hochart | 4:ed21ec4a79ad | 99 | posBallX = 5; |
Alex_Hochart | 4:ed21ec4a79ad | 100 | posBallY = 19; |
Alex_Hochart | 4:ed21ec4a79ad | 101 | angBall = 25; |
Alex_Hochart | 3:6bcdaa2636ec | 102 | wait(1); |
Alex_Hochart | 3:6bcdaa2636ec | 103 | //Initialisation de l'interruption : en appuyant sur le bouton bleu de la carte, le programme change d'état |
Mickado | 2:35466dfc81fe | 104 | |
Mickado | 2:35466dfc81fe | 105 | |
Mickado | 2:35466dfc81fe | 106 | //Boucle d'exécution du programme |
Mickado | 2:35466dfc81fe | 107 | while(1) { |
Alex_Hochart | 4:ed21ec4a79ad | 108 | led=1; |
Alex_Hochart | 4:ed21ec4a79ad | 109 | for(int i=0; i<=29; i++){ |
Alex_Hochart | 4:ed21ec4a79ad | 110 | pos1prec[i]= pos1[i]; |
Alex_Hochart | 4:ed21ec4a79ad | 111 | pos2prec[i]= pos2[i]; |
Alex_Hochart | 4:ed21ec4a79ad | 112 | pos1[i]= 0; |
Alex_Hochart | 4:ed21ec4a79ad | 113 | pos2[i]= 0; |
Alex_Hochart | 4:ed21ec4a79ad | 114 | } |
Alex_Hochart | 4:ed21ec4a79ad | 115 | distance1 = sensor1.distance(1); |
Alex_Hochart | 4:ed21ec4a79ad | 116 | distance2 = sensor2.distance(1); |
Alex_Hochart | 4:ed21ec4a79ad | 117 | set_pos_vector(pos1,distance1); |
Alex_Hochart | 4:ed21ec4a79ad | 118 | set_pos_vector(pos2,distance2); |
Alex_Hochart | 4:ed21ec4a79ad | 119 | print_cursor_ttempro(pos1,pos1prec,pos2,pos2prec); |
Alex_Hochart | 4:ed21ec4a79ad | 120 | pc.printf("\033[%d;%dH",(int)posBallY, (int)posBallX); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 121 | pc.printf(" "); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 122 | posBallX = posBallX + 2.0*cos((double)angBall*3.1415/180.0); |
Alex_Hochart | 4:ed21ec4a79ad | 123 | posBallY = posBallY - 2.0*sin((double)angBall*3.1415/180.0); |
Alex_Hochart | 4:ed21ec4a79ad | 124 | if(posBallX <= 4){ |
Alex_Hochart | 4:ed21ec4a79ad | 125 | if(pos1[(int)posBallY] == 1){ |
Alex_Hochart | 4:ed21ec4a79ad | 126 | posBallX = 8 - posBallX; |
Alex_Hochart | 4:ed21ec4a79ad | 127 | angBall = 180 - angBall; |
Alex_Hochart | 3:6bcdaa2636ec | 128 | } |
Alex_Hochart | 4:ed21ec4a79ad | 129 | else { |
Alex_Hochart | 4:ed21ec4a79ad | 130 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 4:ed21ec4a79ad | 131 | pc.printf("\033[16;60H"); |
Alex_Hochart | 4:ed21ec4a79ad | 132 | pc.printf("P1 Loose"); |
Alex_Hochart | 4:ed21ec4a79ad | 133 | wait(5); |
Alex_Hochart | 3:6bcdaa2636ec | 134 | } |
Alex_Hochart | 4:ed21ec4a79ad | 135 | } |
Alex_Hochart | 4:ed21ec4a79ad | 136 | else if(posBallX >= 125){ |
Alex_Hochart | 4:ed21ec4a79ad | 137 | if(pos2[(int)posBallY] == 1){ |
Alex_Hochart | 4:ed21ec4a79ad | 138 | posBallX = 250 - posBallX; |
Alex_Hochart | 4:ed21ec4a79ad | 139 | angBall = 180 - angBall; |
Alex_Hochart | 4:ed21ec4a79ad | 140 | } |
Alex_Hochart | 4:ed21ec4a79ad | 141 | else { |
Alex_Hochart | 4:ed21ec4a79ad | 142 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 4:ed21ec4a79ad | 143 | pc.printf("\033[16;60H"); |
Alex_Hochart | 4:ed21ec4a79ad | 144 | pc.printf("P2 Loose"); |
Alex_Hochart | 4:ed21ec4a79ad | 145 | wait(5); |
Alex_Hochart | 3:6bcdaa2636ec | 146 | } |
Alex_Hochart | 4:ed21ec4a79ad | 147 | } |
Alex_Hochart | 4:ed21ec4a79ad | 148 | |
Alex_Hochart | 4:ed21ec4a79ad | 149 | |
Alex_Hochart | 4:ed21ec4a79ad | 150 | if(posBallY <= 2){ |
Alex_Hochart | 4:ed21ec4a79ad | 151 | posBallY = -posBallY+4; |
Alex_Hochart | 4:ed21ec4a79ad | 152 | angBall = -angBall; |
Alex_Hochart | 4:ed21ec4a79ad | 153 | } |
Alex_Hochart | 4:ed21ec4a79ad | 154 | else if(posBallY >= 31){ |
Alex_Hochart | 4:ed21ec4a79ad | 155 | posBallY = 62 - posBallY; |
Alex_Hochart | 4:ed21ec4a79ad | 156 | angBall = -angBall; |
Alex_Hochart | 4:ed21ec4a79ad | 157 | } |
Alex_Hochart | 4:ed21ec4a79ad | 158 | pc.printf("\033[%d;%dH",(int)posBallY, (int)posBallX); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 159 | pc.printf("o"); //Place le curseur à 0:0 |
Alex_Hochart | 4:ed21ec4a79ad | 160 | |
Alex_Hochart | 4:ed21ec4a79ad | 161 | wait(0.1); |
Mickado | 2:35466dfc81fe | 162 | } |
bcostm | 0:028fac66239d | 163 | } |
Alex_Hochart | 3:6bcdaa2636ec | 164 | |
bcostm | 0:028fac66239d | 165 |