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

Dependents:   semaforinoprova

Committer:
AkinoriHashimoto
Date:
Thu Oct 01 07:14:55 2015 +0000
Revision:
2:931bef8c7fac
Parent:
1:75bb445594e2
Child:
3:df094ed45a88
supports printf() and getc(), and setAttachRx().

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 0:a4ddaf82d43d 20 CR= '\r';
AkinoriHashimoto 0:a4ddaf82d43d 21 if(CRLN)
AkinoriHashimoto 0:a4ddaf82d43d 22 CR= "\r\n";
AkinoriHashimoto 1:75bb445594e2 23 return;
AkinoriHashimoto 0:a4ddaf82d43d 24 }
AkinoriHashimoto 0:a4ddaf82d43d 25
AkinoriHashimoto 2:931bef8c7fac 26 void HC05::setAttachRx(void (*fptr)(void))
AkinoriHashimoto 2:931bef8c7fac 27 {
AkinoriHashimoto 2:931bef8c7fac 28 hc05.attach(fptr, Serial::RxIrq);
AkinoriHashimoto 2:931bef8c7fac 29 return;
AkinoriHashimoto 2:931bef8c7fac 30 }
AkinoriHashimoto 2:931bef8c7fac 31
AkinoriHashimoto 1:75bb445594e2 32 bool HC05::readable()
AkinoriHashimoto 1:75bb445594e2 33 {
AkinoriHashimoto 1:75bb445594e2 34 return hc05.readable();
AkinoriHashimoto 1:75bb445594e2 35 }
AkinoriHashimoto 0:a4ddaf82d43d 36
AkinoriHashimoto 0:a4ddaf82d43d 37 void HC05::sendLine(string str, bool addCR)
AkinoriHashimoto 0:a4ddaf82d43d 38 {
AkinoriHashimoto 0:a4ddaf82d43d 39 if(addCR)
AkinoriHashimoto 0:a4ddaf82d43d 40 str += "\r\n";
AkinoriHashimoto 0:a4ddaf82d43d 41 hc05.printf(str.c_str());
AkinoriHashimoto 0:a4ddaf82d43d 42 return;
AkinoriHashimoto 0:a4ddaf82d43d 43 }
AkinoriHashimoto 0:a4ddaf82d43d 44 void HC05::sendFloat(float num, string foreword)
AkinoriHashimoto 0:a4ddaf82d43d 45 {
AkinoriHashimoto 0:a4ddaf82d43d 46 hc05.printf("%s%f\r\n", foreword.c_str(), num);
AkinoriHashimoto 0:a4ddaf82d43d 47 return;
AkinoriHashimoto 0:a4ddaf82d43d 48 }
AkinoriHashimoto 0:a4ddaf82d43d 49 void HC05::sendInt(int num, string foreword)
AkinoriHashimoto 0:a4ddaf82d43d 50 {
AkinoriHashimoto 0:a4ddaf82d43d 51 hc05.printf("%s%d\r\n", foreword.c_str(), num);
AkinoriHashimoto 0:a4ddaf82d43d 52 return;
AkinoriHashimoto 0:a4ddaf82d43d 53 }
AkinoriHashimoto 0:a4ddaf82d43d 54
AkinoriHashimoto 1:75bb445594e2 55 int HC05::_putc(int val) // for printf()
AkinoriHashimoto 1:75bb445594e2 56 {
AkinoriHashimoto 1:75bb445594e2 57 hc05.putc(val);
AkinoriHashimoto 1:75bb445594e2 58 return val;
AkinoriHashimoto 1:75bb445594e2 59 }
AkinoriHashimoto 1:75bb445594e2 60
AkinoriHashimoto 1:75bb445594e2 61 // for "Stream"
AkinoriHashimoto 1:75bb445594e2 62 int HC05::_getc()
AkinoriHashimoto 1:75bb445594e2 63 {
AkinoriHashimoto 2:931bef8c7fac 64 return hc05.getc();
AkinoriHashimoto 1:75bb445594e2 65 }
AkinoriHashimoto 0:a4ddaf82d43d 66
AkinoriHashimoto 2:931bef8c7fac 67 string HC05::getLine()
AkinoriHashimoto 0:a4ddaf82d43d 68 {
AkinoriHashimoto 0:a4ddaf82d43d 69 static string rxStr;
AkinoriHashimoto 1:75bb445594e2 70
AkinoriHashimoto 0:a4ddaf82d43d 71 rxStr= "";
AkinoriHashimoto 0:a4ddaf82d43d 72 if(!hc05.readable())
AkinoriHashimoto 0:a4ddaf82d43d 73 return rxStr;
AkinoriHashimoto 0:a4ddaf82d43d 74
AkinoriHashimoto 0:a4ddaf82d43d 75 // Bufferを吸い尽くす
AkinoriHashimoto 0:a4ddaf82d43d 76 for(;;) {
AkinoriHashimoto 0:a4ddaf82d43d 77 rxStr += (char)hc05.getc();
AkinoriHashimoto 0:a4ddaf82d43d 78 if(!hc05.readable())
AkinoriHashimoto 0:a4ddaf82d43d 79 return rxStr;//return; //break; // Bufferになくなった
AkinoriHashimoto 0:a4ddaf82d43d 80 } // end for
AkinoriHashimoto 0:a4ddaf82d43d 81 }
AkinoriHashimoto 0:a4ddaf82d43d 82
AkinoriHashimoto 2:931bef8c7fac 83 // EOF