LCD driver
Dependents: host_mouse lcd_keypad_shield TestDeviceIot
TextLCD.h@1:19be52262a2d, 2019-07-26 (annotated)
- Committer:
- duchonic
- Date:
- Fri Jul 26 03:30:12 2019 +0000
- Revision:
- 1:19be52262a2d
- Parent:
- 0:ec079a141883
added lcd shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duchonic | 1:19be52262a2d | 1 | /* mbed TextLCD Library, for a 4-bit LCD based on HD44780 |
duchonic | 1:19be52262a2d | 2 | * Copyright (c) 2007-2010, sford, http://mbed.org |
duchonic | 1:19be52262a2d | 3 | * |
duchonic | 1:19be52262a2d | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
duchonic | 1:19be52262a2d | 5 | * of this software and associated documentation files (the "Software"), to deal |
duchonic | 1:19be52262a2d | 6 | * in the Software without restriction, including without limitation the rights |
duchonic | 1:19be52262a2d | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
duchonic | 1:19be52262a2d | 8 | * copies of the Software, and to permit persons to whom the Software is |
duchonic | 1:19be52262a2d | 9 | * furnished to do so, subject to the following conditions: |
duchonic | 1:19be52262a2d | 10 | * |
duchonic | 1:19be52262a2d | 11 | * The above copyright notice and this permission notice shall be included in |
duchonic | 1:19be52262a2d | 12 | * all copies or substantial portions of the Software. |
duchonic | 1:19be52262a2d | 13 | * |
duchonic | 1:19be52262a2d | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
duchonic | 1:19be52262a2d | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
duchonic | 1:19be52262a2d | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
duchonic | 1:19be52262a2d | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
duchonic | 1:19be52262a2d | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
duchonic | 1:19be52262a2d | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
duchonic | 1:19be52262a2d | 20 | * THE SOFTWARE. |
pangsk | 0:ec079a141883 | 21 | */ |
duchonic | 1:19be52262a2d | 22 | |
pangsk | 0:ec079a141883 | 23 | #ifndef MBED_TEXTLCD_H |
pangsk | 0:ec079a141883 | 24 | #define MBED_TEXTLCD_H |
pangsk | 0:ec079a141883 | 25 | |
duchonic | 1:19be52262a2d | 26 | #include "mbed.h" |
pangsk | 0:ec079a141883 | 27 | |
duchonic | 1:19be52262a2d | 28 | /** A TextLCD interface for driving 4-bit HD44780-based LCDs |
pangsk | 0:ec079a141883 | 29 | * |
duchonic | 1:19be52262a2d | 30 | * Currently supports 16x2, 20x2 and 20x4 panels |
pangsk | 0:ec079a141883 | 31 | * |
duchonic | 1:19be52262a2d | 32 | * @code |
duchonic | 1:19be52262a2d | 33 | * #include "mbed.h" |
duchonic | 1:19be52262a2d | 34 | * #include "TextLCD.h" |
duchonic | 1:19be52262a2d | 35 | * |
duchonic | 1:19be52262a2d | 36 | * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7 |
duchonic | 1:19be52262a2d | 37 | * |
duchonic | 1:19be52262a2d | 38 | * int main() { |
duchonic | 1:19be52262a2d | 39 | * lcd.printf("Hello World!\n"); |
duchonic | 1:19be52262a2d | 40 | * } |
duchonic | 1:19be52262a2d | 41 | * @endcode |
pangsk | 0:ec079a141883 | 42 | */ |
pangsk | 0:ec079a141883 | 43 | class TextLCD : public Stream { |
duchonic | 1:19be52262a2d | 44 | public: |
pangsk | 0:ec079a141883 | 45 | |
duchonic | 1:19be52262a2d | 46 | /** LCD panel format */ |
duchonic | 1:19be52262a2d | 47 | enum LCDType { |
duchonic | 1:19be52262a2d | 48 | LCD16x2 /**< 16x2 LCD panel (default) */ |
duchonic | 1:19be52262a2d | 49 | , LCD16x2B /**< 16x2 LCD panel alternate addressing */ |
duchonic | 1:19be52262a2d | 50 | , LCD20x2 /**< 20x2 LCD panel */ |
duchonic | 1:19be52262a2d | 51 | , LCD20x4 /**< 20x4 LCD panel */ |
duchonic | 1:19be52262a2d | 52 | }; |
duchonic | 1:19be52262a2d | 53 | |
duchonic | 1:19be52262a2d | 54 | /** Create a TextLCD interface |
pangsk | 0:ec079a141883 | 55 | * |
duchonic | 1:19be52262a2d | 56 | * @param rs Instruction/data control line |
duchonic | 1:19be52262a2d | 57 | * @param e Enable line (clock) |
duchonic | 1:19be52262a2d | 58 | * @param d4-d7 Data lines for using as a 4-bit interface |
duchonic | 1:19be52262a2d | 59 | * @param type Sets the panel size/addressing mode (default = LCD16x2) |
pangsk | 0:ec079a141883 | 60 | */ |
duchonic | 1:19be52262a2d | 61 | TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2); |
pangsk | 0:ec079a141883 | 62 | |
duchonic | 1:19be52262a2d | 63 | #if DOXYGEN_ONLY |
duchonic | 1:19be52262a2d | 64 | /** Write a character to the LCD |
pangsk | 0:ec079a141883 | 65 | * |
duchonic | 1:19be52262a2d | 66 | * @param c The character to write to the display |
pangsk | 0:ec079a141883 | 67 | */ |
pangsk | 0:ec079a141883 | 68 | int putc(int c); |
pangsk | 0:ec079a141883 | 69 | |
duchonic | 1:19be52262a2d | 70 | /** Write a formated string to the LCD |
pangsk | 0:ec079a141883 | 71 | * |
duchonic | 1:19be52262a2d | 72 | * @param format A printf-style format string, followed by the |
duchonic | 1:19be52262a2d | 73 | * variables to use in formating the string. |
pangsk | 0:ec079a141883 | 74 | */ |
pangsk | 0:ec079a141883 | 75 | int printf(const char* format, ...); |
pangsk | 0:ec079a141883 | 76 | #endif |
duchonic | 1:19be52262a2d | 77 | |
duchonic | 1:19be52262a2d | 78 | /** Locate to a screen column and row |
pangsk | 0:ec079a141883 | 79 | * |
duchonic | 1:19be52262a2d | 80 | * @param column The horizontal position from the left, indexed from 0 |
duchonic | 1:19be52262a2d | 81 | * @param row The vertical position from the top, indexed from 0 |
pangsk | 0:ec079a141883 | 82 | */ |
duchonic | 1:19be52262a2d | 83 | void locate(int column, int row); |
duchonic | 1:19be52262a2d | 84 | |
duchonic | 1:19be52262a2d | 85 | /** Clear the screen and locate to 0,0 */ |
duchonic | 1:19be52262a2d | 86 | void cls(); |
duchonic | 1:19be52262a2d | 87 | |
duchonic | 1:19be52262a2d | 88 | int rows(); |
duchonic | 1:19be52262a2d | 89 | int columns(); |
duchonic | 1:19be52262a2d | 90 | |
pangsk | 0:ec079a141883 | 91 | protected: |
pangsk | 0:ec079a141883 | 92 | |
duchonic | 1:19be52262a2d | 93 | // Stream implementation functions |
duchonic | 1:19be52262a2d | 94 | virtual int _putc(int value); |
duchonic | 1:19be52262a2d | 95 | virtual int _getc(); |
pangsk | 0:ec079a141883 | 96 | |
duchonic | 1:19be52262a2d | 97 | int address(int column, int row); |
duchonic | 1:19be52262a2d | 98 | void character(int column, int row, int c); |
duchonic | 1:19be52262a2d | 99 | void writeByte(int value); |
duchonic | 1:19be52262a2d | 100 | void writeCommand(int command); |
duchonic | 1:19be52262a2d | 101 | void writeData(int data); |
duchonic | 1:19be52262a2d | 102 | |
duchonic | 1:19be52262a2d | 103 | DigitalOut _rs, _e; |
duchonic | 1:19be52262a2d | 104 | BusOut _d; |
duchonic | 1:19be52262a2d | 105 | LCDType _type; |
duchonic | 1:19be52262a2d | 106 | |
duchonic | 1:19be52262a2d | 107 | int _column; |
duchonic | 1:19be52262a2d | 108 | int _row; |
pangsk | 0:ec079a141883 | 109 | }; |
pangsk | 0:ec079a141883 | 110 | |
pangsk | 0:ec079a141883 | 111 | #endif |