Akinori Hashimoto / RN41

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

RN41.h

Committer:
AkinoriHashimoto
Date:
2015-10-20
Revision:
0:812e6b59aa54
Child:
1:5ed110051e39

File content as of revision 0:812e6b59aa54:

/**     RN41/42 is Bluetooth module. RN41 is calss 1, RN42 is class 2.
 *  RN42 refer to http://akizukidenshi.com/catalog/g/gM-07612/ .
 */

#pragma once

#include "mbed.h"
#include "StrLib.h"

/*
 * cmdで送る改行コードは、CRのみ。LFは文字として認識されるので?となる。
 * RN41 Returns (default); AOK(valid cmd), ERR(invalid cmod), ?(unrecongnized cmd).

%を付ける初期化処理必須

*/


/**     Bluetooth module RN41/42 control class.
 *
 */
class RN41
{
public:

//    static const int
    static const int NG, OK;//=                  1;
    static const int ERR_AddrUnder12, ERR_AddrOver12, ERR_Connect, FAIL_Connect;
    static const int ERR_EnterCmdMode, ERR_Disconnect;

//    static const int None, Odd, Even;
    static const int ERR_NameSize;

    /** Create Serial port to RN41. for LPC1768
     *  @param TX, RX;              Serial port.
     *  @param baud-rate;                Baud rate (bps).
     *  @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).
     */
    RN41(PinName TX, PinName RX, int baudrate, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);

    /**
     *      RxStrのCRまでを返す。
     */
    string getLine();

    /**
     *
     */
    void sendCMD( string str, bool addCR=true);
    void sendLINE(string str, bool addCR=true);


    int setDev_Name(string str, bool usingAddr=true);
    int setDev_UART(int boad, int stop=1, int parity=SerialBase::None);


// Command to RN41
    bool enterCMD();
    int connect(string addr);     // 必ず、Checkを呼ぶこと。2-5sかかるよ
    bool disconnect();

private:
    Serial rn41;
    string CR;
    string rxStr, str4GetLine;      // 内部バッファ。read()での保管用と、GetLineまでの保管用。

    Timer timerLocal;

    static const int findCmp1, findCmp2, findCmp12;
    /** check reply from RN41.
     *  @Param  cmp; string to compare
     *  @Param  timeout; timeout [ms]
     *  @return bool; true: ok, false: NG.
     */
    int chkReply(string cmp1, int timeout, string cmp2="");

    bool chkReply_woCR(string cmp, int timeout);

    /** Just copy to rxStr from Buffer.
    *
    */
    void read();

};