Improved version of Simon Ford's TextDisplay library, with addressing and timing fixes. Supports up to 20x4 text displays.
Revision 0:2c5bba968d7c, committed 2010-02-14
- Comitter:
- bikeNomad
- Date:
- Sun Feb 14 00:28:08 2010 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 2c5bba968d7c Terminal.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.cpp Sun Feb 14 00:28:08 2010 +0000 @@ -0,0 +1,73 @@ +/* mbed Terminal TextDisplay Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + */ + +#include "Terminal.h" + +#include "mbed.h" + +Terminal::Terminal(PinName tx, PinName rx) : _serial(tx, rx) { + cls(); +} + +void Terminal::character(uint16_t column, uint16_t row, int c) { + // Cursor Home <ESC>[{ROW};{COLUMN}H + _serial.printf("\033[%u;%uH%c", row + 1, column + 1, c); +} + +uint16_t Terminal::columns() { + return 80; +} + +uint16_t Terminal::rows() { + return 35; +} + +void Terminal::cls() { + _serial.printf("\033[2J"); +} + +void Terminal::foreground(uint32_t colour) { + + /* Set Attribute Mode <ESC>[{n}m + * - Sets display attribute settings. The following lists standard attributes: + * + * Foreground Colours + * 30 Black + * 31 Red + * 32 Green + * 33 Yellow + * 34 Blue + * 35 Magenta + * 36 Cyan + * 37 White + */ + uint32_t r = (colour >> 23) & 1; + uint32_t g = (colour >> 15) & 1; + uint32_t b = (colour >> 7) & 1; + uint32_t bgr = (b << 2) | (g << 1) | (r << 0); + uint32_t c = 30 + bgr; + _serial.printf("\033[%um", c); +} + +void Terminal::background(uint32_t colour) { + + /* Background Colours + * 40 Black + * 41 Red + * 42 Green + * 43 Yellow + * 44 Blue + * 45 Magenta + * 46 Cyan + * 47 White + */ + uint32_t r = (colour >> 23) & 1; + uint32_t g = (colour >> 15) & 1; + uint32_t b = (colour >> 7) & 1; + uint32_t bgr = (b << 2) | (g << 1) | (r << 0); + uint32_t c = 40 + bgr; + _serial.printf("\033[%um", c); +} +
diff -r 000000000000 -r 2c5bba968d7c Terminal.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.h Sun Feb 14 00:28:08 2010 +0000 @@ -0,0 +1,37 @@ +/* mbed Terminal TextDisplay Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + * + * Modified by Ned Konz + * to add baud rate setting + * + * Implementation of a TextDisplay using a VT100 Terminal and + * escape codes (e.g. Teraterm, Hyperterminal) + */ + +#include "TextDisplay.h" + +#ifndef MBED_TERMINAL_H +#define MBED_TERMINAL_H + +class Terminal : public TextDisplay { +public: + + Terminal(PinName tx, PinName rx); + + virtual void character(uint16_t column, uint16_t row, int c); + virtual uint16_t rows(); + virtual uint16_t columns(); + + virtual void cls(); + virtual void foreground(uint32_t colour); + virtual void background(uint32_t colour); + virtual void baud(uint16_t newbaud) { _serial.baud(newbaud); } + +protected: + + Serial _serial; + +}; + +#endif
diff -r 000000000000 -r 2c5bba968d7c TextDisplay.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextDisplay.cpp Sun Feb 14 00:28:08 2010 +0000 @@ -0,0 +1,57 @@ +/* mbed TextDisplay Display Library Base Class + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + */ + +#include "TextDisplay.h" + +TextDisplay::TextDisplay() { + _row = 0; + _column = 0; +} + +int TextDisplay::_putc(int value) { + if(value == '\n') { + _column = 0; + _row++; + if(_row >= rows()) { + _row = 0; + } + } else { + character(_column, _row, value); + _column++; + if(_column >= columns()) { + _column = 0; + _row++; + if(_row >= rows()) { + _row = 0; + } + } + } + return value; +} + +// crude cls implementation, should generally be overwritten in derived class +void TextDisplay::cls() { + locate(0, 0); + for(int i=0; i<columns()*rows(); i++) { + putc(' '); + } +} + +void TextDisplay::locate(uint16_t column, uint16_t row) { + _column = column; + _row = row; +} + +int TextDisplay::_getc() { + return -1; +} + +void TextDisplay::foreground(uint32_t colour) { + _foreground = colour; +} + +void TextDisplay::background(uint32_t colour) { + _background = colour; +}
diff -r 000000000000 -r 2c5bba968d7c TextDisplay.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextDisplay.h Sun Feb 14 00:28:08 2010 +0000 @@ -0,0 +1,52 @@ +/* mbed TextDisplay Library Base Class + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + * + * A common base class for Text displays + * To port a new display, derive from this class and implement + * the constructor (setup the display), character (put a character + * at a location), rows and columns (number of rows/cols) functions. + * Everything else (locate, printf, putc, cls) will come for free + * + * The model is the display will wrap at the right and bottom, so you can + * keep writing and will always get valid characters. The location is + * maintained internally to the class to make this easy + */ + +#ifndef MBED_TEXTDISPLAY_H +#define MBED_TEXTDISPLAY_H + +#include "mbed.h" + +class TextDisplay : public Stream { +public: + + // functions needing implementation in derived implementation class + TextDisplay(); + virtual void character(uint16_t column, uint16_t row, int c) = 0; + virtual uint16_t rows() = 0; + virtual uint16_t columns() = 0; + + // functions that come for free, but can be overwritten + virtual void cls(); + virtual void locate(uint16_t column, uint16_t row); + virtual void foreground(uint32_t colour); + virtual void background(uint32_t colour); + // putc (from Stream) + // printf (from Stream) + +protected: + + virtual int _putc(int value); + virtual int _getc(); + + // character location + uint16_t _column; + uint16_t _row; + + // colours + uint32_t _foreground; + uint32_t _background; +}; + +#endif
diff -r 000000000000 -r 2c5bba968d7c TextDisplays.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextDisplays.h Sun Feb 14 00:28:08 2010 +0000 @@ -0,0 +1,20 @@ +/* mbed Text Displays Collection Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + * + * A library for providing a common base class for Text + * displays, to provide commonality and simplify porting of new displays + */ + +#ifndef MBED_TEXT_DISPLAYS_H +#define MBED_TEXT_DISPLAYS_H + +// Display Base Class +#include "TextDisplay.h" + +// Text Displays +#include "TextLCD.h" +#include "Terminal.h" + + +#endif
diff -r 000000000000 -r 2c5bba968d7c TextLCD.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.cpp Sun Feb 14 00:28:08 2010 +0000 @@ -0,0 +1,161 @@ +/* mbed TextLCD Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + * + * Modified by Ned Konz to provide better support for 4-line LCDs and ones with other controller chips. + */ + +#include "TextLCD.h" +#include "mbed.h" + +/* + * useful info found at http://www.a-netz.de/lcd.en.php + * + * Initialisation + * ============== + * + * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state + * + * - wait approximately 15 ms so the display is ready to execute commands + * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). + * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command. + * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. + * - Execute the "clear display" command + * + * Timing + * ====== + * + * Nearly all commands transmitted to the display need 40us for execution. + * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" + * These commands need 1.64ms for execution. These timings are valid for all displays working with an + * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you + * can use the busy flag to test if the display is ready to accept the next command. + * + * _e is kept low except when being used. + * _rw is kept 0 (write) apart from actions that use it differently + * _rs is set by the data/command writes + */ + +TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, + PinName d2, PinName d3, uint16_t rows, uint16_t cols) : _rw(rw), _rs(rs), + _e(e), _d(d0, d1, d2, d3), _rows(rows), _columns(cols) { + + _rw = 0; + wait_us(1); // min. 100nsec delay + _e = 0; + _rs = 0; // command mode + _d.output(); + + reset(); + cls(); +} + +void TextLCD::reset() { + wait_ms(15); + // e is low at this point, as is rw. + // 2. Send 0x3 and wait 150 ms (will stay in 8-bit mode if already there) + writeHalfByte(0x3); + wait_ms(5); + // 3. Send 0x3 and wait 150 ms (will go to 8-bit mode if was in 4-bit without any garbage nibble) + writeHalfByte(0x3); + wait_ms(5); + // 4. Send 0x3 and wait 250 ms (will go to 8-bit mode even if garbage nibble was previously received) + writeHalfByte(0x3); + wait_ms(5); + // 5. Send 0x2 and wait 200 ms (should go to 4-bit mode now) + writeHalfByte(0x2); + wait_ms(5); + // 7. Send LCD setup sequence (eg 0x2, 0x8 (=0x28), 0x0, 0x8 (=0x08), etc.) + writeCommand(0x28); // Function set 001 BW N F - - + wait_ms(15); + + writeCommand(0x08); // display off, cursor invisible + wait_ms(15); + + writeCommand(0x01); + wait_ms(15); // 1.64ms command + + writeCommand(0x0C); // display enabled, cursor invisible + wait_ms(15); + + writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes + wait_ms(15); + + locate(0,0); +} + +// memory starts at 0x80, and is 0x40 chars long per row +// However, rows 2 and 3 of 4-line displays are actually adjacent to rows 0 and 1. +// 16x4 displays are addressed the same way as 20x4 ones. + +void TextLCD::character(uint16_t column, uint16_t row, int c) { + int address; + address = 0x80 + ((row & ~2) * 0x40) + column; + if (row > 1) + address += 20; + writeCommand(address); + writeData(c); +} + +void TextLCD::writeHalfByte(uint16_t value) { + _e = 1; + wait_us(1); + _d = value & 0x0F; // send data on bus + wait_us(1); // setup time + _e = 0; // strobe + wait_us(1); // hold time +} + +void TextLCD::writeByte(uint16_t value) { + writeHalfByte(value>>4); + writeHalfByte(value); +} + +void TextLCD::writeCommand(uint16_t command) { + _rs = 0; + writeByte(command); + waitUntilDone(); +} + +void TextLCD::writeData(uint16_t data) { + _rs = 1; + writeByte(data); + waitUntilDone(); +} + +void TextLCD::cls() { + writeCommand(0x01); + wait_us(2000); // 1.64ms command + locate(0,0); +} + +// This should be changed to use readAddressAndBusy() when that works. +void TextLCD::waitUntilDone() { + wait_us(60); +} + +// Return the busy/address byte. +// The busy flag is the high bit. +// Not yet working reliably. +uint16_t TextLCD::readAddressAndBusy() { + _d.input(); + _rw = 1; + wait_us(1); + _e = 1; + wait_us(1); + _e = 0; + + uint16_t retval = _d.read() << 4; + + wait_us(1); + _e = 1; + wait_us(1); + _e = 0; + + retval |= _d.read(); + _rw = 0; + + _d.output(); + return retval; +} +
diff -r 000000000000 -r 2c5bba968d7c TextLCD.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.h Sun Feb 14 00:28:08 2010 +0000 @@ -0,0 +1,37 @@ +/* mbed TextLCD Library Base Class + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + * Modified by Ned Konz to provide better support for 4-line LCDs and ones with other controller chips. + */ +#include "TextDisplay.h" + +#ifndef MBED_TEXTLCD_H +#define MBED_TEXTLCD_H + +class TextLCD : public TextDisplay { + public: + TextLCD(PinName rs, PinName rw, PinName e, + PinName d0, PinName d1, PinName d2, PinName d3, + uint16_t rows = 2, uint16_t columns = 16); + virtual void character(uint16_t column, uint16_t row, int c); + virtual uint16_t rows() { return _rows; } + virtual uint16_t columns() { return _columns; } + virtual void reset(); + virtual void cls(); + + // locate, putc, printf come from parent class + + protected: + void writeByte(uint16_t value); + void writeHalfByte(uint16_t value); + void writeCommand(uint16_t command); + void writeData(uint16_t data); + uint16_t readAddressAndBusy(); + void waitUntilDone(); + + DigitalOut _rw, _rs, _e; + BusInOut _d; + uint16_t _rows, _columns; +}; + +#endif