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 Oct 28 06:14:40 2015 +0000
Revision:
3:df094ed45a88
Parent:
2:931bef8c7fac
Child:
4:488e26ebc411
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').

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:a4ddaf82d43d 1 #include "HC05.h"
AkinoriHashimoto 0:a4ddaf82d43d 2
AkinoriHashimoto 0:a4ddaf82d43d 3
AkinoriHashimoto 1:75bb445594e2 4 HC05::HC05(PinName TX, PinName RX)
AkinoriHashimoto 1:75bb445594e2 5 : hc05(TX, RX)
AkinoriHashimoto 0:a4ddaf82d43d 6 {
AkinoriHashimoto 1:75bb445594e2 7 }
AkinoriHashimoto 1:75bb445594e2 8 void HC05::init(int baudrate, int bit, int parity, int stop, bool CRLN)
AkinoriHashimoto 1:75bb445594e2 9 {
AkinoriHashimoto 0:a4ddaf82d43d 10 hc05.baud(baudrate);
AkinoriHashimoto 0:a4ddaf82d43d 11
AkinoriHashimoto 0:a4ddaf82d43d 12 // 力技
AkinoriHashimoto 0:a4ddaf82d43d 13 if (parity == SerialBase::Odd)
AkinoriHashimoto 0:a4ddaf82d43d 14 hc05.format(bit, SerialBase::Odd, stop);
AkinoriHashimoto 0:a4ddaf82d43d 15 else if (parity == SerialBase::Even)
AkinoriHashimoto 0:a4ddaf82d43d 16 hc05.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:a4ddaf82d43d 17 else// if (parity == SerialBase::None)
AkinoriHashimoto 0:a4ddaf82d43d 18 hc05.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:a4ddaf82d43d 19
AkinoriHashimoto 3:df094ed45a88 20 // attach rxIrq. because, rx buf of serial equals to 16 Bytes.
AkinoriHashimoto 3:df094ed45a88 21 this->hc05.attach(this, &HC05::_readIrq, Serial::RxIrq);
AkinoriHashimoto 3:df094ed45a88 22
AkinoriHashimoto 0:a4ddaf82d43d 23 CR= '\r';
AkinoriHashimoto 0:a4ddaf82d43d 24 if(CRLN)
AkinoriHashimoto 0:a4ddaf82d43d 25 CR= "\r\n";
AkinoriHashimoto 1:75bb445594e2 26 return;
AkinoriHashimoto 0:a4ddaf82d43d 27 }
AkinoriHashimoto 0:a4ddaf82d43d 28
AkinoriHashimoto 3:df094ed45a88 29 void HC05::setAttachRx(void (*fptr)(unsigned int))
AkinoriHashimoto 2:931bef8c7fac 30 {
AkinoriHashimoto 3:df094ed45a88 31 // hc05.attach(fptr, Serial::RxIrq);
AkinoriHashimoto 3:df094ed45a88 32 fptrRxIrq= fptr;
AkinoriHashimoto 2:931bef8c7fac 33 return;
AkinoriHashimoto 2:931bef8c7fac 34 }
AkinoriHashimoto 2:931bef8c7fac 35
AkinoriHashimoto 1:75bb445594e2 36 bool HC05::readable()
AkinoriHashimoto 1:75bb445594e2 37 {
AkinoriHashimoto 3:df094ed45a88 38 // return hc05.readable();
AkinoriHashimoto 3:df094ed45a88 39 return hc05.readable() || !rxStr.empty();
AkinoriHashimoto 3:df094ed45a88 40 }
AkinoriHashimoto 3:df094ed45a88 41
AkinoriHashimoto 3:df094ed45a88 42 string HC05::read() // public:
AkinoriHashimoto 3:df094ed45a88 43 {
AkinoriHashimoto 3:df094ed45a88 44 _read();
AkinoriHashimoto 3:df094ed45a88 45 if(rxStr.empty())
AkinoriHashimoto 3:df094ed45a88 46 return "";
AkinoriHashimoto 3:df094ed45a88 47
AkinoriHashimoto 3:df094ed45a88 48 // rxStr is not empty.
AkinoriHashimoto 3:df094ed45a88 49 string tmp= rxStr;
AkinoriHashimoto 3:df094ed45a88 50 rxStr.erase();
AkinoriHashimoto 3:df094ed45a88 51 return tmp;
AkinoriHashimoto 1:75bb445594e2 52 }
AkinoriHashimoto 0:a4ddaf82d43d 53
AkinoriHashimoto 3:df094ed45a88 54 string HC05::getLine()
AkinoriHashimoto 3:df094ed45a88 55 {
AkinoriHashimoto 3:df094ed45a88 56 string tmp= this->read();
AkinoriHashimoto 3:df094ed45a88 57 // rxStr is NULL;
AkinoriHashimoto 3:df094ed45a88 58
AkinoriHashimoto 3:df094ed45a88 59 if(tmp.empty())
AkinoriHashimoto 3:df094ed45a88 60 return "";
AkinoriHashimoto 3:df094ed45a88 61
AkinoriHashimoto 3:df094ed45a88 62 // tmp is not empty.
AkinoriHashimoto 3:df094ed45a88 63 int idx= tmp.rfind('\r');
AkinoriHashimoto 3:df094ed45a88 64 if(idx == string::npos) {
AkinoriHashimoto 3:df094ed45a88 65 rxStr= tmp; // 戻す
AkinoriHashimoto 3:df094ed45a88 66 return "";
AkinoriHashimoto 3:df094ed45a88 67 }
AkinoriHashimoto 3:df094ed45a88 68
AkinoriHashimoto 3:df094ed45a88 69 // find \r
AkinoriHashimoto 3:df094ed45a88 70 if(tmp[++idx] == '\n')
AkinoriHashimoto 3:df094ed45a88 71 idx++;
AkinoriHashimoto 3:df094ed45a88 72
AkinoriHashimoto 3:df094ed45a88 73 // idxは改行後の文字先頭Indexを示す。
AkinoriHashimoto 3:df094ed45a88 74 string rtn= tmp.substr(0, idx);
AkinoriHashimoto 3:df094ed45a88 75 rxStr= tmp.substr(idx);
AkinoriHashimoto 3:df094ed45a88 76 return rtn;
AkinoriHashimoto 3:df094ed45a88 77 }
AkinoriHashimoto 3:df094ed45a88 78
AkinoriHashimoto 3:df094ed45a88 79 /*
AkinoriHashimoto 3:df094ed45a88 80
AkinoriHashimoto 3:df094ed45a88 81 string HC05::getLine()
AkinoriHashimoto 3:df094ed45a88 82 {
AkinoriHashimoto 3:df094ed45a88 83 static string rxStr;
AkinoriHashimoto 3:df094ed45a88 84
AkinoriHashimoto 3:df094ed45a88 85 rxStr= "";
AkinoriHashimoto 3:df094ed45a88 86 if(!hc05.readable())
AkinoriHashimoto 3:df094ed45a88 87 return rxStr;
AkinoriHashimoto 3:df094ed45a88 88
AkinoriHashimoto 3:df094ed45a88 89 // Bufferを吸い尽くす
AkinoriHashimoto 3:df094ed45a88 90 while(true) { //(;;) {
AkinoriHashimoto 3:df094ed45a88 91 rxStr += (char)hc05.getc();
AkinoriHashimoto 3:df094ed45a88 92 if(!hc05.readable())
AkinoriHashimoto 3:df094ed45a88 93 return rxStr;//return; //break; // Bufferになくなった
AkinoriHashimoto 3:df094ed45a88 94 } // end for
AkinoriHashimoto 3:df094ed45a88 95 }
AkinoriHashimoto 3:df094ed45a88 96 */
AkinoriHashimoto 0:a4ddaf82d43d 97 void HC05::sendLine(string str, bool addCR)
AkinoriHashimoto 0:a4ddaf82d43d 98 {
AkinoriHashimoto 0:a4ddaf82d43d 99 if(addCR)
AkinoriHashimoto 0:a4ddaf82d43d 100 str += "\r\n";
AkinoriHashimoto 0:a4ddaf82d43d 101 hc05.printf(str.c_str());
AkinoriHashimoto 0:a4ddaf82d43d 102 return;
AkinoriHashimoto 0:a4ddaf82d43d 103 }
AkinoriHashimoto 3:df094ed45a88 104 /*
AkinoriHashimoto 0:a4ddaf82d43d 105 void HC05::sendFloat(float num, string foreword)
AkinoriHashimoto 0:a4ddaf82d43d 106 {
AkinoriHashimoto 0:a4ddaf82d43d 107 hc05.printf("%s%f\r\n", foreword.c_str(), num);
AkinoriHashimoto 0:a4ddaf82d43d 108 return;
AkinoriHashimoto 0:a4ddaf82d43d 109 }
AkinoriHashimoto 0:a4ddaf82d43d 110 void HC05::sendInt(int num, string foreword)
AkinoriHashimoto 0:a4ddaf82d43d 111 {
AkinoriHashimoto 0:a4ddaf82d43d 112 hc05.printf("%s%d\r\n", foreword.c_str(), num);
AkinoriHashimoto 0:a4ddaf82d43d 113 return;
AkinoriHashimoto 0:a4ddaf82d43d 114 }
AkinoriHashimoto 3:df094ed45a88 115 */
AkinoriHashimoto 1:75bb445594e2 116 int HC05::_putc(int val) // for printf()
AkinoriHashimoto 1:75bb445594e2 117 {
AkinoriHashimoto 1:75bb445594e2 118 hc05.putc(val);
AkinoriHashimoto 1:75bb445594e2 119 return val;
AkinoriHashimoto 1:75bb445594e2 120 }
AkinoriHashimoto 1:75bb445594e2 121
AkinoriHashimoto 1:75bb445594e2 122 // for "Stream"
AkinoriHashimoto 1:75bb445594e2 123 int HC05::_getc()
AkinoriHashimoto 1:75bb445594e2 124 {
AkinoriHashimoto 3:df094ed45a88 125 return -1;//hc05.getc();
AkinoriHashimoto 1:75bb445594e2 126 }
AkinoriHashimoto 0:a4ddaf82d43d 127
AkinoriHashimoto 3:df094ed45a88 128 void HC05::_readIrq(void)
AkinoriHashimoto 0:a4ddaf82d43d 129 {
AkinoriHashimoto 3:df094ed45a88 130 // if(hc05.readable())
AkinoriHashimoto 3:df094ed45a88 131 // rxStr += (char)hc05.getc();
AkinoriHashimoto 1:75bb445594e2 132
AkinoriHashimoto 0:a4ddaf82d43d 133 if(!hc05.readable())
AkinoriHashimoto 3:df094ed45a88 134 return;
AkinoriHashimoto 3:df094ed45a88 135
AkinoriHashimoto 3:df094ed45a88 136 unsigned int chr= hc05.getc();
AkinoriHashimoto 3:df094ed45a88 137 rxStr += chr;
AkinoriHashimoto 0:a4ddaf82d43d 138
AkinoriHashimoto 3:df094ed45a88 139 if(this->fptrRxIrq == NULL)
AkinoriHashimoto 3:df094ed45a88 140 return;
AkinoriHashimoto 3:df094ed45a88 141 fptrRxIrq(chr);
AkinoriHashimoto 3:df094ed45a88 142
AkinoriHashimoto 3:df094ed45a88 143 return;
AkinoriHashimoto 3:df094ed45a88 144 }
AkinoriHashimoto 3:df094ed45a88 145 void HC05::_read()
AkinoriHashimoto 3:df094ed45a88 146 {
AkinoriHashimoto 0:a4ddaf82d43d 147 // Bufferを吸い尽くす
AkinoriHashimoto 3:df094ed45a88 148 while(hc05.readable())
AkinoriHashimoto 0:a4ddaf82d43d 149 rxStr += (char)hc05.getc();
AkinoriHashimoto 3:df094ed45a88 150 return; // Bufferになくなった
AkinoriHashimoto 0:a4ddaf82d43d 151 }
AkinoriHashimoto 0:a4ddaf82d43d 152
AkinoriHashimoto 2:931bef8c7fac 153 // EOF