SerialStream fork implementing sigio, attach, readable, and writable

Files at this revision

API Documentation at this revision

Comitter:
jasperswallen
Date:
Thu Dec 10 08:54:11 2020 +0000
Parent:
2:3736580f2dbe
Commit message:
add sigio, attach, readable, and writable

Changed in this revision

SerialStream.h Show annotated file Show diff for this revision Revisions of this file
--- a/SerialStream.h	Thu Nov 12 22:25:27 2020 -0800
+++ b/SerialStream.h	Thu Dec 10 08:54:11 2020 +0000
@@ -1,7 +1,8 @@
 #ifndef SERIALSTREAM_H
 #define SERIALSTREAM_H
 
-#include <platform/Stream.h>
+#include <Stream.h>
+#include <mbed.h>
 
 /**
  * SerialStream
@@ -13,41 +14,66 @@
  * - Pass it to code that expects a Stream to print things on.
  *
  */
-template<class SerialClass>
-class SerialStream : public Stream
+template <class SerialClass> class SerialStream : public Stream
 {
-	SerialClass & serialClass;
-
-public:
+    SerialClass &serialClass;
 
-	/**
-	 * Create a SerialStream from a serial port.
-	 * @param _serialClass BufferedSerial or UnbufferedSerial instance
-	 * @param name The name of the stream associated with this serial port (optional)
-	 */
-	SerialStream(SerialClass & _serialClass, const char *name = nullptr):
-	Stream(name),
-	serialClass(_serialClass)
-	{
-	}
+  public:
+    /**
+     * Create a SerialStream from a serial port.
+     * @param _serialClass BufferedSerial or UnbufferedSerial instance
+     * @param name The name of the stream associated with this serial port
+     * (optional)
+     */
+    SerialStream(SerialClass &_serialClass, const char *name = nullptr)
+        : Stream(name), serialClass(_serialClass)
+    {
+    }
+
+    // override Stream::read() and write() to call serial class directly.
+    // This avoids the overhead of feeding in individual characters.
+    virtual ssize_t write(const void *buffer, size_t length)
+    {
+        return serialClass.write(buffer, length);
+    }
+
+    virtual ssize_t read(void *buffer, size_t length)
+    {
+        return serialClass.read(buffer, length);
+    }
 
-	// override Stream::read() and write() to call serial class directly.
-	// This avoids the overhead of feeding in individual characters.
-	virtual ssize_t write(const void *buffer, size_t length)
-	{
-		return serialClass.write(buffer, length);
-	}
+    void sigio(Callback<void()> func)
+    {
+        serialClass.sigio(func);
+    }
+
+    void attach(Callback<void()> func, SerialBase::IrqType type = RxIrq)
+    {
+        serialClass.attach(func, type);
+    }
+
+    bool writable() const
+    {
+        return serialClass.writable();
+    }
 
-	virtual ssize_t read(void *buffer, size_t length)
-	{
-		return serialClass.read(buffer, length);
-	}
+    bool readable() const
+    {
+        return serialClass.readable();
+    }
 
-protected:
-	// Dummy implementations -- these will never be called because we override write() and read() instead.
-	// but we have to override them since they're pure virtual.
-	virtual int _putc(int c) { return 0; }
-	virtual int _getc() { return 0; }
+  protected:
+    // Dummy implementations -- these will never be called because we override
+    // write() and read() instead. but we have to override them since they're
+    // pure virtual.
+    virtual int _putc(int c)
+    {
+        return 0;
+    }
+    virtual int _getc()
+    {
+        return 0;
+    }
 };
 
-#endif //SERIALSTREAM_H
+#endif // SERIALSTREAM_H