Bluetooth module HC-05 Lib. suppotrs printf(); support IrqAttach, with using RTOS. include feature as background rxIrq attach. the lib store rx buf of serial, therefore can have over 16 bytes string. if you want to get string, you call read() or getLine(). read() is ALL string, getLine() is under CR('\r').

Dependencies:   RingBuffer

Fork of HC05 by Akinori Hashimoto

Committer:
AkinoriHashimoto
Date:
Wed Sep 02 06:23:22 2015 +0000
Revision:
0:a4ddaf82d43d
Child:
1:75bb445594e2
15/09/02

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:a4ddaf82d43d 1 #include "HC05.h"
AkinoriHashimoto 0:a4ddaf82d43d 2 //#include "SerialBase.h"
AkinoriHashimoto 0:a4ddaf82d43d 3
AkinoriHashimoto 0:a4ddaf82d43d 4 /*
AkinoriHashimoto 0:a4ddaf82d43d 5 const int RN41::NG= 0;
AkinoriHashimoto 0:a4ddaf82d43d 6 const int RN41::OK= 1;
AkinoriHashimoto 0:a4ddaf82d43d 7
AkinoriHashimoto 0:a4ddaf82d43d 8 // PRIVATE
AkinoriHashimoto 0:a4ddaf82d43d 9 const int RN41::findCmp12= 100;
AkinoriHashimoto 0:a4ddaf82d43d 10 const int RN41::findCmp1= 101;
AkinoriHashimoto 0:a4ddaf82d43d 11 const int RN41::findCmp2= 102;
AkinoriHashimoto 0:a4ddaf82d43d 12
AkinoriHashimoto 0:a4ddaf82d43d 13 // PUBLIC
AkinoriHashimoto 0:a4ddaf82d43d 14 const int RN41::ERR_AddrUnder12= 210;
AkinoriHashimoto 0:a4ddaf82d43d 15 const int RN41::ERR_AddrOver12= 211;
AkinoriHashimoto 0:a4ddaf82d43d 16 const int RN41::ERR_Connect= 220;
AkinoriHashimoto 0:a4ddaf82d43d 17 const int RN41::FAIL_Connect= 221;
AkinoriHashimoto 0:a4ddaf82d43d 18 const int RN41::ERR_EnterCmdMode= 230;
AkinoriHashimoto 0:a4ddaf82d43d 19 const int RN41::ERR_Disconnect= 240;
AkinoriHashimoto 0:a4ddaf82d43d 20
AkinoriHashimoto 0:a4ddaf82d43d 21 const int RN41::ERR_NameSize= 310;
AkinoriHashimoto 0:a4ddaf82d43d 22 */
AkinoriHashimoto 0:a4ddaf82d43d 23
AkinoriHashimoto 0:a4ddaf82d43d 24 HC05::HC05(PinName TX, PinName RX, int baudrate, PinName _resetPin, int bit, int parity, int stop, bool CRLN)
AkinoriHashimoto 0:a4ddaf82d43d 25 : hc05(TX, RX), resetPin(_resetPin)
AkinoriHashimoto 0:a4ddaf82d43d 26 {
AkinoriHashimoto 0:a4ddaf82d43d 27 resetPin= 1;
AkinoriHashimoto 0:a4ddaf82d43d 28 hc05.baud(baudrate);
AkinoriHashimoto 0:a4ddaf82d43d 29
AkinoriHashimoto 0:a4ddaf82d43d 30 // 力技
AkinoriHashimoto 0:a4ddaf82d43d 31 if (parity == SerialBase::Odd)
AkinoriHashimoto 0:a4ddaf82d43d 32 hc05.format(bit, SerialBase::Odd, stop);
AkinoriHashimoto 0:a4ddaf82d43d 33 else if (parity == SerialBase::Even)
AkinoriHashimoto 0:a4ddaf82d43d 34 hc05.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:a4ddaf82d43d 35 else// if (parity == SerialBase::None)
AkinoriHashimoto 0:a4ddaf82d43d 36 hc05.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:a4ddaf82d43d 37
AkinoriHashimoto 0:a4ddaf82d43d 38 CR= '\r';
AkinoriHashimoto 0:a4ddaf82d43d 39 if(CRLN)
AkinoriHashimoto 0:a4ddaf82d43d 40 CR= "\r\n";
AkinoriHashimoto 0:a4ddaf82d43d 41 }
AkinoriHashimoto 0:a4ddaf82d43d 42 /*
AkinoriHashimoto 0:a4ddaf82d43d 43 void HC05::setResetPin(PinName pin)
AkinoriHashimoto 0:a4ddaf82d43d 44 {
AkinoriHashimoto 0:a4ddaf82d43d 45 resetPin(pin);
AkinoriHashimoto 0:a4ddaf82d43d 46 return;
AkinoriHashimoto 0:a4ddaf82d43d 47 }*/
AkinoriHashimoto 0:a4ddaf82d43d 48
AkinoriHashimoto 0:a4ddaf82d43d 49
AkinoriHashimoto 0:a4ddaf82d43d 50
AkinoriHashimoto 0:a4ddaf82d43d 51 /*
AkinoriHashimoto 0:a4ddaf82d43d 52 string HC05::getLine()
AkinoriHashimoto 0:a4ddaf82d43d 53 {
AkinoriHashimoto 0:a4ddaf82d43d 54 read();
AkinoriHashimoto 0:a4ddaf82d43d 55 // CRまでを返す
AkinoriHashimoto 0:a4ddaf82d43d 56
AkinoriHashimoto 0:a4ddaf82d43d 57 if(str4GetLine.size()==0 && rxStr.size()==0)
AkinoriHashimoto 0:a4ddaf82d43d 58 return "";
AkinoriHashimoto 0:a4ddaf82d43d 59
AkinoriHashimoto 0:a4ddaf82d43d 60 // rxStr.size() > 0
AkinoriHashimoto 0:a4ddaf82d43d 61 int idx= rxStr.rfind(CR);
AkinoriHashimoto 0:a4ddaf82d43d 62
AkinoriHashimoto 0:a4ddaf82d43d 63 if(idx == string::npos) // not find CR!
AkinoriHashimoto 0:a4ddaf82d43d 64 return str4GetLine;
AkinoriHashimoto 0:a4ddaf82d43d 65
AkinoriHashimoto 0:a4ddaf82d43d 66 // rxStr has CR.
AkinoriHashimoto 0:a4ddaf82d43d 67 idx += CR.size(); // \rなら+1、\r\nは+2
AkinoriHashimoto 0:a4ddaf82d43d 68 string rtn= str4GetLine;
AkinoriHashimoto 0:a4ddaf82d43d 69 rtn += rxStr.substr(0, idx); // 切り取り
AkinoriHashimoto 0:a4ddaf82d43d 70 rxStr= rxStr.substr(idx);
AkinoriHashimoto 0:a4ddaf82d43d 71
AkinoriHashimoto 0:a4ddaf82d43d 72 return rtn;
AkinoriHashimoto 0:a4ddaf82d43d 73 }*/
AkinoriHashimoto 0:a4ddaf82d43d 74
AkinoriHashimoto 0:a4ddaf82d43d 75 void HC05::sendLine(string str, bool addCR)
AkinoriHashimoto 0:a4ddaf82d43d 76 {
AkinoriHashimoto 0:a4ddaf82d43d 77 if(addCR)
AkinoriHashimoto 0:a4ddaf82d43d 78 str += "\r\n";
AkinoriHashimoto 0:a4ddaf82d43d 79 hc05.printf(str.c_str());
AkinoriHashimoto 0:a4ddaf82d43d 80 return;
AkinoriHashimoto 0:a4ddaf82d43d 81 }
AkinoriHashimoto 0:a4ddaf82d43d 82
AkinoriHashimoto 0:a4ddaf82d43d 83
AkinoriHashimoto 0:a4ddaf82d43d 84 void HC05::sendFloat(float num, string foreword)
AkinoriHashimoto 0:a4ddaf82d43d 85 {
AkinoriHashimoto 0:a4ddaf82d43d 86 hc05.printf("%s%f\r\n", foreword.c_str(), num);
AkinoriHashimoto 0:a4ddaf82d43d 87 return;
AkinoriHashimoto 0:a4ddaf82d43d 88 }
AkinoriHashimoto 0:a4ddaf82d43d 89
AkinoriHashimoto 0:a4ddaf82d43d 90 void HC05::sendInt(int num, string foreword)
AkinoriHashimoto 0:a4ddaf82d43d 91 {
AkinoriHashimoto 0:a4ddaf82d43d 92 hc05.printf("%s%d\r\n", foreword.c_str(), num);
AkinoriHashimoto 0:a4ddaf82d43d 93 return;
AkinoriHashimoto 0:a4ddaf82d43d 94 }
AkinoriHashimoto 0:a4ddaf82d43d 95
AkinoriHashimoto 0:a4ddaf82d43d 96
AkinoriHashimoto 0:a4ddaf82d43d 97
AkinoriHashimoto 0:a4ddaf82d43d 98 string HC05::read()
AkinoriHashimoto 0:a4ddaf82d43d 99 {
AkinoriHashimoto 0:a4ddaf82d43d 100 static string rxStr;
AkinoriHashimoto 0:a4ddaf82d43d 101
AkinoriHashimoto 0:a4ddaf82d43d 102 rxStr= "";
AkinoriHashimoto 0:a4ddaf82d43d 103 if(!hc05.readable())
AkinoriHashimoto 0:a4ddaf82d43d 104 return rxStr;
AkinoriHashimoto 0:a4ddaf82d43d 105
AkinoriHashimoto 0:a4ddaf82d43d 106 // Bufferを吸い尽くす
AkinoriHashimoto 0:a4ddaf82d43d 107 for(;;) {
AkinoriHashimoto 0:a4ddaf82d43d 108 rxStr += (char)hc05.getc();
AkinoriHashimoto 0:a4ddaf82d43d 109 if(!hc05.readable())
AkinoriHashimoto 0:a4ddaf82d43d 110 return rxStr;//return; //break; // Bufferになくなった
AkinoriHashimoto 0:a4ddaf82d43d 111 } // end for
AkinoriHashimoto 0:a4ddaf82d43d 112 }
AkinoriHashimoto 0:a4ddaf82d43d 113
AkinoriHashimoto 0:a4ddaf82d43d 114 /*
AkinoriHashimoto 0:a4ddaf82d43d 115 void HC05::read()
AkinoriHashimoto 0:a4ddaf82d43d 116 {
AkinoriHashimoto 0:a4ddaf82d43d 117 if(!hc05.readable())
AkinoriHashimoto 0:a4ddaf82d43d 118 return;
AkinoriHashimoto 0:a4ddaf82d43d 119
AkinoriHashimoto 0:a4ddaf82d43d 120 // Bufferを吸い尽くす
AkinoriHashimoto 0:a4ddaf82d43d 121 // char ch;
AkinoriHashimoto 0:a4ddaf82d43d 122 for(;;) {
AkinoriHashimoto 0:a4ddaf82d43d 123 // char ch= rn41.getc(); // 1文字取る
AkinoriHashimoto 0:a4ddaf82d43d 124 // ch= rn41.getc(); // 1文字取る
AkinoriHashimoto 0:a4ddaf82d43d 125 // rxStr += ch; // 上書き前に退避
AkinoriHashimoto 0:a4ddaf82d43d 126 rxStr += (char)hc05.getc();
AkinoriHashimoto 0:a4ddaf82d43d 127 if(!hc05.readable())
AkinoriHashimoto 0:a4ddaf82d43d 128 return; //break; // Bufferになくなった
AkinoriHashimoto 0:a4ddaf82d43d 129 } // end for
AkinoriHashimoto 0:a4ddaf82d43d 130 }
AkinoriHashimoto 0:a4ddaf82d43d 131 */
AkinoriHashimoto 0:a4ddaf82d43d 132
AkinoriHashimoto 0:a4ddaf82d43d 133 /*
AkinoriHashimoto 0:a4ddaf82d43d 134 void readBT()
AkinoriHashimoto 0:a4ddaf82d43d 135 {
AkinoriHashimoto 0:a4ddaf82d43d 136 if(!UART_BT.readable())
AkinoriHashimoto 0:a4ddaf82d43d 137 return;
AkinoriHashimoto 0:a4ddaf82d43d 138
AkinoriHashimoto 0:a4ddaf82d43d 139 static char buf[maxRxBufSize];
AkinoriHashimoto 0:a4ddaf82d43d 140 int idx;
AkinoriHashimoto 0:a4ddaf82d43d 141
AkinoriHashimoto 0:a4ddaf82d43d 142 for(idx=0; idx < maxRxBufSize; idx++)
AkinoriHashimoto 0:a4ddaf82d43d 143 buf[idx]= 0x00;
AkinoriHashimoto 0:a4ddaf82d43d 144
AkinoriHashimoto 0:a4ddaf82d43d 145 for(idx= 0; idx < maxRxBufSize-3; idx++) {
AkinoriHashimoto 0:a4ddaf82d43d 146 // readableだから,1回は確実に読み取れる
AkinoriHashimoto 0:a4ddaf82d43d 147 // char ch= UART_BT.getc(); // 1文字取る
AkinoriHashimoto 0:a4ddaf82d43d 148
AkinoriHashimoto 0:a4ddaf82d43d 149 buf[idx]= UART_BT.getc();//ch[0];
AkinoriHashimoto 0:a4ddaf82d43d 150
AkinoriHashimoto 0:a4ddaf82d43d 151 if(!UART_BT.readable())
AkinoriHashimoto 0:a4ddaf82d43d 152 break; // Bufferになくなった
AkinoriHashimoto 0:a4ddaf82d43d 153 }
AkinoriHashimoto 0:a4ddaf82d43d 154 buf[idx+1]= 's';
AkinoriHashimoto 0:a4ddaf82d43d 155 buf[idx+2]= 0x00;
AkinoriHashimoto 0:a4ddaf82d43d 156
AkinoriHashimoto 0:a4ddaf82d43d 157 // とりあエコーしてるけど、本当なら解析しないと。
AkinoriHashimoto 0:a4ddaf82d43d 158
AkinoriHashimoto 0:a4ddaf82d43d 159 UART_BT.printf(buf);
AkinoriHashimoto 0:a4ddaf82d43d 160
AkinoriHashimoto 0:a4ddaf82d43d 161 }
AkinoriHashimoto 0:a4ddaf82d43d 162 */
AkinoriHashimoto 0:a4ddaf82d43d 163
AkinoriHashimoto 0:a4ddaf82d43d 164 void HC05::reset()
AkinoriHashimoto 0:a4ddaf82d43d 165 {
AkinoriHashimoto 0:a4ddaf82d43d 166 resetPin= 0;
AkinoriHashimoto 0:a4ddaf82d43d 167 wait(0.3);
AkinoriHashimoto 0:a4ddaf82d43d 168 resetPin= 1;
AkinoriHashimoto 0:a4ddaf82d43d 169 return;
AkinoriHashimoto 0:a4ddaf82d43d 170 }
AkinoriHashimoto 0:a4ddaf82d43d 171
AkinoriHashimoto 0:a4ddaf82d43d 172