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)

Committer:
jk1lot
Date:
Wed Jun 22 08:28:51 2016 +0000
Revision:
3:183bcab4951b
Parent:
2:eaa44bec9b96
Child:
4:3a1291526e04
delete lcd_streambuf default constractor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jk1lot 0:4ba062d3d524 1 #ifndef TEXTLCD_OSTREAM_H
jk1lot 0:4ba062d3d524 2 #define TEXTLCD_OSTREAM_H
jk1lot 0:4ba062d3d524 3
jk1lot 0:4ba062d3d524 4 #include "TextLCD.h"
jk1lot 0:4ba062d3d524 5 #include<iostream>
jk1lot 0:4ba062d3d524 6 #include <streambuf>
jk1lot 0:4ba062d3d524 7
jk1lot 0:4ba062d3d524 8 class lcd_streambuf : public std::streambuf {
jk1lot 0:4ba062d3d524 9 public:
jk1lot 1:e46139d4b8ba 10 lcd_streambuf(TextLCD_Base *p) : lcd(p) {}
jk1lot 1:e46139d4b8ba 11 TextLCD_Base *getlcd() {return lcd;}
jk1lot 1:e46139d4b8ba 12
jk1lot 1:e46139d4b8ba 13 virtual int_type overflow( int_type c = EOF );
jk1lot 0:4ba062d3d524 14 private:
jk1lot 1:e46139d4b8ba 15 TextLCD_Base *lcd;
jk1lot 0:4ba062d3d524 16 };
jk1lot 0:4ba062d3d524 17
jk1lot 2:eaa44bec9b96 18 /** ostream wrapper for TextLCD
jk1lot 1:e46139d4b8ba 19 @code
jk1lot 1:e46139d4b8ba 20 #include "mbed.h"
jk1lot 1:e46139d4b8ba 21 #include "TextLCD_ostream.h"
jk1lot 1:e46139d4b8ba 22
jk1lot 1:e46139d4b8ba 23 I2C i2c(p28,p27); // SDA, SCL
jk1lot 1:e46139d4b8ba 24 TextLCD_I2C_N lcd(&i2c, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3);
jk1lot 1:e46139d4b8ba 25 lcd_ostream lcdstream(&lcd);
jk1lot 1:e46139d4b8ba 26
jk1lot 1:e46139d4b8ba 27 int main() {
jk1lot 1:e46139d4b8ba 28 using namespace std;
jk1lot 1:e46139d4b8ba 29 lcd.setContrast(32);
jk1lot 1:e46139d4b8ba 30 lcdstream.cls() << "Hello world" << endl << "LCD ostream";
jk1lot 1:e46139d4b8ba 31 }
jk1lot 1:e46139d4b8ba 32 @endcode
jk1lot 1:e46139d4b8ba 33 */
jk1lot 0:4ba062d3d524 34 class lcd_ostream : public std::ostream {
jk1lot 0:4ba062d3d524 35 public:
jk1lot 1:e46139d4b8ba 36 /// @param p pointer to object of TextLCD_Base and derived from it
jk1lot 3:183bcab4951b 37 lcd_ostream(TextLCD_Base *p)
jk1lot 3:183bcab4951b 38 : std::ostream(&lcd_buf),lcd(p),lcd_buf(p) {}
jk1lot 1:e46139d4b8ba 39 /// set cursor position.
jk1lot 1:e46139d4b8ba 40 /// same as TextLCD::locate(), except return value type
jk1lot 0:4ba062d3d524 41 lcd_ostream& locate(int column, int row);
jk1lot 1:e46139d4b8ba 42 /// clear screen
jk1lot 1:e46139d4b8ba 43 /// same as TextLCD::cls(), except return value type
jk1lot 0:4ba062d3d524 44 lcd_ostream& cls();
jk1lot 1:e46139d4b8ba 45 protected:
jk1lot 1:e46139d4b8ba 46 TextLCD_Base *lcd;
jk1lot 0:4ba062d3d524 47 private:
jk1lot 1:e46139d4b8ba 48 lcd_streambuf lcd_buf;
jk1lot 0:4ba062d3d524 49 };
jk1lot 0:4ba062d3d524 50 #endif