Projecte final de Sistemes Encastats
Dependencies: Hc05 RawSerialPc mbed-rtos mbed
Diff: main.cpp
- Revision:
- 3:4a598ab10e87
- Parent:
- 2:1ac2d1debc92
- Child:
- 4:0a37a963b724
diff -r 1ac2d1debc92 -r 4a598ab10e87 main.cpp --- a/main.cpp Wed Dec 16 18:09:25 2015 +0000 +++ b/main.cpp Fri Dec 18 00:23:49 2015 +0000 @@ -1,6 +1,6 @@ #include "mbed.h" #include "rtos.h" -#include <string> + // ----------------------------- // ---- NO TOCAR DESDE AQUI ---- // ----------------------------- @@ -45,15 +45,22 @@ class Hc05 : public RawSerial { private: int mode, vel, ang; + Timer *timer; char strLlegit[longString]; public: //Mutex mutex; Hc05(PinName tx, PinName rx) : RawSerial(tx, rx){ - baud(115200); + baud(230400); mode = vel = ang = 0; iniString(); + timer = new Timer; + timer->start(); }; + ~Hc05(){ + //timer.stop(); + }; + void iniString(){ strLlegit[0] = 'S'; strLlegit[1] = 't'; @@ -76,14 +83,17 @@ bool llegirString(){ if(readable()){ + char c = getc(); int i = 0; + //timer->reset(); //mutex.lock(); while((i < longString) && (c != 13)) { if(c!='@'){ strLlegit[i] = c; i++; } + timer->reset(); c = getc(); } strLlegit[i] = '\0'; @@ -129,6 +139,20 @@ } str[i] = '\0'; }; + + void comprovaConnexio(){ + //char m[128]; + //snprintf(m, 128, "Mode: %d", getMode()); + //Pc::enviaString(m); + if(getTimer() > 300) + mode = -1; + }; + + int getTimer(){ + return timer->read_ms(); + }; + + }; // ---------------------------- // ---- NO TOCAR FINS AQUI ---- @@ -142,10 +166,14 @@ //Aquest serà el meu fil, que va llegint cada 300ms del HC-05 void llegir_thread(void const *args) { while (true) { - bt.llegirString(); + if(bt.llegirString()) + bt.tractaString(); + + bt.comprovaConnexio(); bt.getStringLlegit(prova); //Per provar si funciona, no cal al programa - bt.tractaString(); - Thread::wait(300); + Thread::wait(100); + //bt.comprovaConnexio(); + } } @@ -153,16 +181,19 @@ int main() { Thread thread(llegir_thread); //Inicia el fil llegir_thread - + char m[128]; 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 + //if(bt.getMode()<0) + //pc.enviaString("ALERTA! S'ha perdut la connexio"); //Retorna 0 si ha llegit l'ordre parar, 1 per avançar, 2 per rotar, -1 si hi ha error de connexio + snprintf(m, 128, "Mode: %d | timer: %dms", bt.getMode(), bt.getTimer()); + pc.enviaString(m); 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); + Thread::wait(500); } } \ No newline at end of file