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:
Sat Jun 18 14:20:29 2016 +0000
Parent:
0:4ba062d3d524
Child:
2:eaa44bec9b96
Commit message:
1st published version

Changed in this revision

TextLCD_ostream.cpp Show annotated file Show diff for this revision Revisions of this file
TextLCD_ostream.h Show annotated file Show diff for this revision Revisions of this file
--- a/TextLCD_ostream.cpp	Sat Jun 18 08:57:07 2016 +0000
+++ b/TextLCD_ostream.cpp	Sat Jun 18 14:20:29 2016 +0000
@@ -1,30 +1,26 @@
 #include "TextLCD_ostream.h"
 
-int lcd_streambuf::overflow(int c)
+lcd_streambuf::int_type lcd_streambuf::overflow(lcd_streambuf::int_type c)
 {
-    if( c != EOF ) _lcd->putc(c);
+    if( c != EOF ) lcd->putc(c);
     return c;
 }
 
-lcd_ostream::lcd_ostream(TextLCD_Base *lcd) : std::ostream( lcd_buf )
+
+lcd_ostream::lcd_ostream(TextLCD_Base *p) : std::ostream(&lcd_buf)
 {
-    _lcd=lcd;
-    lcd_buf = new lcd_streambuf(lcd);
-}
-
-lcd_ostream::~lcd_ostream()
-{
-    delete lcd_buf;
+    lcd_buf.setlcd(p);
+    lcd=p;
 }
 
 lcd_ostream& lcd_ostream::locate(int column, int row)
 {
-    _lcd->locate(column,row);
+    lcd->locate(column,row);
     return *this;
 }
 
 lcd_ostream& lcd_ostream::cls()
 {
-    _lcd->cls();
+    lcd->cls();
     return *this;
 }
--- a/TextLCD_ostream.h	Sat Jun 18 08:57:07 2016 +0000
+++ b/TextLCD_ostream.h	Sat Jun 18 14:20:29 2016 +0000
@@ -3,27 +3,49 @@
 
 #include "TextLCD.h"
 #include<iostream>
-#include<fstream>
 #include <streambuf>
 
 class lcd_streambuf : public std::streambuf {
 public:
-    lcd_streambuf(TextLCD_Base *lcd) : _lcd(lcd) {}
-    virtual int overflow( int c = EOF );
+    lcd_streambuf() : lcd(NULL) {}
+    lcd_streambuf(TextLCD_Base *p) : lcd(p) {}
+    void setlcd(TextLCD_Base *p) {lcd=p;}
+    TextLCD_Base *getlcd() {return lcd;}
+
+    virtual int_type overflow( int_type c = EOF );
 private:
-    TextLCD_Base *_lcd;
+    TextLCD_Base *lcd;
 };
 
+/** ostream rapper for TextLCD
+@code
+#include "mbed.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);
+
+int main() {
+    using namespace std;
+    lcd.setContrast(32);
+    lcdstream.cls() << "Hello world" << endl << "LCD ostream";
+}
+@endcode
+ */
 class lcd_ostream : public std::ostream {
 public:
-    lcd_ostream(TextLCD_Base *lcd);
-    virtual ~lcd_ostream();
+    /// @param p pointer to object of TextLCD_Base and derived from it
+    lcd_ostream(TextLCD_Base *p);
+    /// set cursor position.
+    /// same as TextLCD::locate(), except return value type
     lcd_ostream& locate(int column, int row);
+    /// clear screen
+    /// same as TextLCD::cls(), except return value type
     lcd_ostream& cls();
+protected:
+    TextLCD_Base *lcd;
 private:
-    TextLCD_Base *_lcd;
-    lcd_streambuf *lcd_buf;
+    lcd_streambuf lcd_buf;
 };
-inline lcd_ostream& cls(lcd_ostream& s) {s.cls(); return s;}  //manipulator
-
 #endif
\ No newline at end of file