SerialStream fork implementing sigio, attach, readable, and writable

Committer:
jasperswallen
Date:
Thu Dec 10 08:54:11 2020 +0000
Revision:
3:8c05c0310859
Parent:
2:3736580f2dbe
add sigio, attach, readable, and writable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jamie Smith 0:889cfd2df4cd 1 #ifndef SERIALSTREAM_H
Jamie Smith 0:889cfd2df4cd 2 #define SERIALSTREAM_H
Jamie Smith 0:889cfd2df4cd 3
jasperswallen 3:8c05c0310859 4 #include <Stream.h>
jasperswallen 3:8c05c0310859 5 #include <mbed.h>
Jamie Smith 0:889cfd2df4cd 6
Jamie Smith 0:889cfd2df4cd 7 /**
Jamie Smith 0:889cfd2df4cd 8 * SerialStream
Jamie Smith 0:889cfd2df4cd 9 * Bringing MBed serial ports back like it's 1999... or at least 2019.
Jamie Smith 0:889cfd2df4cd 10 *
Jamie Smith 0:889cfd2df4cd 11 * This class adapts an MBed 6.0 serial port class into a Stream instance.
Jamie Smith 0:889cfd2df4cd 12 * This lets you do two useful things with it:
Jamie Smith 0:889cfd2df4cd 13 * - Call printf() and scanf() on it
Jamie Smith 0:889cfd2df4cd 14 * - Pass it to code that expects a Stream to print things on.
Jamie Smith 0:889cfd2df4cd 15 *
Jamie Smith 0:889cfd2df4cd 16 */
jasperswallen 3:8c05c0310859 17 template <class SerialClass> class SerialStream : public Stream
Jamie Smith 0:889cfd2df4cd 18 {
jasperswallen 3:8c05c0310859 19 SerialClass &serialClass;
Jamie Smith 0:889cfd2df4cd 20
jasperswallen 3:8c05c0310859 21 public:
jasperswallen 3:8c05c0310859 22 /**
jasperswallen 3:8c05c0310859 23 * Create a SerialStream from a serial port.
jasperswallen 3:8c05c0310859 24 * @param _serialClass BufferedSerial or UnbufferedSerial instance
jasperswallen 3:8c05c0310859 25 * @param name The name of the stream associated with this serial port
jasperswallen 3:8c05c0310859 26 * (optional)
jasperswallen 3:8c05c0310859 27 */
jasperswallen 3:8c05c0310859 28 SerialStream(SerialClass &_serialClass, const char *name = nullptr)
jasperswallen 3:8c05c0310859 29 : Stream(name), serialClass(_serialClass)
jasperswallen 3:8c05c0310859 30 {
jasperswallen 3:8c05c0310859 31 }
jasperswallen 3:8c05c0310859 32
jasperswallen 3:8c05c0310859 33 // override Stream::read() and write() to call serial class directly.
jasperswallen 3:8c05c0310859 34 // This avoids the overhead of feeding in individual characters.
jasperswallen 3:8c05c0310859 35 virtual ssize_t write(const void *buffer, size_t length)
jasperswallen 3:8c05c0310859 36 {
jasperswallen 3:8c05c0310859 37 return serialClass.write(buffer, length);
jasperswallen 3:8c05c0310859 38 }
jasperswallen 3:8c05c0310859 39
jasperswallen 3:8c05c0310859 40 virtual ssize_t read(void *buffer, size_t length)
jasperswallen 3:8c05c0310859 41 {
jasperswallen 3:8c05c0310859 42 return serialClass.read(buffer, length);
jasperswallen 3:8c05c0310859 43 }
Jamie Smith 0:889cfd2df4cd 44
jasperswallen 3:8c05c0310859 45 void sigio(Callback<void()> func)
jasperswallen 3:8c05c0310859 46 {
jasperswallen 3:8c05c0310859 47 serialClass.sigio(func);
jasperswallen 3:8c05c0310859 48 }
jasperswallen 3:8c05c0310859 49
jasperswallen 3:8c05c0310859 50 void attach(Callback<void()> func, SerialBase::IrqType type = RxIrq)
jasperswallen 3:8c05c0310859 51 {
jasperswallen 3:8c05c0310859 52 serialClass.attach(func, type);
jasperswallen 3:8c05c0310859 53 }
jasperswallen 3:8c05c0310859 54
jasperswallen 3:8c05c0310859 55 bool writable() const
jasperswallen 3:8c05c0310859 56 {
jasperswallen 3:8c05c0310859 57 return serialClass.writable();
jasperswallen 3:8c05c0310859 58 }
Jamie Smith 0:889cfd2df4cd 59
jasperswallen 3:8c05c0310859 60 bool readable() const
jasperswallen 3:8c05c0310859 61 {
jasperswallen 3:8c05c0310859 62 return serialClass.readable();
jasperswallen 3:8c05c0310859 63 }
Jamie Smith 0:889cfd2df4cd 64
jasperswallen 3:8c05c0310859 65 protected:
jasperswallen 3:8c05c0310859 66 // Dummy implementations -- these will never be called because we override
jasperswallen 3:8c05c0310859 67 // write() and read() instead. but we have to override them since they're
jasperswallen 3:8c05c0310859 68 // pure virtual.
jasperswallen 3:8c05c0310859 69 virtual int _putc(int c)
jasperswallen 3:8c05c0310859 70 {
jasperswallen 3:8c05c0310859 71 return 0;
jasperswallen 3:8c05c0310859 72 }
jasperswallen 3:8c05c0310859 73 virtual int _getc()
jasperswallen 3:8c05c0310859 74 {
jasperswallen 3:8c05c0310859 75 return 0;
jasperswallen 3:8c05c0310859 76 }
Jamie Smith 0:889cfd2df4cd 77 };
Jamie Smith 0:889cfd2df4cd 78
jasperswallen 3:8c05c0310859 79 #endif // SERIALSTREAM_H