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 Jan 21 06:29:54 2016 +0000
Revision:
6:2eefa9ea0bf1
Parent:
HC05.h@5:a05c7662c51f
1st publish.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 6:2eefa9ea0bf1 1 /** Bluetooth module mySerial 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 6:2eefa9ea0bf1 7 #include "mySerial.h"
AkinoriHashimoto 3:df094ed45a88 8
AkinoriHashimoto 5:a05c7662c51f 9 DigitalOut led[]= {LED1, LED2, LED3, LED4};
AkinoriHashimoto 6:2eefa9ea0bf1 10 mySerial pc(USBTX, USBRX);
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 6:2eefa9ea0bf1 24 pc.init();
AkinoriHashimoto 6:2eefa9ea0bf1 25 pc.setAttachRx(rcvIrq);
AkinoriHashimoto 5:a05c7662c51f 26 while(true) {
AkinoriHashimoto 6:2eefa9ea0bf1 27 pc.printf("@");
AkinoriHashimoto 6:2eefa9ea0bf1 28 pc.sendLine(pc.get());
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 6:2eefa9ea0bf1 42 /** Serial communicate class with Tx & Rx ringbuf.
AkinoriHashimoto 0:a4ddaf82d43d 43 *
AkinoriHashimoto 0:a4ddaf82d43d 44 */
AkinoriHashimoto 6:2eefa9ea0bf1 45 class mySerial : public Stream
AkinoriHashimoto 0:a4ddaf82d43d 46 {
AkinoriHashimoto 0:a4ddaf82d43d 47 public:
AkinoriHashimoto 6:2eefa9ea0bf1 48 /** Create Serial port to mySerial.
AkinoriHashimoto 0:a4ddaf82d43d 49 * @param TX, RX; Serial port.
AkinoriHashimoto 0:a4ddaf82d43d 50 */
AkinoriHashimoto 6:2eefa9ea0bf1 51 mySerial(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 6:2eefa9ea0bf1 63
AkinoriHashimoto 1:75bb445594e2 64 bool readable();
AkinoriHashimoto 6:2eefa9ea0bf1 65 string get();
AkinoriHashimoto 2:931bef8c7fac 66 string getLine();
AkinoriHashimoto 6:2eefa9ea0bf1 67
AkinoriHashimoto 0:a4ddaf82d43d 68 void sendLine(string str, bool addCR=true);
AkinoriHashimoto 0:a4ddaf82d43d 69
AkinoriHashimoto 0:a4ddaf82d43d 70 private:
AkinoriHashimoto 6:2eefa9ea0bf1 71 RawSerial serial;
AkinoriHashimoto 0:a4ddaf82d43d 72 string CR;
AkinoriHashimoto 6:2eefa9ea0bf1 73 RingBuffer ringBufRx;
AkinoriHashimoto 6:2eefa9ea0bf1 74 RingBuffer ringBufTx;
AkinoriHashimoto 3:df094ed45a88 75
AkinoriHashimoto 3:df094ed45a88 76 // RxIrq function pointer as out of class.
AkinoriHashimoto 4:488e26ebc411 77 void (*fptrRxIrq)(char);
AkinoriHashimoto 3:df094ed45a88 78
AkinoriHashimoto 3:df094ed45a88 79 // copy buf of rx to private string.
AkinoriHashimoto 5:a05c7662c51f 80 void _read(); // copy buf of rx to private string.
AkinoriHashimoto 5:a05c7662c51f 81 void _readIrq(); // copy buf of rx to private string.
AkinoriHashimoto 0:a4ddaf82d43d 82
AkinoriHashimoto 1:75bb445594e2 83 // virtual func for printf() in Stream-class.
AkinoriHashimoto 1:75bb445594e2 84 virtual int _putc(int val);
AkinoriHashimoto 1:75bb445594e2 85 virtual int _getc();
AkinoriHashimoto 6:2eefa9ea0bf1 86 void putcIrq();
AkinoriHashimoto 2:931bef8c7fac 87 };
AkinoriHashimoto 2:931bef8c7fac 88
AkinoriHashimoto 2:931bef8c7fac 89 // EOF