Last

Dependencies:   BLE_API HCSR04 X_NUCLEO_IDB0XA1 mbed

Fork of contest_IOT5 by Contest IOT GSE5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jeu.h Source File

jeu.h

00001 #ifndef JEU_H
00002 #define JEU_H
00003 
00004 #include "mbed.h"
00005 #include "ble/BLE.h"
00006 #include "ble/services/HeartRateService.h"
00007 #include "ble/services/BatteryService.h"
00008 #include "ble/services/DeviceInformationService.h"
00009 
00010 DigitalOut led1(LED1);
00011 
00012 /******************************
00013  |      CONFIGURATION BLE     |
00014  ******************************/
00015 
00016 uint16_t customServiceUUID  = 0xA000;
00017 uint16_t readP1UUID         = 0xA001;
00018 uint16_t readP2UUID         = 0xA002;
00019 uint16_t chronoUUID         = 0xA003;
00020 
00021 const static char     DEVICE_NAME[]        = "Pong!";
00022 static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development
00023 
00024 
00025 // Set Up custom Characteristics
00026 static uint8_t readP1[10] = {0};
00027 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readP1)> player1(readP1UUID, readP1);
00028 static uint8_t readP2[10] = {0};
00029 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readP2)> player2(readP2UUID, readP2);
00030 static uint8_t readChrono[10] = {0};
00031 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readChrono)> chronoJeu(chronoUUID, readChrono);
00032 
00033 // Set up custom service
00034 GattCharacteristic *characteristics[] = {&player1, &player2, &chronoJeu};
00035 GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
00036 
00037 static volatile bool  triggerSensorPolling = false;
00038 
00039 void periodicCallback(void){
00040     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00041     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00042      * heavy-weight sensor polling from the main thread. */
00043     triggerSensorPolling = true;
00044 }
00045 
00046 void initJeuBLE(BLE &ble){
00047     ble.init();
00048 
00049     /* Setup advertising. */
00050     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00051     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00052     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00053     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00054     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00055     ble.addService(customService);
00056     ble.gap().startAdvertising();
00057 }
00058 void miseAJourBLE(BLE &ble,uint8_t &p1_score,uint8_t &p2_score,uint8_t &chrono){
00059     const uint8_t scoreP1const = p1_score;
00060     const uint8_t scoreP2const = p2_score;
00061     const uint8_t chronoconst = chrono;
00062     
00063     ble.gattServer().write(player1.getValueHandle(), &scoreP1const, sizeof(scoreP1const));
00064     ble.gattServer().write(player2.getValueHandle(), &scoreP2const, sizeof(scoreP2const));
00065     ble.gattServer().write(chronoJeu.getValueHandle(), &chronoconst, sizeof(chronoconst));
00066 }
00067 
00068 
00069 /******************************
00070  |      CONFIGURATION JEU     |
00071  ******************************/
00072 
00073 class InfosJeu {
00074   public:
00075     InfosJeu(){
00076         chrono=0;
00077         p1_score=0;
00078         p2_score=0;
00079         chronoActive=0;
00080         
00081         posBallX = 5.0;
00082         posBallY = 19.0;
00083         angBall = 25.0;
00084         state = 0;
00085         p1_score = 0;
00086         p2_score = 0;
00087         wait(1);
00088         //led=1;
00089         distance1=0;
00090         distance2=0;
00091         for(int i=0;i<30;i++){
00092             pos1prec[i]=0;
00093             pos2prec[i]=0;
00094             pos1[i]=0;
00095             pos2[i]=0;
00096         }
00097     }
00098     uint8_t chrono;
00099     bool chronoActive;
00100     uint8_t p1_score;
00101     uint8_t p2_score;
00102     
00103     int pos1prec[30];
00104     int pos2prec[30];
00105     int pos1[30];
00106     int pos2[30];
00107     double posBallX;
00108     double posBallY;
00109     double angBall;
00110     int distance1;
00111     int distance2;
00112     
00113     int state;
00114     
00115     void addPointP1(){p1_score++;}
00116     void addPointP2(){p2_score++;}
00117     void setChronoActive(){chronoActive=true;}
00118     void incrementeChrono(){if(chronoActive){chrono++;}}
00119     void stopChrono(){chronoActive=false;}
00120     void resetAndStart(){chrono=0; p1_score=0; p2_score=0; setChronoActive();}
00121     
00122     void reinitPosition(int p1_sc, int p2_sc, int posX, int posY, int angle){
00123         p1_score = p1_sc;
00124         p2_score = p2_sc;
00125         posBallX = posX;
00126         posBallY = posY;
00127         angBall = angle;
00128     }
00129 };
00130 
00131 
00132 
00133 
00134 #endif