Depart L476 Version 1 Communication UART par BLE avec App Inventor Communication HC05 avec F411

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Revision:
14:ac6cbe6ffaab
Parent:
13:38d6b18040c7
diff -r 38d6b18040c7 -r ac6cbe6ffaab main.cpp
--- a/main.cpp	Mon Nov 09 18:12:58 2020 +0000
+++ b/main.cpp	Tue Nov 17 17:18:52 2020 +0000
@@ -2,69 +2,89 @@
 #include "ble/BLE.h"
 #include "ble/services/UARTService.h"
 #include "Serial.h"
-#include "LIS3DH.h"
 #include "stdlib.h"
 
 #define UART_BUFFER (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN)
 
 
-const static char DEVICE_NAME[] = "ObCP";
+const static char DEVICE_NAME[] = "OBCP";
 UARTService *uartServicePtr;
 
 //PWM output
-PwmOut PWMoutput(PB_1);          //Main PWM output
 PwmOut Green(PC_8);              //PWM Red LED
-PwmOut Red(PC_6);                //PWM Green LED
-PwmOut Blue(PC_9);               //PWM Blue LED
-
-//Accelerometer
-
-#define MOSI PC_12
-#define MISO PC_11
-#define CS PC_5
-#define SCLK PC_10
+PwmOut Red(PC_6);              //PWM Red LED
+PwmOut Blue(PC_9);              //PWM Red LED
 
 // Def Peripheriques
-DigitalIn myButton(PC_13);    // USER BUTTON
-//Init accelerometer
-LIS3DH      acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G);
+InterruptIn myButton(PC_13);    // USER BUTTON
 
-Ticker Acceleration;
+///////////Init HC05/////////////////
+Serial HC05(D8, D2);
+//Bluetooth
+//Maitre (Arrivee) : ADDR = 98d3,b1,fd5c5d
+//Esclave (Départ) : ADDR = 0019,10,08FCB5
+/////////////////////////////////////
+//Ticker t;
+////////////TIMER////////////////////
+Timer timer;
+int begin;
+int end;
+float chrono=0;
+void Timer_triggered()
+{
+    timer.start();
+    begin = timer.read_ms();
+    Blue=1;
+    wait(0.5);
+    Blue=0;
+}
+void Timer_Reset()
+{
+    timer.stop();
+    begin=0;
+    end=0;
+    chrono=0;
+}
+/////////////////////////////////////
+
+
+/////////COMMUNICATION///////////////
+void HC05_receive()
+{
+    char IncomingValue = HC05.getc();
+    if(IncomingValue == 'u') {
+        end = timer.read_ms();
+        timer.stop();
+        chrono = end-begin;
+
+    }
+}
+/////////////////////////////////////
+
+
+
+
+
+
 
 // Tableau et index de communication avec UART
 static char uartBuff[UART_BUFFER];
-// Variables contenant accelerations
-double accX, accY, accZ;
 // Routine de traitement des erreurs
 void onBleError(ble_error_t error);
 
 /****************************************************/
 /* Ticker actif lorsque la connexion BLE est présente */
 /****************************************************/
-void ConnectedBLE(void)
-{
-    // Signal de vie: allumer et éteindre la LED
-    Green.period(1.0f);  // période = 1 seconde
-    Green.write(0.01f);  // élaire 1% de la période
-}
 
