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