Last versionnnn

Dependencies:   BLE_API HCSR04 X_NUCLEO_IDB0XA1 mbed

Fork of contest_IOT3 by Contest IOT GSE5

Files at this revision

API Documentation at this revision

Comitter:
Mickado
Date:
Wed Nov 04 15:02:59 2015 +0000
Parent:
5:9437083564d0
Commit message:
Last version

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
X_NUCLEO_IDB0XA1.lib Show annotated file Show diff for this revision Revisions of this file
display.h Show annotated file Show diff for this revision Revisions of this file
jeu.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 9437083564d0 -r 55319ed07a08 BLE_API.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_API.lib	Wed Nov 04 15:02:59 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#d494ad3e87bd
diff -r 9437083564d0 -r 55319ed07a08 X_NUCLEO_IDB0XA1.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_IDB0XA1.lib	Wed Nov 04 15:02:59 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1/#46ae62a90136
diff -r 9437083564d0 -r 55319ed07a08 display.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/display.h	Wed Nov 04 15:02:59 2015 +0000
@@ -0,0 +1,103 @@
+
+#ifndef DISPLAY_H
+#define DISPLAY_H
+
+#include "mbed.h"
+
+void pong_init_ttempro(Serial &pc){
+
+    pc.printf("\033[2J");       //Efface la console
+    pc.printf("\033[?25l");     //Cache le curseur
+    
+    for(int i=0; i <= 128 ; i++){
+        pc.printf("\033[0;%dH",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+        pc.printf("\033[32;%dH",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+    }
+    
+    for(int i=0; i <= 32 ; i++){
+        pc.printf("\033[%d;0H",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+        pc.printf("\033[%d;128H",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+    }
+    
+}
+
+void print_cursor_ttempro(Serial &pc, int *pos1, int *pos1prec, int *pos2, int *pos2prec){
+    for(int i=0;i<=29;i++){
+        if( pos1[i] != pos1prec[i] ){
+            if(pos1[i] == 1){
+                pc.printf("\033[%d;3H",i+2);     //Place le curseur à 0:0
+                pc.printf("X");     //Place le curseur à 0:0  
+            }
+            else{
+                pc.printf("\033[%d;3H",i+2);     //Place le curseur à 0:0
+                pc.printf(" ");     //Place le curseur à 0:0  
+            }
+        }
+                
+        if( pos2[i] != pos2prec[i] ){
+            if(pos2[i] == 1){
+                pc.printf("\033[%d;126H",i+2);     //Place le curseur à 0:0
+                pc.printf("X");     //Place le curseur à 0:0  
+            }
+            else{
+                pc.printf("\033[%d;126H",i+2);     //Place le curseur à 0:0
+                pc.printf(" ");     //Place le curseur à 0:0  
+            }
+        }   
+    } 
+}
+
+void set_pos_vector(int *pos, int distance){
+            
+    if(distance <= 10){
+        for(int i=0;i<=5;i++){ pos[i] = 1; }
+    }
+    else if(distance >= 34){
+        for(int i=24;i<=29;i++){ pos[i] = 1; }
+    }
+    else{
+        for(int i=(distance-10);i<=(distance-4);i++){ pos[i] = 1; }
+    }   
+}
+
+int set_print_ball(Serial &pc, int *pos1,int *pos2,double *posBallX, double *posBallY, double *angBall){
+    pc.printf("\033[%d;%dH",(int)*posBallY, (int)*posBallX);     //Place le curseur à 0:0
+    pc.printf(" ");     //Place le curseur à 0:0  
+    *posBallX = *posBallX + 2.0*cos((double)*angBall*3.1415/180.0);
+    *posBallY = *posBallY - 2.0*sin((double)*angBall*3.1415/180.0);
+    
+    if(*posBallX <= 4){
+        if(pos1[(int)*posBallY] == 1){
+            *posBallX = 8 - *posBallX;
+            *angBall = 180 - *angBall;
+        }
+        else{ return 1; }
+        }
+    else if(*posBallX >= 125){
+        if(pos2[(int)*posBallY] == 1){
+            *posBallX = 250 - *posBallX;
+            *angBall = 180 - *angBall;
+        }
+        else{ return 2; }
+    }
+        
+        
+    if(*posBallY <= 2){
+        *posBallY = -*posBallY+4;
+        *angBall = -*angBall;
+    }
+    else if(*posBallY >= 31){
+        *posBallY = 62 - *posBallY;
+        *angBall = -*angBall;
+    }
+    pc.printf("\033[%d;%dH",(int)*posBallY, (int)*posBallX);     //Place le curseur à 0:0
+    pc.printf("o");     //Place le curseur à 0:0  
+    return 3;
+}
+
+
+#endif
\ No newline at end of file
diff -r 9437083564d0 -r 55319ed07a08 jeu.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jeu.h	Wed Nov 04 15:02:59 2015 +0000
@@ -0,0 +1,134 @@
+#ifndef JEU_H
+#define JEU_H
+
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "ble/services/HeartRateService.h"
+#include "ble/services/BatteryService.h"
+#include "ble/services/DeviceInformationService.h"
+
+DigitalOut led1(LED1);
+
+/******************************
+ |      CONFIGURATION BLE     |
+ ******************************/
+
+uint16_t customServiceUUID  = 0xA000;
+uint16_t readP1UUID         = 0xA001;
+uint16_t readP2UUID         = 0xA002;
+uint16_t chronoUUID         = 0xA003;
+
+const static char     DEVICE_NAME[]        = "Pong!";
+static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development
+
+
+// Set Up custom Characteristics
+static uint8_t readP1[10] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readP1)> player1(readP1UUID, readP1);
+static uint8_t readP2[10] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readP2)> player2(readP2UUID, readP2);
+static uint8_t readChrono[10] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readChrono)> chronoJeu(chronoUUID, readChrono);
+
+// Set up custom service
+GattCharacteristic *characteristics[] = {&player1, &player2, &chronoJeu};
+GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+
+static volatile bool  triggerSensorPolling = false;
+
+void periodicCallback(void){
+    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
+     * heavy-weight sensor polling from the main thread. */
+    triggerSensorPolling = true;
+}
+
+void initJeuBLE(BLE &ble){
+    ble.init();
+
+    /* Setup advertising. */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
+    ble.addService(customService);
+    ble.gap().startAdvertising();
+}
+void miseAJourBLE(BLE &ble,uint8_t &p1_score,uint8_t &p2_score,uint8_t &chrono){
+    const uint8_t scoreP1const = p1_score;
+    const uint8_t scoreP2const = p2_score;
+    const uint8_t chronoconst = chrono;
+    
+    ble.gattServer().write(player1.getValueHandle(), &scoreP1const, sizeof(scoreP1const));
+    ble.gattServer().write(player2.getValueHandle(), &scoreP2const, sizeof(scoreP2const));
+    ble.gattServer().write(chronoJeu.getValueHandle(), &chronoconst, sizeof(chronoconst));
+}
+
+
+/******************************
+ |      CONFIGURATION JEU     |
+ ******************************/
+
+class InfosJeu {
+  public:
+    InfosJeu(){
+        chrono=0;
+        p1_score=0;
+        p2_score=0;
+        chronoActive=0;
+        
+        posBallX = 5.0;
+        posBallY = 19.0;
+        angBall = 25.0;
+        state = 0;
+        p1_score = 0;
+        p2_score = 0;
+        wait(1);
+        //led=1;
+        distance1=0;
+        distance2=0;
+        for(int i=0;i<30;i++){
+            pos1prec[i]=0;
+            pos2prec[i]=0;
+            pos1[i]=0;
+            pos2[i]=0;
+        }
+    }
+    uint8_t chrono;
+    bool chronoActive;
+    uint8_t p1_score;
+    uint8_t p2_score;
+    
+    int pos1prec[30];
+    int pos2prec[30];
+    int pos1[30];
+    int pos2[30];
+    double posBallX;
+    double posBallY;
+    double angBall;
+    int distance1;
+    int distance2;
+    
+    int state;
+    
+    void addPointP1(){p1_score++;}
+    void addPointP2(){p2_score++;}
+    void setChronoActive(){chronoActive=true;}
+    void incrementeChrono(){if(chronoActive){chrono++;}}
+    void stopChrono(){chronoActive=false;}
+    void resetAndStart(){chrono=0; p1_score=0; p2_score=0; setChronoActive();}
+    
+    void reinitPosition(int p1_sc, int p2_sc, int posX, int posY, int angle){
+        p1_score = p1_sc;
+        p2_score = p2_sc;
+        posBallX = posX;
+        posBallY = posY;
+        angBall = angle;
+    }
+};
+
+
+
+
+#endif
\ No newline at end of file
diff -r 9437083564d0 -r 55319ed07a08 main.cpp
--- a/main.cpp	Tue Nov 03 11:55:50 2015 +0000
+++ b/main.cpp	Wed Nov 04 15:02:59 2015 +0000
@@ -1,221 +1,160 @@
 #include "mbed.h"
 #include "HCSR04.h"
+#include "jeu.h"
+#include "display.h"
 #include <math.h>
 
-#define ECHO_1  PA_8
-#define ECHO_2  PB_4
-#define TRIG_1  PB_10
-#define TRIG_2  PB_5
+
+#define ECHO_1  PA_9
+#define ECHO_2  PB_9
+#define TRIG_1  PC_7
+#define TRIG_2  PB_8
 #define PUSH    USER_BUTTON //PC_13
 #define LED_1   LED1
 
+/*
+#define ECHO_1  PA_4
+#define ECHO_2  PB_3
+#define TRIG_1  PB_5
+#define TRIG_2  PA_10
+#define PUSH    PA_9 //PC_13
+#define LED_1   PA_8
+*/
+
 //------------------------------------
 // Hyperterminal configuration
 // 115200 bauds, 8-bit data, no parity
 //------------------------------------
 
 
+DigitalOut led(LED_1);      //Led d'état
+DigitalIn bp(PUSH);
 Serial pc(USBTX, USBRX);    //UART
-DigitalOut led(LED_1);      //Led d'état
-DigitalIn bp(USER_BUTTON);
+
 
 HCSR04 sensor1(TRIG_1, ECHO_1);
 HCSR04 sensor2(TRIG_2, ECHO_2); 
 
-void pong_init_ttempro(){
-
-    pc.printf("\033[2J");       //Efface la console
-    pc.printf("\033[?25l");     //Cache le curseur
-    
-    for(int i=0; i <= 128 ; i++){
-        pc.printf("\033[0;%dH",i);     //Place le curseur à 0:0
-        pc.printf("X");     //Place le curseur à 0:0
-        pc.printf("\033[32;%dH",i);     //Place le curseur à 0:0
-        pc.printf("X");     //Place le curseur à 0:0
-    }
-    
-    for(int i=0; i <= 32 ; i++){
-        pc.printf("\033[%d;0H",i);     //Place le curseur à 0:0
-        pc.printf("X");     //Place le curseur à 0:0
-        pc.printf("\033[%d;128H",i);     //Place le curseur à 0:0
-        pc.printf("X");     //Place le curseur à 0:0
-    }
-    
-}
+BLE  ble;
 
-void print_cursor_ttempro(int *pos1, int *pos1prec, int *pos2, int *pos2prec){
-    for(int i=0;i<=29;i++){
-        if( pos1[i] != pos1prec[i] ){
-            if(pos1[i] == 1){
-                pc.printf("\033[%d;3H",i+2);     //Place le curseur à 0:0
-                pc.printf("X");     //Place le curseur à 0:0  
-            }
-            else{
-                pc.printf("\033[%d;3H",i+2);     //Place le curseur à 0:0
-                pc.printf(" ");     //Place le curseur à 0:0  
-            }
-        }
-                
-        if( pos2[i] != pos2prec[i] ){
-            if(pos2[i] == 1){
-                pc.printf("\033[%d;126H",i+2);     //Place le curseur à 0:0
-                pc.printf("X");     //Place le curseur à 0:0  
-            }
-            else{
-                pc.printf("\033[%d;126H",i+2);     //Place le curseur à 0:0
-                pc.printf(" ");     //Place le curseur à 0:0  
-            }
-        }   
-    } 
-}
-
-void set_pos_vector(int *pos, int distance){
-            
-    if(distance <= 10){
-        for(int i=0;i<=5;i++){ pos[i] = 1; }
-    }
-    else if(distance >= 34){
-        for(int i=24;i<=29;i++){ pos[i] = 1; }
-    }
-    else{
-        for(int i=(distance-10);i<=(distance-4);i++){ pos[i] = 1; }
-    }   
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
+    ble.gap().startAdvertising(); // restart advertising
 }
 
-int set_print_ball(int *pos1,int *pos2,double *posBallX, double *posBallY, double *angBall){
-    
-    pc.printf("\033[%d;%dH",(int)*posBallY, (int)*posBallX);     //Place le curseur à 0:0
-    pc.printf(" ");     //Place le curseur à 0:0  
-    *posBallX = *posBallX + 2.0*cos((double)*angBall*3.1415/180.0);
-    *posBallY = *posBallY - 2.0*sin((double)*angBall*3.1415/180.0);
-    
-    if(*posBallX <= 4){
-        if(pos1[(int)*posBallY] == 1){
-            *posBallX = 8 - *posBallX;
-            *angBall = 180 - *angBall;
-        }
-        else{ return 1; }
-        }
-    else if(*posBallX >= 125){
-        if(pos2[(int)*posBallY] == 1){
-            *posBallX = 250 - *posBallX;
-            *angBall = 180 - *angBall;
-        }
-        else{ return 2; }
-        }
-    }
-        
-        
-    if(*posBallY <= 2){
-        *posBallY = -*posBallY+4;
-        *angBall = -*angBall;
-    }
-    else if(*posBallY >= 31){
-        *posBallY = 62 - *posBallY;
-        *angBall = -*angBall;
-    }
-    pc.printf("\033[%d;%dH",(int)*posBallY, (int)*posBallX);     //Place le curseur à 0:0
-    pc.printf("o");     //Place le curseur à 0:0  
-    return 3;
-}
+InfosJeu jeu;
+/*  ACTION POSSIBLES : 
+ *  --> Accès aux valeurs :
+ *      jeu.p1_score
+ *      jeu.p2_score
+ *      jeu.chrono
+ *      jeu.pos1prec[30];
+ *      jeu.pos2prec[30];
+ *      jeu.pos1[30];
+ *      jeu.pos2[30];
+ *      jeu.posBallX;
+ *      jeu.posBallY;
+ *      jeu.angBall;
+ *      jeu.distance1;
+ *      jeu.distance2;
+ *      jeu.state;
+ *
+ *  --> Méthodes utiles :
+ *      jeu.addPointP1();
+ *      jeu.addPointP2();
+ *      jeu.setChronoActive();
+ *      jeu.incrementeChrono();
+ *      jeu.stopChrono();
+ *      jeu.resetAndStart();
+ */   
 
 int main() {
-    int pos1prec[30];
-    int pos2prec[30];
-    int pos1[30];
-    int pos2[30];
-    double posBallX;
-    double posBallY;
-    double angBall;
-    int distance1;
-    int distance2;
+    //Initialisation des fonctions récurrentes (toutes les secondes)
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1); // blink LED every second
+    Ticker tickerChrono;
+    tickerChrono.attach(&jeu, &InfosJeu::incrementeChrono, 1);
     
