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@2:eaa44bec9b96, 2016-06-19 (annotated)
- Committer:
- jk1lot
- Date:
- Sun Jun 19 01:10:35 2016 +0000
- Revision:
- 2:eaa44bec9b96
- Parent:
- 1:e46139d4b8ba
- Child:
- 3:183bcab4951b
collect miss spell
Who changed what in which revision?
User | Revision | Line number | New 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() : lcd(NULL) {} |
jk1lot | 1:e46139d4b8ba | 11 | lcd_streambuf(TextLCD_Base *p) : lcd(p) {} |
jk1lot | 1:e46139d4b8ba | 12 | void setlcd(TextLCD_Base *p) {lcd=p;} |
jk1lot | 1:e46139d4b8ba | 13 | TextLCD_Base *getlcd() {return lcd;} |
jk1lot | 1:e46139d4b8ba | 14 | |
jk1lot | 1:e46139d4b8ba | 15 | virtual int_type overflow( int_type c = EOF ); |
jk1lot | 0:4ba062d3d524 | 16 | private: |
jk1lot | 1:e46139d4b8ba | 17 | TextLCD_Base *lcd; |
jk1lot | 0:4ba062d3d524 | 18 | }; |
jk1lot | 0:4ba062d3d524 | 19 | |
jk1lot | 2:eaa44bec9b96 | 20 | /** ostream wrapper for TextLCD |
jk1lot | 1:e46139d4b8ba | 21 | @code |
jk1lot | 1:e46139d4b8ba | 22 | #include "mbed.h" |
jk1lot | 1:e46139d4b8ba | 23 | #include "TextLCD_ostream.h" |
jk1lot | 1:e46139d4b8ba | 24 | |
jk1lot | 1:e46139d4b8ba | 25 | I2C i2c(p28,p27); // SDA, SCL |
jk1lot | 1:e46139d4b8ba | 26 | TextLCD_I2C_N lcd(&i2c, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3); |
jk1lot | 1:e46139d4b8ba | 27 | lcd_ostream lcdstream(&lcd); |
jk1lot | 1:e46139d4b8ba | 28 | |
jk1lot | 1:e46139d4b8ba | 29 | int main() { |
jk1lot | 1:e46139d4b8ba | 30 | using namespace std; |
jk1lot | 1:e46139d4b8ba | 31 | lcd.setContrast(32); |
jk1lot | 1:e46139d4b8ba | 32 | lcdstream.cls() << "Hello world" << endl << "LCD ostream"; |
jk1lot | 1:e46139d4b8ba | 33 | } |
jk1lot | 1:e46139d4b8ba | 34 | @endcode |
jk1lot | 1:e46139d4b8ba | 35 | */ |
jk1lot | 0:4ba062d3d524 | 36 | class lcd_ostream : public std::ostream { |
jk1lot | 0:4ba062d3d524 | 37 | public: |
jk1lot | 1:e46139d4b8ba | 38 | /// @param p pointer to object of TextLCD_Base and derived from it |
jk1lot | 1:e46139d4b8ba | 39 | lcd_ostream(TextLCD_Base *p); |
jk1lot | 1:e46139d4b8ba | 40 | /// set cursor position. |
jk1lot | 1:e46139d4b8ba | 41 | /// same as TextLCD::locate(), except return value type |
jk1lot | 0:4ba062d3d524 | 42 | lcd_ostream& locate(int column, int row); |
jk1lot | 1:e46139d4b8ba | 43 | /// clear screen |
jk1lot | 1:e46139d4b8ba | 44 | /// same as TextLCD::cls(), except return value type |
jk1lot | 0:4ba062d3d524 | 45 | lcd_ostream& cls(); |
jk1lot | 1:e46139d4b8ba | 46 | protected: |
jk1lot | 1:e46139d4b8ba | 47 | TextLCD_Base *lcd; |
jk1lot | 0:4ba062d3d524 | 48 | private: |
jk1lot | 1:e46139d4b8ba | 49 | lcd_streambuf lcd_buf; |
jk1lot | 0:4ba062d3d524 | 50 | }; |
jk1lot | 0:4ba062d3d524 | 51 | #endif |