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.h@5:a05c7662c51f, 2015-11-02 (annotated)
- Committer:
- AkinoriHashimoto
- Date:
- Mon Nov 02 07:22:50 2015 +0000
- Revision:
- 5:a05c7662c51f
- Parent:
- 4:488e26ebc411
Adj. header file.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkinoriHashimoto | 5:a05c7662c51f | 1 | /** Bluetooth module HC05 control class with using RTOS. |
AkinoriHashimoto | 5:a05c7662c51f | 2 | * |
AkinoriHashimoto | 5:a05c7662c51f | 3 | */ |
AkinoriHashimoto | 3:df094ed45a88 | 4 | /** |
AkinoriHashimoto | 3:df094ed45a88 | 5 | * @code |
AkinoriHashimoto | 5:a05c7662c51f | 6 | #include "mbed.h" |
AkinoriHashimoto | 5:a05c7662c51f | 7 | #include "HC05.h" |
AkinoriHashimoto | 3:df094ed45a88 | 8 | |
AkinoriHashimoto | 5:a05c7662c51f | 9 | DigitalOut led[]= {LED1, LED2, LED3, LED4}; |
AkinoriHashimoto | 5:a05c7662c51f | 10 | HC05 hc05(p9, p10); |
AkinoriHashimoto | 3:df094ed45a88 | 11 | |
AkinoriHashimoto | 5:a05c7662c51f | 12 | void rcvIrq(char id) |
AkinoriHashimoto | 5:a05c7662c51f | 13 | { |
AkinoriHashimoto | 5:a05c7662c51f | 14 | led[0]= !led[0]; |
AkinoriHashimoto | 5:a05c7662c51f | 15 | if('2'<=id && id<='4') { // LED2 ~ 4 |
AkinoriHashimoto | 5:a05c7662c51f | 16 | id -= '1'; |
AkinoriHashimoto | 5:a05c7662c51f | 17 | led[id]= !led[id]; |
AkinoriHashimoto | 5:a05c7662c51f | 18 | } |
AkinoriHashimoto | 5:a05c7662c51f | 19 | return; |
AkinoriHashimoto | 5:a05c7662c51f | 20 | } |
AkinoriHashimoto | 5:a05c7662c51f | 21 | |
AkinoriHashimoto | 5:a05c7662c51f | 22 | int main() |
AkinoriHashimoto | 5:a05c7662c51f | 23 | { |
AkinoriHashimoto | 5:a05c7662c51f | 24 | hc05.init(); |
AkinoriHashimoto | 5:a05c7662c51f | 25 | hc05.setAttachRx(rcvIrq); |
AkinoriHashimoto | 5:a05c7662c51f | 26 | while(true) { |
AkinoriHashimoto | 5:a05c7662c51f | 27 | hc05.sendLine(hc05.read()); |
AkinoriHashimoto | 5:a05c7662c51f | 28 | // hc05.sendLine(hc05.getLine()); |
AkinoriHashimoto | 5:a05c7662c51f | 29 | wait(10); |
AkinoriHashimoto | 5:a05c7662c51f | 30 | } |
AkinoriHashimoto | 5:a05c7662c51f | 31 | } |
AkinoriHashimoto | 5:a05c7662c51f | 32 | // EOF |
AkinoriHashimoto | 3:df094ed45a88 | 33 | * @endcode |
AkinoriHashimoto | 2:931bef8c7fac | 34 | */ |
AkinoriHashimoto | 2:931bef8c7fac | 35 | |
AkinoriHashimoto | 5:a05c7662c51f | 36 | #pragma once |
AkinoriHashimoto | 5:a05c7662c51f | 37 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 38 | #include "mbed.h" |
AkinoriHashimoto | 0:a4ddaf82d43d | 39 | #include <string> |
AkinoriHashimoto | 4:488e26ebc411 | 40 | #include "RingBuffer.h" |
AkinoriHashimoto | 0:a4ddaf82d43d | 41 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 42 | /** Bluetooth module HC05 control class. |
AkinoriHashimoto | 0:a4ddaf82d43d | 43 | * |
AkinoriHashimoto | 0:a4ddaf82d43d | 44 | */ |
AkinoriHashimoto | 1:75bb445594e2 | 45 | class HC05 : public Stream |
AkinoriHashimoto | 0:a4ddaf82d43d | 46 | { |
AkinoriHashimoto | 0:a4ddaf82d43d | 47 | public: |
AkinoriHashimoto | 0:a4ddaf82d43d | 48 | /** Create Serial port to HC05. |
AkinoriHashimoto | 0:a4ddaf82d43d | 49 | * @param TX, RX; Serial port. |
AkinoriHashimoto | 0:a4ddaf82d43d | 50 | */ |
AkinoriHashimoto | 1:75bb445594e2 | 51 | HC05(PinName tx, PinName rx); |
AkinoriHashimoto | 1:75bb445594e2 | 52 | |
AkinoriHashimoto | 1:75bb445594e2 | 53 | /** init |
AkinoriHashimoto | 1:75bb445594e2 | 54 | * @param baud-rate; Baud rate (bps). 9600, 38,400, 115,200, 230,400 |
AkinoriHashimoto | 1:75bb445594e2 | 55 | * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit. |
AkinoriHashimoto | 1:75bb445594e2 | 56 | * -- parity select; N(None), O(Odd), E(Even). |
AkinoriHashimoto | 1:75bb445594e2 | 57 | * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r). |
AkinoriHashimoto | 1:75bb445594e2 | 58 | */ |
AkinoriHashimoto | 2:931bef8c7fac | 59 | void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true); |
AkinoriHashimoto | 0:a4ddaf82d43d | 60 | |
AkinoriHashimoto | 3:df094ed45a88 | 61 | |
AkinoriHashimoto | 4:488e26ebc411 | 62 | void setAttachRx(void (*fptr)(char)); |
AkinoriHashimoto | 1:75bb445594e2 | 63 | bool readable(); |
AkinoriHashimoto | 3:df094ed45a88 | 64 | string read(); |
AkinoriHashimoto | 2:931bef8c7fac | 65 | string getLine(); |
AkinoriHashimoto | 3:df094ed45a88 | 66 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 67 | void sendLine(string str, bool addCR=true); |
AkinoriHashimoto | 0:a4ddaf82d43d | 68 | |
AkinoriHashimoto | 0:a4ddaf82d43d | 69 | private: |
AkinoriHashimoto | 4:488e26ebc411 | 70 | RawSerial hc05; |
AkinoriHashimoto | 0:a4ddaf82d43d | 71 | string CR; |
AkinoriHashimoto | 4:488e26ebc411 | 72 | RingBuffer ringBuf; |
AkinoriHashimoto | 3:df094ed45a88 | 73 | |
AkinoriHashimoto | 3:df094ed45a88 | 74 | // RxIrq function pointer as out of class. |
AkinoriHashimoto | 4:488e26ebc411 | 75 | void (*fptrRxIrq)(char); |
AkinoriHashimoto | 3:df094ed45a88 | 76 | |
AkinoriHashimoto | 3:df094ed45a88 | 77 | // copy buf of rx to private string. |
AkinoriHashimoto | 5:a05c7662c51f | 78 | void _read(); // copy buf of rx to private string. |
AkinoriHashimoto | 5:a05c7662c51f | 79 | void _readIrq(); // copy buf of rx to private string. |
AkinoriHashimoto | 0:a4ddaf82d43d | 80 | |
AkinoriHashimoto | 1:75bb445594e2 | 81 | // virtual func for printf() in Stream-class. |
AkinoriHashimoto | 1:75bb445594e2 | 82 | virtual int _putc(int val); |
AkinoriHashimoto | 1:75bb445594e2 | 83 | virtual int _getc(); |
AkinoriHashimoto | 0:a4ddaf82d43d | 84 | |
AkinoriHashimoto | 2:931bef8c7fac | 85 | }; |
AkinoriHashimoto | 2:931bef8c7fac | 86 | |
AkinoriHashimoto | 2:931bef8c7fac | 87 | // EOF |