my lcd

Fork of TextLCD by Simon Ford

Committer:
Janet
Date:
Thu Dec 17 14:55:22 2015 +0000
Revision:
9:609f5265134b
Parent:
8:308d188a2d3a
Zmiana przycisku i lcd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Janet 9:609f5265134b 1 /* Extended TextLCD library.
Janet 9:609f5265134b 2 *
Janet 9:609f5265134b 3 * Based on the mbed TextLCD Library, for a 4-bit LCD based on HD44780
simon 6:e4cb7ddee0d3 4 * Copyright (c) 2007-2010, sford, http://mbed.org
simon 1:ac48b187213c 5 *
Janet 9:609f5265134b 6 * Original license:
simon 1:ac48b187213c 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:ac48b187213c 8 * of this software and associated documentation files (the "Software"), to deal
simon 1:ac48b187213c 9 * in the Software without restriction, including without limitation the rights
simon 1:ac48b187213c 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:ac48b187213c 11 * copies of the Software, and to permit persons to whom the Software is
simon 1:ac48b187213c 12 * furnished to do so, subject to the following conditions:
simon 2:227356c7d12c 13 *
simon 1:ac48b187213c 14 * The above copyright notice and this permission notice shall be included in
simon 1:ac48b187213c 15 * all copies or substantial portions of the Software.
simon 2:227356c7d12c 16 *
simon 1:ac48b187213c 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:ac48b187213c 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:ac48b187213c 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:ac48b187213c 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:ac48b187213c 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:ac48b187213c 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:ac48b187213c 23 * THE SOFTWARE.
simon 1:ac48b187213c 24 */
simon 1:ac48b187213c 25
Janet 9:609f5265134b 26 #ifndef EXT_TEXT_LCD_TEXTLCD_H
Janet 9:609f5265134b 27 #define EXT_TEXT_LCD_TEXTLCD_H
simon 1:ac48b187213c 28
simon 1:ac48b187213c 29 #include "mbed.h"
simon 2:227356c7d12c 30
Janet 9:609f5265134b 31 namespace ext_text_lcd {
Janet 9:609f5265134b 32
Janet 9:609f5265134b 33 /** A TextLCD interface for driving 4-bit HD44780-based LCDs
simon 2:227356c7d12c 34 *
simon 5:a53b3e2d6f1e 35 * Currently supports 16x2, 20x2 and 20x4 panels
simon 2:227356c7d12c 36 *
simon 2:227356c7d12c 37 * @code
simon 2:227356c7d12c 38 * #include "mbed.h"
simon 2:227356c7d12c 39 * #include "TextLCD.h"
simon 5:a53b3e2d6f1e 40 *
Janet 9:609f5265134b 41 * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7 on individual pins
Janet 9:609f5265134b 42 * TextLCD lcd(p10, p12, Port2, 2); // rs, e, d4-d7 on P2.2-P2.5
simon 5:a53b3e2d6f1e 43 *
simon 2:227356c7d12c 44 * int main() {
simon 2:227356c7d12c 45 * lcd.printf("Hello World!\n");
simon 2:227356c7d12c 46 * }
simon 2:227356c7d12c 47 * @endcode
simon 2:227356c7d12c 48 */
simon 1:ac48b187213c 49 class TextLCD : public Stream {
simon 1:ac48b187213c 50 public:
Janet 9:609f5265134b 51 /** LCD panel format */
Janet 9:609f5265134b 52 class LCDType {
Janet 9:609f5265134b 53 public:
Janet 9:609f5265134b 54 int columns() const;
Janet 9:609f5265134b 55 int rows() const;
Janet 9:609f5265134b 56 int address(int column, int row) const;
Janet 9:609f5265134b 57
Janet 9:609f5265134b 58 typedef int (*MakeAddrFunc)(const LCDType &, int, int);
Janet 9:609f5265134b 59
Janet 9:609f5265134b 60 private:
Janet 9:609f5265134b 61 LCDType(int columns, int rows, MakeAddrFunc makeAddr) :
Janet 9:609f5265134b 62 _columns(columns), _rows(rows), _makeAddr(makeAddr)
Janet 9:609f5265134b 63 { }
simon 1:ac48b187213c 64
Janet 9:609f5265134b 65 friend class TextLCD;
Janet 9:609f5265134b 66
Janet 9:609f5265134b 67 int _columns;
Janet 9:609f5265134b 68 int _rows;
Janet 9:609f5265134b 69 MakeAddrFunc _makeAddr;
simon 1:ac48b187213c 70 };
Janet 9:609f5265134b 71
Janet 9:609f5265134b 72 static const LCDType LCD16x2;
Janet 9:609f5265134b 73 static const LCDType LCD16x2B;
Janet 9:609f5265134b 74 static const LCDType LCD20x2;
Janet 9:609f5265134b 75 static const LCDType LCD20x4;
simon 1:ac48b187213c 76
simon 2:227356c7d12c 77 /** Create a TextLCD interface
simon 2:227356c7d12c 78 *
simon 2:227356c7d12c 79 * @param rs Instruction/data control line
simon 2:227356c7d12c 80 * @param e Enable line (clock)
simon 7:44f34c09bd37 81 * @param d4-d7 Data lines for using as a 4-bit interface
simon 2:227356c7d12c 82 * @param type Sets the panel size/addressing mode (default = LCD16x2)
simon 2:227356c7d12c 83 */
Janet 9:609f5265134b 84 TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, const LCDType &type = LCD20x4);
simon 2:227356c7d12c 85
Janet 9:609f5265134b 86 /** Create a TextLCD interface
Janet 9:609f5265134b 87 *
Janet 9:609f5265134b 88 * @param rs Instruction/data control line
Janet 9:609f5265134b 89 * @param e Enable line (clock)
Janet 9:609f5265134b 90 * @param dport GPIO port with all data lines
Janet 9:609f5265134b 91 * @param dlsb Index of the lowest data bit in the GPIO port
Janet 9:609f5265134b 92 * @param type Sets the panel size/addressing mode (default = LCD16x2)
Janet 9:609f5265134b 93 */
Janet 9:609f5265134b 94 TextLCD(PinName rs, PinName e, PortName dport, std::size_t dlsb, const LCDType &type = LCD16x2);
Janet 9:609f5265134b 95
simon 2:227356c7d12c 96 #if DOXYGEN_ONLY
simon 2:227356c7d12c 97 /** Write a character to the LCD
simon 2:227356c7d12c 98 *
simon 2:227356c7d12c 99 * @param c The character to write to the display
simon 2:227356c7d12c 100 */
simon 2:227356c7d12c 101 int putc(int c);
simon 2:227356c7d12c 102
simon 2:227356c7d12c 103 /** Write a formated string to the LCD
simon 2:227356c7d12c 104 *
simon 2:227356c7d12c 105 * @param format A printf-style format string, followed by the
simon 2:227356c7d12c 106 * variables to use in formating the string.
simon 2:227356c7d12c 107 */
simon 2:227356c7d12c 108 int printf(const char* format, ...);
simon 2:227356c7d12c 109 #endif
simon 2:227356c7d12c 110
simon 2:227356c7d12c 111 /** Locate to a screen column and row
simon 2:227356c7d12c 112 *
simon 2:227356c7d12c 113 * @param column The horizontal position from the left, indexed from 0
simon 2:227356c7d12c 114 * @param row The vertical position from the top, indexed from 0
simon 2:227356c7d12c 115 */
simon 1:ac48b187213c 116 void locate(int column, int row);
simon 2:227356c7d12c 117
simon 2:227356c7d12c 118 /** Clear the screen and locate to 0,0 */
simon 1:ac48b187213c 119 void cls();
simon 2:227356c7d12c 120
Janet 9:609f5265134b 121 int columns() { return _type.columns(); }
Janet 9:609f5265134b 122 int rows() const { return _type.rows(); }
Janet 9:609f5265134b 123
Janet 9:609f5265134b 124 enum DisplayControl {
Janet 9:609f5265134b 125 DisplayOn = 0x04,
Janet 9:609f5265134b 126 DisplayOff = 0
Janet 9:609f5265134b 127 };
Janet 9:609f5265134b 128 enum CursorDisplayControl {
Janet 9:609f5265134b 129 CursorOn = 0x02,
Janet 9:609f5265134b 130 CursorOff = 0
Janet 9:609f5265134b 131 };
Janet 9:609f5265134b 132 enum CursorStyle {
Janet 9:609f5265134b 133 BlinkingCursor = 0x01,
Janet 9:609f5265134b 134 UnderlineCursor = 0
Janet 9:609f5265134b 135 };
Janet 9:609f5265134b 136 void setDisplayControl(DisplayControl d = DisplayOn, CursorDisplayControl c = CursorOff, CursorStyle b = UnderlineCursor);
Janet 9:609f5265134b 137
Janet 9:609f5265134b 138 void writeToCGMem(unsigned addr, const char *buffer, std::size_t size);
simon 2:227356c7d12c 139
simon 1:ac48b187213c 140 protected:
simon 1:ac48b187213c 141 // Stream implementation functions
simon 1:ac48b187213c 142 virtual int _putc(int value);
simon 1:ac48b187213c 143 virtual int _getc();
simon 1:ac48b187213c 144
Janet 9:609f5265134b 145 int address(int column, int row) const { return _type.address(column, row); }
Janet 9:609f5265134b 146
simon 1:ac48b187213c 147 void writeByte(int value);
simon 1:ac48b187213c 148 void writeCommand(int command);
simon 1:ac48b187213c 149 void writeData(int data);
simon 1:ac48b187213c 150
Janet 9:609f5265134b 151 private:
Janet 9:609f5265134b 152 void init();
Janet 9:609f5265134b 153
Janet 9:609f5265134b 154 struct LcdOutput {
Janet 9:609f5265134b 155 virtual void writeNibble(int value) = 0;
Janet 9:609f5265134b 156 virtual ~LcdOutput() { }
Janet 9:609f5265134b 157 };
Janet 9:609f5265134b 158
Janet 9:609f5265134b 159 struct BusOutput : public LcdOutput {
Janet 9:609f5265134b 160 BusOutput(PinName d4, PinName d5, PinName d6, PinName d7) : _bus(d4, d5, d6, d7) { }
Janet 9:609f5265134b 161 virtual void writeNibble(int value);
Janet 9:609f5265134b 162 private:
Janet 9:609f5265134b 163 BusOut _bus;
Janet 9:609f5265134b 164 };
Janet 9:609f5265134b 165
Janet 9:609f5265134b 166 struct PortOutput : public LcdOutput {
Janet 9:609f5265134b 167 PortOutput(PortName dport, std::size_t dlsb) : _port(dport, 0x0f << dlsb), _shift(dlsb) { }
Janet 9:609f5265134b 168 virtual void writeNibble(int value);
Janet 9:609f5265134b 169
Janet 9:609f5265134b 170 private:
Janet 9:609f5265134b 171 PortOut _port;
Janet 9:609f5265134b 172 std::size_t _shift;
Janet 9:609f5265134b 173 };
Janet 9:609f5265134b 174
simon 1:ac48b187213c 175 DigitalOut _rs, _e;
Janet 9:609f5265134b 176 LcdOutput *_output;
simon 1:ac48b187213c 177 LCDType _type;
simon 1:ac48b187213c 178 int _column;
Janet 9:609f5265134b 179 int _row;
simon 1:ac48b187213c 180 };
simon 1:ac48b187213c 181
Janet 9:609f5265134b 182 inline int TextLCD::LCDType::columns() const {
Janet 9:609f5265134b 183 return this->_columns;
Janet 9:609f5265134b 184 }
Janet 9:609f5265134b 185 inline int TextLCD::LCDType::rows() const {
Janet 9:609f5265134b 186 return this->_rows;
Janet 9:609f5265134b 187 }
Janet 9:609f5265134b 188 inline int TextLCD::LCDType::address(int column, int row) const {
Janet 9:609f5265134b 189 return this->_makeAddr(*this, column, row);
Janet 9:609f5265134b 190 }
Janet 9:609f5265134b 191
Janet 9:609f5265134b 192 } // ext_text_lcd namespace
Janet 9:609f5265134b 193
simon 1:ac48b187213c 194 #endif