RN41/42 control lib. RN41/42 is bluetooth module with class 1 or 2.
Dependencies: StrLib myTimer RingBuffer
Diff: RN41.cpp
- Revision:
- 3:2a87c102ddd3
- Parent:
- 2:3a28bc9332b6
- Child:
- 4:8b74db22e4c6
diff -r 3a28bc9332b6 -r 2a87c102ddd3 RN41.cpp --- a/RN41.cpp Wed Oct 28 04:49:41 2015 +0000 +++ b/RN41.cpp Mon Nov 02 07:39:14 2015 +0000 @@ -84,6 +84,7 @@ return ""; string tmp= rxStrStored+ rxStr; +// string tmp= rxStrStored+ ringBuf.get(); rxStr.erase(); rxStrStored.erase(); return tmp; @@ -92,26 +93,25 @@ string RN41::getLine() { _read(); + // CRまでを返す - if(rxStrStored.empty() && rxStr.empty()) return ""; int idx= rxStr.rfind('\r'); - if(idx != string::npos) { // CR存在 - if(rxStr[++idx] == '\n') + if(idx != string::npos) { + // CR存在 + if(rxStr[++idx] == '\n') // LF idx++; rxStrStored += rxStr.substr(0, idx); // size rxStr= rxStr.substr(idx); // idx } // rxStrはCRなし、rxStrStoredに調整済み - string tmp= rxStrStored; rxStrStored.erase(); return tmp; } - int RN41::setDev_UART(int baud, int bit, int parity, int stop) { rn41.baud(baud); @@ -183,7 +183,7 @@ sendCMD("\r", false); // 既にCMDモード if(chkReply("?\r", 500).b1) return true; - + return false; } @@ -224,29 +224,19 @@ while(true) { _read(); // rxStrを確認する。 - idxCR= rxStr.find('\r'); + if(idxCR != string::npos) { // CR found. // 通常通信、目的語(wCR)、目的語(woCR)+通常通信。の3パターンのみのはず。 - - // tmpに切り取って確認する。 - idxCR++; - if(rxStr[idxCR] == '\n') - idxCR++; - // rxStr[idxCR]は次の文字列のidx - tmpStr= rxStr.substr(0, idxCR); // substr(idx, count) - rxStr= rxStr.substr(idxCR); - - tmpFind= chkReply_line(tmpStr, cmp1, cmp2); + tmpFind= chkReply_process_wCR(cmp1, cmp2); if(tmpFind.b1) find.b1= true; if(tmpFind.b2) find.b2= true; - if(!(tmpFind.b1 || tmpFind.b2)) // 通常通信 - rxStrStored += tmpStr; + // CRがrxStrになくなるまで繰り返す。 } else { // CRなし - tmpFind= chkReply_line(rxStr, cmp1, cmp2); + tmpFind= chkReply_process_woCR(cmp1, cmp2, wCR); // この時点で、tmpとfindの両方がTrueの場合、目的のリプが2回出てきた。おかしい。 // とりあ、そのケースは無視。 if(tmpFind.b1) @@ -258,31 +248,8 @@ if(find.b1||find.b2) flagRtn= true; - // Return準備 - if( wCR && (tmpFind.b1||tmpFind.b2)) - flagRtn= false; - - if(!wCR && (tmpFind.b1||tmpFind.b2)) { - int idx1, idx2; - idx1= rxStr.find(cmp1); - if(!cmp2.empty()) - idx2= rxStr.find(cmp2); - else - idx2= string::npos; - - if(idx1 != string::npos) - idx1 += cmp1.size(); - else - idx1= 0; - if(idx2 != string::npos) - idx2 += cmp2.size(); - else - idx2= 0; - - if(idx1 < idx2) - idx1= idx2; - rxStr= rxStr.substr(idx1); - } + if(wCR && (tmpFind.b1||tmpFind.b2)) + flagRtn= false; // CRが出てくるまで待つ if(flagRtn) return find; @@ -290,143 +257,86 @@ if(timer.read_ms(false, false, false) > timeout) return find; - wait_ms(2); // 1ms waiting. + wait_ms(1); // 1ms waiting. } // end while } -RN41::bool2 RN41::chkReply_line(string trg, string cmp1, string cmp2) //, bool wCR) // withCR +RN41::bool2 RN41::chkReply_process_wCR(string cmp1, string cmp2) +{ + bool2 rtn; + + int idx= rxStr.find('\r'); + if(rxStr[++idx] == '\n') + idx++; + // rxStr[idx]は次の文字列のindex + + // tmpに切り取って確認する。 + string tmpStr= rxStr.substr(0, idx); // substr(idx, count) + rxStr= rxStr.substr(idx); + + b2i2 tmpFind= chkReply_find(tmpStr, cmp1, cmp2); + if(tmpFind.b1) + rtn.b1= true; + if(tmpFind.b2) + rtn.b2= true; + if(!(tmpFind.b1 || tmpFind.b2)) // 通常通信 + rxStrStored += tmpStr; + // cmpと合致していれば、保存しておく必要性がない + return rtn; +} +RN41::bool2 RN41::chkReply_process_woCR(string cmp1, string cmp2, bool wCR) { - bool2 tmp; + b2i2 tmpFind= chkReply_find(rxStr, cmp1, cmp2); +// この時点で、tmpとfindの両方がTrueの場合、目的のリプが2回出てきた。おかしい。 +// とりあ、そのケースは無視。 + + // 文字列rxStr処理 + if(!wCR && (tmpFind.b1||tmpFind.b2)) { + int idx1= 0, idx2= 0; + if(tmpFind.b1) + idx1= tmpFind.i1+ cmp1.size(); + if(tmpFind.b2) + idx2= tmpFind.i2+ cmp2.size(); + + if(idx1 < idx2) + idx1= idx2; + if(idx1 > 0) + rxStr= rxStr.substr(idx1); + } + + bool2 rtn; + rtn.b1= tmpFind.b1; + rtn.b2= tmpFind.b2; + return rtn; +} +RN41::b2i2 RN41::chkReply_find(string trg, string cmp1, string cmp2) //, bool wCR) // withCR +{ + b2i2 tmp; tmp.b1= tmp.b2= false; + tmp.i1= tmp.i2= string::npos; - if( trg.find(cmp1) != string::npos) + tmp.i1= trg.find(cmp1); + if( (tmp.i1= trg.find(cmp1)) != string::npos) tmp.b1= true; if(!cmp2.empty()) - if(trg.find(cmp2) != string::npos) + if((tmp.i2= trg.find(cmp2)) != string::npos) tmp.b2= true; - return tmp; } -/* -int RN41::chkReply(string cmp1, int timeout, string cmp2) -{ -// _read();// rxStrStored += rxStr; - - timer.start(true); -// int cmp2Size= cmp2.size(); - int idxCR, idx;//1, idx2;//, idxCut; - bool find1, find2; - - // RxStrのチェック必要じゃね?初期化とか - while(true) { - _read(); - idxCR= rxStr.find('\r'); - if(idxCR != string::npos) { // CR found. - // 最初のCRまでを確認。 - find1= find2= false; - // idx1: 0< or npos, idx2: 0< or npos(notfined or size0) - //idx1(~CR) -> idx2(~CR) - idx= rxStr.find(cmp1); - if((idx != string::npos) && (idx < idxCR)) - find1= true; - - if(!cmp2.empty()) { //(cmp2.size() != 0) { - idx= rxStr.find(cmp2); - if((idx != string::npos) && (idx < idxCR)) - find2= true; - } - - // CRまで切り取り。findならば削除。見つからなければ目的でない、通常通信とみなし、コピー。 - idxCR++;//idxCut= idxCR+ 1; - if(rxStr[idxCR] == '\n') - idxCR++; - // 切り捨てはidxからの切り取りを上書き。コピーは文字数。 - if(!(find1 || find2)) - rxStrStored += rxStr.substr(0, idxCR); - rxStr= rxStr.substr(idxCR); // 切り取りは共通。 - - if(find1 && find2) // find1有、find2有 - return findCmp12; - if(find1) - return findCmp1; - if(find2) - return findCmp2; - // CR内にCMP無い場合は、ループへ - } // (idxCR != string::npos) - - if(timer.read_ms(false, false, false) > timeout) - return ERR_Timeout; - wait_ms(2); // 1ms waiting. - } // end for -} -bool RN41::chkReply_woCR(string cmp, int timeout) -{ -// _read();rxStrStored += rxStr; - - timer.start(true); - int idx, idxCR; - while(true) { - _read(); - idx= rxStr.find(cmp); - - if(idx != string::npos) { - idxCR= rxStr.find('\r'); - - if(idxCR == string::npos) - rxStr.erase(); // 目的の単語のみと判断する。使用済みなので消去。 - else { // CR存在するので、目的単語の前後で意図が異なる。 - idxCR++; - if(rxStr[idxCR] == '\n') - idxCR++; - // CR後の最初の文字位置 - - rxStr= rxStr.substr(idx); - } - return true; - } - - if(timer.read_ms(false, false, false) > timeout) - return false; - wait_ms(2); - } -} -*/ - - void RN41::_readIrq(void) { - if(rn41.readable()) - rxStr += (char)rn41.getc(); + while(rn41.readable()) + ringBuf.set((char)rn41.getc()); return; } void RN41::_read() { // Bufferを吸い尽くす - while(rn41.readable()) - rxStr += (char)rn41.getc(); - - return; // Bufferになくなった + this->_readIrq(); // ringBufにSerialバッファからコピーされた。 + rxStr += ringBuf.get(); + return; } -/* -void RN41::_read() -{ - // Bufferを吸い尽くす - // bps(Baudrate)から一文字分待ち、それでも受信がなければBuf空と判断。 - int iter= 0; - while(true) { - if(rn41.readable()) { - rxStr += (char)rn41.getc(); - iter= 0; //reset - } else { - wait_us(usWait4readBuf); - iter++; - if(iter > 13) // 安全率30% - return; // Bufferになくなった - } - } // end while -} -*/ // EOF \ No newline at end of file