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)

TextLCD_ostream.h

Committer:
jk1lot
Date:
2016-06-18
Revision:
1:e46139d4b8ba
Parent:
0:4ba062d3d524
Child:
2:eaa44bec9b96

File content as of revision 1:e46139d4b8ba:

#ifndef TEXTLCD_OSTREAM_H
#define TEXTLCD_OSTREAM_H

#include "TextLCD.h"
#include<iostream>
#include <streambuf>

class lcd_streambuf : public std::streambuf {
public:
    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;
};

/** 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:
    /// @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:
    lcd_streambuf lcd_buf;
};
#endif