Llibreria per fer servir un HC-06 o HC-05 en mode esclau. És compatible amb les llibreries RTOS de mbed.

Dependents:   projecte_v4_rtos ProjecteRobotFinal

Hc05.cpp

Committer:
jcabello7
Date:
2015-12-18
Revision:
0:acddf8d6d0cb
Child:
1:cd73654d13ed

File content as of revision 0:acddf8d6d0cb:

#include "Hc05.h"
#include "mbed.h"


        Hc05::Hc05(PinName tx, PinName rx) : RawSerial(tx, rx){
            baud(230400);
            mode = vel = ang = 0;
            iniString();
            timer.start();
            };
            
        Hc05::~Hc05(){
        };
        
        void Hc05::iniString(){
            strLlegit[0] = 'S';
            strLlegit[1] = 't';
            strLlegit[2] = 'r';
            strLlegit[3] = 'i';
            strLlegit[4] = 'n';
            strLlegit[5] = 'g';
            strLlegit[6] = '\0';
        };
            
        void Hc05::enviaString(char* str){
            int i = 0;
            while((i < longString) && (str[i] != '\0')) {
                putc(str[i]);
                i++;
            }
            putc(13);
        };
        
        
        bool Hc05::llegirString(){
            if(readable()){
                char c = getc();
                int i = 0;
                while((i < longString) && (c != 13)) {
                    if(c!='@'){
                        strLlegit[i] = c;
                        i++;
                    }
                    timer.reset();
                    c = getc();
                }
                strLlegit[i] = '\0';
                return true;
                
            }
            return false;
        };
        
        void Hc05::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 Hc05::getMode(){return mode;};
        int Hc05::getVel(){return vel;};
        int Hc05::getAng(){return ang;};
        void Hc05::getStringLlegit(char* str){
            int i = 0;
            while((i < longString) && (strLlegit[i] != '\0')) {
                str[i] = strLlegit[i];
                i++;
            }
            str[i] = '\0';
        };
        
        void Hc05::comprovaConnexio(){
            if(getTimer() > 300)
                mode = -1;
        };
        
        int Hc05::getTimer(){
            return  timer.read_ms();   
        };