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.h

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

File content as of revision 2:931bef8c7fac:

#pragma once

/**     Sample code
 * #include "mbed.h"
* #include "HC05.h"
*   // 2,3,4が入力されたら、LED2,3,4が反転するコード。
* DigitalOut led[]= {LED1, LED2, LED3, LED4};
* HC05 hc05(p9, p10);
* void receive()
* {
*     int id= hc05.getc() - '1';
*     if(0<id && id<4)
*         led[id]= !led[id];
*     return;
* }
* int main()
* {
*     hc05.init();
*     int iter= 0;
*     hc05.setAttachRx(receive);      // 割り込み
*     while(1) {
*         hc05.printf("iter: %3d\r\n", iter);
* //        if(hc05.readable())       // 0.2s毎に確認
* //            receive();
*         led[0] = !led[0];
*         iter++;
*         wait(0.2);
*     }
* }
*/

#include "mbed.h"
#include <string>

/**     Bluetooth module HC05 control class.
 *
 */
class HC05 : public Stream
{
public:
    /** Create Serial port to HC05.
     *  @param TX, RX;              Serial port.
     */
    HC05(PinName tx, PinName rx);

    /** init
     *  @param baud-rate;           Baud rate (bps). 9600, 38,400, 115,200, 230,400
     *  @param bit, parity, stop;   Default: 1Stopbit, NoneParity, 1StopBit.
     *                              -- parity select; N(None), O(Odd), E(Even).
     *  @param CRLN;                true -> CR&LN (\r\n), false -> CR only (\r).
     */
    void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);

    bool readable();
    void setAttachRx(void (*fptr)(void));
    
    string getLine();
    void sendLine(string str, bool addCR=true);
    void sendFloat(float num, string foreword="");
    void sendInt(int num, string foreword="");

private:
    Serial hc05;
    string CR;

    // virtual func for printf() in Stream-class.
    virtual int _putc(int val);
    virtual int _getc();

};

// EOF