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:
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 #pragma once
AkinoriHashimoto 0:a4ddaf82d43d 2
AkinoriHashimoto 2:931bef8c7fac 3 /** Sample code
AkinoriHashimoto 2:931bef8c7fac 4 * #include "mbed.h"
AkinoriHashimoto 2:931bef8c7fac 5 * #include "HC05.h"
AkinoriHashimoto 2:931bef8c7fac 6 * // 2,3,4が入力されたら、LED2,3,4が反転するコード。
AkinoriHashimoto 2:931bef8c7fac 7 * DigitalOut led[]= {LED1, LED2, LED3, LED4};
AkinoriHashimoto 2:931bef8c7fac 8 * HC05 hc05(p9, p10);
AkinoriHashimoto 2:931bef8c7fac 9 * void receive()
AkinoriHashimoto 2:931bef8c7fac 10 * {
AkinoriHashimoto 2:931bef8c7fac 11 * int id= hc05.getc() - '1';
AkinoriHashimoto 2:931bef8c7fac 12 * if(0<id && id<4)
AkinoriHashimoto 2:931bef8c7fac 13 * led[id]= !led[id];
AkinoriHashimoto 2:931bef8c7fac 14 * return;
AkinoriHashimoto 2:931bef8c7fac 15 * }
AkinoriHashimoto 2:931bef8c7fac 16 * int main()
AkinoriHashimoto 2:931bef8c7fac 17 * {
AkinoriHashimoto 2:931bef8c7fac 18 * hc05.init();
AkinoriHashimoto 2:931bef8c7fac 19 * int iter= 0;
AkinoriHashimoto 2:931bef8c7fac 20 * hc05.setAttachRx(receive); // 割り込み
AkinoriHashimoto 2:931bef8c7fac 21 * while(1) {
AkinoriHashimoto 2:931bef8c7fac 22 * hc05.printf("iter: %3d\r\n", iter);
AkinoriHashimoto 2:931bef8c7fac 23 * // if(hc05.readable()) // 0.2s毎に確認
AkinoriHashimoto 2:931bef8c7fac 24 * // receive();
AkinoriHashimoto 2:931bef8c7fac 25 * led[0] = !led[0];
AkinoriHashimoto 2:931bef8c7fac 26 * iter++;
AkinoriHashimoto 2:931bef8c7fac 27 * wait(0.2);
AkinoriHashimoto 2:931bef8c7fac 28 * }
AkinoriHashimoto 2:931bef8c7fac 29 * }
AkinoriHashimoto 2:931bef8c7fac 30 */
AkinoriHashimoto 2:931bef8c7fac 31
AkinoriHashimoto 0:a4ddaf82d43d 32 #include "mbed.h"
AkinoriHashimoto 0:a4ddaf82d43d 33 #include <string>
AkinoriHashimoto 0:a4ddaf82d43d 34
AkinoriHashimoto 0:a4ddaf82d43d 35 /** Bluetooth module HC05 control class.
AkinoriHashimoto 0:a4ddaf82d43d 36 *
AkinoriHashimoto 0:a4ddaf82d43d 37 */
AkinoriHashimoto 1:75bb445594e2 38 class HC05 : public Stream
AkinoriHashimoto 0:a4ddaf82d43d 39 {
AkinoriHashimoto 0:a4ddaf82d43d 40 public:
AkinoriHashimoto 0:a4ddaf82d43d 41 /** Create Serial port to HC05.
AkinoriHashimoto 0:a4ddaf82d43d 42 * @param TX, RX; Serial port.
AkinoriHashimoto 0:a4ddaf82d43d 43 */
AkinoriHashimoto 1:75bb445594e2 44 HC05(PinName tx, PinName rx);
AkinoriHashimoto 1:75bb445594e2 45
AkinoriHashimoto 1:75bb445594e2 46 /** init
AkinoriHashimoto 1:75bb445594e2 47 * @param baud-rate; Baud rate (bps). 9600, 38,400, 115,200, 230,400
AkinoriHashimoto 1:75bb445594e2 48 * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit.
AkinoriHashimoto 1:75bb445594e2 49 * -- parity select; N(None), O(Odd), E(Even).
AkinoriHashimoto 1:75bb445594e2 50 * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r).
AkinoriHashimoto 1:75bb445594e2 51 */
AkinoriHashimoto 2:931bef8c7fac 52 void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);
AkinoriHashimoto 0:a4ddaf82d43d 53
AkinoriHashimoto 1:75bb445594e2 54 bool readable();
AkinoriHashimoto 2:931bef8c7fac 55 void setAttachRx(void (*fptr)(void));
AkinoriHashimoto 2:931bef8c7fac 56
AkinoriHashimoto 2:931bef8c7fac 57 string getLine();
AkinoriHashimoto 0:a4ddaf82d43d 58 void sendLine(string str, bool addCR=true);
AkinoriHashimoto 0:a4ddaf82d43d 59 void sendFloat(float num, string foreword="");
AkinoriHashimoto 0:a4ddaf82d43d 60 void sendInt(int num, string foreword="");
AkinoriHashimoto 0:a4ddaf82d43d 61
AkinoriHashimoto 0:a4ddaf82d43d 62 private:
AkinoriHashimoto 0:a4ddaf82d43d 63 Serial hc05;
AkinoriHashimoto 0:a4ddaf82d43d 64 string CR;
AkinoriHashimoto 0:a4ddaf82d43d 65
AkinoriHashimoto 1:75bb445594e2 66 // virtual func for printf() in Stream-class.
AkinoriHashimoto 1:75bb445594e2 67 virtual int _putc(int val);
AkinoriHashimoto 1:75bb445594e2 68 virtual int _getc();
AkinoriHashimoto 0:a4ddaf82d43d 69
AkinoriHashimoto 2:931bef8c7fac 70 };
AkinoriHashimoto 2:931bef8c7fac 71
AkinoriHashimoto 2:931bef8c7fac 72 // EOF