ENSMM / Mbed 2 deprecated Laser_Gate_Timer_Depart

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 #include "ble/services/UARTService.h"
00004 #include "Serial.h"
00005 #include "stdlib.h"
00006 
00007 #define UART_BUFFER (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN)
00008 
00009 
00010 const static char DEVICE_NAME[] = "ObCP";
00011 UARTService *uartServicePtr;
00012 
00013 ///////////Périphériques/////////////////
00014 InterruptIn myButton(USER_BUTTON);
00015 InterruptIn photodiode(A2);
00016 DigitalOut myled(LED1);
00017 DigitalOut laser(A3);
00018 DigitalIn photodiode1(A2);
00019 DigitalOut compteur(A4);
00020 float photodiode2 = 0;
00021 
00022 ///////////Init devices//////////////////
00023 Serial device(D8, D2);
00024 Serial pc(USBTX, USBRX);
00025 
00026 //////////Initialisations////////////////
00027 Timer timer_player_1;
00028 Timer timer_player_2;
00029 int begin1;
00030 int begin2;
00031 int end1;
00032 int end2;
00033 float chrono1;
00034 float chrono2;
00035 char temp=0;
00036 char temp2=0;
00037 char temp3=0;
00038 char gamemode = 'M';
00039 
00040 void Timer_Reset()
00041 {
00042     timer_player_1.stop();
00043     timer_player_2.stop();
00044     timer_player_1.reset();
00045     timer_player_2.reset();
00046     begin1=0;
00047     begin2=0;
00048     end1=0;
00049     end2=0;
00050     chrono1=0;
00051     chrono2=0;
00052 }
00053 
00054 void photodiode_triggered()
00055 {
00056     if (gamemode == 'S') {
00057         compteur = 1;
00058         wait(0.001);
00059         compteur = 0;
00060         Timer_Reset();
00061         timer_player_1.start();
00062         begin1 = timer_player_1.read_ms();
00063         myled=1;
00064         wait(0.5);
00065         myled=0;
00066         
00067     }
00068     if (gamemode == 'D') {
00069         wait(0.001);
00070         compteur = 0;
00071         end2 = timer_player_2.read_ms();
00072         timer_player_2.stop();
00073         chrono2 = end2-begin2;
00074     }
00075     if (gamemode == 'L') {
00076         wait(0.001);
00077         compteur = 0;
00078         timer_player_1.start();
00079         chrono1 = timer_player_1.read_ms();
00080         myled=1;
00081         wait(0.5);
00082         myled=0;
00083     }
00084 }
00085 /////////////////////////////////////
00086 
00087 
00088 /////////COMMUNICATION///////////////
00089 void device_receive()
00090 {
00091     temp=device.getc();
00092     //Ignore Sync pattern and do not pass on to PC
00093     if (temp!=0xAA) {
00094         //Mode solo, la cellule arrivée envoie u à la cellule départ
00095         if(temp == 0xBB and temp2!=0xBB) {
00096             temp2=0xBB;
00097         }
00098         if(temp == 0xBC and temp2==0xBB) {
00099             temp3 = 0xBC;
00100         }
00101         if(temp == 0xBD and temp3==0xBC) {
00102             end1 = timer_player_1.read_ms();
00103             timer_player_1.stop();
00104             chrono1 = end1-begin1-0.02;
00105             photodiode2 = 0;
00106             pc.printf("C'est bon");
00107         }
00108         if(temp == 0xCC) {
00109             photodiode2 = 1;
00110         }
00111         pc.putc(temp);
00112     }
00113 
00114 
00115 }
00116 /////////////////////////////////////
00117 
00118 
00119 // Tableau et index de communication avec UART
00120 static char uartBuff[UART_BUFFER];
00121 // Routine de traitement des erreurs
00122 void onBleError(ble_error_t error);
00123 
00124 /****************************************************/
00125 /* Ticker actif lorsque la connexion BLE est présente */
00126 /****************************************************/
00127 void ConnectedBLE(void)
00128 {
00129     // Signal de vie: allumer et éteindre la LED
00130 
00131 }
00132 
00133 void update(void)
00134 {
00135 
00136     if (gamemode != 'A') {
00137         char train1[5];
00138         char train2[5];
00139         float chrono_value_J1 = float(chrono1)* 0.001F;
00140         float chrono_value_J2 = float(chrono2)* 0.001F;
00141 
00142         //Transformation des valeurs numeriques en chaine de caracteres
00143         sprintf(train1,"%5.2f",chrono_value_J1);
00144         sprintf(train2,"%5.2f",chrono_value_J2);
00145         //Integre les trois chaines de caractere contenant les ts dans la chaine uartBuff
00146         sprintf(uartBuff, "%s %s", train1, train2) ;
00147         //Envoie la chaine uartBuff sur le sevice TX UART BLE
00148         uartServicePtr->write(uartBuff, UARTService::BLE_UART_SERVICE_MAX_DATA_LEN);
00149         // Réinitialiser la chaîne uartBuff en entrant 0 dans les premiers caractères UART_BUFFER
00150         memset(uartBuff, 0, UART_BUFFER);
00151     } else {
00152         char laser1[1];
00153         char laser2[1];
00154 
00155         //Transformation des valeurs numeriques en chaine de caracteres
00156         float meas = photodiode1.read();
00157         sprintf(laser1,"%1.0f",meas);
00158         sprintf(laser2,"%1.0f",photodiode2);
00159         //Integre les trois chaines de caractere contenant les ts dans la chaine uartBuff
00160         sprintf(uartBuff, "%s %s", laser1, laser2) ;
00161         //Envoie la chaine uartBuff sur le sevice TX UART BLE
00162         uartServicePtr->write(uartBuff, UARTService::BLE_UART_SERVICE_MAX_DATA_LEN);
00163         // Réinitialiser la chaîne uartBuff en entrant 0 dans les premiers caractères UART_BUFFER
00164         memset(uartBuff, 0, UART_BUFFER);
00165     }
00166 
00167 
00168 }
00169 
00170 /*************************/
00171 /* Connexion BLE réussie */
00172 /*************************/
00173 void BleConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
00174 {
00175     // Signal de connexion BLE: allume / éteint la LED avec une période de 1 seconde
00176     ConnectedBLE();
00177 }
00178 
00179 /*****************************/
00180 /* Déconnexion du client BLE */
00181 /*****************************/
00182 void BleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00183 {
00184     (void)params;
00185     // Redémarrer la publicité
00186     Timer_Reset();
00187     BLE::Instance().gap().startAdvertising();
00188 
00189 
00190 }
00191 
00192 /***************************/
00193 /* Rx de BLE et Rx vers USB*/
00194 /***************************/
00195 void BleOnDataWrittenCallback(const GattWriteCallbackParams *params)
00196 {
00197     char reception[UART_BUFFER];
00198     char commande[3];
00199     if (params->handle == uartServicePtr->getTXCharacteristicHandle()) {
00200         // Copie de la chaine reçue dans reception
00201         sprintf(reception,"%s", params->data);
00202         // Copie dans la chaine commande des deux premier caracteres de la chaine reception
00203         sprintf(commande,"%c%c%c", reception[0], reception[1], reception[2]);
00204 
00205         if( strcmp(commande, "RST" )==0 ) {
00206             Timer_Reset();
00207             update();
00208         }
00209         if( strcmp(commande, "SOL" )==0 ) {
00210             gamemode = 'S';
00211             laser=1;
00212             update();
00213         }
00214         if( strcmp(commande, "DUO" )==0 ) {
00215             gamemode = 'D';
00216             laser=1;
00217             update();
00218         }
00219         if( strcmp(commande, "LAP" )==0 ) {
00220             gamemode = 'L';
00221             laser=1;
00222             update();
00223         }
00224         if( strcmp(commande, "MEN" )==0 ) {
00225             gamemode = 'M';
00226             laser=0;
00227             update();
00228         }
00229         if( strcmp(commande, "LAS" )==0 ) {
00230             gamemode = 'A';
00231             laser=1;
00232             update();
00233         }
00234         if( strcmp(commande, "BIP" )==0 ) {
00235             Timer_Reset();
00236             timer_player_1.start();
00237             timer_player_2.start();
00238             begin1 = timer_player_1.read_ms();
00239             begin1 = timer_player_2.read_ms();
00240             myled=1;
00241             wait(0.5);
00242             myled=0;
00243         }
00244     }
00245 }
00246 
00247 /***************************/
00248 /* Erreur sur le canal BLE */
00249 /***************************/
00250 
00251 void onBleError(ble_error_t error)
00252 {
00253     /* Entrer le traitement des erreurs */
00254 }
00255 
00256 /**************************************/
00257 /* Initialisation du service BLE UART */
00258 /**************************************/
00259 
00260 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00261 {
00262     BLE&        ble   = params->ble;
00263     ble_error_t error = params->error;
00264 
00265     if (error != BLE_ERROR_NONE) {
00266 
00267         /* En cas d'erreur, transmettez le traitement d'erreur à onBleInitError*/
00268         onBleError(error);
00269         return;
00270     }
00271 
00272     /* Assurez-vous qu'il s'agit de l'instance par défaut de BLE */
00273     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00274         return;
00275     }
00276 
00277     ble.gap().onConnection(BleConnectionCallback);
00278     ble.gap().onDisconnection(BleDisconnectionCallback);
00279     ble.gattServer().onDataWritten(BleOnDataWrittenCallback);
00280 
00281     /* Configuration du service primaire. */
00282     UARTService uartService(ble);
00283     uartServicePtr = &uartService;
00284 
00285     /* Configurer la publicité */
00286     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00287     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00288     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00289     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00290     ble.gap().setAdvertisingInterval(500); /* 500ms. */
00291     ble.gap().startAdvertising();
00292 
00293     // Attend les événements sur BLE
00294     while (true) {
00295         
00296         ble.waitForEvent();
00297         update();
00298         //RF Receive Code
00299         device_receive();
00300         wait(0.01);
00301     }
00302 }
00303 
00304 /********/
00305 /* MAIN */
00306 /********/
00307 int main(void)
00308 {
00309     device.baud(2400);
00310     myButton.fall(&photodiode_triggered);
00311     photodiode.fall(&photodiode_triggered);
00312     
00313 
00314     /****** START Initialiser BLE **********/
00315     BLE &ble = BLE::Instance();
00316     ble.init(bleInitComplete);
00317     /******* FIN initialise BLE ***********/
00318 }
00319