-void AccMeasurement(void)
+void update(void)
 {
-    char outstrX[5];
-    char outstrY[5];
-    char outstrZ[5];
-
-    // Mesure des accélérations sur les 3 axes
-    accX = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15;
-    accY = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15;
-    accZ = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15;
+    char train[5];
+    float chrono_value = float(chrono)* 0.001F;
 
     //Transformation des valeurs numeriques en chaine de caracteres
-    sprintf(outstrX,"%5.2f",accX);
-    sprintf(outstrY,"%5.2f",accY);
-    sprintf(outstrZ,"%5.2f",accZ);
-    //Integre les trois chaines de caractere contenant les accelerations dans la chaine uartBuff
-    sprintf(uartBuff, "X%s Y%s Z%s", outstrX, outstrY, outstrZ) ;
+    sprintf(train,"%5.2f",chrono_value);
+    //Integre les trois chaines de caractere contenant les ts dans la chaine uartBuff
+    sprintf(uartBuff, "%s", train) ;
     //Envoie la chaine uartBuff sur le sevice TX UART BLE
     uartServicePtr->write(uartBuff, UARTService::BLE_UART_SERVICE_MAX_DATA_LEN);
     // Réinitialiser la chaîne uartBuff en entrant 0 dans les premiers caractères UART_BUFFER
@@ -77,9 +97,8 @@
 /*************************/
 void BleConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
-    // Signal de connexion BLE: allume / éteint la LED avec une période de 1 seconde
-    ConnectedBLE();
-    Acceleration.attach(AccMeasurement, 1);
+    Timer_Reset();
+    update();
 }
 
 /*****************************/
@@ -89,54 +108,39 @@
 {
     (void)params;
     // Redémarrer la publicité
-    Acceleration.detach();
+    Timer_Reset();
+    update();
+    
     BLE::Instance().gap().startAdvertising();
     
-    PWMoutput.write(0.0f);
+    Blue.write(0.0f);
     Green.write(0.0f);
     Red.write(0.0f);
-    Blue.write(0.0f);
 }
 
 /***************************/
 /* Rx de BLE et Rx vers USB*/
 /***************************/
+
 void BleOnDataWrittenCallback(const GattWriteCallbackParams *params)
 {
     char reception[UART_BUFFER];
-    char commande[2];
-    float valeur;
+    char commande[3];
 
     // Reçoit une chaîne de BLE
     if (params->handle == uartServicePtr->getTXCharacteristicHandle()) {
         // Copie de la chaine reçue dans reception
         sprintf(reception,"%s", params->data);
         // Copie dans la chaine commande des deux premier caracteres de la chaine reception
-        sprintf(commande,"%c%c", reception[0], reception[1]);
-        // Transformation des 3 caracteres suivants en valeur numerique
-        valeur = ((reception[2] - '0')* 100 + (reception[3] - '0')* 10 +(reception[4] - '0')) ;
-        valeur = valeur/255;
-                
-        if( strcmp(commande, "LR")==0) {
-            Red.period(0.01f);
-            Red.write(valeur);
+        sprintf(commande,"%c%c%c", reception[0], reception[1], reception[2]);
+        if( strcmp(commande, "RST" )==0 or strcmp(commande, "rst" )==0 ) {
+            Timer_Reset();
+            update();
         }
-        if( strcmp(commande, "LV")==0) {
-            Green.period(0.01f);
-            Green.write(valeur);
-        }
-        if( strcmp(commande, "LB")==0) {
-            Blue.period(0.01f);
-            Blue.write(valeur);
-        }
-        if( strcmp(commande, "PW")==0) {
-            PWMoutput.period(0.001f);
-            PWMoutput.write(valeur);
-        }
-
     }
 }
 
+
 /***************************/
 /* Erreur sur le canal BLE */
 /***************************/
@@ -188,6 +192,11 @@
     // Attend les événements sur BLE
     while (true) {
         ble.waitForEvent();
+        update();
+        while(HC05.readable()) {
+            HC05_receive();
+        }
+        wait(0.01);
     }
 }
 
@@ -196,8 +205,8 @@
 /********/
 int main(void)
 {
-    //Définit le fonctionnement du bouton sur "PullDown": Open = '0'. L’autre mode de fonctionnement est PullUp
-    myButton.mode(PullDown);
+    HC05.baud(38400);
+    myButton.fall(&Timer_triggered);
 
     // Initialiser LED
     Green.write(0.0f);
@@ -208,6 +217,5 @@
     BLE &ble = BLE::Instance();
     ble.init(bleInitComplete);
     /******* FIN initialise BLE ***********/
-
 }