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

Revision:
3:df094ed45a88
Parent:
2:931bef8c7fac
Child:
4:488e26ebc411
--- a/HC05.cpp	Thu Oct 01 07:14:55 2015 +0000
+++ b/HC05.cpp	Wed Oct 28 06:14:40 2015 +0000
@@ -17,23 +17,83 @@
     else// if (parity == SerialBase::None)
         hc05.format(bit, SerialBase::None, stop);     // 8bit, NonParity, 1stopbit
 
+    // attach rxIrq. because, rx buf of serial equals to 16 Bytes.
+    this->hc05.attach(this, &HC05::_readIrq, Serial::RxIrq);
+
     CR= '\r';
     if(CRLN)
         CR= "\r\n";
     return;
 }
 
-void HC05::setAttachRx(void (*fptr)(void))
+void HC05::setAttachRx(void (*fptr)(unsigned int))
 {
-    hc05.attach(fptr, Serial::RxIrq);
+//    hc05.attach(fptr, Serial::RxIrq);
+    fptrRxIrq= fptr;
     return;
 }
 
 bool HC05::readable()
 {
-    return hc05.readable();
+//    return hc05.readable();
+    return hc05.readable() || !rxStr.empty();
+}
+
+string HC05::read() // public:
+{
+    _read();
+    if(rxStr.empty())
+        return "";
+
+    // rxStr is not empty.
+    string tmp= rxStr;
+    rxStr.erase();
+    return tmp;
 }
 
+string HC05::getLine()
+{
+    string tmp= this->read();
+// rxStr is NULL;
+
+    if(tmp.empty())
+        return "";
+
+    // tmp is not empty.
+    int idx= tmp.rfind('\r');
+    if(idx == string::npos) {
+        rxStr= tmp;     // 戻す
+        return "";
+    }
+
+    // find \r
+    if(tmp[++idx] == '\n')
+        idx++;
+
+    // idxは改行後の文字先頭Indexを示す。
+    string rtn= tmp.substr(0, idx);
+    rxStr= tmp.substr(idx);
+    return rtn;
+}
+
+/*
+
+string HC05::getLine()
+{
+    static string rxStr;
+
+    rxStr= "";
+    if(!hc05.readable())
+        return rxStr;
+
+    // Bufferを吸い尽くす
+    while(true) { //(;;) {
+        rxStr += (char)hc05.getc();
+        if(!hc05.readable())
+            return rxStr;//return; //break;    // Bufferになくなった
+    } // end for
+}
+*/
 void HC05::sendLine(string str, bool addCR)
 {
     if(addCR)
@@ -41,6 +101,7 @@
     hc05.printf(str.c_str());
     return;
 }
+/*
 void HC05::sendFloat(float num, string foreword)
 {
     hc05.printf("%s%f\r\n", foreword.c_str(), num);
@@ -51,7 +112,7 @@
     hc05.printf("%s%d\r\n", foreword.c_str(), num);
     return;
 }
-
+*/
 int HC05::_putc(int val)     // for printf()
 {
     hc05.putc(val);
@@ -61,23 +122,32 @@
 // for "Stream"
 int HC05::_getc()
 {
-    return hc05.getc();
+    return -1;//hc05.getc();
 }
 
-string HC05::getLine()
+void HC05::_readIrq(void)
 {
-    static string rxStr;
+//    if(hc05.readable())
+//        rxStr += (char)hc05.getc();
 
-    rxStr= "";
     if(!hc05.readable())
-        return rxStr;
+        return;
+
+    unsigned int chr= hc05.getc();
+    rxStr += chr;
 
+    if(this->fptrRxIrq == NULL)
+        return;
+    fptrRxIrq(chr);
+
+    return;
+}
+void HC05::_read()
+{
     // Bufferを吸い尽くす
-    for(;;) {
+    while(hc05.readable())
         rxStr += (char)hc05.getc();
-        if(!hc05.readable())
-            return rxStr;//return; //break;    // Bufferになくなった
-    } // end for
+    return;         // Bufferになくなった
 }
 
 // EOF
\ No newline at end of file