![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Projecte final de Sistemes Encastats
Dependencies: Hc05 RawSerialPc mbed-rtos mbed
Diff: main.cpp
- Revision:
- 4:0a37a963b724
- Parent:
- 3:4a598ab10e87
- Child:
- 6:c4af95f6e155
--- a/main.cpp Fri Dec 18 00:23:49 2015 +0000 +++ b/main.cpp Sat Dec 19 01:09:57 2015 +0000 @@ -1,164 +1,9 @@ #include "mbed.h" #include "rtos.h" - -// ----------------------------- -// ---- 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; - }; - +#include "Hc05.h" +#include "RawSerialPc.h" - }; - - 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(230400); - mode = vel = ang = 0; - iniString(); - timer = new Timer; - timer->start(); - }; - - ~Hc05(){ - //timer.stop(); - }; - - 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; - //timer->reset(); - //mutex.lock(); - while((i < longString) && (c != 13)) { - if(c!='@'){ - strLlegit[i] = c; - i++; - } - timer->reset(); - 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'; - }; - - 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 ---- -// ---------------------------- - -Pc pc(USBTX, USBRX); //Per provar si funciona, no cal al programa +RawSerialPc 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