Bluetooth module HC-05 Lib. suppotrs printf(); support IrqAttach, with using RTOS. include feature as background rxIrq attach. the lib store rx buf of serial, therefore can have over 16 bytes string. if you want to get string, you call read() or getLine(). read() is ALL string, getLine() is under CR('\r').

Dependencies:   RingBuffer

Dependents:   semaforinoprova

HC05.cpp

Committer:
AkinoriHashimoto
Date:
2015-09-02
Revision:
0:a4ddaf82d43d
Child:
1:75bb445594e2

File content as of revision 0:a4ddaf82d43d:

#include "HC05.h"
//#include "SerialBase.h"

/*
const int RN41::NG=                   0;
const int RN41::OK=                   1;

// PRIVATE
const int RN41::findCmp12=          100;
const int RN41::findCmp1=           101;
const int RN41::findCmp2=           102;

// PUBLIC
const int RN41::ERR_AddrUnder12=    210;
const int RN41::ERR_AddrOver12=     211;
const int RN41::ERR_Connect=        220;
const int RN41::FAIL_Connect=       221;
const int RN41::ERR_EnterCmdMode=   230;
const int RN41::ERR_Disconnect=     240;

const int RN41::ERR_NameSize=       310;
*/

HC05::HC05(PinName TX, PinName RX, int baudrate, PinName _resetPin, int bit, int parity, int stop, bool CRLN)
    : hc05(TX, RX), resetPin(_resetPin)
{
    resetPin= 1;
    hc05.baud(baudrate);

    // 力技
    if      (parity == SerialBase::Odd)
        hc05.format(bit, SerialBase::Odd,  stop);
    else if (parity == SerialBase::Even)
        hc05.format(bit, SerialBase::Even, stop);     // 8bit, NonParity, 1stopbit
    else// if (parity == SerialBase::None)
        hc05.format(bit, SerialBase::None, stop);     // 8bit, NonParity, 1stopbit

    CR= '\r';
    if(CRLN)
        CR= "\r\n";
}
/*
void HC05::setResetPin(PinName pin)
{
    resetPin(pin);
    return;
}*/



/*
string HC05::getLine()
{
    read();
    // CRまでを返す

    if(str4GetLine.size()==0 && rxStr.size()==0)
        return "";

    // rxStr.size() > 0
    int idx= rxStr.rfind(CR);

    if(idx == string::npos)     // not find CR!
        return str4GetLine;

    // rxStr has CR.
    idx += CR.size();   // \rなら+1、\r\nは+2
    string rtn= str4GetLine;
    rtn += rxStr.substr(0, idx);   // 切り取り
    rxStr= rxStr.substr(idx);

    return rtn;
}*/

void HC05::sendLine(string str, bool addCR)
{
    if(addCR)
        str += "\r\n";
    hc05.printf(str.c_str());
    return;
}


void HC05::sendFloat(float num, string foreword)
{
    hc05.printf("%s%f\r\n", foreword.c_str(), num);
    return;
}

void HC05::sendInt(int num, string foreword)
{
    hc05.printf("%s%d\r\n", foreword.c_str(), num);
    return;
}
    


string HC05::read()
{
    static string rxStr;
    
    rxStr= "";
    if(!hc05.readable())
        return rxStr;

    // Bufferを吸い尽くす
    for(;;) {
        rxStr += (char)hc05.getc();
        if(!hc05.readable())
            return rxStr;//return; //break;    // Bufferになくなった
    } // end for
}

/*
void HC05::read()
{
    if(!hc05.readable())
        return;

    // Bufferを吸い尽くす
//    char ch;
    for(;;) {
//        char ch= rn41.getc();    // 1文字取る
//        ch= rn41.getc();    // 1文字取る
//        rxStr += ch;            // 上書き前に退避
        rxStr += (char)hc05.getc();
        if(!hc05.readable())
            return; //break;    // Bufferになくなった
    } // end for
}
*/

/*
void readBT()
{
    if(!UART_BT.readable())
        return;

    static char buf[maxRxBufSize];
    int idx;
    
    for(idx=0; idx < maxRxBufSize; idx++)
        buf[idx]= 0x00;

    for(idx= 0; idx < maxRxBufSize-3; idx++) {
        // readableだから,1回は確実に読み取れる
//        char ch= UART_BT.getc();    // 1文字取る

        buf[idx]= UART_BT.getc();//ch[0];
        
        if(!UART_BT.readable())
            break;                  // Bufferになくなった
    }
    buf[idx+1]= 's';
    buf[idx+2]= 0x00;

    // とりあエコーしてるけど、本当なら解析しないと。

    UART_BT.printf(buf);

}
*/

void HC05::reset()
{
    resetPin= 0;
    wait(0.3);
    resetPin= 1;
    return;
}