RN41/42 control lib. RN41/42 is bluetooth module with class 1 or 2.

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RN41.h Source File

RN41.h

00001 /**     RN41/42 is Bluetooth module. RN41 is calss 1, RN42 is class 2.
00002  *  RN42 refer to http://akizukidenshi.com/catalog/g/gM-07612/ .
00003  */
00004 
00005 /*
00006  * cmdで送る改行コードは、CRのみ。LFは文字として認識されるので?となる。
00007  * RN41 Returns (default); AOK(valid cmd), ERR(invalid cmod), ?(unrecongnized cmd).
00008 
00009 %を付ける初期化処理必須
00010 
00011 */
00012 
00013 /**
00014  @code
00015  #include "mbed.h"
00016 #include <string>
00017 #include "RN41.h"
00018 RN41 rn41(p13, p14, 9600);
00019 DigitalOut led[]= {LED1, LED2, LED3, LED4};
00020 void connectRN41(string tmpAddr);
00021 int main()
00022 {
00023     while(true) {
00024         led[0]= !led[0];
00025         connectRN41("target BT address");
00026         rn41.sendLine("test");
00027         wait_ms(100);
00028         printf(rn41.getLine().c_str());
00029         if(rn41.disconnect())
00030             printf("disconnect");
00031         wait(5.0);
00032     }
00033 }
00034 void connectRN41(string tmpAddr)
00035 {
00036     printf("Connect to: %s.\r\n", tmpAddr);
00037     int status= rn41.connect(tmpAddr);
00038     if(status == RN41::OK)                      printf("OK, Connect.");
00039     else if(status == RN41::ERR_AddrUnder12)    printf("NG, Connect. Under12 Addr.");
00040     else if(status == RN41::ERR_AddrOver12)     printf("NG, Connect. Over12 Addr.");
00041     else if(status == RN41::ERR_EnterCmdMode)   printf("Error, failed in to enter Cmd-mode.");
00042     else if(status == RN41::ERR_Disconnect)     printf("Error, failed in DisConnect.");
00043     else if(status == RN41::ERR_Connect)        printf("Error, Connect.");
00044     else if(status == RN41::FAIL_Connect)       printf("Fail, Connect.");
00045     else if(status == RN41::ERR_Timeout )       printf("Error, TimeOut.");
00046     else                                        printf("Error Connection: %d.",status);
00047     return;
00048 }
00049  @endcode
00050 */
00051 
00052 #pragma once
00053 
00054 #include "mbed.h"
00055 #include "StrLib.h"
00056 #include "myTimer.h"
00057 #include "RingBuffer.h"
00058 
00059 /**     Bluetooth module RN41/42 control class.
00060  *
00061  */
00062 class RN41
00063 {
00064 public:
00065 
00066 //    static const int
00067     static const int NG, OK;//=                  1;
00068     static const int ERR_AddrUnder12, ERR_AddrOver12, ERR_Connect, FAIL_Connect;
00069     static const int ERR_EnterCmdMode, ERR_Disconnect, ERR_Timeout;
00070 
00071 //    static const int None, Odd, Even;
00072     static const int ERR_NameSize;
00073 
00074     /** Create Serial port to RN41. for LPC1768
00075      *  @param TX, RX;              Serial port.
00076      *  @param baud-rate;                Baud rate (bps).
00077      *  @param bit, parity, stop;   Default: 1Stopbit, NoneParity, 1StopBit.
00078      *                              -- parity select; N(None), O(Odd), E(Even).
00079      *  @param CRLN;                true -> CR&LN (\r\n), false -> CR only (\r).
00080      */
00081     RN41(PinName TX, PinName RX, int baudrate, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);
00082 
00083     string read();
00084 
00085     /**
00086      *      RxStrのCRまでを返す。
00087      */
00088     string getLine();
00089 
00090     /**
00091      *
00092      */
00093     void sendCMD( string str, bool addCR=true);
00094     void sendLine(string str, bool addCR=true);
00095 
00096 
00097     int setDev_Name(string str, bool usingAddr=true);
00098     int setDev_UART(int baud, int bit=8, int parity=SerialBase::None, int stop=1);
00099 
00100 
00101 // Command to RN41
00102     bool enterCMD();
00103     int connect(string addr);     // 必ず、Checkを呼ぶこと。2-5sかかるよ
00104     bool disconnect();
00105     bool reboot();
00106 
00107 private:
00108     RawSerial rn41;
00109     string cr;
00110     string rxStr, rxStrStored;      // 内部バッファ。read()での保管用と、GetLineまでの保管用。
00111     RingBuffer ringBuf;             // rxIRQ用の循環バッファ256kB。
00112 //    string rxStrStored;      // 内部バッファ。read()での保管用と、GetLineまでの保管用。
00113     // 9,600bpsなら、1文字受信に833us必要。安全率1.3として13ループさせるため、待ち時間us/0.1文字とする。
00114 //    int usWait4readBuf;       // -> attach
00115 //    Timer timerLocal;
00116     myTimer timer;
00117 
00118     static const int findCmp1, findCmp2, findCmp12;
00119 
00120     struct bool2 {
00121         bool b1, b2;
00122     };
00123     struct b2i2 {
00124         bool b1, b2;
00125         int i1, i2;
00126     };
00127 
00128     /** check reply from RN41.
00129      *  @Param  cmp; string to compare
00130      *  @Param  timeout; timeout [ms]
00131      *  @return bool; true: ok, false: NG.
00132      */
00133 //    int chkReply(string cmp1, int timeout, string cmp2="");
00134 //    bool chkReply_woCR(string cmp, int timeout);
00135     bool2 chkReply(string cmp1, int timeout, string cmp2= "", bool wCR= true);
00136     bool2 chkReply_process_wCR( string cmp1, string cmp2);
00137     bool2 chkReply_process_woCR(string cmp1, string cmp2, bool wCR);
00138     b2i2 chkReply_find(string trg, string cmp1, string cmp2);
00139 
00140 
00141     /** Just copy to rxStr from Buffer. */
00142     void _read();
00143     void _readIrq();
00144 };
00145 
00146 // EOF