Serial Communicate Control Class. This has printf() and Tx & Rx buffer with (RxIRQ) RingBuffer, andmore can add user rxFunc to RxIrq.

Dependencies:   RingBuffer

Committer:
AkinoriHashimoto
Date:
Tue Nov 15 06:27:34 2016 +0000
Revision:
3:3658415b257b
Parent:
0:fa077fb53f44
add comments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 3:3658415b257b 1 /** Serial control class with using RTOS.
AkinoriHashimoto 3:3658415b257b 2 * You can use printf(), rxIrq();
AkinoriHashimoto 0:fa077fb53f44 3 *
AkinoriHashimoto 0:fa077fb53f44 4 */
AkinoriHashimoto 0:fa077fb53f44 5 /**
AkinoriHashimoto 0:fa077fb53f44 6 * @code
AkinoriHashimoto 0:fa077fb53f44 7 #include "mbed.h"
AkinoriHashimoto 0:fa077fb53f44 8 #include "mySerial.h"
AkinoriHashimoto 0:fa077fb53f44 9
AkinoriHashimoto 0:fa077fb53f44 10 DigitalOut led[]= {LED1, LED2, LED3, LED4};
AkinoriHashimoto 0:fa077fb53f44 11 mySerial pc(USBTX, USBRX);
AkinoriHashimoto 0:fa077fb53f44 12
AkinoriHashimoto 0:fa077fb53f44 13 void rcvIrq(char id)
AkinoriHashimoto 0:fa077fb53f44 14 {
AkinoriHashimoto 0:fa077fb53f44 15 led[0]= !led[0];
AkinoriHashimoto 0:fa077fb53f44 16 if('2'<=id && id<='4') { // LED2 ~ 4
AkinoriHashimoto 0:fa077fb53f44 17 id -= '1';
AkinoriHashimoto 0:fa077fb53f44 18 led[id]= !led[id];
AkinoriHashimoto 0:fa077fb53f44 19 }
AkinoriHashimoto 0:fa077fb53f44 20 return;
AkinoriHashimoto 0:fa077fb53f44 21 }
AkinoriHashimoto 0:fa077fb53f44 22
AkinoriHashimoto 0:fa077fb53f44 23 int main()
AkinoriHashimoto 0:fa077fb53f44 24 {
AkinoriHashimoto 0:fa077fb53f44 25 pc.init();
AkinoriHashimoto 0:fa077fb53f44 26 pc.setAttachRx(rcvIrq);
AkinoriHashimoto 0:fa077fb53f44 27 while(true) {
AkinoriHashimoto 0:fa077fb53f44 28 pc.printf("@");
AkinoriHashimoto 0:fa077fb53f44 29 pc.sendLine(pc.get());
AkinoriHashimoto 0:fa077fb53f44 30 wait(10);
AkinoriHashimoto 0:fa077fb53f44 31 }
AkinoriHashimoto 0:fa077fb53f44 32 }
AkinoriHashimoto 0:fa077fb53f44 33 // EOF
AkinoriHashimoto 0:fa077fb53f44 34 * @endcode
AkinoriHashimoto 0:fa077fb53f44 35 */
AkinoriHashimoto 0:fa077fb53f44 36
AkinoriHashimoto 0:fa077fb53f44 37 #pragma once
AkinoriHashimoto 0:fa077fb53f44 38
AkinoriHashimoto 0:fa077fb53f44 39 #include "mbed.h"
AkinoriHashimoto 0:fa077fb53f44 40 #include <string>
AkinoriHashimoto 0:fa077fb53f44 41 #include "RingBuffer.h"
AkinoriHashimoto 0:fa077fb53f44 42
AkinoriHashimoto 0:fa077fb53f44 43 /** Serial communicate class with Tx & Rx ringbuf.
AkinoriHashimoto 0:fa077fb53f44 44 *
AkinoriHashimoto 0:fa077fb53f44 45 */
AkinoriHashimoto 0:fa077fb53f44 46 class mySerial : public Stream
AkinoriHashimoto 0:fa077fb53f44 47 {
AkinoriHashimoto 0:fa077fb53f44 48 public:
AkinoriHashimoto 0:fa077fb53f44 49 /** Create Serial port to mySerial.
AkinoriHashimoto 0:fa077fb53f44 50 * @param TX, RX; Serial port.
AkinoriHashimoto 0:fa077fb53f44 51 */
AkinoriHashimoto 0:fa077fb53f44 52 mySerial(PinName tx, PinName rx);
AkinoriHashimoto 0:fa077fb53f44 53
AkinoriHashimoto 0:fa077fb53f44 54 /** init
AkinoriHashimoto 0:fa077fb53f44 55 * @param baud-rate; Baud rate (bps). 9600, 38,400, 115,200, 230,400
AkinoriHashimoto 0:fa077fb53f44 56 * @param bit, parity, stop; Default: 1Stopbit, NoneParity, 1StopBit.
AkinoriHashimoto 0:fa077fb53f44 57 * -- parity select; N(None), O(Odd), E(Even).
AkinoriHashimoto 0:fa077fb53f44 58 * @param CRLN; true -> CR&LN (\r\n), false -> CR only (\r).
AkinoriHashimoto 0:fa077fb53f44 59 */
AkinoriHashimoto 0:fa077fb53f44 60 void init(int baudrate= 115200, int bit=8, int parity=SerialBase::None, int stop=1, bool CRLN=true);
AkinoriHashimoto 0:fa077fb53f44 61
AkinoriHashimoto 3:3658415b257b 62 /** add rxFuncPtr to AttachRxIrq.
AkinoriHashimoto 3:3658415b257b 63 * @param (*fptr)(char chr)
AkinoriHashimoto 3:3658415b257b 64 */
AkinoriHashimoto 0:fa077fb53f44 65 void setAttachRx(void (*fptr)(char));
AkinoriHashimoto 0:fa077fb53f44 66
AkinoriHashimoto 3:3658415b257b 67 /** check Buffer
AkinoriHashimoto 3:3658415b257b 68 * @return bool; true->readable.
AkinoriHashimoto 3:3658415b257b 69 */
AkinoriHashimoto 0:fa077fb53f44 70 bool readable();
AkinoriHashimoto 3:3658415b257b 71
AkinoriHashimoto 3:3658415b257b 72 /** get string in Rx-RingBuffer.
AkinoriHashimoto 3:3658415b257b 73 * return string; all chars in ringBuffer.
AkinoriHashimoto 3:3658415b257b 74 */
AkinoriHashimoto 0:fa077fb53f44 75 string get();
AkinoriHashimoto 3:3658415b257b 76
AkinoriHashimoto 3:3658415b257b 77 /** get string 1 line in Rx-RingBuffer.
AkinoriHashimoto 3:3658415b257b 78 * 1 line is string until CR (or CR&LN <- cf.init()).
AkinoriHashimoto 3:3658415b257b 79 * @return string; 1 line chars.
AkinoriHashimoto 3:3658415b257b 80 */
AkinoriHashimoto 0:fa077fb53f44 81 string getLine();
AkinoriHashimoto 0:fa077fb53f44 82
AkinoriHashimoto 3:3658415b257b 83 /** send Line with add CR lineEnd.
AkinoriHashimoto 3:3658415b257b 84 * @param str; strings
AkinoriHashimoto 3:3658415b257b 85 * @param addCR; true -> add CR str's end.
AkinoriHashimoto 3:3658415b257b 86 */
AkinoriHashimoto 0:fa077fb53f44 87 void sendLine(string str, bool addCR=true);
AkinoriHashimoto 0:fa077fb53f44 88
AkinoriHashimoto 0:fa077fb53f44 89 private:
AkinoriHashimoto 0:fa077fb53f44 90 RawSerial serial;
AkinoriHashimoto 0:fa077fb53f44 91 string CR;
AkinoriHashimoto 0:fa077fb53f44 92 RingBuffer ringBufRx;
AkinoriHashimoto 0:fa077fb53f44 93 RingBuffer ringBufTx;
AkinoriHashimoto 0:fa077fb53f44 94
AkinoriHashimoto 0:fa077fb53f44 95 // RxIrq function pointer as out of class.
AkinoriHashimoto 0:fa077fb53f44 96 void (*fptrRxIrq)(char);
AkinoriHashimoto 0:fa077fb53f44 97
AkinoriHashimoto 0:fa077fb53f44 98 // copy buf of rx to private string.
AkinoriHashimoto 0:fa077fb53f44 99 void _read(); // copy buf of rx to private string.
AkinoriHashimoto 0:fa077fb53f44 100 void _readIrq(); // copy buf of rx to private string.
AkinoriHashimoto 0:fa077fb53f44 101
AkinoriHashimoto 0:fa077fb53f44 102 // virtual func for printf() in Stream-class.
AkinoriHashimoto 0:fa077fb53f44 103 virtual int _putc(int val);
AkinoriHashimoto 0:fa077fb53f44 104 virtual int _getc();
AkinoriHashimoto 0:fa077fb53f44 105 void putcIrq();
AkinoriHashimoto 0:fa077fb53f44 106 };
AkinoriHashimoto 0:fa077fb53f44 107
AkinoriHashimoto 0:fa077fb53f44 108 // EOF