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

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

Committer:
AkinoriHashimoto
Date:
Thu Oct 22 05:02:36 2015 +0000
Revision:
1:5ed110051e39
Parent:
0:812e6b59aa54
Child:
2:3a28bc9332b6
Adj. 15.10.22

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 /*
AkinoriHashimoto 0:812e6b59aa54 6 * cmdで送る改行コードは、CRのみ。LFは文字として認識されるので?となる。
AkinoriHashimoto 0:812e6b59aa54 7 * RN41 Returns (default); AOK(valid cmd), ERR(invalid cmod), ?(unrecongnized cmd).
AkinoriHashimoto 0:812e6b59aa54 8
AkinoriHashimoto 0:812e6b59aa54 9 %を付ける初期化処理必須
AkinoriHashimoto 0:812e6b59aa54 10
AkinoriHashimoto 0:812e6b59aa54 11 */
AkinoriHashimoto 0:812e6b59aa54 12
AkinoriHashimoto 1:5ed110051e39 13 /**
AkinoriHashimoto 1:5ed110051e39 14 @code
AkinoriHashimoto 1:5ed110051e39 15 #include "mbed.h"
AkinoriHashimoto 1:5ed110051e39 16 #include <string>
AkinoriHashimoto 1:5ed110051e39 17 #include "RN41.h"
AkinoriHashimoto 1:5ed110051e39 18 RN41 rn41(p13, p14, 9600);
AkinoriHashimoto 1:5ed110051e39 19 DigitalOut led[]= {LED1, LED2, LED3, LED4};
AkinoriHashimoto 1:5ed110051e39 20 void connectRN41(string tmpAddr);
AkinoriHashimoto 1:5ed110051e39 21 int main()
AkinoriHashimoto 1:5ed110051e39 22 {
AkinoriHashimoto 1:5ed110051e39 23 while(true) {
AkinoriHashimoto 1:5ed110051e39 24 led[0]= !led[0];
AkinoriHashimoto 1:5ed110051e39 25 connectRN41("target BT address");
AkinoriHashimoto 1:5ed110051e39 26 rn41.sendLine("test");
AkinoriHashimoto 1:5ed110051e39 27 wait_ms(100);
AkinoriHashimoto 1:5ed110051e39 28 printf(rn41.getLine().c_str());
AkinoriHashimoto 1:5ed110051e39 29 if(rn41.disconnect())
AkinoriHashimoto 1:5ed110051e39 30 printf("disconnect");
AkinoriHashimoto 1:5ed110051e39 31 wait(5.0);
AkinoriHashimoto 1:5ed110051e39 32 }
AkinoriHashimoto 1:5ed110051e39 33 }
AkinoriHashimoto 1:5ed110051e39 34 void connectRN41(string tmpAddr)
AkinoriHashimoto 1:5ed110051e39 35 {
AkinoriHashimoto 1:5ed110051e39 36 printf("Connect to: %s.\r\n", tmpAddr);
AkinoriHashimoto 1:5ed110051e39 37 int status= rn41.connect(tmpAddr);
AkinoriHashimoto 1:5ed110051e39 38 if(status == RN41::OK) printf("OK, Connect.");
AkinoriHashimoto 1:5ed110051e39 39 else if(status == RN41::ERR_AddrUnder12) printf("NG, Connect. Under12 Addr.");
AkinoriHashimoto 1:5ed110051e39 40 else if(status == RN41::ERR_AddrOver12) printf("NG, Connect. Over12 Addr.");
AkinoriHashimoto 1:5ed110051e39 41 else if(status == RN41::ERR_EnterCmdMode) printf("Error, failed in to enter Cmd-mode.");
AkinoriHashimoto 1:5ed110051e39 42 else if(status == RN41::ERR_Disconnect) printf("Error, failed in DisConnect.");
AkinoriHashimoto 1:5ed110051e39 43 else if(status == RN41::ERR_Connect) printf("Error, Connect.");
AkinoriHashimoto 1:5ed110051e39 44 else if(status == RN41::FAIL_Connect) printf("Fail, Connect.");
AkinoriHashimoto 1:5ed110051e39 45 else if(status == RN41::ERR_Timeout ) printf("Error, TimeOut.");
AkinoriHashimoto 1:5ed110051e39 46 else printf("Error Connection: %d.",status);
AkinoriHashimoto 1:5ed110051e39 47 return;
AkinoriHashimoto 1:5ed110051e39 48 }
AkinoriHashimoto 1:5ed110051e39 49 @endcode
AkinoriHashimoto 1:5ed110051e39 50 */
AkinoriHashimoto 1:5ed110051e39 51
AkinoriHashimoto 1:5ed110051e39 52
AkinoriHashimoto 1:5ed110051e39 53 #pragma once
AkinoriHashimoto 1:5ed110051e39 54
AkinoriHashimoto 1:5ed110051e39 55 #include "mbed.h"
AkinoriHashimoto 1:5ed110051e39 56 #include "StrLib.h"
AkinoriHashimoto 1:5ed110051e39 57 #include "myTimer.h"
AkinoriHashimoto 1:5ed110051e39 58
AkinoriHashimoto 1:5ed110051e39 59
AkinoriHashimoto 0:812e6b59aa54 60
AkinoriHashimoto 0:812e6b59aa54 61 /** Bluetooth module RN41/42 control class.
AkinoriHashimoto 0:812e6b59aa54 62 *
AkinoriHashimoto 0:812e6b59aa54 63 */
AkinoriHashimoto 0:812e6b59aa54 64 class RN41
AkinoriHashimoto 0:812e6b59aa54 65 {
AkinoriHashimoto 0:812e6b59aa54 66 public:
AkinoriHashimoto 0:812e6b59aa54 67
AkinoriHashimoto 0:812e6b59aa54 68 // static const int
AkinoriHashimoto 0:812e6b59aa54 69 static const int NG, OK;//= 1;
AkinoriHashimoto 0:812e6b59aa54 70 static const int ERR_AddrUnder12, ERR_AddrOver12, ERR_Connect, FAIL_Connect;
AkinoriHashimoto 1:5ed110051e39 71 static const int ERR_EnterCmdMode, ERR_Disconnect, ERR_Timeout;
AkinoriHashimoto 0:812e6b59aa54 72
AkinoriHashimoto 0:812e6b59aa54 73 // static const int None, Odd, Even;
AkinoriHashimoto 0:812e6b59aa54 74 static const int ERR_NameSize;
AkinoriHashimoto 0:812e6b59aa54 75
AkinoriHashimoto 0:812e6b59aa54 76 /** Create Serial port to RN41. for LPC1768
AkinoriHashimoto 0:812e6b59aa54 77 * @param TX, RX; Serial port.
AkinoriHashimoto 0:812e6b59aa54 78 * @param baud-rate; Baud rate (bps).
AkinoriHashimoto 0:812e6b59aa54 79 * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit.
AkinoriHashimoto 0:812e6b59aa54 80 * -- parity select; N(None), O(Odd), E(Even).
AkinoriHashimoto 0:812e6b59aa54 81 * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r).
AkinoriHashimoto 0:812e6b59aa54 82 */
AkinoriHashimoto 0:812e6b59aa54 83 RN41(PinName TX, PinName RX, int baudrate, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);
AkinoriHashimoto 0:812e6b59aa54 84
AkinoriHashimoto 0:812e6b59aa54 85 /**
AkinoriHashimoto 0:812e6b59aa54 86 * RxStrのCRまでを返す。
AkinoriHashimoto 0:812e6b59aa54 87 */
AkinoriHashimoto 0:812e6b59aa54 88 string getLine();
AkinoriHashimoto 0:812e6b59aa54 89
AkinoriHashimoto 0:812e6b59aa54 90 /**
AkinoriHashimoto 0:812e6b59aa54 91 *
AkinoriHashimoto 0:812e6b59aa54 92 */
AkinoriHashimoto 0:812e6b59aa54 93 void sendCMD( string str, bool addCR=true);
AkinoriHashimoto 1:5ed110051e39 94 void sendLine(string str, bool addCR=true);
AkinoriHashimoto 0:812e6b59aa54 95
AkinoriHashimoto 0:812e6b59aa54 96
AkinoriHashimoto 0:812e6b59aa54 97 int setDev_Name(string str, bool usingAddr=true);
AkinoriHashimoto 1:5ed110051e39 98 int setDev_UART(int baud, int bit=8, int parity=SerialBase::None, int stop=1);
AkinoriHashimoto 0:812e6b59aa54 99
AkinoriHashimoto 0:812e6b59aa54 100
AkinoriHashimoto 0:812e6b59aa54 101 // Command to RN41
AkinoriHashimoto 0:812e6b59aa54 102 bool enterCMD();
AkinoriHashimoto 0:812e6b59aa54 103 int connect(string addr); // 必ず、Checkを呼ぶこと。2-5sかかるよ
AkinoriHashimoto 0:812e6b59aa54 104 bool disconnect();
AkinoriHashimoto 0:812e6b59aa54 105
AkinoriHashimoto 0:812e6b59aa54 106 private:
AkinoriHashimoto 0:812e6b59aa54 107 Serial rn41;
AkinoriHashimoto 1:5ed110051e39 108 string cr;
AkinoriHashimoto 0:812e6b59aa54 109 string rxStr, str4GetLine; // 内部バッファ。read()での保管用と、GetLineまでの保管用。
AkinoriHashimoto 0:812e6b59aa54 110
AkinoriHashimoto 1:5ed110051e39 111 // Timer timerLocal;
AkinoriHashimoto 1:5ed110051e39 112 myTimer timer;
AkinoriHashimoto 0:812e6b59aa54 113
AkinoriHashimoto 0:812e6b59aa54 114 static const int findCmp1, findCmp2, findCmp12;
AkinoriHashimoto 0:812e6b59aa54 115 /** check reply from RN41.
AkinoriHashimoto 0:812e6b59aa54 116 * @Param cmp; string to compare
AkinoriHashimoto 0:812e6b59aa54 117 * @Param timeout; timeout [ms]
AkinoriHashimoto 0:812e6b59aa54 118 * @return bool; true: ok, false: NG.
AkinoriHashimoto 0:812e6b59aa54 119 */
AkinoriHashimoto 0:812e6b59aa54 120 int chkReply(string cmp1, int timeout, string cmp2="");
AkinoriHashimoto 0:812e6b59aa54 121
AkinoriHashimoto 0:812e6b59aa54 122 bool chkReply_woCR(string cmp, int timeout);
AkinoriHashimoto 0:812e6b59aa54 123
AkinoriHashimoto 0:812e6b59aa54 124 /** Just copy to rxStr from Buffer.
AkinoriHashimoto 0:812e6b59aa54 125 *
AkinoriHashimoto 0:812e6b59aa54 126 */
AkinoriHashimoto 0:812e6b59aa54 127 void read();
AkinoriHashimoto 0:812e6b59aa54 128
AkinoriHashimoto 0:812e6b59aa54 129 };