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:
Paulbearne
Date:
Sun Apr 17 05:46:34 2016 +0000
Revision:
7:035347f476f4
Parent:
6:2eefa9ea0bf1
.

Who changed what in which revision?

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