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').
Fork of HC05 by
Diff: HC05.cpp
- Revision:
- 1:75bb445594e2
- Parent:
- 0:a4ddaf82d43d
- Child:
- 2:931bef8c7fac
diff -r a4ddaf82d43d -r 75bb445594e2 HC05.cpp --- a/HC05.cpp Wed Sep 02 06:23:22 2015 +0000 +++ b/HC05.cpp Thu Oct 01 04:40:15 2015 +0000 @@ -1,30 +1,13 @@ #include "HC05.h" -//#include "SerialBase.h" -/* -const int RN41::NG= 0; -const int RN41::OK= 1; - -// PRIVATE -const int RN41::findCmp12= 100; -const int RN41::findCmp1= 101; -const int RN41::findCmp2= 102; -// PUBLIC -const int RN41::ERR_AddrUnder12= 210; -const int RN41::ERR_AddrOver12= 211; -const int RN41::ERR_Connect= 220; -const int RN41::FAIL_Connect= 221; -const int RN41::ERR_EnterCmdMode= 230; -const int RN41::ERR_Disconnect= 240; - -const int RN41::ERR_NameSize= 310; -*/ - -HC05::HC05(PinName TX, PinName RX, int baudrate, PinName _resetPin, int bit, int parity, int stop, bool CRLN) - : hc05(TX, RX), resetPin(_resetPin) +HC05::HC05(PinName TX, PinName RX) + : hc05(TX, RX) { - resetPin= 1; +} +//HC05::HC05(Serial &_serial) : hc05(_serial){} +void HC05::init(int baudrate, int bit, int parity, int stop, bool CRLN) +{ hc05.baud(baudrate); // 力技 @@ -38,39 +21,13 @@ CR= '\r'; if(CRLN) CR= "\r\n"; + return; } -/* -void HC05::setResetPin(PinName pin) -{ - resetPin(pin); - return; -}*/ - - - -/* -string HC05::getLine() -{ - read(); - // CRまでを返す - if(str4GetLine.size()==0 && rxStr.size()==0) - return ""; - - // rxStr.size() > 0 - int idx= rxStr.rfind(CR); - - if(idx == string::npos) // not find CR! - return str4GetLine; - - // rxStr has CR. - idx += CR.size(); // \rなら+1、\r\nは+2 - string rtn= str4GetLine; - rtn += rxStr.substr(0, idx); // 切り取り - rxStr= rxStr.substr(idx); - - return rtn; -}*/ +bool HC05::readable() +{ + return hc05.readable(); +} void HC05::sendLine(string str, bool addCR) { @@ -79,26 +36,33 @@ hc05.printf(str.c_str()); return; } - - void HC05::sendFloat(float num, string foreword) { hc05.printf("%s%f\r\n", foreword.c_str(), num); return; } - void HC05::sendInt(int num, string foreword) { hc05.printf("%s%d\r\n", foreword.c_str(), num); return; } - +int HC05::_putc(int val) // for printf() +{ + hc05.putc(val); + return val; +} + +// for "Stream" +int HC05::_getc() +{ + return -1; +} string HC05::read() { static string rxStr; - + rxStr= ""; if(!hc05.readable()) return rxStr; @@ -111,56 +75,8 @@ } // end for } -/* -void HC05::read() -{ - if(!hc05.readable()) - return; - - // Bufferを吸い尽くす -// char ch; - for(;;) { -// char ch= rn41.getc(); // 1文字取る -// ch= rn41.getc(); // 1文字取る -// rxStr += ch; // 上書き前に退避 - rxStr += (char)hc05.getc(); - if(!hc05.readable()) - return; //break; // Bufferになくなった - } // end for -} -*/ /* -void readBT() -{ - if(!UART_BT.readable()) - return; - - static char buf[maxRxBufSize]; - int idx; - - for(idx=0; idx < maxRxBufSize; idx++) - buf[idx]= 0x00; - - for(idx= 0; idx < maxRxBufSize-3; idx++) { - // readableだから,1回は確実に読み取れる -// char ch= UART_BT.getc(); // 1文字取る - - buf[idx]= UART_BT.getc();//ch[0]; - - if(!UART_BT.readable()) - break; // Bufferになくなった - } - buf[idx+1]= 's'; - buf[idx+2]= 0x00; - - // とりあエコーしてるけど、本当なら解析しないと。 - - UART_BT.printf(buf); - -} -*/ - void HC05::reset() { resetPin= 0; @@ -168,5 +84,5 @@ resetPin= 1; return; } +*/ -