Serial Communicate Control Class. This has printf() and Tx & Rx buffer with (RxIRQ) RingBuffer, andmore can add user rxFunc to RxIrq.
mySerial.h@0:fa077fb53f44, 2016-01-21 (annotated)
- Committer:
- AkinoriHashimoto
- Date:
- Thu Jan 21 06:35:10 2016 +0000
- Revision:
- 0:fa077fb53f44
- Child:
- 3:3658415b257b
1st publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkinoriHashimoto | 0:fa077fb53f44 | 1 | /** Bluetooth module mySerial control class with using RTOS. |
AkinoriHashimoto | 0:fa077fb53f44 | 2 | * |
AkinoriHashimoto | 0:fa077fb53f44 | 3 | */ |
AkinoriHashimoto | 0:fa077fb53f44 | 4 | /** |
AkinoriHashimoto | 0:fa077fb53f44 | 5 | * @code |
AkinoriHashimoto | 0:fa077fb53f44 | 6 | #include "mbed.h" |
AkinoriHashimoto | 0:fa077fb53f44 | 7 | #include "mySerial.h" |
AkinoriHashimoto | 0:fa077fb53f44 | 8 | |
AkinoriHashimoto | 0:fa077fb53f44 | 9 | DigitalOut led[]= {LED1, LED2, LED3, LED4}; |
AkinoriHashimoto | 0:fa077fb53f44 | 10 | mySerial pc(USBTX, USBRX); |
AkinoriHashimoto | 0:fa077fb53f44 | 11 | |
AkinoriHashimoto | 0:fa077fb53f44 | 12 | void rcvIrq(char id) |
AkinoriHashimoto | 0:fa077fb53f44 | 13 | { |
AkinoriHashimoto | 0:fa077fb53f44 | 14 | led[0]= !led[0]; |
AkinoriHashimoto | 0:fa077fb53f44 | 15 | if('2'<=id && id<='4') { // LED2 ~ 4 |
AkinoriHashimoto | 0:fa077fb53f44 | 16 | id -= '1'; |
AkinoriHashimoto | 0:fa077fb53f44 | 17 | led[id]= !led[id]; |
AkinoriHashimoto | 0:fa077fb53f44 | 18 | } |
AkinoriHashimoto | 0:fa077fb53f44 | 19 | return; |
AkinoriHashimoto | 0:fa077fb53f44 | 20 | } |
AkinoriHashimoto | 0:fa077fb53f44 | 21 | |
AkinoriHashimoto | 0:fa077fb53f44 | 22 | int main() |
AkinoriHashimoto | 0:fa077fb53f44 | 23 | { |
AkinoriHashimoto | 0:fa077fb53f44 | 24 | pc.init(); |
AkinoriHashimoto | 0:fa077fb53f44 | 25 | pc.setAttachRx(rcvIrq); |
AkinoriHashimoto | 0:fa077fb53f44 | 26 | while(true) { |
AkinoriHashimoto | 0:fa077fb53f44 | 27 | pc.printf("@"); |
AkinoriHashimoto | 0:fa077fb53f44 | 28 | pc.sendLine(pc.get()); |
AkinoriHashimoto | 0:fa077fb53f44 | 29 | wait(10); |
AkinoriHashimoto | 0:fa077fb53f44 | 30 | } |
AkinoriHashimoto | 0:fa077fb53f44 | 31 | } |
AkinoriHashimoto | 0:fa077fb53f44 | 32 | // EOF |
AkinoriHashimoto | 0:fa077fb53f44 | 33 | * @endcode |
AkinoriHashimoto | 0:fa077fb53f44 | 34 | */ |
AkinoriHashimoto | 0:fa077fb53f44 | 35 | |
AkinoriHashimoto | 0:fa077fb53f44 | 36 | #pragma once |
AkinoriHashimoto | 0:fa077fb53f44 | 37 | |
AkinoriHashimoto | 0:fa077fb53f44 | 38 | #include "mbed.h" |
AkinoriHashimoto | 0:fa077fb53f44 | 39 | #include <string> |
AkinoriHashimoto | 0:fa077fb53f44 | 40 | #include "RingBuffer.h" |
AkinoriHashimoto | 0:fa077fb53f44 | 41 | |
AkinoriHashimoto | 0:fa077fb53f44 | 42 | /** Serial communicate class with Tx & Rx ringbuf. |
AkinoriHashimoto | 0:fa077fb53f44 | 43 | * |
AkinoriHashimoto | 0:fa077fb53f44 | 44 | */ |
AkinoriHashimoto | 0:fa077fb53f44 | 45 | class mySerial : public Stream |
AkinoriHashimoto | 0:fa077fb53f44 | 46 | { |
AkinoriHashimoto | 0:fa077fb53f44 | 47 | public: |
AkinoriHashimoto | 0:fa077fb53f44 | 48 | /** Create Serial port to mySerial. |
AkinoriHashimoto | 0:fa077fb53f44 | 49 | * @param TX, RX; Serial port. |
AkinoriHashimoto | 0:fa077fb53f44 | 50 | */ |
AkinoriHashimoto | 0:fa077fb53f44 | 51 | mySerial(PinName tx, PinName rx); |
AkinoriHashimoto | 0:fa077fb53f44 | 52 | |
AkinoriHashimoto | 0:fa077fb53f44 | 53 | /** init |
AkinoriHashimoto | 0:fa077fb53f44 | 54 | * @param baud-rate; Baud rate (bps). 9600, 38,400, 115,200, 230,400 |
AkinoriHashimoto | 0:fa077fb53f44 | 55 | * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit. |
AkinoriHashimoto | 0:fa077fb53f44 | 56 | * -- parity select; N(None), O(Odd), E(Even). |
AkinoriHashimoto | 0:fa077fb53f44 | 57 | * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r). |
AkinoriHashimoto | 0:fa077fb53f44 | 58 | */ |
AkinoriHashimoto | 0:fa077fb53f44 | 59 | void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true); |
AkinoriHashimoto | 0:fa077fb53f44 | 60 | |
AkinoriHashimoto | 0:fa077fb53f44 | 61 | |
AkinoriHashimoto | 0:fa077fb53f44 | 62 | void setAttachRx(void (*fptr)(char)); |
AkinoriHashimoto | 0:fa077fb53f44 | 63 | |
AkinoriHashimoto | 0:fa077fb53f44 | 64 | bool readable(); |
AkinoriHashimoto | 0:fa077fb53f44 | 65 | string get(); |
AkinoriHashimoto | 0:fa077fb53f44 | 66 | string getLine(); |
AkinoriHashimoto | 0:fa077fb53f44 | 67 | |
AkinoriHashimoto | 0:fa077fb53f44 | 68 | void sendLine(string str, bool addCR=true); |
AkinoriHashimoto | 0:fa077fb53f44 | 69 | |
AkinoriHashimoto | 0:fa077fb53f44 | 70 | private: |
AkinoriHashimoto | 0:fa077fb53f44 | 71 | RawSerial serial; |
AkinoriHashimoto | 0:fa077fb53f44 | 72 | string CR; |
AkinoriHashimoto | 0:fa077fb53f44 | 73 | RingBuffer ringBufRx; |
AkinoriHashimoto | 0:fa077fb53f44 | 74 | RingBuffer ringBufTx; |
AkinoriHashimoto | 0:fa077fb53f44 | 75 | |
AkinoriHashimoto | 0:fa077fb53f44 | 76 | // RxIrq function pointer as out of class. |
AkinoriHashimoto | 0:fa077fb53f44 | 77 | void (*fptrRxIrq)(char); |
AkinoriHashimoto | 0:fa077fb53f44 | 78 | |
AkinoriHashimoto | 0:fa077fb53f44 | 79 | // copy buf of rx to private string. |
AkinoriHashimoto | 0:fa077fb53f44 | 80 | void _read(); // copy buf of rx to private string. |
AkinoriHashimoto | 0:fa077fb53f44 | 81 | void _readIrq(); // copy buf of rx to private string. |
AkinoriHashimoto | 0:fa077fb53f44 | 82 | |
AkinoriHashimoto | 0:fa077fb53f44 | 83 | // virtual func for printf() in Stream-class. |
AkinoriHashimoto | 0:fa077fb53f44 | 84 | virtual int _putc(int val); |
AkinoriHashimoto | 0:fa077fb53f44 | 85 | virtual int _getc(); |
AkinoriHashimoto | 0:fa077fb53f44 | 86 | void putcIrq(); |
AkinoriHashimoto | 0:fa077fb53f44 | 87 | }; |
AkinoriHashimoto | 0:fa077fb53f44 | 88 | |
AkinoriHashimoto | 0:fa077fb53f44 | 89 | // EOF |