For Contest
Dependencies: BLE_API HCSR04 X_NUCLEO_IDB0XA1 mbed
Fork of contest_IOT6 by
main.cpp@8:8d6474336444, 2015-11-04 (annotated)
- Committer:
- Alex_Hochart
- Date:
- Wed Nov 04 20:41:31 2015 +0000
- Revision:
- 8:8d6474336444
- Parent:
- 7:fd3602c86d75
For Contest
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" |
Mickado | 6:55319ed07a08 | 3 | #include "jeu.h" |
Mickado | 6:55319ed07a08 | 4 | #include "display.h" |
Alex_Hochart | 4:ed21ec4a79ad | 5 | #include <math.h> |
Mickado | 2:35466dfc81fe | 6 | |
Mickado | 6:55319ed07a08 | 7 | |
Alex_Hochart | 8:8d6474336444 | 8 | #define TRIG_1 PA_9 |
Alex_Hochart | 8:8d6474336444 | 9 | #define TRIG_2 PB_10 |
Alex_Hochart | 8:8d6474336444 | 10 | #define ECHO_1 PC_7 |
Alex_Hochart | 8:8d6474336444 | 11 | #define ECHO_2 PA_8 |
Mickado | 2:35466dfc81fe | 12 | #define PUSH USER_BUTTON //PC_13 |
Mickado | 2:35466dfc81fe | 13 | #define LED_1 LED1 |
bcostm | 1:e9d1c42a73ae | 14 | |
Mickado | 6:55319ed07a08 | 15 | /* |
Mickado | 6:55319ed07a08 | 16 | #define ECHO_1 PA_4 |
Mickado | 6:55319ed07a08 | 17 | #define ECHO_2 PB_3 |
Alex_Hochart | 8:8d6474336444 | 18 | #define TRIG_1 PA_5 |
Mickado | 6:55319ed07a08 | 19 | #define TRIG_2 PA_10 |
Mickado | 6:55319ed07a08 | 20 | #define PUSH PA_9 //PC_13 |
Mickado | 6:55319ed07a08 | 21 | #define LED_1 PA_8 |
Mickado | 6:55319ed07a08 | 22 | */ |
Mickado | 6:55319ed07a08 | 23 | |
bcostm | 1:e9d1c42a73ae | 24 | //------------------------------------ |
bcostm | 1:e9d1c42a73ae | 25 | // Hyperterminal configuration |
Alex_Hochart | 4:ed21ec4a79ad | 26 | // 115200 bauds, 8-bit data, no parity |
bcostm | 1:e9d1c42a73ae | 27 | //------------------------------------ |
bcostm | 1:e9d1c42a73ae | 28 | |
Mickado | 2:35466dfc81fe | 29 | |
Alex_Hochart | 8:8d6474336444 | 30 | //DigitalOut led(LED_1); //Led d'état |
Mickado | 6:55319ed07a08 | 31 | DigitalIn bp(PUSH); |
Alex_Hochart | 3:6bcdaa2636ec | 32 | Serial pc(USBTX, USBRX); //UART |
Mickado | 6:55319ed07a08 | 33 | |
Mickado | 2:35466dfc81fe | 34 | |
Alex_Hochart | 3:6bcdaa2636ec | 35 | HCSR04 sensor1(TRIG_1, ECHO_1); |
Alex_Hochart | 3:6bcdaa2636ec | 36 | HCSR04 sensor2(TRIG_2, ECHO_2); |
Alex_Hochart | 3:6bcdaa2636ec | 37 | |
Mickado | 6:55319ed07a08 | 38 | BLE ble; |
Mickado | 2:35466dfc81fe | 39 | |
Mickado | 6:55319ed07a08 | 40 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
Mickado | 6:55319ed07a08 | 41 | ble.gap().startAdvertising(); // restart advertising |
Alex_Hochart | 4:ed21ec4a79ad | 42 | } |
Alex_Hochart | 4:ed21ec4a79ad | 43 | |
Mickado | 6:55319ed07a08 | 44 | InfosJeu jeu; |
Mickado | 6:55319ed07a08 | 45 | /* ACTION POSSIBLES : |
Mickado | 6:55319ed07a08 | 46 | * --> Accès aux valeurs : |
Mickado | 6:55319ed07a08 | 47 | * jeu.p1_score |
Mickado | 6:55319ed07a08 | 48 | * jeu.p2_score |
Mickado | 6:55319ed07a08 | 49 | * jeu.chrono |
Mickado | 6:55319ed07a08 | 50 | * jeu.pos1prec[30]; |
Mickado | 6:55319ed07a08 | 51 | * jeu.pos2prec[30]; |
Mickado | 6:55319ed07a08 | 52 | * jeu.pos1[30]; |
Mickado | 6:55319ed07a08 | 53 | * jeu.pos2[30]; |
Mickado | 6:55319ed07a08 | 54 | * jeu.posBallX; |
Mickado | 6:55319ed07a08 | 55 | * jeu.posBallY; |
Mickado | 6:55319ed07a08 | 56 | * jeu.angBall; |
Mickado | 6:55319ed07a08 | 57 | * jeu.distance1; |
Mickado | 6:55319ed07a08 | 58 | * jeu.distance2; |
Mickado | 6:55319ed07a08 | 59 | * jeu.state; |
Mickado | 6:55319ed07a08 | 60 | * |
Mickado | 6:55319ed07a08 | 61 | * --> Méthodes utiles : |
Mickado | 6:55319ed07a08 | 62 | * jeu.addPointP1(); |
Mickado | 6:55319ed07a08 | 63 | * jeu.addPointP2(); |
Mickado | 6:55319ed07a08 | 64 | * jeu.setChronoActive(); |
Mickado | 6:55319ed07a08 | 65 | * jeu.incrementeChrono(); |
Mickado | 6:55319ed07a08 | 66 | * jeu.stopChrono(); |
Mickado | 6:55319ed07a08 | 67 | * jeu.resetAndStart(); |
Mickado | 6:55319ed07a08 | 68 | */ |
Alex_Hochart | 4:ed21ec4a79ad | 69 | |
bcostm | 0:028fac66239d | 70 | int main() { |
Mickado | 6:55319ed07a08 | 71 | //Initialisation des fonctions récurrentes (toutes les secondes) |
Mickado | 6:55319ed07a08 | 72 | Ticker ticker; |
Mickado | 6:55319ed07a08 | 73 | ticker.attach(periodicCallback, 1); // blink LED every second |
Mickado | 6:55319ed07a08 | 74 | Ticker tickerChrono; |
Mickado | 6:55319ed07a08 | 75 | tickerChrono.attach(&jeu, &InfosJeu::incrementeChrono, 1); |
Alex_Hochart | 5:9437083564d0 | 76 | |
Mickado | 6:55319ed07a08 | 77 | //Initialisation du BLE |
Alex_Hochart | 8:8d6474336444 | 78 | /* |
Mickado | 6:55319ed07a08 | 79 | initJeuBLE(ble); |
Mickado | 6:55319ed07a08 | 80 | ble.gap().onDisconnection(disconnectionCallback); |
Alex_Hochart | 8:8d6474336444 | 81 | */ |
Mickado | 6:55319ed07a08 | 82 | |
Mickado | 6:55319ed07a08 | 83 | //Initialisation de l'affichage |
Mickado | 6:55319ed07a08 | 84 | pong_init_ttempro(pc); |
Alex_Hochart | 3:6bcdaa2636ec | 85 | pc.baud(115200); |
Mickado | 2:35466dfc81fe | 86 | |
Mickado | 2:35466dfc81fe | 87 | //Boucle d'exécution du programme |
Mickado | 6:55319ed07a08 | 88 | while(1) { |
Mickado | 6:55319ed07a08 | 89 | |
Mickado | 6:55319ed07a08 | 90 | /* |
Mickado | 6:55319ed07a08 | 91 | ******* Mise à jour des données BLE chaque seconde ******* |
Alex_Hochart | 8:8d6474336444 | 92 | |
Mickado | 6:55319ed07a08 | 93 | if(triggerSensorPolling && ble.getGapState().connected){ |
Mickado | 6:55319ed07a08 | 94 | triggerSensorPolling = false; // flag indiquant qu'une seconde s'est écoulée, il faut mettre à jour les données BLE |
Mickado | 6:55319ed07a08 | 95 | miseAJourBLE(ble, jeu.p1_score, jeu.p2_score, jeu.chrono); |
Mickado | 6:55319ed07a08 | 96 | }else{ |
Mickado | 6:55319ed07a08 | 97 | ble.waitForEvent(); // low power wait for event |
Mickado | 6:55319ed07a08 | 98 | } |
Alex_Hochart | 8:8d6474336444 | 99 | */ |
Mickado | 6:55319ed07a08 | 100 | /* |
Mickado | 6:55319ed07a08 | 101 | ******* Mécanique de jeu ******** |
Mickado | 6:55319ed07a08 | 102 | */ |
Mickado | 6:55319ed07a08 | 103 | switch(jeu.state){ |
Alex_Hochart | 5:9437083564d0 | 104 | case 0 : if(bp.read() != 1){ |
Alex_Hochart | 8:8d6474336444 | 105 | jeu.state = 4; |
Mickado | 6:55319ed07a08 | 106 | pong_init_ttempro(pc); |
Alex_Hochart | 7:fd3602c86d75 | 107 | } |
Alex_Hochart | 7:fd3602c86d75 | 108 | break; |
Alex_Hochart | 7:fd3602c86d75 | 109 | |
Alex_Hochart | 7:fd3602c86d75 | 110 | case 4 : for(int i=0; i<=29; i++){ |
Alex_Hochart | 7:fd3602c86d75 | 111 | jeu.pos1prec[i]= jeu.pos1[i]; |
Alex_Hochart | 7:fd3602c86d75 | 112 | jeu.pos2prec[i]= jeu.pos2[i]; |
Alex_Hochart | 7:fd3602c86d75 | 113 | jeu.pos1[i]= 0; |
Alex_Hochart | 7:fd3602c86d75 | 114 | jeu.pos2[i]= 0; |
Alex_Hochart | 7:fd3602c86d75 | 115 | } |
Alex_Hochart | 7:fd3602c86d75 | 116 | jeu.distance1 = sensor1.distance(1); |
Alex_Hochart | 7:fd3602c86d75 | 117 | jeu.distance2 = sensor2.distance(1); |
Alex_Hochart | 7:fd3602c86d75 | 118 | set_pos_vector(jeu.pos1,jeu.distance1); |
Alex_Hochart | 7:fd3602c86d75 | 119 | set_pos_vector(jeu.pos2,jeu.distance2); |
Alex_Hochart | 7:fd3602c86d75 | 120 | print_cursor_ttempro(pc,jeu.pos1,jeu.pos1prec,jeu.pos2,jeu.pos2prec); |
Alex_Hochart | 7:fd3602c86d75 | 121 | print_ball(pc,&jeu.distance1,&jeu.distance2,&jeu.posBallX,&jeu.posBallY); |
Alex_Hochart | 7:fd3602c86d75 | 122 | if(bp.read() != 1){ |
Alex_Hochart | 7:fd3602c86d75 | 123 | jeu.state = 3; |
Mickado | 6:55319ed07a08 | 124 | jeu.setChronoActive(); |
Alex_Hochart | 5:9437083564d0 | 125 | } |
Alex_Hochart | 5:9437083564d0 | 126 | break; |
Alex_Hochart | 5:9437083564d0 | 127 | |
Alex_Hochart | 5:9437083564d0 | 128 | case 3 : for(int i=0; i<=29; i++){ |
Mickado | 6:55319ed07a08 | 129 | jeu.pos1prec[i]= jeu.pos1[i]; |
Mickado | 6:55319ed07a08 | 130 | jeu.pos2prec[i]= jeu.pos2[i]; |
Mickado | 6:55319ed07a08 | 131 | jeu.pos1[i]= 0; |
Mickado | 6:55319ed07a08 | 132 | jeu.pos2[i]= 0; |
Alex_Hochart | 5:9437083564d0 | 133 | } |
Mickado | 6:55319ed07a08 | 134 | jeu.distance1 = sensor1.distance(1); |
Mickado | 6:55319ed07a08 | 135 | jeu.distance2 = sensor2.distance(1); |
Mickado | 6:55319ed07a08 | 136 | set_pos_vector(jeu.pos1,jeu.distance1); |
Mickado | 6:55319ed07a08 | 137 | set_pos_vector(jeu.pos2,jeu.distance2); |
Mickado | 6:55319ed07a08 | 138 | print_cursor_ttempro(pc,jeu.pos1,jeu.pos1prec,jeu.pos2,jeu.pos2prec); |
Mickado | 6:55319ed07a08 | 139 | jeu.state = set_print_ball(pc,jeu.pos1,jeu.pos2,&jeu.posBallX,&jeu.posBallY,&jeu.angBall); |
Alex_Hochart | 5:9437083564d0 | 140 | break; |
Alex_Hochart | 5:9437083564d0 | 141 | |
Mickado | 6:55319ed07a08 | 142 | case 1 : jeu.addPointP2(); |
Mickado | 6:55319ed07a08 | 143 | if( jeu.p2_score >= 3){ |
Alex_Hochart | 5:9437083564d0 | 144 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 5:9437083564d0 | 145 | pc.printf("\033[16;60H"); |
Alex_Hochart | 5:9437083564d0 | 146 | pc.printf("P2 WIN "); |
Mickado | 6:55319ed07a08 | 147 | jeu.stopChrono(); |
Alex_Hochart | 8:8d6474336444 | 148 | jeu.reinitPosition(0, 0, 4.0, 19.0, 25.0); // (p1_score, p2_score, posBallX, posBallY, angBall) |
Alex_Hochart | 5:9437083564d0 | 149 | } |
Alex_Hochart | 5:9437083564d0 | 150 | else{ |
Alex_Hochart | 5:9437083564d0 | 151 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 8:8d6474336444 | 152 | pc.printf("\033[16;55H"); |
Mickado | 6:55319ed07a08 | 153 | pc.printf("P1 : %d // P2 : %d",jeu.p1_score,jeu.p2_score); |
Alex_Hochart | 8:8d6474336444 | 154 | jeu.reinitPosition(jeu.p1_score, jeu.p2_score, 4.0, 19.0, 25.0); // (p1_score, p2_score, posBallX, posBallY, angBall) |
Alex_Hochart | 5:9437083564d0 | 155 | } |
Mickado | 6:55319ed07a08 | 156 | jeu.state = 0; |
Alex_Hochart | 5:9437083564d0 | 157 | break; |
Alex_Hochart | 5:9437083564d0 | 158 | |
Mickado | 6:55319ed07a08 | 159 | case 2 : jeu.addPointP1(); |
Mickado | 6:55319ed07a08 | 160 | if( jeu.p1_score >= 3){ |
Alex_Hochart | 5:9437083564d0 | 161 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 5:9437083564d0 | 162 | pc.printf("\033[16;60H"); |
Alex_Hochart | 5:9437083564d0 | 163 | pc.printf("P1 WIN "); |
Mickado | 6:55319ed07a08 | 164 | jeu.stopChrono(); |
Alex_Hochart | 8:8d6474336444 | 165 | jeu.reinitPosition(0, 0, 125.0, 19.0, 145.0); // (p1_score, p2_score, posBallX, posBallY, angBall) |
Alex_Hochart | 5:9437083564d0 | 166 | } |
Alex_Hochart | 5:9437083564d0 | 167 | else{ |
Alex_Hochart | 5:9437083564d0 | 168 | pc.printf("\033[2J"); //Efface la console |
Alex_Hochart | 8:8d6474336444 | 169 | pc.printf("\033[16;55H"); |
Mickado | 6:55319ed07a08 | 170 | pc.printf("P1 : %d // P2 : %d",jeu.p1_score,jeu.p2_score); |
Alex_Hochart | 8:8d6474336444 | 171 | jeu.reinitPosition(jeu.p1_score, jeu.p2_score, 125.0, 19.0, 145.0); // (p1_score, p2_score, posBallX, posBallY, angBall) |
Alex_Hochart | 5:9437083564d0 | 172 | } |
Mickado | 6:55319ed07a08 | 173 | jeu.state = 0; |
Alex_Hochart | 5:9437083564d0 | 174 | break; |
Alex_Hochart | 4:ed21ec4a79ad | 175 | } |
Alex_Hochart | 8:8d6474336444 | 176 | wait(0.075); |
Mickado | 2:35466dfc81fe | 177 | } |
bcostm | 0:028fac66239d | 178 | } |
Alex_Hochart | 3:6bcdaa2636ec | 179 | |
bcostm | 0:028fac66239d | 180 |