Using std::ostream with TextLCD

Dependencies:   ACM1602NI TextLCD

Dependents:   TextLCD_ostream_sample

This is class library inherited std::ostream for TextLCD
Because a large amount of memory is needed, do not work in low memory MPU

Sample program is here. And notebook page is here (sorry notebook page is only in Japanese)

Files at this revision

API Documentation at this revision

Comitter:
jk1lot
Date:
Fri Jul 29 16:13:36 2016 +0000
Parent:
4:3a1291526e04
Child:
6:2494d76a38b0
Commit message:
Change from normal class to template class.; It is necessary to support ACM1602NI.; add STM32-Nucleo support.

Changed in this revision

ACM1602NI.lib Show annotated file Show diff for this revision Revisions of this file
Nucleo_streamfix.cpp Show annotated file Show diff for this revision Revisions of this file
TextLCD_ostream.cpp Show diff for this revision Revisions of this file
TextLCD_ostream.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACM1602NI.lib	Fri Jul 29 16:13:36 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/takuo/code/ACM1602NI/#661827681a12
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Nucleo_streamfix.cpp	Fri Jul 29 16:13:36 2016 +0000
@@ -0,0 +1,27 @@
+#include "mbed.h"
+
+#ifdef TARGET_STM
+#ifdef __MICROLIB /*__CC_ARM*/
+/// To suppress MDK-ARM Linker Error: L6218E: Undefined symbol __fread_bytes_avail (referred from ios.o).
+size_t std::__fread_bytes_avail(void * __restrict ptr,
+                    size_t count, FILE * __restrict stream) //__attribute__((__nonnull__(1,3)))
+{
+        return 0;
+}
+
+
+#include <wchar.h>
+/// To suppress MDK-ARM Linker Error: L6218E: Undefined symbol mbsinit (referred from ios.o).
+int std::mbsinit(const mbstate_t * ps)
+{
+        return -1;
+}
+
+/// To suppress MDK-ARM Linker Error: L6218E: Undefined symbol mbsinit (referred from ios.o).
+wchar_t *std::wmemmove(wchar_t * __restrict s1,
+                      const wchar_t * __restrict s2, size_t n) //__attribute__((__nonnull__(1,2)))
+{
+        return NULL;
+}
+#endif /*__MICROLIB*/ /*__CC_ARM*/
+#endif
--- a/TextLCD_ostream.cpp	Sun Jun 26 08:32:25 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-#include "TextLCD_ostream.h"
-
-lcd_streambuf::int_type lcd_streambuf::overflow(lcd_streambuf::int_type c)
-{
-    if( c != EOF ) lcd->putc(c);
-    return c;
-}
-
-lcd_ostream& lcd_ostream::locate(int column, int row)
-{
-    lcdp->locate(column,row);
-    return *this;
-}
-
-lcd_ostream& lcd_ostream::cls()
-{
-    lcdp->cls();
-    return *this;
-}
--- a/TextLCD_ostream.h	Sun Jun 26 08:32:25 2016 +0000
+++ b/TextLCD_ostream.h	Fri Jul 29 16:13:36 2016 +0000
@@ -1,26 +1,34 @@
 #ifndef TEXTLCD_OSTREAM_H
 #define TEXTLCD_OSTREAM_H
 
-#include "TextLCD.h"
-#include <iostream>
+#include <ostream>
 #include <streambuf>
 
-class lcd_streambuf : public std::streambuf {
+/*
+template <class LCDCLASS>
+class TextLCD_streambuf : public std::streambuf {
 public:
-    lcd_streambuf(TextLCD_Base *p) : lcd(p) {}
-    virtual int_type overflow( int_type c = EOF );
+    TextLCD_streambuf(LCDCLASS *p) : lcdp(p) {}
+    virtual int_type overflow( int_type c = EOF ) {
+        if( c != EOF ) lcdp->putc(c);
+        return c;
+    }
+
 private:
-    TextLCD_Base *lcd;
+    LCDCLASS *lcdp;
 };
+*/
 
-/** ostream wrapper for TextLCD
+/** ostream wrapper for TextLCD.
+LCDCLASS(TextLCD) need to have putc(),locate(),cls() function.
 @code
 #include "mbed.h"
+#include "TextLCD.h"
 #include "TextLCD_ostream.h"
  
 I2C i2c(p28,p27); // SDA, SCL
 TextLCD_I2C_N lcd(&i2c, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3);
-lcd_ostream lcdstream(&lcd);
+TextLCD_ostream<TextLCD_I2C_N> lcdstream(&lcd);
 
 int main() {
     using namespace std;
@@ -29,22 +37,47 @@
 }
 @endcode
  */
-class lcd_ostream : public std::ostream {
+template <class LCDCLASS>
+class TextLCD_ostream : public std::ostream {
 public:
-    /// @param p pointer to object of TextLCD_Base and derived from it
-    lcd_ostream(TextLCD_Base *p)
+    /// @param p pointer to object of LCD class
+    TextLCD_ostream(LCDCLASS *p)
       : std::ostream(&lcd_buf),lcdp(p),lcd_buf(p) {}
-    /// access TextLCD object  
-    TextLCD_Base& lcd() {return *lcdp;}
-    //TextLCD_Base& operator*() const {return *lcd;}
+    /// access LCD object  
+    LCDCLASS& lcd() {return *lcdp;}
     /// set cursor position.
-    /// same as TextLCD::locate(), except return value type
-    lcd_ostream& locate(int column, int row);
+    /// same as LCDCLASS::locate(), except return value type
+    TextLCD_ostream& locate(int column, int row) {
+        lcdp->locate(column,row);
+        return *this;
+    }
     /// clear screen
-    /// same as TextLCD::cls(), except return value type
-    lcd_ostream& cls();
+    /// same as LCDCLASS::cls(), except return value type
+    TextLCD_ostream& cls(){
+        lcdp->cls();
+        return *this;
+    }
 private:
-    TextLCD_Base* const lcdp;
-    lcd_streambuf lcd_buf;
+    LCDCLASS* const lcdp;
+    class TextLCD_streambuf : public std::streambuf {
+    public:
+        TextLCD_streambuf(LCDCLASS *p) : lcdp(p) {}
+        virtual int_type overflow( int_type c = EOF ) {
+            if( c != EOF ) lcdp->putc(c);
+            return c;
+        }
+
+    private:
+        LCDCLASS *lcdp;
+    } lcd_buf;
+//    TextLCD_streambuf<LCDCLASS> lcd_buf;
 };
+
+#ifndef NOTextLCD_h
+//for compatibility to previous version
+#include "TextLCD.h"
+/// specialized version
+typedef TextLCD_ostream<TextLCD_Base> lcd_ostream;
+#endif
+
 #endif
\ No newline at end of file