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)
Diff: TextLCD_ostream.h
- Revision:
- 5:6c1109b4dfb8
- Parent:
- 4:3a1291526e04
- Child:
- 6:2494d76a38b0
--- 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