Better Serial C++ class library with interrupt
Dependents: Dumb_box_rev2 mbed_GPSproject
iSerial class library can easy be used just by swapping Serial declarations.
API
iSerial | |
Examples
#include "mbed.h" #include "iSerial.h" char s[] = "12345678901234567890123456789012345678901234567890"; int main() { iSerial pc(USBTX,USBRX); char* c = s; while(1) { pc.puts(s); while(pc.readable()){ int ret = pc.getc(); if(ret) *c++ = ret; if(c >= s+sizeof(s)-1) c = s; } wait(1); } }
Example 2
Comparing speed of ordinary Serial functions
#include "mbed.h" #include "iSerial.h" char s[] = "12345678901234567890123456789012345678901234567890"; // 50 bytes int main() { Timer t; Serial pc(USBTX,USBRX); pc.baud(921600); t.reset(); t.start(); for(int i=0; i<30; i++) { pc.printf(s); wait(0.001); } t.stop(); int t1 = t.read_us(); pc.puts("\n\n\n"); iSerial ipc(USBTX,USBRX,NULL,1000,100); // try NOT to set the buffer over flowed // iSerial ipc(USBTX,USBRX); // set as default = 100, 100 ipc.baud(921600); t.reset(); t.start(); for(int i=0; i<30; i++) { ipc.printf(s); wait(0.001); } t.stop(); int t2 = t.read_us(); ipc.printf("\n\n\nThe time %d, %d [us] was taken.\n", t1, t2); ipc.puts("End."); ipc.flush(); }
iSerial.h@4:b38ef9675d39, 2012-09-03 (annotated)
- Committer:
- ykuroda
- Date:
- Mon Sep 03 09:15:34 2012 +0000
- Revision:
- 4:b38ef9675d39
- Parent:
- 3:d5353b68105f
- Child:
- 5:d83fc550ccbc
printf using interrupt is implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 3:d5353b68105f | 1 | // |
ykuroda | 3:d5353b68105f | 2 | // iSerial.h ... Serial Driver with Interrupt Rec/Send |
ykuroda | 3:d5353b68105f | 3 | // |
ykuroda | 3:d5353b68105f | 4 | // Copyright 2012 Yoji KURODA |
ykuroda | 3:d5353b68105f | 5 | // |
ykuroda | 3:d5353b68105f | 6 | // 2009.11.13 ... Originally written by Y.Kuroda for Renesas H83664 |
ykuroda | 3:d5353b68105f | 7 | // 2012.08.31 ... Code convert for mbed in C++ |
ykuroda | 3:d5353b68105f | 8 | // |
ykuroda | 3:d5353b68105f | 9 | #ifndef _ISERIAL_H |
ykuroda | 3:d5353b68105f | 10 | #define _ISERIAL_H |
ykuroda | 3:d5353b68105f | 11 | |
ykuroda | 3:d5353b68105f | 12 | #include <string.h> |
ykuroda | 3:d5353b68105f | 13 | #include "RingBuffer.h" |
ykuroda | 3:d5353b68105f | 14 | |
ykuroda | 3:d5353b68105f | 15 | |
ykuroda | 3:d5353b68105f | 16 | class iSerial : public Serial { |
ykuroda | 3:d5353b68105f | 17 | protected: |
ykuroda | 3:d5353b68105f | 18 | |
ykuroda | 4:b38ef9675d39 | 19 | const int txbufsize; |
ykuroda | 4:b38ef9675d39 | 20 | const int rxbufsize; |
ykuroda | 3:d5353b68105f | 21 | RingBuffer txbuf; |
ykuroda | 3:d5353b68105f | 22 | RingBuffer rxbuf; |
ykuroda | 4:b38ef9675d39 | 23 | char* str; |
ykuroda | 3:d5353b68105f | 24 | |
ykuroda | 3:d5353b68105f | 25 | void tx_handler(void); |
ykuroda | 3:d5353b68105f | 26 | void rx_handler(void); |
ykuroda | 3:d5353b68105f | 27 | |
ykuroda | 3:d5353b68105f | 28 | public: |
ykuroda | 3:d5353b68105f | 29 | |
ykuroda | 3:d5353b68105f | 30 | enum TERMINL_CODES { CR=0x0D, LF=0x0A }; |
ykuroda | 3:d5353b68105f | 31 | |
ykuroda | 3:d5353b68105f | 32 | iSerial(PinName _tx, PinName _rx, const char *_name=NULL, int _txbufsize=100, int _rxbufsize=100); |
ykuroda | 4:b38ef9675d39 | 33 | virtual ~iSerial(); |
ykuroda | 3:d5353b68105f | 34 | |
ykuroda | 3:d5353b68105f | 35 | short int putstr(const char* s); |
ykuroda | 3:d5353b68105f | 36 | |
ykuroda | 4:b38ef9675d39 | 37 | int readable(void); |
ykuroda | 4:b38ef9675d39 | 38 | int getc(void); |
ykuroda | 4:b38ef9675d39 | 39 | void putc(short ch); |
ykuroda | 3:d5353b68105f | 40 | short int puts(const char* s); |
ykuroda | 4:b38ef9675d39 | 41 | //void printf(); |
ykuroda | 4:b38ef9675d39 | 42 | char* printf(const char* format, ...); |
ykuroda | 3:d5353b68105f | 43 | }; |
ykuroda | 3:d5353b68105f | 44 | |
ykuroda | 3:d5353b68105f | 45 | |
ykuroda | 3:d5353b68105f | 46 | #endif /* _SCI_H */ |