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

Dependencies:   StrLib myTimer RingBuffer

Dependents:   Theremin

Committer:
AkinoriHashimoto
Date:
Fri Nov 20 07:15:40 2015 +0000
Revision:
5:63925964cbd5
Parent:
3:2a87c102ddd3
add reboot().

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 #pragma once
AkinoriHashimoto 1:5ed110051e39 53
AkinoriHashimoto 1:5ed110051e39 54 #include "mbed.h"
AkinoriHashimoto 1:5ed110051e39 55 #include "StrLib.h"
AkinoriHashimoto 1:5ed110051e39 56 #include "myTimer.h"
AkinoriHashimoto 3:2a87c102ddd3 57 #include "RingBuffer.h"
AkinoriHashimoto 0:812e6b59aa54 58
AkinoriHashimoto 0:812e6b59aa54 59 /** Bluetooth module RN41/42 control class.
AkinoriHashimoto 0:812e6b59aa54 60 *
AkinoriHashimoto 0:812e6b59aa54 61 */
AkinoriHashimoto 0:812e6b59aa54 62 class RN41
AkinoriHashimoto 0:812e6b59aa54 63 {
AkinoriHashimoto 0:812e6b59aa54 64 public:
AkinoriHashimoto 0:812e6b59aa54 65
AkinoriHashimoto 0:812e6b59aa54 66 // static const int
AkinoriHashimoto 0:812e6b59aa54 67 static const int NG, OK;//= 1;
AkinoriHashimoto 0:812e6b59aa54 68 static const int ERR_AddrUnder12, ERR_AddrOver12, ERR_Connect, FAIL_Connect;
AkinoriHashimoto 1:5ed110051e39 69 static const int ERR_EnterCmdMode, ERR_Disconnect, ERR_Timeout;
AkinoriHashimoto 0:812e6b59aa54 70
AkinoriHashimoto 0:812e6b59aa54 71 // static const int None, Odd, Even;
AkinoriHashimoto 0:812e6b59aa54 72 static const int ERR_NameSize;
AkinoriHashimoto 0:812e6b59aa54 73
AkinoriHashimoto 0:812e6b59aa54 74 /** Create Serial port to RN41. for LPC1768
AkinoriHashimoto 0:812e6b59aa54 75 * @param TX, RX; Serial port.
AkinoriHashimoto 0:812e6b59aa54 76 * @param baud-rate; Baud rate (bps).
AkinoriHashimoto 0:812e6b59aa54 77 * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit.
AkinoriHashimoto 0:812e6b59aa54 78 * -- parity select; N(None), O(Odd), E(Even).
AkinoriHashimoto 0:812e6b59aa54 79 * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r).
AkinoriHashimoto 0:812e6b59aa54 80 */
AkinoriHashimoto 0:812e6b59aa54 81 RN41(PinName TX, PinName RX, int baudrate, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);
AkinoriHashimoto 0:812e6b59aa54 82
AkinoriHashimoto 2:3a28bc9332b6 83 string read();
AkinoriHashimoto 2:3a28bc9332b6 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 5:63925964cbd5 105 bool reboot();
AkinoriHashimoto 0:812e6b59aa54 106
AkinoriHashimoto 0:812e6b59aa54 107 private:
AkinoriHashimoto 3:2a87c102ddd3 108 RawSerial rn41;
AkinoriHashimoto 1:5ed110051e39 109 string cr;
AkinoriHashimoto 2:3a28bc9332b6 110 string rxStr, rxStrStored; // 内部バッファ。read()での保管用と、GetLineまでの保管用。
AkinoriHashimoto 3:2a87c102ddd3 111 RingBuffer ringBuf; // rxIRQ用の循環バッファ256kB。
AkinoriHashimoto 3:2a87c102ddd3 112 // string rxStrStored; // 内部バッファ。read()での保管用と、GetLineまでの保管用。
AkinoriHashimoto 2:3a28bc9332b6 113 // 9,600bpsなら、1文字受信に833us必要。安全率1.3として13ループさせるため、待ち時間us/0.1文字とする。
AkinoriHashimoto 2:3a28bc9332b6 114 // int usWait4readBuf; // -> attach
AkinoriHashimoto 1:5ed110051e39 115 // Timer timerLocal;
AkinoriHashimoto 1:5ed110051e39 116 myTimer timer;
AkinoriHashimoto 0:812e6b59aa54 117
AkinoriHashimoto 0:812e6b59aa54 118 static const int findCmp1, findCmp2, findCmp12;
AkinoriHashimoto 2:3a28bc9332b6 119
AkinoriHashimoto 2:3a28bc9332b6 120 struct bool2 {
AkinoriHashimoto 2:3a28bc9332b6 121 bool b1, b2;
AkinoriHashimoto 2:3a28bc9332b6 122 };
AkinoriHashimoto 3:2a87c102ddd3 123 struct b2i2 {
AkinoriHashimoto 3:2a87c102ddd3 124 bool b1, b2;
AkinoriHashimoto 3:2a87c102ddd3 125 int i1, i2;
AkinoriHashimoto 3:2a87c102ddd3 126 };
AkinoriHashimoto 2:3a28bc9332b6 127
AkinoriHashimoto 0:812e6b59aa54 128 /** check reply from RN41.
AkinoriHashimoto 0:812e6b59aa54 129 * @Param cmp; string to compare
AkinoriHashimoto 0:812e6b59aa54 130 * @Param timeout; timeout [ms]
AkinoriHashimoto 0:812e6b59aa54 131 * @return bool; true: ok, false: NG.
AkinoriHashimoto 0:812e6b59aa54 132 */
AkinoriHashimoto 2:3a28bc9332b6 133 // int chkReply(string cmp1, int timeout, string cmp2="");
AkinoriHashimoto 2:3a28bc9332b6 134 // bool chkReply_woCR(string cmp, int timeout);
AkinoriHashimoto 2:3a28bc9332b6 135 bool2 chkReply(string cmp1, int timeout, string cmp2= "", bool wCR= true);
AkinoriHashimoto 3:2a87c102ddd3 136 bool2 chkReply_process_wCR( string cmp1, string cmp2);
AkinoriHashimoto 3:2a87c102ddd3 137 bool2 chkReply_process_woCR(string cmp1, string cmp2, bool wCR);
AkinoriHashimoto 3:2a87c102ddd3 138 b2i2 chkReply_find(string trg, string cmp1, string cmp2);
AkinoriHashimoto 3:2a87c102ddd3 139
AkinoriHashimoto 0:812e6b59aa54 140
AkinoriHashimoto 2:3a28bc9332b6 141 /** Just copy to rxStr from Buffer. */
AkinoriHashimoto 2:3a28bc9332b6 142 void _read();
AkinoriHashimoto 2:3a28bc9332b6 143 void _readIrq();
AkinoriHashimoto 2:3a28bc9332b6 144 };
AkinoriHashimoto 0:812e6b59aa54 145
AkinoriHashimoto 2:3a28bc9332b6 146 // EOF