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

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

Committer:
AkinoriHashimoto
Date:
Tue Oct 20 01:32:17 2015 +0000
Revision:
0:812e6b59aa54
Child:
1:5ed110051e39
beta publish.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:812e6b59aa54 1 /** RN41/42 is Bluetooth module. RN41 is calss 1, RN42 is class 2.
AkinoriHashimoto 0:812e6b59aa54 2 * RN42 refer to http://akizukidenshi.com/catalog/g/gM-07612/ .
AkinoriHashimoto 0:812e6b59aa54 3 */
AkinoriHashimoto 0:812e6b59aa54 4
AkinoriHashimoto 0:812e6b59aa54 5 #pragma once
AkinoriHashimoto 0:812e6b59aa54 6
AkinoriHashimoto 0:812e6b59aa54 7 #include "mbed.h"
AkinoriHashimoto 0:812e6b59aa54 8 #include "StrLib.h"
AkinoriHashimoto 0:812e6b59aa54 9
AkinoriHashimoto 0:812e6b59aa54 10 /*
AkinoriHashimoto 0:812e6b59aa54 11 * cmdで送る改行コードは、CRのみ。LFは文字として認識されるので?となる。
AkinoriHashimoto 0:812e6b59aa54 12 * RN41 Returns (default); AOK(valid cmd), ERR(invalid cmod), ?(unrecongnized cmd).
AkinoriHashimoto 0:812e6b59aa54 13
AkinoriHashimoto 0:812e6b59aa54 14 %を付ける初期化処理必須
AkinoriHashimoto 0:812e6b59aa54 15
AkinoriHashimoto 0:812e6b59aa54 16 */
AkinoriHashimoto 0:812e6b59aa54 17
AkinoriHashimoto 0:812e6b59aa54 18
AkinoriHashimoto 0:812e6b59aa54 19 /** Bluetooth module RN41/42 control class.
AkinoriHashimoto 0:812e6b59aa54 20 *
AkinoriHashimoto 0:812e6b59aa54 21 */
AkinoriHashimoto 0:812e6b59aa54 22 class RN41
AkinoriHashimoto 0:812e6b59aa54 23 {
AkinoriHashimoto 0:812e6b59aa54 24 public:
AkinoriHashimoto 0:812e6b59aa54 25
AkinoriHashimoto 0:812e6b59aa54 26 // static const int
AkinoriHashimoto 0:812e6b59aa54 27 static const int NG, OK;//= 1;
AkinoriHashimoto 0:812e6b59aa54 28 static const int ERR_AddrUnder12, ERR_AddrOver12, ERR_Connect, FAIL_Connect;
AkinoriHashimoto 0:812e6b59aa54 29 static const int ERR_EnterCmdMode, ERR_Disconnect;
AkinoriHashimoto 0:812e6b59aa54 30
AkinoriHashimoto 0:812e6b59aa54 31 // static const int None, Odd, Even;
AkinoriHashimoto 0:812e6b59aa54 32 static const int ERR_NameSize;
AkinoriHashimoto 0:812e6b59aa54 33
AkinoriHashimoto 0:812e6b59aa54 34 /** Create Serial port to RN41. for LPC1768
AkinoriHashimoto 0:812e6b59aa54 35 * @param TX, RX; Serial port.
AkinoriHashimoto 0:812e6b59aa54 36 * @param baud-rate; Baud rate (bps).
AkinoriHashimoto 0:812e6b59aa54 37 * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit.
AkinoriHashimoto 0:812e6b59aa54 38 * -- parity select; N(None), O(Odd), E(Even).
AkinoriHashimoto 0:812e6b59aa54 39 * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r).
AkinoriHashimoto 0:812e6b59aa54 40 */
AkinoriHashimoto 0:812e6b59aa54 41 RN41(PinName TX, PinName RX, int baudrate, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);
AkinoriHashimoto 0:812e6b59aa54 42
AkinoriHashimoto 0:812e6b59aa54 43 /**
AkinoriHashimoto 0:812e6b59aa54 44 * RxStrのCRまでを返す。
AkinoriHashimoto 0:812e6b59aa54 45 */
AkinoriHashimoto 0:812e6b59aa54 46 string getLine();
AkinoriHashimoto 0:812e6b59aa54 47
AkinoriHashimoto 0:812e6b59aa54 48 /**
AkinoriHashimoto 0:812e6b59aa54 49 *
AkinoriHashimoto 0:812e6b59aa54 50 */
AkinoriHashimoto 0:812e6b59aa54 51 void sendCMD( string str, bool addCR=true);
AkinoriHashimoto 0:812e6b59aa54 52 void sendLINE(string str, bool addCR=true);
AkinoriHashimoto 0:812e6b59aa54 53
AkinoriHashimoto 0:812e6b59aa54 54
AkinoriHashimoto 0:812e6b59aa54 55 int setDev_Name(string str, bool usingAddr=true);
AkinoriHashimoto 0:812e6b59aa54 56 int setDev_UART(int boad, int stop=1, int parity=SerialBase::None);
AkinoriHashimoto 0:812e6b59aa54 57
AkinoriHashimoto 0:812e6b59aa54 58
AkinoriHashimoto 0:812e6b59aa54 59 // Command to RN41
AkinoriHashimoto 0:812e6b59aa54 60 bool enterCMD();
AkinoriHashimoto 0:812e6b59aa54 61 int connect(string addr); // 必ず、Checkを呼ぶこと。2-5sかかるよ
AkinoriHashimoto 0:812e6b59aa54 62 bool disconnect();
AkinoriHashimoto 0:812e6b59aa54 63
AkinoriHashimoto 0:812e6b59aa54 64 private:
AkinoriHashimoto 0:812e6b59aa54 65 Serial rn41;
AkinoriHashimoto 0:812e6b59aa54 66 string CR;
AkinoriHashimoto 0:812e6b59aa54 67 string rxStr, str4GetLine; // 内部バッファ。read()での保管用と、GetLineまでの保管用。
AkinoriHashimoto 0:812e6b59aa54 68
AkinoriHashimoto 0:812e6b59aa54 69 Timer timerLocal;
AkinoriHashimoto 0:812e6b59aa54 70
AkinoriHashimoto 0:812e6b59aa54 71 static const int findCmp1, findCmp2, findCmp12;
AkinoriHashimoto 0:812e6b59aa54 72 /** check reply from RN41.
AkinoriHashimoto 0:812e6b59aa54 73 * @Param cmp; string to compare
AkinoriHashimoto 0:812e6b59aa54 74 * @Param timeout; timeout [ms]
AkinoriHashimoto 0:812e6b59aa54 75 * @return bool; true: ok, false: NG.
AkinoriHashimoto 0:812e6b59aa54 76 */
AkinoriHashimoto 0:812e6b59aa54 77 int chkReply(string cmp1, int timeout, string cmp2="");
AkinoriHashimoto 0:812e6b59aa54 78
AkinoriHashimoto 0:812e6b59aa54 79 bool chkReply_woCR(string cmp, int timeout);
AkinoriHashimoto 0:812e6b59aa54 80
AkinoriHashimoto 0:812e6b59aa54 81 /** Just copy to rxStr from Buffer.
AkinoriHashimoto 0:812e6b59aa54 82 *
AkinoriHashimoto 0:812e6b59aa54 83 */
AkinoriHashimoto 0:812e6b59aa54 84 void read();
AkinoriHashimoto 0:812e6b59aa54 85
AkinoriHashimoto 0:812e6b59aa54 86 };