-    int state;
-    int p1_score;
-    int p2_score;
-
+    //Initialisation du BLE
+    initJeuBLE(ble);
+    ble.gap().onDisconnection(disconnectionCallback);
+    
+    //Initialisation de l'affichage
+    pong_init_ttempro(pc);
     pc.baud(115200);
-    pong_init_ttempro();
-    posBallX = 5.0;
-    posBallY = 19.0;
-    angBall = 25.0;
-    state = 0;
-    p1_score = 0;
-    p2_score = 0;
-    wait(1);
-    led=1;
-    //Initialisation de l'interruption : en appuyant sur le bouton bleu de la carte, le programme change d'état
-    
     
     //Boucle d'exécution du programme
-    while(1) { 
-        switch(state){
+    while(1) {
+        
+        /*
+        ******* Mise à jour des données BLE chaque seconde *******
+        */
+        if(triggerSensorPolling && ble.getGapState().connected){
+            triggerSensorPolling = false; // flag indiquant qu'une seconde s'est écoulée, il faut mettre à jour les données BLE           
+            miseAJourBLE(ble, jeu.p1_score, jeu.p2_score, jeu.chrono);
+        }else{
+            ble.waitForEvent(); // low power wait for event
+        }
+        
+        /*
+        ******* Mécanique de jeu ********
+        */
+        switch(jeu.state){
         case 0 :    if(bp.read() != 1){ 
-                    state = 3; 
-                    pong_init_ttempro();
+                        jeu.state = 3; 
+                        pong_init_ttempro(pc);
+                        jeu.setChronoActive();
                     }
                     break;
                     
         case 3 :    for(int i=0; i<=29; i++){
-                        pos1prec[i]= pos1[i];
-                        pos2prec[i]= pos2[i];
-                        pos1[i]= 0;
-                        pos2[i]= 0;
+                        jeu.pos1prec[i]= jeu.pos1[i];
+                        jeu.pos2prec[i]= jeu.pos2[i];
+                        jeu.pos1[i]= 0;
+                        jeu.pos2[i]= 0;
                     }
-                    distance1 = 22;//sensor1.distance(1);
-                    distance2 = 10;//sensor2.distance(1);
-                    set_pos_vector(pos1,distance1);
-                    set_pos_vector(pos2,distance2);     
-                    print_cursor_ttempro(pos1,pos1prec,pos2,pos2prec);
-                    state = set_print_ball(pos1,pos2,&posBallX,&posBallY,&angBall);
+                    jeu.distance1 = sensor1.distance(1);
+                    jeu.distance2 = sensor2.distance(1);
+                    set_pos_vector(jeu.pos1,jeu.distance1);
+                    set_pos_vector(jeu.pos2,jeu.distance2);     
+                    print_cursor_ttempro(pc,jeu.pos1,jeu.pos1prec,jeu.pos2,jeu.pos2prec);
+                    jeu.state = set_print_ball(pc,jeu.pos1,jeu.pos2,&jeu.posBallX,&jeu.posBallY,&jeu.angBall);
                     break;
                     
-        case 1 :    p2_score++;
-                    if( p2_score >= 3){ 
+        case 1 :    jeu.addPointP2();
+                    if( jeu.p2_score >= 3){ 
                         pc.printf("\033[2J");       //Efface la console
                         pc.printf("\033[16;60H"); 
                         pc.printf("P2  WIN ");
-                        p1_score = 0;
-                        p2_score = 0;
-                        posBallX = 5.0;
-                        posBallY = 19.0;
-                        angBall = 25.0;
+                        jeu.stopChrono();
+                        jeu.reinitPosition(0, 0, 5.0, 19.0, 25.0); // (p1_score, p2_score, posBallX, posBallY, angBall)
                     }
                     else{
                         pc.printf("\033[2J");       //Efface la console
                         pc.printf("\033[10;60H"); 
-                        pc.printf("P1 : %d  // P2 : %d",p1_score,p2_score);
-                        posBallX = 5.0;
-                        posBallY = 19.0;
-                        angBall = 25.0;
-                        
+                        pc.printf("P1 : %d  // P2 : %d",jeu.p1_score,jeu.p2_score);
+                        jeu.reinitPosition(jeu.p1_score, jeu.p2_score, 5.0, 19.0, 25.0); // (p1_score, p2_score, posBallX, posBallY, angBall)
                     }
-                    state = 0;
+                    jeu.state = 0;
                     break;
                     
-        case 2 :    p1_score++;
-                    if( p1_score >= 3){ 
+        case 2 :    jeu.addPointP1();
+                    if( jeu.p1_score >= 3){ 
                         pc.printf("\033[2J");       //Efface la console
                         pc.printf("\033[16;60H"); 
                         pc.printf("P1  WIN ");
-                        p1_score = 0;
-                        p2_score = 0; 
-                        posBallX = 124.0;
-                        posBallY = 19.0;
-                        angBall = 145.0; 
+                        jeu.stopChrono();
+                        jeu.reinitPosition(0, 0, 124.0, 19.0, 145.0); // (p1_score, p2_score, posBallX, posBallY, angBall)
                     }
                     else{
                         pc.printf("\033[2J");       //Efface la console
                         pc.printf("\033[10;60H"); 
-                        pc.printf("P1 : %d  // P2 : %d",p1_score,p2_score);
-                        posBallX = 124.0;
-                        posBallY = 19.0;
-                        angBall = 145.0;
+                        pc.printf("P1 : %d  // P2 : %d",jeu.p1_score,jeu.p2_score);
+                        jeu.reinitPosition(jeu.p1_score, jeu.p2_score, 124.0, 19.0, 145.0); // (p1_score, p2_score, posBallX, posBallY, angBall)
                     }
-                    state = 0;
+                    jeu.state = 0;
                     break;
         }
-        wait(0.1);   
+        wait(0.1);
     }
 }