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').
Fork of HC05 by
HC05.cpp@1:75bb445594e2, 2015-10-01 (annotated)
- Committer:
- AkinoriHashimoto
- Date:
- Thu Oct 01 04:40:15 2015 +0000
- Revision:
- 1:75bb445594e2
- Parent:
- 0:a4ddaf82d43d
- Child:
- 2:931bef8c7fac
1st publish.
Who changed what in which revision?
User | Revision | Line number | New 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 | //HC05::HC05(Serial &_serial) : hc05(_serial){} |
AkinoriHashimoto | 1:75bb445594e2 | 9 | void HC05::init(int baudrate, int bit, int parity, int stop, bool CRLN) |
AkinoriHashimoto | 1:75bb445594e2 | 10 | { |
AkinoriHashimoto | 0:a4ddaf82d43d | 11 | hc05.baud(baudrate); |
AkinoriHashimoto | 0:a4ddaf82d43d | 12 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 13 | // 力技 |
AkinoriHashimoto | 0:a4ddaf82d43d | 14 | if (parity == SerialBase::Odd) |
AkinoriHashimoto | 0:a4ddaf82d43d | 15 | hc05.format(bit, SerialBase::Odd, stop); |
AkinoriHashimoto | 0:a4ddaf82d43d | 16 | else if (parity == SerialBase::Even) |
AkinoriHashimoto | 0:a4ddaf82d43d | 17 | hc05.format(bit, SerialBase::Even, stop); // 8bit, NonParity, 1stopbit |
AkinoriHashimoto | 0:a4ddaf82d43d | 18 | else// if (parity == SerialBase::None) |
AkinoriHashimoto | 0:a4ddaf82d43d | 19 | hc05.format(bit, SerialBase::None, stop); // 8bit, NonParity, 1stopbit |
AkinoriHashimoto | 0:a4ddaf82d43d | 20 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 21 | CR= '\r'; |
AkinoriHashimoto | 0:a4ddaf82d43d | 22 | if(CRLN) |
AkinoriHashimoto | 0:a4ddaf82d43d | 23 | CR= "\r\n"; |
AkinoriHashimoto | 1:75bb445594e2 | 24 | return; |
AkinoriHashimoto | 0:a4ddaf82d43d | 25 | } |
AkinoriHashimoto | 0:a4ddaf82d43d | 26 | |
AkinoriHashimoto | 1:75bb445594e2 | 27 | bool HC05::readable() |
AkinoriHashimoto | 1:75bb445594e2 | 28 | { |
AkinoriHashimoto | 1:75bb445594e2 | 29 | return hc05.readable(); |
AkinoriHashimoto | 1:75bb445594e2 | 30 | } |
AkinoriHashimoto | 0:a4ddaf82d43d | 31 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 32 | void HC05::sendLine(string str, bool addCR) |
AkinoriHashimoto | 0:a4ddaf82d43d | 33 | { |
AkinoriHashimoto | 0:a4ddaf82d43d | 34 | if(addCR) |
AkinoriHashimoto | 0:a4ddaf82d43d | 35 | str += "\r\n"; |
AkinoriHashimoto | 0:a4ddaf82d43d | 36 | hc05.printf(str.c_str()); |
AkinoriHashimoto | 0:a4ddaf82d43d | 37 | return; |
AkinoriHashimoto | 0:a4ddaf82d43d | 38 | } |
AkinoriHashimoto | 0:a4ddaf82d43d | 39 | void HC05::sendFloat(float num, string foreword) |
AkinoriHashimoto | 0:a4ddaf82d43d | 40 | { |
AkinoriHashimoto | 0:a4ddaf82d43d | 41 | hc05.printf("%s%f\r\n", foreword.c_str(), num); |
AkinoriHashimoto | 0:a4ddaf82d43d | 42 | return; |
AkinoriHashimoto | 0:a4ddaf82d43d | 43 | } |
AkinoriHashimoto | 0:a4ddaf82d43d | 44 | void HC05::sendInt(int num, string foreword) |
AkinoriHashimoto | 0:a4ddaf82d43d | 45 | { |
AkinoriHashimoto | 0:a4ddaf82d43d | 46 | hc05.printf("%s%d\r\n", foreword.c_str(), num); |
AkinoriHashimoto | 0:a4ddaf82d43d | 47 | return; |
AkinoriHashimoto | 0:a4ddaf82d43d | 48 | } |
AkinoriHashimoto | 0:a4ddaf82d43d | 49 | |
AkinoriHashimoto | 1:75bb445594e2 | 50 | int HC05::_putc(int val) // for printf() |
AkinoriHashimoto | 1:75bb445594e2 | 51 | { |
AkinoriHashimoto | 1:75bb445594e2 | 52 | hc05.putc(val); |
AkinoriHashimoto | 1:75bb445594e2 | 53 | return val; |
AkinoriHashimoto | 1:75bb445594e2 | 54 | } |
AkinoriHashimoto | 1:75bb445594e2 | 55 | |
AkinoriHashimoto | 1:75bb445594e2 | 56 | // for "Stream" |
AkinoriHashimoto | 1:75bb445594e2 | 57 | int HC05::_getc() |
AkinoriHashimoto | 1:75bb445594e2 | 58 | { |
AkinoriHashimoto | 1:75bb445594e2 | 59 | return -1; |
AkinoriHashimoto | 1:75bb445594e2 | 60 | } |
AkinoriHashimoto | 0:a4ddaf82d43d | 61 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 62 | string HC05::read() |
AkinoriHashimoto | 0:a4ddaf82d43d | 63 | { |
AkinoriHashimoto | 0:a4ddaf82d43d | 64 | static string rxStr; |
AkinoriHashimoto | 1:75bb445594e2 | 65 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 66 | rxStr= ""; |
AkinoriHashimoto | 0:a4ddaf82d43d | 67 | if(!hc05.readable()) |
AkinoriHashimoto | 0:a4ddaf82d43d | 68 | return rxStr; |
AkinoriHashimoto | 0:a4ddaf82d43d | 69 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 70 | // Bufferを吸い尽くす |
AkinoriHashimoto | 0:a4ddaf82d43d | 71 | for(;;) { |
AkinoriHashimoto | 0:a4ddaf82d43d | 72 | rxStr += (char)hc05.getc(); |
AkinoriHashimoto | 0:a4ddaf82d43d | 73 | if(!hc05.readable()) |
AkinoriHashimoto | 0:a4ddaf82d43d | 74 | return rxStr;//return; //break; // Bufferになくなった |
AkinoriHashimoto | 0:a4ddaf82d43d | 75 | } // end for |
AkinoriHashimoto | 0:a4ddaf82d43d | 76 | } |
AkinoriHashimoto | 0:a4ddaf82d43d | 77 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 78 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 79 | /* |
AkinoriHashimoto | 0:a4ddaf82d43d | 80 | void HC05::reset() |
AkinoriHashimoto | 0:a4ddaf82d43d | 81 | { |
AkinoriHashimoto | 0:a4ddaf82d43d | 82 | resetPin= 0; |
AkinoriHashimoto | 0:a4ddaf82d43d | 83 | wait(0.3); |
AkinoriHashimoto | 0:a4ddaf82d43d | 84 | resetPin= 1; |
AkinoriHashimoto | 0:a4ddaf82d43d | 85 | return; |
AkinoriHashimoto | 0:a4ddaf82d43d | 86 | } |
AkinoriHashimoto | 1:75bb445594e2 | 87 | */ |
AkinoriHashimoto | 0:a4ddaf82d43d | 88 |