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
Diff: HC05.h
- Revision:
- 3:df094ed45a88
- Parent:
- 2:931bef8c7fac
- Child:
- 4:488e26ebc411
diff -r 931bef8c7fac -r df094ed45a88 HC05.h --- a/HC05.h Thu Oct 01 07:14:55 2015 +0000 +++ b/HC05.h Wed Oct 28 06:14:40 2015 +0000 @@ -1,32 +1,35 @@ #pragma once -/** Sample code - * #include "mbed.h" -* #include "HC05.h" -* // 2,3,4が入力されたら、LED2,3,4が反転するコード。 -* DigitalOut led[]= {LED1, LED2, LED3, LED4}; -* HC05 hc05(p9, p10); -* void receive() -* { -* int id= hc05.getc() - '1'; -* if(0<id && id<4) -* led[id]= !led[id]; -* return; -* } -* int main() -* { -* hc05.init(); -* int iter= 0; -* hc05.setAttachRx(receive); // 割り込み -* while(1) { -* hc05.printf("iter: %3d\r\n", iter); -* // if(hc05.readable()) // 0.2s毎に確認 -* // receive(); -* led[0] = !led[0]; -* iter++; -* wait(0.2); -* } -* } +/** + * @code +#include "mbed.h" +#include "HC05.h" + +DigitalOut led[]= {LED1, LED2, LED3, LED4}; +HC05 hc05(p9, p10); + +void rcvIrq(unsigned int id) +{ + led[0]= !led[0]; + if('2'<=id && id<='4') { // LED2 ~ 4 + id -= '1'; + led[id]= !led[id]; + } + return; +} + +int main() +{ + hc05.init(); + hc05.setAttachRx(rcvIrq); + while(true) { +// hc05.sendLine(hc05.read()); + hc05.sendLine(hc05.getLine()); + wait(10); + } +} +// EOF + * @endcode */ #include "mbed.h" @@ -51,17 +54,27 @@ */ void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true); + + void setAttachRx(void (*fptr)(unsigned int)); bool readable(); - void setAttachRx(void (*fptr)(void)); - + string read(); string getLine(); + void sendLine(string str, bool addCR=true); - void sendFloat(float num, string foreword=""); - void sendInt(int num, string foreword=""); +// void sendFloat(float num, string foreword=""); // to use printf() +// void sendInt(int num, string foreword=""); private: Serial hc05; string CR; + string rxStr; + + // RxIrq function pointer as out of class. + void (*fptrRxIrq)(unsigned int); + + // copy buf of rx to private string. + void _read(); // copy buf of rx to private string. + void _readIrq(); // copy buf of rx to private string. // virtual func for printf() in Stream-class. virtual int _putc(int val);