Només falta afegir les vostres parts, a mi em falta fer la comprovació de la connexió. S'hauria de separar les classes en arxius separats per deixar-ho més net.
Dependencies: mbed-rtos mbed Hc05 RawSerialPc
main.cpp
- Committer:
- jcabello7
- Date:
- 2015-12-16
- Revision:
- 2:1ac2d1debc92
- Parent:
- 1:c603de57c8b6
- Child:
- 3:4a598ab10e87
File content as of revision 2:1ac2d1debc92:
#include "mbed.h"
#include "rtos.h"
#include <string>
// -----------------------------
// ---- NO TOCAR DESDE AQUI ----
// -----------------------------
//0, 60, 120, 180 i en negatiu
#define longString 128 //longString es la longitud màxima del string
class Pc : public RawSerial {
public:
Pc(PinName tx, PinName rx) : RawSerial(tx, rx){
baud(115200);
};
void enviaString(char* str){
int i = 0;
while((i < longString) && (str[i] != '\0')) {
putc(str[i]);
i++;
}
putc(10);
};
bool llegirString(char* str){
if(readable()){
int i= 0;
str[i] = getc();
while((i < longString) && (str[i] != 13)) {
if (str[i] != '@')
i++;
str[i] = getc();
}
str[i] = '\0';
return true;
}
return false;
};
};
class Hc05 : public RawSerial {
private:
int mode, vel, ang;
char strLlegit[longString];
public:
//Mutex mutex;
Hc05(PinName tx, PinName rx) : RawSerial(tx, rx){
baud(115200);
mode = vel = ang = 0;
iniString();
};
void iniString(){
strLlegit[0] = 'S';
strLlegit[1] = 't';
strLlegit[2] = 'r';
strLlegit[3] = 'i';
strLlegit[4] = 'n';
strLlegit[5] = 'g';
strLlegit[6] = '\0';
};
void enviaString(char* str){
int i = 0;
while((i < longString) && (str[i] != '\0')) {
putc(str[i]);
i++;
}
putc(13);
};
bool llegirString(){
if(readable()){
char c = getc();
int i = 0;
//mutex.lock();
while((i < longString) && (c != 13)) {
if(c!='@'){
strLlegit[i] = c;
i++;
}
c = getc();
}
strLlegit[i] = '\0';
//mutex.unlock();
return true;
}
return false;
};
void tractaString(){
if ((strLlegit[0] == 'a') && (strLlegit[1] == 'v') && (strLlegit[2] == 'a') && (strLlegit[3] == 'n'))
mode = 1;
if ((strLlegit[0] == 'r') && (strLlegit[1] == 'o') && (strLlegit[2] == 't') && (strLlegit[3] == 'a'))
mode = 2;
if ((strLlegit[0] == 's') && (strLlegit[1] == 't') && (strLlegit[2] == 'o') && (strLlegit[3] == 'p')){
mode = 0;
vel = 0;
ang = 0;
}
if ((mode == 1) || (mode==2)){
char svel[5], sang[5];
for(int i = 0; i<=4;i++){
sang[i] = strLlegit[i+5];
svel[i] = strLlegit[i+10];
}
sang[4] = svel[4] = '\0';
vel = atoi(svel);
ang = atoi(sang);
}
if ((strLlegit[0] == 'm') && (strLlegit[1] == 'o') && (strLlegit[2] == 'd') && (strLlegit[3] == 'e') && (strLlegit[5] == 'a') && (strLlegit[6] == 'u') && (strLlegit[7] == 't') && (strLlegit[8] == 'o'))
mode = 3;
};
int getMode(){return mode;};
int getVel(){return vel;};
int getAng(){return ang;};
void getStringLlegit(char* str){
int i = 0;
while((i < longString) && (strLlegit[i] != '\0')) {
str[i] = strLlegit[i];
i++;
}
str[i] = '\0';
};
};
// ----------------------------
// ---- NO TOCAR FINS AQUI ----
// ----------------------------
Pc pc(USBTX, USBRX); //Per provar si funciona, no cal al programa
Hc05 bt(D8, D2); //Crea l'objecte bt, connectat als pins D8 (TX) i D2 (RX) (és la connexió sèrie BT)
char prova[longString] = "hola"; //Per provar si funciona, no cal al programa
//Aquest serà el meu fil, que va llegint cada 300ms del HC-05
void llegir_thread(void const *args) {
while (true) {
bt.llegirString();
bt.getStringLlegit(prova); //Per provar si funciona, no cal al programa
bt.tractaString();
Thread::wait(300);
}
}
//Aquest serà el programa principal
int main() {
Thread thread(llegir_thread); //Inicia el fil llegir_thread
while (true) {
pc.enviaString(prova); //Per provar si funciona, no cal al programa
if(bt.getAng()<0) //Per provar si funciona, no cal al programa
pc.enviaString("ANGLE<0"); //Per provar si funciona, no cal al programa
if(bt.getVel()== 60) //Per provar si funciona, no cal al programa
pc.enviaString("VEL=60"); //Per provar si funciona, no cal al programa
bt.getMode(); //Retorna 0 si ha llegit l'ordre parar, 1 per avançar, 2 per rotar, -1 si hi ha error de connexio
bt.getVel(); //Retorna un enter amb la velocitat (de 0 a 100)
bt.getAng(); //Retorna un enter amb l'angle (de -180º a 180º)
Thread::wait(1000);
}
}