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

Fork of HC05 by Akinori Hashimoto

HC05.cpp

Committer:
AkinoriHashimoto
Date:
2015-10-01
Revision:
1:75bb445594e2
Parent:
0:a4ddaf82d43d
Child:
2:931bef8c7fac

File content as of revision 1:75bb445594e2:

#include "HC05.h"


HC05::HC05(PinName TX, PinName RX)
    : hc05(TX, RX)
{
}
//HC05::HC05(Serial &_serial)    : hc05(_serial){}
void HC05::init(int baudrate, int bit, int parity, int stop, bool CRLN)
{
    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";
    return;
}

bool HC05::readable()
{
    return hc05.readable();
}

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;
}

int HC05::_putc(int val)     // for printf()
{
    hc05.putc(val);
    return val;
}

// for "Stream"
int HC05::_getc()
{
    return -1;
}

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::reset()
{
    resetPin= 0;
    wait(0.3);
    resetPin= 1;
    return;
}
*/