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:
Mon Nov 02 07:19:11 2015 +0000
Revision:
4:488e26ebc411
Parent:
3:df094ed45a88
Include Ring Buffer for with using RTOS.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:a4ddaf82d43d 1 #include "HC05.h"
AkinoriHashimoto 0:a4ddaf82d43d 2
AkinoriHashimoto 1:75bb445594e2 3 HC05::HC05(PinName TX, PinName RX)
AkinoriHashimoto 1:75bb445594e2 4 : hc05(TX, RX)
AkinoriHashimoto 0:a4ddaf82d43d 5 {
AkinoriHashimoto 1:75bb445594e2 6 }
AkinoriHashimoto 1:75bb445594e2 7 void HC05::init(int baudrate, int bit, int parity, int stop, bool CRLN)
AkinoriHashimoto 1:75bb445594e2 8 {
AkinoriHashimoto 0:a4ddaf82d43d 9 hc05.baud(baudrate);
AkinoriHashimoto 0:a4ddaf82d43d 10
AkinoriHashimoto 0:a4ddaf82d43d 11 // 力技
AkinoriHashimoto 0:a4ddaf82d43d 12 if (parity == SerialBase::Odd)
AkinoriHashimoto 0:a4ddaf82d43d 13 hc05.format(bit, SerialBase::Odd, stop);
AkinoriHashimoto 0:a4ddaf82d43d 14 else if (parity == SerialBase::Even)
AkinoriHashimoto 0:a4ddaf82d43d 15 hc05.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:a4ddaf82d43d 16 else// if (parity == SerialBase::None)
AkinoriHashimoto 0:a4ddaf82d43d 17 hc05.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit
AkinoriHashimoto 0:a4ddaf82d43d 18
AkinoriHashimoto 3:df094ed45a88 19 // attach rxIrq. because, rx buf of serial equals to 16 Bytes.
AkinoriHashimoto 3:df094ed45a88 20 this->hc05.attach(this, &HC05::_readIrq, Serial::RxIrq);
AkinoriHashimoto 3:df094ed45a88 21
AkinoriHashimoto 0:a4ddaf82d43d 22 CR= '\r';
AkinoriHashimoto 0:a4ddaf82d43d 23 if(CRLN)
AkinoriHashimoto 0:a4ddaf82d43d 24 CR= "\r\n";
AkinoriHashimoto 1:75bb445594e2 25 return;
AkinoriHashimoto 0:a4ddaf82d43d 26 }
AkinoriHashimoto 0:a4ddaf82d43d 27
AkinoriHashimoto 4:488e26ebc411 28 void HC05::setAttachRx(void (*fptr)(char))
AkinoriHashimoto 2:931bef8c7fac 29 {
AkinoriHashimoto 4:488e26ebc411 30 // Serial.attachから呼ばれる_readIrq()内から、fptrをCall.
AkinoriHashimoto 3:df094ed45a88 31 fptrRxIrq= fptr;
AkinoriHashimoto 2:931bef8c7fac 32 return;
AkinoriHashimoto 2:931bef8c7fac 33 }
AkinoriHashimoto 2:931bef8c7fac 34
AkinoriHashimoto 1:75bb445594e2 35 bool HC05::readable()
AkinoriHashimoto 1:75bb445594e2 36 {
AkinoriHashimoto 4:488e26ebc411 37 return hc05.readable() || !ringBuf.empty();
AkinoriHashimoto 3:df094ed45a88 38 }
AkinoriHashimoto 3:df094ed45a88 39
AkinoriHashimoto 3:df094ed45a88 40 string HC05::read() // public:
AkinoriHashimoto 3:df094ed45a88 41 {
AkinoriHashimoto 3:df094ed45a88 42 _read();
AkinoriHashimoto 4:488e26ebc411 43 if(ringBuf.empty())
AkinoriHashimoto 3:df094ed45a88 44 return "";
AkinoriHashimoto 4:488e26ebc411 45 return ringBuf.get();
AkinoriHashimoto 1:75bb445594e2 46 }
AkinoriHashimoto 0:a4ddaf82d43d 47
AkinoriHashimoto 3:df094ed45a88 48 string HC05::getLine()
AkinoriHashimoto 3:df094ed45a88 49 {
AkinoriHashimoto 3:df094ed45a88 50 string tmp= this->read();
AkinoriHashimoto 3:df094ed45a88 51 if(tmp.empty())
AkinoriHashimoto 3:df094ed45a88 52 return "";
AkinoriHashimoto 3:df094ed45a88 53
AkinoriHashimoto 3:df094ed45a88 54 // tmp is not empty.
AkinoriHashimoto 3:df094ed45a88 55 int idx= tmp.rfind('\r');
AkinoriHashimoto 3:df094ed45a88 56 if(idx == string::npos) {
AkinoriHashimoto 4:488e26ebc411 57 ringBuf.set(tmp); // 戻す
AkinoriHashimoto 3:df094ed45a88 58 return "";
AkinoriHashimoto 3:df094ed45a88 59 }
AkinoriHashimoto 3:df094ed45a88 60
AkinoriHashimoto 3:df094ed45a88 61 // find \r
AkinoriHashimoto 3:df094ed45a88 62 if(tmp[++idx] == '\n')
AkinoriHashimoto 3:df094ed45a88 63 idx++;
AkinoriHashimoto 3:df094ed45a88 64
AkinoriHashimoto 3:df094ed45a88 65 // idxは改行後の文字先頭Indexを示す。
AkinoriHashimoto 3:df094ed45a88 66 string rtn= tmp.substr(0, idx);
AkinoriHashimoto 4:488e26ebc411 67 tmp= tmp.substr(idx);
AkinoriHashimoto 4:488e26ebc411 68 ringBuf.set(tmp);
AkinoriHashimoto 3:df094ed45a88 69 return rtn;
AkinoriHashimoto 3:df094ed45a88 70 }
AkinoriHashimoto 3:df094ed45a88 71
AkinoriHashimoto 0:a4ddaf82d43d 72 void HC05::sendLine(string str, bool addCR)
AkinoriHashimoto 0:a4ddaf82d43d 73 {
AkinoriHashimoto 0:a4ddaf82d43d 74 if(addCR)
AkinoriHashimoto 0:a4ddaf82d43d 75 str += "\r\n";
AkinoriHashimoto 0:a4ddaf82d43d 76 hc05.printf(str.c_str());
AkinoriHashimoto 0:a4ddaf82d43d 77 return;
AkinoriHashimoto 0:a4ddaf82d43d 78 }
AkinoriHashimoto 4:488e26ebc411 79
AkinoriHashimoto 1:75bb445594e2 80 int HC05::_putc(int val) // for printf()
AkinoriHashimoto 1:75bb445594e2 81 {
AkinoriHashimoto 1:75bb445594e2 82 hc05.putc(val);
AkinoriHashimoto 1:75bb445594e2 83 return val;
AkinoriHashimoto 1:75bb445594e2 84 }
AkinoriHashimoto 1:75bb445594e2 85
AkinoriHashimoto 4:488e26ebc411 86 int HC05::_getc() // for "Stream"
AkinoriHashimoto 1:75bb445594e2 87 {
AkinoriHashimoto 3:df094ed45a88 88 return -1;//hc05.getc();
AkinoriHashimoto 1:75bb445594e2 89 }
AkinoriHashimoto 0:a4ddaf82d43d 90
AkinoriHashimoto 4:488e26ebc411 91 // Internal READ()
AkinoriHashimoto 3:df094ed45a88 92 void HC05::_readIrq(void)
AkinoriHashimoto 0:a4ddaf82d43d 93 {
AkinoriHashimoto 0:a4ddaf82d43d 94 if(!hc05.readable())
AkinoriHashimoto 3:df094ed45a88 95 return;
AkinoriHashimoto 3:df094ed45a88 96
AkinoriHashimoto 4:488e26ebc411 97 char chr= hc05.getc();
AkinoriHashimoto 4:488e26ebc411 98 ringBuf.set(chr);
AkinoriHashimoto 0:a4ddaf82d43d 99
AkinoriHashimoto 3:df094ed45a88 100 if(this->fptrRxIrq == NULL)
AkinoriHashimoto 3:df094ed45a88 101 return;
AkinoriHashimoto 3:df094ed45a88 102 fptrRxIrq(chr);
AkinoriHashimoto 3:df094ed45a88 103
AkinoriHashimoto 3:df094ed45a88 104 return;
AkinoriHashimoto 3:df094ed45a88 105 }
AkinoriHashimoto 3:df094ed45a88 106 void HC05::_read()
AkinoriHashimoto 3:df094ed45a88 107 {
AkinoriHashimoto 0:a4ddaf82d43d 108 // Bufferを吸い尽くす
AkinoriHashimoto 3:df094ed45a88 109 while(hc05.readable())
AkinoriHashimoto 4:488e26ebc411 110 ringBuf.set((char)hc05.getc());
AkinoriHashimoto 3:df094ed45a88 111 return; // Bufferになくなった
AkinoriHashimoto 0:a4ddaf82d43d 112 }
AkinoriHashimoto 0:a4ddaf82d43d 113
AkinoriHashimoto 2:931bef8c7fac 114 // EOF