SerialStream fork implementing sigio, attach, readable, and writable
Embed:
(wiki syntax)
Show/hide line numbers
SerialStream.h
00001 #ifndef SERIALSTREAM_H 00002 #define SERIALSTREAM_H 00003 00004 #include <Stream.h> 00005 #include <mbed.h> 00006 00007 /** 00008 * SerialStream 00009 * Bringing MBed serial ports back like it's 1999... or at least 2019. 00010 * 00011 * This class adapts an MBed 6.0 serial port class into a Stream instance. 00012 * This lets you do two useful things with it: 00013 * - Call printf() and scanf() on it 00014 * - Pass it to code that expects a Stream to print things on. 00015 * 00016 */ 00017 template <class SerialClass> class SerialStream : public Stream 00018 { 00019 SerialClass &serialClass; 00020 00021 public: 00022 /** 00023 * Create a SerialStream from a serial port. 00024 * @param _serialClass BufferedSerial or UnbufferedSerial instance 00025 * @param name The name of the stream associated with this serial port 00026 * (optional) 00027 */ 00028 SerialStream(SerialClass &_serialClass, const char *name = nullptr) 00029 : Stream(name), serialClass(_serialClass) 00030 { 00031 } 00032 00033 // override Stream::read() and write() to call serial class directly. 00034 // This avoids the overhead of feeding in individual characters. 00035 virtual ssize_t write(const void *buffer, size_t length) 00036 { 00037 return serialClass.write(buffer, length); 00038 } 00039 00040 virtual ssize_t read(void *buffer, size_t length) 00041 { 00042 return serialClass.read(buffer, length); 00043 } 00044 00045 void sigio(Callback<void()> func) 00046 { 00047 serialClass.sigio(func); 00048 } 00049 00050 void attach(Callback<void()> func, SerialBase::IrqType type = RxIrq) 00051 { 00052 serialClass.attach(func, type); 00053 } 00054 00055 bool writable() const 00056 { 00057 return serialClass.writable(); 00058 } 00059 00060 bool readable() const 00061 { 00062 return serialClass.readable(); 00063 } 00064 00065 protected: 00066 // Dummy implementations -- these will never be called because we override 00067 // write() and read() instead. but we have to override them since they're 00068 // pure virtual. 00069 virtual int _putc(int c) 00070 { 00071 return 0; 00072 } 00073 virtual int _getc() 00074 { 00075 return 0; 00076 } 00077 }; 00078 00079 #endif // SERIALSTREAM_H
Generated on Tue Jul 26 2022 01:22:37 by 1.7.2