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:
AkinoriHashimoto
Date:
Wed Oct 28 06:14:40 2015 +0000
Revision:
3:df094ed45a88
Parent:
2:931bef8c7fac
Child:
4:488e26ebc411
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').

Who changed what in which revision?

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