Olivier Smeesters / ExtTextLCD

Dependents:   DtmfKit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD.h Source File

TextLCD.h

00001 /* Extended TextLCD library.
00002  *
00003  * Based on the mbed TextLCD Library, for a 4-bit LCD based on HD44780
00004  * Copyright (c) 2007-2010, sford, http://mbed.org
00005  *
00006  * Original license:
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 #ifndef EXT_TEXT_LCD_TEXTLCD_H
00027 #define EXT_TEXT_LCD_TEXTLCD_H
00028 
00029 #include "mbed.h"
00030 
00031 namespace ext_text_lcd {
00032 
00033 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
00034  *
00035  * Currently supports 16x2, 20x2 and 20x4 panels
00036  *
00037  * @code
00038  * #include "mbed.h"
00039  * #include "TextLCD.h"
00040  * 
00041  * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7 on individual pins
00042  * TextLCD lcd(p10, p12, Port2, 2); // rs, e, d4-d7 on P2.2-P2.5
00043  * 
00044  * int main() {
00045  *     lcd.printf("Hello World!\n");
00046  * }
00047  * @endcode
00048  */
00049 class TextLCD : public Stream {
00050 public:
00051     /** LCD panel format */
00052     class LCDType {
00053     public: 
00054         int columns() const;
00055         int rows() const;
00056         int address(int column, int row) const;
00057                 
00058         typedef int (*MakeAddrFunc)(const LCDType &, int, int);
00059         
00060     private:
00061         LCDType(int columns, int rows, MakeAddrFunc makeAddr) : 
00062             _columns(columns), _rows(rows), _makeAddr(makeAddr)
00063         { }
00064 
00065         friend class TextLCD;
00066                 
00067         int _columns;
00068         int _rows;
00069         MakeAddrFunc _makeAddr;
00070     };
00071     
00072     static const LCDType LCD16x2;
00073     static const LCDType LCD16x2B;
00074     static const LCDType LCD20x2;
00075     static const LCDType LCD20x4;
00076 
00077     /** Create a TextLCD interface
00078      *
00079      * @param rs    Instruction/data control line
00080      * @param e     Enable line (clock)
00081      * @param d4-d7 Data lines for using as a 4-bit interface
00082      * @param type  Sets the panel size/addressing mode (default = LCD16x2)
00083      */
00084     TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, const LCDType &type = LCD16x2);
00085 
00086     /** Create a TextLCD interface
00087      *
00088      * @param rs    Instruction/data control line
00089      * @param e     Enable line (clock)
00090      * @param dport GPIO port with all data lines
00091      * @param dlsb  Index of the lowest data bit in the GPIO port
00092      * @param type  Sets the panel size/addressing mode (default = LCD16x2)
00093      */
00094     TextLCD(PinName rs, PinName e, PortName dport, std::size_t dlsb, const LCDType &type = LCD16x2);
00095     
00096 #if DOXYGEN_ONLY
00097     /** Write a character to the LCD
00098      *
00099      * @param c The character to write to the display
00100      */
00101     int putc(int c);
00102 
00103     /** Write a formated string to the LCD
00104      *
00105      * @param format A printf-style format string, followed by the
00106      *               variables to use in formating the string.
00107      */
00108     int printf(const char* format, ...);
00109 #endif
00110 
00111     /** Locate to a screen column and row
00112      *
00113      * @param column  The horizontal position from the left, indexed from 0
00114      * @param row     The vertical position from the top, indexed from 0
00115      */
00116     void locate(int column, int row);
00117 
00118     /** Clear the screen and locate to 0,0 */
00119     void cls();
00120 
00121     int columns() { return _type.columns(); }
00122     int rows() const { return _type.rows(); }
00123 
00124     enum DisplayControl {
00125         DisplayOn = 0x04,
00126         DisplayOff = 0
00127     };
00128     enum CursorDisplayControl {
00129         CursorOn = 0x02,
00130         CursorOff = 0
00131     };
00132     enum CursorStyle {
00133         BlinkingCursor = 0x01,
00134         UnderlineCursor = 0
00135     }; 
00136     void setDisplayControl(DisplayControl d = DisplayOn, CursorDisplayControl c = CursorOff, CursorStyle b = UnderlineCursor);
00137 
00138     void writeToCGMem(unsigned addr, const char *buffer, std::size_t size);
00139 
00140 protected:
00141     // Stream implementation functions
00142     virtual int _putc(int value);
00143     virtual int _getc();
00144 
00145     int address(int column, int row) const { return _type.address(column, row); }
00146 
00147     void writeByte(int value);
00148     void writeCommand(int command);
00149     void writeData(int data);
00150 
00151 private:
00152     void init();
00153     
00154     struct LcdOutput {
00155         virtual void writeNibble(int value) = 0;
00156         virtual ~LcdOutput() { }
00157     };
00158     
00159     struct BusOutput : public LcdOutput {
00160         BusOutput(PinName d4, PinName d5, PinName d6, PinName d7) : _bus(d4, d5, d6, d7) { }
00161         virtual void writeNibble(int value);
00162     private:
00163         BusOut _bus;
00164     };
00165     
00166     struct PortOutput : public LcdOutput {
00167         PortOutput(PortName dport, std::size_t dlsb) : _port(dport, 0x0f << dlsb), _shift(dlsb) { }
00168         virtual void writeNibble(int value);
00169         
00170     private:
00171         PortOut _port;
00172         std::size_t _shift;
00173     };
00174     
00175     DigitalOut _rs, _e;
00176     LcdOutput *_output;
00177     LCDType _type;
00178     int _column;
00179     int _row;    
00180 };
00181 
00182 inline int TextLCD::LCDType::columns() const { 
00183     return this->_columns; 
00184 }
00185 inline int TextLCD::LCDType::rows() const { 
00186     return this->_rows; 
00187 }
00188 inline int TextLCD::LCDType::address(int column, int row) const { 
00189     return this->_makeAddr(*this, column, row); 
00190 }
00191 
00192 } // ext_text_lcd namespace
00193 
00194 #endif