Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ForEhab Serial_HelloWorld_Mbed ForOfek
Fork of LCDSerial by
SerialLCD.h@1:96f055419f71, 2013-05-22 (annotated)
- Committer:
- fossum_13
- Date:
- Wed May 22 02:10:44 2013 +0000
- Revision:
- 1:96f055419f71
- Parent:
- 0:df5850d83ee5
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| Phlaphead | 0:df5850d83ee5 | 1 | #ifndef SerialLCD_h | 
| Phlaphead | 0:df5850d83ee5 | 2 | #define SerialLCD_h | 
| Phlaphead | 0:df5850d83ee5 | 3 | |
| Phlaphead | 0:df5850d83ee5 | 4 | #include "mbed.h" | 
| Phlaphead | 0:df5850d83ee5 | 5 | |
| Phlaphead | 0:df5850d83ee5 | 6 | #define LCD_BAUD 9600 | 
| Phlaphead | 0:df5850d83ee5 | 7 | |
| fossum_13 | 1:96f055419f71 | 8 | // Commands | 
| fossum_13 | 1:96f055419f71 | 9 | #define LCD_BACKLIGHT 0x80 | 
| fossum_13 | 1:96f055419f71 | 10 | #define LCD_CLEARDISPLAY 0x01 | 
| fossum_13 | 1:96f055419f71 | 11 | #define LCD_CURSORSHIFT 0x10 | 
| fossum_13 | 1:96f055419f71 | 12 | #define LCD_DISPLAYCONTROL 0x08 | 
| fossum_13 | 1:96f055419f71 | 13 | #define LCD_ENTRYMODESET 0x04 | 
| fossum_13 | 1:96f055419f71 | 14 | #define LCD_FUNCTIONSET 0x20 | 
| fossum_13 | 1:96f055419f71 | 15 | #define LCD_SETCGRAMADDR 0x40 | 
| fossum_13 | 1:96f055419f71 | 16 | #define LCD_SETDDRAMADDR 0x80 | 
| fossum_13 | 1:96f055419f71 | 17 | #define LCD_SETSPLASHSCREEN 0x0A | 
| fossum_13 | 1:96f055419f71 | 18 | #define LCD_SPLASHTOGGLE 0x09 | 
| fossum_13 | 1:96f055419f71 | 19 | #define LCD_RETURNHOME 0x02 | 
| fossum_13 | 1:96f055419f71 | 20 | |
| fossum_13 | 1:96f055419f71 | 21 | // Flags for display entry mode | 
| fossum_13 | 1:96f055419f71 | 22 | #define LCD_ENTRYRIGHT 0x00 | 
| fossum_13 | 1:96f055419f71 | 23 | #define LCD_ENTRYLEFT 0x02 | 
| fossum_13 | 1:96f055419f71 | 24 | |
| fossum_13 | 1:96f055419f71 | 25 | // Flags for display on/off control | 
| fossum_13 | 1:96f055419f71 | 26 | #define LCD_BLINKON 0x01 | 
| fossum_13 | 1:96f055419f71 | 27 | #define LCD_CURSORON 0x02 | 
| fossum_13 | 1:96f055419f71 | 28 | #define LCD_DISPLAYON 0x04 | 
| fossum_13 | 1:96f055419f71 | 29 | |
| fossum_13 | 1:96f055419f71 | 30 | // Flags for display size | 
| fossum_13 | 1:96f055419f71 | 31 | #define LCD_2LINE 0x02 | 
| fossum_13 | 1:96f055419f71 | 32 | #define LCD_4LINE 0x04 | 
| fossum_13 | 1:96f055419f71 | 33 | #define LCD_16CHAR 0x10 | 
| fossum_13 | 1:96f055419f71 | 34 | #define LCD_20CHAR 0x14 | 
| fossum_13 | 1:96f055419f71 | 35 | |
| fossum_13 | 1:96f055419f71 | 36 | // Flags for setting display size | 
| fossum_13 | 1:96f055419f71 | 37 | #define LCD_SET2LINE 0x06 | 
| fossum_13 | 1:96f055419f71 | 38 | #define LCD_SET4LINE 0x05 | 
| fossum_13 | 1:96f055419f71 | 39 | #define LCD_SET16CHAR 0x04 | 
| fossum_13 | 1:96f055419f71 | 40 | #define LCD_SET20CHAR 0x03 | 
| fossum_13 | 1:96f055419f71 | 41 | |
| fossum_13 | 1:96f055419f71 | 42 | // LCD Types | 
| fossum_13 | 1:96f055419f71 | 43 | #define LCD_2X16 3 | 
| fossum_13 | 1:96f055419f71 | 44 | #define LCD_2X20 4 | 
| fossum_13 | 1:96f055419f71 | 45 | #define LCD_4X16 5 | 
| fossum_13 | 1:96f055419f71 | 46 | #define LCD_4X20 6 | 
| fossum_13 | 1:96f055419f71 | 47 | |
| Phlaphead | 0:df5850d83ee5 | 48 | /** | 
| Phlaphead | 0:df5850d83ee5 | 49 | * SparkFun SerLCD v2.5 Controller | 
| fossum_13 | 1:96f055419f71 | 50 | * | 
| fossum_13 | 1:96f055419f71 | 51 | * Copied from the Arduino.cc SerLCD library. | 
| fossum_13 | 1:96f055419f71 | 52 | * @see http://playground.arduino.cc/Code/SerLCD | 
| Phlaphead | 0:df5850d83ee5 | 53 | */ | 
| Phlaphead | 0:df5850d83ee5 | 54 | class SerialLCD : public Serial | 
| Phlaphead | 0:df5850d83ee5 | 55 | { | 
| Phlaphead | 0:df5850d83ee5 | 56 | public: | 
| fossum_13 | 1:96f055419f71 | 57 | /** Constructor. | 
| Phlaphead | 0:df5850d83ee5 | 58 | * @param tx Connected to rx pin of LCD | 
| fossum_13 | 1:96f055419f71 | 59 | * @param type Type of LCD (Optional default 16x2) | 
| Phlaphead | 0:df5850d83ee5 | 60 | */ | 
| fossum_13 | 1:96f055419f71 | 61 | SerialLCD(PinName tx, uint8_t type = 3); | 
| Phlaphead | 0:df5850d83ee5 | 62 | |
| fossum_13 | 1:96f055419f71 | 63 | /** Blinks the cursor. This blinks the whole | 
| fossum_13 | 1:96f055419f71 | 64 | * box or just the cursor bar. | 
| fossum_13 | 1:96f055419f71 | 65 | */ | 
| fossum_13 | 1:96f055419f71 | 66 | void blink(); | 
| fossum_13 | 1:96f055419f71 | 67 | |
| fossum_13 | 1:96f055419f71 | 68 | /** Clears the whole LCD and sets the | 
| fossum_13 | 1:96f055419f71 | 69 | * cursor to the first row, first column. | 
| Phlaphead | 0:df5850d83ee5 | 70 | */ | 
| Phlaphead | 0:df5850d83ee5 | 71 | void clear(); | 
| fossum_13 | 1:96f055419f71 | 72 | /** Clears the current line and resets | 
| fossum_13 | 1:96f055419f71 | 73 | * the cursor to the beginning of the | 
| fossum_13 | 1:96f055419f71 | 74 | * line. | 
| Phlaphead | 0:df5850d83ee5 | 75 | */ | 
| fossum_13 | 1:96f055419f71 | 76 | void clearLine(uint8_t); | 
| fossum_13 | 1:96f055419f71 | 77 | /** Creates a custom character for the | 
| fossum_13 | 1:96f055419f71 | 78 | * LCD. | 
| fossum_13 | 1:96f055419f71 | 79 | * @param location Location to store the char (1-8) | 
| fossum_13 | 1:96f055419f71 | 80 | * @param data Byte array containing the enabled pixel bits | 
| fossum_13 | 1:96f055419f71 | 81 | * @code | 
| fossum_13 | 1:96f055419f71 | 82 | * #include <NewSoftSerial.h> | 
| fossum_13 | 1:96f055419f71 | 83 | * #include <serLCD.h> | 
| fossum_13 | 1:96f055419f71 | 84 | * | 
| fossum_13 | 1:96f055419f71 | 85 | * // Set pin to the LCD's rxPin | 
| fossum_13 | 1:96f055419f71 | 86 | * int pin = 2; | 
| fossum_13 | 1:96f055419f71 | 87 | * | 
| fossum_13 | 1:96f055419f71 | 88 | * serLCD lcd(pin); | 
| fossum_13 | 1:96f055419f71 | 89 | * | 
| fossum_13 | 1:96f055419f71 | 90 | * byte bars[8][8] = { | 
| fossum_13 | 1:96f055419f71 | 91 | * {B00000,B00000,B00000,B00000,B00000,B00000,B00000,B11111}, | 
| fossum_13 | 1:96f055419f71 | 92 | * {B00000,B00000,B00000,B00000,B00000,B00000,B11111,B11111}, | 
| fossum_13 | 1:96f055419f71 | 93 | * {B00000,B00000,B00000,B00000,B00000,B11111,B11111,B11111}, | 
| fossum_13 | 1:96f055419f71 | 94 | * {B00000,B00000,B00000,B00000,B11111,B11111,B11111,B11111}, | 
| fossum_13 | 1:96f055419f71 | 95 | * {B00000,B00000,B00000,B11111,B11111,B11111,B11111,B11111}, | 
| fossum_13 | 1:96f055419f71 | 96 | * {B00000,B00000,B11111,B11111,B11111,B11111,B11111,B11111}, | 
| fossum_13 | 1:96f055419f71 | 97 | * {B00000,B11111,B11111,B11111,B11111,B11111,B11111,B11111}, | 
| fossum_13 | 1:96f055419f71 | 98 | * {B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111} | 
| fossum_13 | 1:96f055419f71 | 99 | * }; | 
| fossum_13 | 1:96f055419f71 | 100 | * | 
| fossum_13 | 1:96f055419f71 | 101 | * void main() { | 
| fossum_13 | 1:96f055419f71 | 102 | * for (int i=1; i < 9; i++){ | 
| fossum_13 | 1:96f055419f71 | 103 | * lcd.createChar(i, bars[i-1]); | 
| fossum_13 | 1:96f055419f71 | 104 | * } | 
| fossum_13 | 1:96f055419f71 | 105 | * for (int i=1; i < 9; i++){ | 
| fossum_13 | 1:96f055419f71 | 106 | * lcd.printCustomChar(i); | 
| fossum_13 | 1:96f055419f71 | 107 | * } | 
| fossum_13 | 1:96f055419f71 | 108 | * while(1); | 
| fossum_13 | 1:96f055419f71 | 109 | * } | 
| fossum_13 | 1:96f055419f71 | 110 | * | 
| fossum_13 | 1:96f055419f71 | 111 | * @endcode | 
| Phlaphead | 0:df5850d83ee5 | 112 | */ | 
| fossum_13 | 1:96f055419f71 | 113 | void createChar(uint8_t, uint8_t[]); | 
| fossum_13 | 1:96f055419f71 | 114 | /** Display the LCD cursor: an underscore (line) at the | 
| fossum_13 | 1:96f055419f71 | 115 | * position to which the next character will be written. | 
| fossum_13 | 1:96f055419f71 | 116 | * This does not define if it blinks or not. | 
| fossum_13 | 1:96f055419f71 | 117 | */ | 
| fossum_13 | 1:96f055419f71 | 118 | void cursor(); | 
| fossum_13 | 1:96f055419f71 | 119 | /** Turns on the LCD display, after it's been turned off | 
| fossum_13 | 1:96f055419f71 | 120 | * with noDisplay(). This will restore the text (and | 
| fossum_13 | 1:96f055419f71 | 121 | * cursor) that was on the display. | 
| fossum_13 | 1:96f055419f71 | 122 | */ | 
| fossum_13 | 1:96f055419f71 | 123 | void display(); | 
| fossum_13 | 1:96f055419f71 | 124 | /** Positions the cursor in the upper-left of the LCD. | 
| fossum_13 | 1:96f055419f71 | 125 | */ | 
| fossum_13 | 1:96f055419f71 | 126 | void home(); | 
| fossum_13 | 1:96f055419f71 | 127 | /** Set the direction for text written to the LCD to | 
| fossum_13 | 1:96f055419f71 | 128 | * left-to-right, the default. This means that subsequent | 
| fossum_13 | 1:96f055419f71 | 129 | * characters written to the display will go from left to | 
| fossum_13 | 1:96f055419f71 | 130 | * right, but does not affect previously-output text. | 
| fossum_13 | 1:96f055419f71 | 131 | */ | 
| fossum_13 | 1:96f055419f71 | 132 | void leftToRight(); | 
| fossum_13 | 1:96f055419f71 | 133 | /** Turns off the blinking LCD cursor. | 
| Phlaphead | 0:df5850d83ee5 | 134 | */ | 
| fossum_13 | 1:96f055419f71 | 135 | void noBlink(); | 
| fossum_13 | 1:96f055419f71 | 136 | /** Hides the LCD cursor. | 
| fossum_13 | 1:96f055419f71 | 137 | */ | 
| fossum_13 | 1:96f055419f71 | 138 | void noCursor(); | 
| fossum_13 | 1:96f055419f71 | 139 | /** Turns off the LCD display, without losing the text | 
| fossum_13 | 1:96f055419f71 | 140 | * currently shown on it. | 
| fossum_13 | 1:96f055419f71 | 141 | */ | 
| fossum_13 | 1:96f055419f71 | 142 | void noDisplay(); | 
| fossum_13 | 1:96f055419f71 | 143 | /** Print a custom character (gylph) on to the LCD. Up | 
| fossum_13 | 1:96f055419f71 | 144 | * to eight characters of 5x8 pixels are supported | 
| fossum_13 | 1:96f055419f71 | 145 | * (numbered 1 to 8). The custom characters are designed | 
| fossum_13 | 1:96f055419f71 | 146 | * by the createChar() command. | 
| fossum_13 | 1:96f055419f71 | 147 | * @param location Which character to print (1 to 8), created by createChar() | 
| fossum_13 | 1:96f055419f71 | 148 | */ | 
| fossum_13 | 1:96f055419f71 | 149 | void printCustomChar(uint8_t); | 
| fossum_13 | 1:96f055419f71 | 150 | /** Set the direction for text written to the LCD to | 
| fossum_13 | 1:96f055419f71 | 151 | * right-to-left (the default is left-to-right). This | 
| fossum_13 | 1:96f055419f71 | 152 | * means that subsequent characters written to the | 
| fossum_13 | 1:96f055419f71 | 153 | * display will go from right to left, but does not | 
| fossum_13 | 1:96f055419f71 | 154 | * affect previously-output text. | 
| fossum_13 | 1:96f055419f71 | 155 | */ | 
| fossum_13 | 1:96f055419f71 | 156 | void rightToLeft(); | 
| fossum_13 | 1:96f055419f71 | 157 | /** Scrolls the text on the LCD one position to the left. | 
| Phlaphead | 0:df5850d83ee5 | 158 | */ | 
| Phlaphead | 0:df5850d83ee5 | 159 | void scrollLeft(); | 
| fossum_13 | 1:96f055419f71 | 160 | /** Scrolls the text on the LCD one position to the right. | 
| Phlaphead | 0:df5850d83ee5 | 161 | */ | 
| Phlaphead | 0:df5850d83ee5 | 162 | void scrollRight(); | 
| fossum_13 | 1:96f055419f71 | 163 | /** Moves the cursor to the beginning of selected line. | 
| fossum_13 | 1:96f055419f71 | 164 | * Line numbers start with 1. Valid line numbers are 1-4. | 
| fossum_13 | 1:96f055419f71 | 165 | * @param line Line number to go to | 
| fossum_13 | 1:96f055419f71 | 166 | */ | 
| fossum_13 | 1:96f055419f71 | 167 | void selectLine(uint8_t line); | 
| fossum_13 | 1:96f055419f71 | 168 | /** Sets the backlight brightness based on input value. | 
| fossum_13 | 1:96f055419f71 | 169 | * Values range from 1-30. | 
| fossum_13 | 1:96f055419f71 | 170 | * @params brightness 1 = Off -> 30 = Full Brightness | 
| fossum_13 | 1:96f055419f71 | 171 | */ | 
| fossum_13 | 1:96f055419f71 | 172 | void setBrightness(uint8_t num); | 
| fossum_13 | 1:96f055419f71 | 173 | /** Position the LCD cursor. | 
| fossum_13 | 1:96f055419f71 | 174 | * @param row Row position of the cursor (1 being the first row) | 
| fossum_13 | 1:96f055419f71 | 175 | * @param col Column position the cursor (1 being the first column) | 
| fossum_13 | 1:96f055419f71 | 176 | */ | 
| fossum_13 | 1:96f055419f71 | 177 | void setCursor(uint8_t row, uint8_t col); | 
| fossum_13 | 1:96f055419f71 | 178 | /** Saves the first 2 lines of text that are currently | 
| fossum_13 | 1:96f055419f71 | 179 | * printed on the LCD screen to memory as the new splash screen. | 
| fossum_13 | 1:96f055419f71 | 180 | * @code | 
| fossum_13 | 1:96f055419f71 | 181 | * void setup() | 
| fossum_13 | 1:96f055419f71 | 182 | * { | 
| fossum_13 | 1:96f055419f71 | 183 | * lcd.print(" New "); | 
| fossum_13 | 1:96f055419f71 | 184 | * lcd.print(" Splash Screen! "); | 
| fossum_13 | 1:96f055419f71 | 185 | * lcd.setSplash(); | 
| fossum_13 | 1:96f055419f71 | 186 | * } | 
| fossum_13 | 1:96f055419f71 | 187 | * @endcode | 
| fossum_13 | 1:96f055419f71 | 188 | */ | 
| fossum_13 | 1:96f055419f71 | 189 | void setSplash(); | 
| fossum_13 | 1:96f055419f71 | 190 | /** The SerLCD firmware v2.5 supports setting different types | 
| fossum_13 | 1:96f055419f71 | 191 | * of LCD's. This function allows to easily set the type of LCD. | 
| fossum_13 | 1:96f055419f71 | 192 | * | 
| fossum_13 | 1:96f055419f71 | 193 | * The function expects a parameter value of 3 - 6: | 
| fossum_13 | 1:96f055419f71 | 194 | * Defines: | 
| fossum_13 | 1:96f055419f71 | 195 | * LCD_2X16(3) - 2x16 LCD | 
| fossum_13 | 1:96f055419f71 | 196 | * LCD_2X20(4) - 2x20 LCD | 
| fossum_13 | 1:96f055419f71 | 197 | * LCD_4X16(5) - 4x16 LCD | 
| fossum_13 | 1:96f055419f71 | 198 | * LCD_4X20(6) - 4x20 LCD | 
| fossum_13 | 1:96f055419f71 | 199 | * | 
| fossum_13 | 1:96f055419f71 | 200 | * After changing the settings, the value is written to the EEPROM | 
| fossum_13 | 1:96f055419f71 | 201 | * so the SerialLCD will remember it even after a power-off. | 
| fossum_13 | 1:96f055419f71 | 202 | */ | 
| fossum_13 | 1:96f055419f71 | 203 | void setType(uint8_t type); | 
| fossum_13 | 1:96f055419f71 | 204 | /** Toggles the splash screen on/off. | 
| fossum_13 | 1:96f055419f71 | 205 | */ | 
| fossum_13 | 1:96f055419f71 | 206 | void toggleSplash(); | 
| Phlaphead | 0:df5850d83ee5 | 207 | |
| fossum_13 | 1:96f055419f71 | 208 | private: | 
| fossum_13 | 1:96f055419f71 | 209 | void command(uint8_t); | 
| fossum_13 | 1:96f055419f71 | 210 | void specialCommand(uint8_t); | 
| fossum_13 | 1:96f055419f71 | 211 | |
| fossum_13 | 1:96f055419f71 | 212 | uint8_t _displayfunction; | 
| fossum_13 | 1:96f055419f71 | 213 | uint8_t _displaycontrol; | 
| fossum_13 | 1:96f055419f71 | 214 | uint8_t _displaymode; | 
| fossum_13 | 1:96f055419f71 | 215 | uint8_t _numlines; | 
| fossum_13 | 1:96f055419f71 | 216 | uint8_t _numchars; | 
| fossum_13 | 1:96f055419f71 | 217 | uint8_t _rowoffset; | 
| Phlaphead | 0:df5850d83ee5 | 218 | }; | 
| Phlaphead | 0:df5850d83ee5 | 219 | |
| Phlaphead | 0:df5850d83ee5 | 220 | #endif | 
