Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Bluetooth/IHM_Bluetooth.cpp@2:1103f5d61035, 2020-03-11 (annotated)
- Committer:
- Wael_H
- Date:
- Wed Mar 11 14:24:48 2020 +0000
- Revision:
- 2:1103f5d61035
- Child:
- 3:07109a6bb472
publish IHM_bt pour ajout de commentaires
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Wael_H | 2:1103f5d61035 | 1 | #include "mbed.h" |
| Wael_H | 2:1103f5d61035 | 2 | #include "math.h" |
| Wael_H | 2:1103f5d61035 | 3 | #include "IHM_Bluetooth.h" //ticker pour gérer la frequence d'appelle d'une fonction |
| Wael_H | 2:1103f5d61035 | 4 | |
| Wael_H | 2:1103f5d61035 | 5 | extern Serial bt; |
| Wael_H | 2:1103f5d61035 | 6 | |
| Wael_H | 2:1103f5d61035 | 7 | // Constructeur |
| Wael_H | 2:1103f5d61035 | 8 | IHM_Bluetooth::IHM_Bluetooth() : /*blueTxBuffer{STARTBYTE,0,0,0,0,STOPBYTE}, paramRx{1, 0, 0, 0},*/ cb_lm(0), cb_rm(0), cb_err(0), cb_cerr(0), cb_speed(0) |
| Wael_H | 2:1103f5d61035 | 9 | { |
| Wael_H | 2:1103f5d61035 | 10 | unsigned int tab1[6] = {STARTBYTE,0,0,0,0,STOPBYTE}, tab2[4] = {1, 0, 0, 0}; |
| Wael_H | 2:1103f5d61035 | 11 | |
| Wael_H | 2:1103f5d61035 | 12 | for(int i=0; i<6; ++i) |
| Wael_H | 2:1103f5d61035 | 13 | blueRxBuffer[i] = tab1[i]; |
| Wael_H | 2:1103f5d61035 | 14 | |
| Wael_H | 2:1103f5d61035 | 15 | for(int i=0; i<4; ++i) |
| Wael_H | 2:1103f5d61035 | 16 | paramRx[i] = tab2[i]; |
| Wael_H | 2:1103f5d61035 | 17 | } |
| Wael_H | 2:1103f5d61035 | 18 | |
| Wael_H | 2:1103f5d61035 | 19 | /********************************** Main *************************************/ |
| Wael_H | 2:1103f5d61035 | 20 | |
| Wael_H | 2:1103f5d61035 | 21 | void IHM_Bluetooth::run(void) |
| Wael_H | 2:1103f5d61035 | 22 | { |
| Wael_H | 2:1103f5d61035 | 23 | bt.attach(this, &IHM_Bluetooth::receptionBlu, Serial::RxIrq); //Creation interruption de reception |
| Wael_H | 2:1103f5d61035 | 24 | bt.printf("Hello world"); //message de test |
| Wael_H | 2:1103f5d61035 | 25 | timer.attach(this, &IHM_Bluetooth::automate_Tx,0.015); |
| Wael_H | 2:1103f5d61035 | 26 | } |
| Wael_H | 2:1103f5d61035 | 27 | |
| Wael_H | 2:1103f5d61035 | 28 | |
| Wael_H | 2:1103f5d61035 | 29 | /******************************* Fonctions de transmission *****************************/ |
| Wael_H | 2:1103f5d61035 | 30 | |
| Wael_H | 2:1103f5d61035 | 31 | void IHM_Bluetooth::transmitBlu(void) |
| Wael_H | 2:1103f5d61035 | 32 | { |
| Wael_H | 2:1103f5d61035 | 33 | static short i = 0; |
| Wael_H | 2:1103f5d61035 | 34 | if(bt.writable()) //fonctionne si la liaison est libre |
| Wael_H | 2:1103f5d61035 | 35 | { |
| Wael_H | 2:1103f5d61035 | 36 | bt.putc(blueTxBuffer[i]); //transmission de la trame de 6 octets |
| Wael_H | 2:1103f5d61035 | 37 | i++; |
| Wael_H | 2:1103f5d61035 | 38 | } |
| Wael_H | 2:1103f5d61035 | 39 | if(i>5) |
| Wael_H | 2:1103f5d61035 | 40 | { |
| Wael_H | 2:1103f5d61035 | 41 | i=0; |
| Wael_H | 2:1103f5d61035 | 42 | bt.attach(NULL, Serial::TxIrq); // Arret de la transmission lorsqu une trame est envoyee |
| Wael_H | 2:1103f5d61035 | 43 | } |
| Wael_H | 2:1103f5d61035 | 44 | } |
| Wael_H | 2:1103f5d61035 | 45 | |
| Wael_H | 2:1103f5d61035 | 46 | void IHM_Bluetooth::formatTrameT(short reg, int val) |
| Wael_H | 2:1103f5d61035 | 47 | { |
| Wael_H | 2:1103f5d61035 | 48 | val=val*10000; |
| Wael_H | 2:1103f5d61035 | 49 | blueTxBuffer[1]=(char)reg; |
| Wael_H | 2:1103f5d61035 | 50 | blueTxBuffer[2]=(char)((val&0xFF0000)>>16); //Conversion sur trois octets independants |
| Wael_H | 2:1103f5d61035 | 51 | blueTxBuffer[3]=(char)((val&0x00FF00)>>8); |
| Wael_H | 2:1103f5d61035 | 52 | blueTxBuffer[4]=(char)((val&0x0000FF)); |
| Wael_H | 2:1103f5d61035 | 53 | // La trame a envoyer est realisee |
| Wael_H | 2:1103f5d61035 | 54 | bt.attach(this, &IHM_Bluetooth::transmitBlu, Serial::TxIrq); // Ouverture de l'interruption de transmission |
| Wael_H | 2:1103f5d61035 | 55 | } |
| Wael_H | 2:1103f5d61035 | 56 | |
| Wael_H | 2:1103f5d61035 | 57 | void IHM_Bluetooth::TrameCAM( int CAM1, int CAM2) |
| Wael_H | 2:1103f5d61035 | 58 | { |
| Wael_H | 2:1103f5d61035 | 59 | blueTxBuffer[1]=(char)CAMXBYTE; |
| Wael_H | 2:1103f5d61035 | 60 | blueTxBuffer[2]=(char)1; //Conversion sur trois octets independants |
| Wael_H | 2:1103f5d61035 | 61 | blueTxBuffer[3]=(char)CAM1; |
| Wael_H | 2:1103f5d61035 | 62 | blueTxBuffer[4]=(char)CAM2; |
| Wael_H | 2:1103f5d61035 | 63 | // La trame a envoyer est realisee |
| Wael_H | 2:1103f5d61035 | 64 | bt.attach(this, &IHM_Bluetooth::transmitBlu, Serial::TxIrq); // Ouverture de l'interruption de transmission |
| Wael_H | 2:1103f5d61035 | 65 | } |
| Wael_H | 2:1103f5d61035 | 66 | |
| Wael_H | 2:1103f5d61035 | 67 | void IHM_Bluetooth::TramePos(int left, int barc, int right) |
| Wael_H | 2:1103f5d61035 | 68 | { |
| Wael_H | 2:1103f5d61035 | 69 | blueTxBuffer[1]=(char)POSBYTE; |
| Wael_H | 2:1103f5d61035 | 70 | blueTxBuffer[2]=(char)left; //Conversion sur trois octets independants |
| Wael_H | 2:1103f5d61035 | 71 | blueTxBuffer[3]=(char)barc; |
| Wael_H | 2:1103f5d61035 | 72 | blueTxBuffer[4]=(char)right; |
| Wael_H | 2:1103f5d61035 | 73 | // La trame a envoyer est realisee |
| Wael_H | 2:1103f5d61035 | 74 | bt.attach(this, &IHM_Bluetooth::transmitBlu, Serial::TxIrq); // Ouverture de l'interruption de transmission |
| Wael_H | 2:1103f5d61035 | 75 | } |
| Wael_H | 2:1103f5d61035 | 76 | |
| Wael_H | 2:1103f5d61035 | 77 | |
| Wael_H | 2:1103f5d61035 | 78 | void IHM_Bluetooth::automate_Tx(void) |
| Wael_H | 2:1103f5d61035 | 79 | { |
| Wael_H | 2:1103f5d61035 | 80 | static int etat_futur=POSBYTE, val=50; |
| Wael_H | 2:1103f5d61035 | 81 | int etat; |
| Wael_H | 2:1103f5d61035 | 82 | etat = etat_futur; |
| Wael_H | 2:1103f5d61035 | 83 | |
| Wael_H | 2:1103f5d61035 | 84 | // Transmission des donnees à l'application |
| Wael_H | 2:1103f5d61035 | 85 | switch(etat){ // Il faut modifier toutes les valeurs VAL dans les fonctions |
| Wael_H | 2:1103f5d61035 | 86 | case POSBYTE : |
| Wael_H | 2:1103f5d61035 | 87 | TramePos(val+100, val+100, val+100); // Envoie de 3 octets de position par rapport aux lignes, respectivement : Gauche, barycentre (centre de gravite) et droit |
| Wael_H | 2:1103f5d61035 | 88 | if (cb_lm == 1)etat_futur = LMBYTE; |
| Wael_H | 2:1103f5d61035 | 89 | else if(cb_rm == 1)etat_futur=RMBYTE; // Si la checkbox de l'application autorise la transmission de cette donnee |
| Wael_H | 2:1103f5d61035 | 90 | else if(cb_err == 1)etat_futur=ERRORBYTE; |
| Wael_H | 2:1103f5d61035 | 91 | else if(cb_cerr == 1)etat_futur=CERRBYTE; |
| Wael_H | 2:1103f5d61035 | 92 | else if(cb_speed == 1)etat_futur=SGRABYTE; |
| Wael_H | 2:1103f5d61035 | 93 | else etat_futur=CAMXBYTE; |
| Wael_H | 2:1103f5d61035 | 94 | break; |
| Wael_H | 2:1103f5d61035 | 95 | |
| Wael_H | 2:1103f5d61035 | 96 | case LMBYTE : // Moteur gauche |
| Wael_H | 2:1103f5d61035 | 97 | formatTrameT(LMBYTE, val ); |
| Wael_H | 2:1103f5d61035 | 98 | if(cb_rm == 1)etat_futur=RMBYTE; |
| Wael_H | 2:1103f5d61035 | 99 | else if(cb_err == 1)etat_futur=ERRORBYTE; |
| Wael_H | 2:1103f5d61035 | 100 | else if(cb_cerr == 1)etat_futur=CERRBYTE; |
| Wael_H | 2:1103f5d61035 | 101 | else if(cb_speed == 1)etat_futur=SGRABYTE; |
| Wael_H | 2:1103f5d61035 | 102 | else etat_futur=CAMXBYTE; |
| Wael_H | 2:1103f5d61035 | 103 | break; |
| Wael_H | 2:1103f5d61035 | 104 | |
| Wael_H | 2:1103f5d61035 | 105 | case RMBYTE : // Moteur droit |
| Wael_H | 2:1103f5d61035 | 106 | formatTrameT(RMBYTE, val ); |
| Wael_H | 2:1103f5d61035 | 107 | if(cb_err == 1)etat_futur=ERRORBYTE; |
| Wael_H | 2:1103f5d61035 | 108 | else if(cb_cerr == 1)etat_futur=CERRBYTE; |
| Wael_H | 2:1103f5d61035 | 109 | else if(cb_speed == 1)etat_futur=SGRABYTE; |
| Wael_H | 2:1103f5d61035 | 110 | else etat_futur=CAMXBYTE; |
| Wael_H | 2:1103f5d61035 | 111 | break; |
| Wael_H | 2:1103f5d61035 | 112 | |
| Wael_H | 2:1103f5d61035 | 113 | case ERRORBYTE : // Erreur |
| Wael_H | 2:1103f5d61035 | 114 | formatTrameT(ERRORBYTE, val ); |
| Wael_H | 2:1103f5d61035 | 115 | if(cb_cerr == 1)etat_futur=CERRBYTE; |
| Wael_H | 2:1103f5d61035 | 116 | else if(cb_speed == 1)etat_futur=SGRABYTE; |
| Wael_H | 2:1103f5d61035 | 117 | else etat_futur=CAMXBYTE; |
| Wael_H | 2:1103f5d61035 | 118 | break; |
| Wael_H | 2:1103f5d61035 | 119 | |
| Wael_H | 2:1103f5d61035 | 120 | case CERRBYTE : // Correction d'erreur |
| Wael_H | 2:1103f5d61035 | 121 | formatTrameT(CERRBYTE, val ); |
| Wael_H | 2:1103f5d61035 | 122 | if(cb_speed == 1)etat_futur=SGRABYTE; |
| Wael_H | 2:1103f5d61035 | 123 | else etat_futur=CAMXBYTE; |
| Wael_H | 2:1103f5d61035 | 124 | break; |
| Wael_H | 2:1103f5d61035 | 125 | |
| Wael_H | 2:1103f5d61035 | 126 | case SGRABYTE : // Vitesse du véhicule |
| Wael_H | 2:1103f5d61035 | 127 | formatTrameT(SGRABYTE, val ); |
| Wael_H | 2:1103f5d61035 | 128 | etat_futur=CAMXBYTE; |
| Wael_H | 2:1103f5d61035 | 129 | break; |
| Wael_H | 2:1103f5d61035 | 130 | |
| Wael_H | 2:1103f5d61035 | 131 | case CAMXBYTE : // Vecteur gauche et droit |
| Wael_H | 2:1103f5d61035 | 132 | TrameCAM(val+100, val+100); // Retour de la camera, respectivement vecteur Gauche et Droit |
| Wael_H | 2:1103f5d61035 | 133 | etat_futur=POSBYTE; |
| Wael_H | 2:1103f5d61035 | 134 | break; |
| Wael_H | 2:1103f5d61035 | 135 | } |
| Wael_H | 2:1103f5d61035 | 136 | } |
| Wael_H | 2:1103f5d61035 | 137 | |
| Wael_H | 2:1103f5d61035 | 138 | /****************************** Fonctions de reception ******************************/ |
| Wael_H | 2:1103f5d61035 | 139 | |
| Wael_H | 2:1103f5d61035 | 140 | void IHM_Bluetooth::receptionBlu(void) |
| Wael_H | 2:1103f5d61035 | 141 | { |
| Wael_H | 2:1103f5d61035 | 142 | static short j = 0; |
| Wael_H | 2:1103f5d61035 | 143 | |
| Wael_H | 2:1103f5d61035 | 144 | if(bt.readable()) // Si la liaison est libre |
| Wael_H | 2:1103f5d61035 | 145 | { |
| Wael_H | 2:1103f5d61035 | 146 | blueRxBuffer[j]=(unsigned int)bt.getc(); //reception des octets dans un tableau |
| Wael_H | 2:1103f5d61035 | 147 | j++; |
| Wael_H | 2:1103f5d61035 | 148 | if(j==6) |
| Wael_H | 2:1103f5d61035 | 149 | { |
| Wael_H | 2:1103f5d61035 | 150 | if((blueRxBuffer[0]==STARTBYTE) && (blueRxBuffer[5]==STOPBYTE)) // Si la trame commence et termine par Start et Stop |
| Wael_H | 2:1103f5d61035 | 151 | { |
| Wael_H | 2:1103f5d61035 | 152 | switch(blueRxBuffer[1]){ // Automate en fonction de la valeur du registre |
| Wael_H | 2:1103f5d61035 | 153 | case KPBYTE : |
| Wael_H | 2:1103f5d61035 | 154 | paramRx[0] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000; |
| Wael_H | 2:1103f5d61035 | 155 | break; |
| Wael_H | 2:1103f5d61035 | 156 | |
| Wael_H | 2:1103f5d61035 | 157 | case KIBYTE : |
| Wael_H | 2:1103f5d61035 | 158 | paramRx[1] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000; //decomposition du message |
| Wael_H | 2:1103f5d61035 | 159 | break; |
| Wael_H | 2:1103f5d61035 | 160 | |
| Wael_H | 2:1103f5d61035 | 161 | case KDBYTE : |
| Wael_H | 2:1103f5d61035 | 162 | paramRx[2] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000; //valeur envoyee dans un tableau |
| Wael_H | 2:1103f5d61035 | 163 | break; |
| Wael_H | 2:1103f5d61035 | 164 | |
| Wael_H | 2:1103f5d61035 | 165 | case SPCMDBYTE : |
| Wael_H | 2:1103f5d61035 | 166 | paramRx[3] = ((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4])/10000; |
| Wael_H | 2:1103f5d61035 | 167 | break; |
| Wael_H | 2:1103f5d61035 | 168 | |
| Wael_H | 2:1103f5d61035 | 169 | case ERRORBYTE : |
| Wael_H | 2:1103f5d61035 | 170 | if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1) |
| Wael_H | 2:1103f5d61035 | 171 | { |
| Wael_H | 2:1103f5d61035 | 172 | if(cb_err == 0)cb_err=1; |
| Wael_H | 2:1103f5d61035 | 173 | else if(cb_err == 1)cb_err=0; |
| Wael_H | 2:1103f5d61035 | 174 | } |
| Wael_H | 2:1103f5d61035 | 175 | break; |
| Wael_H | 2:1103f5d61035 | 176 | |
| Wael_H | 2:1103f5d61035 | 177 | case CERRBYTE : |
| Wael_H | 2:1103f5d61035 | 178 | if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1) |
| Wael_H | 2:1103f5d61035 | 179 | { |
| Wael_H | 2:1103f5d61035 | 180 | if(cb_cerr == 0)cb_cerr=1; |
| Wael_H | 2:1103f5d61035 | 181 | else if(cb_cerr == 1)cb_cerr=0; |
| Wael_H | 2:1103f5d61035 | 182 | } |
| Wael_H | 2:1103f5d61035 | 183 | break; |
| Wael_H | 2:1103f5d61035 | 184 | |
| Wael_H | 2:1103f5d61035 | 185 | case LMBYTE : |
| Wael_H | 2:1103f5d61035 | 186 | if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1) |
| Wael_H | 2:1103f5d61035 | 187 | { |
| Wael_H | 2:1103f5d61035 | 188 | if(cb_lm == 0)cb_lm=1; |
| Wael_H | 2:1103f5d61035 | 189 | else if(cb_lm == 1)cb_lm=0; |
| Wael_H | 2:1103f5d61035 | 190 | } |
| Wael_H | 2:1103f5d61035 | 191 | break; |
| Wael_H | 2:1103f5d61035 | 192 | // Faire automate pour la transmission |
| Wael_H | 2:1103f5d61035 | 193 | case RMBYTE : |
| Wael_H | 2:1103f5d61035 | 194 | if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1) |
| Wael_H | 2:1103f5d61035 | 195 | { |
| Wael_H | 2:1103f5d61035 | 196 | if(cb_rm == 0)cb_rm=1; |
| Wael_H | 2:1103f5d61035 | 197 | else if(cb_rm == 1)cb_rm=0; |
| Wael_H | 2:1103f5d61035 | 198 | } |
| Wael_H | 2:1103f5d61035 | 199 | break; |
| Wael_H | 2:1103f5d61035 | 200 | |
| Wael_H | 2:1103f5d61035 | 201 | case SGRABYTE : |
| Wael_H | 2:1103f5d61035 | 202 | if(((blueRxBuffer[2]<<16)+(blueRxBuffer[3]<<8)+blueRxBuffer[4]) == 1) |
| Wael_H | 2:1103f5d61035 | 203 | { |
| Wael_H | 2:1103f5d61035 | 204 | if(cb_speed == 0)cb_speed=1; |
| Wael_H | 2:1103f5d61035 | 205 | else if(cb_speed == 1)cb_speed=0; |
| Wael_H | 2:1103f5d61035 | 206 | } |
| Wael_H | 2:1103f5d61035 | 207 | break; |
| Wael_H | 2:1103f5d61035 | 208 | } |
| Wael_H | 2:1103f5d61035 | 209 | j=0; |
| Wael_H | 2:1103f5d61035 | 210 | }else{ //Si le message ne commence pas par start ou ne finit pas par stop |
| Wael_H | 2:1103f5d61035 | 211 | for(int i = 0;i<5;i++){ |
| Wael_H | 2:1103f5d61035 | 212 | blueRxBuffer[i]=blueRxBuffer[i+1]; // On decale le msg reçu dans le buffer |
| Wael_H | 2:1103f5d61035 | 213 | } |
| Wael_H | 2:1103f5d61035 | 214 | j=5; |
| Wael_H | 2:1103f5d61035 | 215 | } |
| Wael_H | 2:1103f5d61035 | 216 | } |
| Wael_H | 2:1103f5d61035 | 217 | } |
| Wael_H | 2:1103f5d61035 | 218 | } |
| Wael_H | 2:1103f5d61035 | 219 | |
| Wael_H | 2:1103f5d61035 | 220 | int IHM_Bluetooth::get_val_pc(short reg) // reg = entree de l'utilisateur pour connaitre une valeur |
| Wael_H | 2:1103f5d61035 | 221 | { |
| Wael_H | 2:1103f5d61035 | 222 | short indice; |
| Wael_H | 2:1103f5d61035 | 223 | |
| Wael_H | 2:1103f5d61035 | 224 | if(reg == KPBYTE)indice = 0; |
| Wael_H | 2:1103f5d61035 | 225 | else if(reg == KIBYTE)indice = 1; |
| Wael_H | 2:1103f5d61035 | 226 | else if(reg == KDBYTE)indice = 2; |
| Wael_H | 2:1103f5d61035 | 227 | else if(reg == SPCMDBYTE)indice = 3; |
| Wael_H | 2:1103f5d61035 | 228 | |
| Wael_H | 2:1103f5d61035 | 229 | return paramRx[indice]; // Renvoie de la valeur souhaitee |
| Wael_H | 2:1103f5d61035 | 230 | } |