An interface to the Sparkfun Serial Graphic LCD, LCD-09351; and Graphic LCD Serial Backpack, LCD-09352. Derived class from Serial so that you can conveniently send text to the display with printf(), putc(), etc.
SerialGraphicLCD.h@1:2f436b8aebf4, 2012-03-28 (annotated)
- Committer:
- shimniok
- Date:
- Wed Mar 28 16:33:27 2012 +0000
- Revision:
- 1:2f436b8aebf4
- Parent:
- 0:a3d518d2f36f
- Child:
- 2:84b78506add6
revised pos() to use text row, column for text position. Added posXY to specify x, y coordinates for text position.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a3d518d2f36f | 1 | /* Serial Graphics LCD Driver for Sparkfun Serial Graphics LCD, LCD-09351; and Graphic |
shimniok | 0:a3d518d2f36f | 2 | * LCD Serial Backpack, LCD-09352. |
shimniok | 0:a3d518d2f36f | 3 | * |
shimniok | 0:a3d518d2f36f | 4 | * @author Michael Shimniok http://www.bot-thoughts.com/ |
shimniok | 0:a3d518d2f36f | 5 | * |
shimniok | 0:a3d518d2f36f | 6 | */ |
shimniok | 0:a3d518d2f36f | 7 | #ifndef _SERIALGRAPHICLCD_H |
shimniok | 0:a3d518d2f36f | 8 | #define _SERIALGRAPHICLCD_H |
shimniok | 0:a3d518d2f36f | 9 | |
shimniok | 0:a3d518d2f36f | 10 | #include "mbed.h" |
shimniok | 0:a3d518d2f36f | 11 | |
shimniok | 1:2f436b8aebf4 | 12 | /** LCD Baud Rates */ |
shimniok | 0:a3d518d2f36f | 13 | #define LCD_4800 1 |
shimniok | 0:a3d518d2f36f | 14 | #define LCD_9600 2 |
shimniok | 0:a3d518d2f36f | 15 | #define LCD_19200 3 |
shimniok | 0:a3d518d2f36f | 16 | #define LCD_38400 4 |
shimniok | 0:a3d518d2f36f | 17 | #define LCD_57600 5 |
shimniok | 0:a3d518d2f36f | 18 | #define LCD_115200 6 |
shimniok | 0:a3d518d2f36f | 19 | |
shimniok | 1:2f436b8aebf4 | 20 | /** LCD Types */ |
shimniok | 1:2f436b8aebf4 | 21 | #define LCD_128x64 1 |
shimniok | 1:2f436b8aebf4 | 22 | #define LCD_160x128 2 |
shimniok | 1:2f436b8aebf4 | 23 | |
shimniok | 0:a3d518d2f36f | 24 | /** Interface to the Sparkfun Serial Graphic LCD, LCD-09351; and Graphic |
shimniok | 0:a3d518d2f36f | 25 | * LCD Serial Backpack, LCD-09352. Derived class from Serial so that you |
shimniok | 0:a3d518d2f36f | 26 | * can conveniently printf(), putc(), etc to the display. |
shimniok | 0:a3d518d2f36f | 27 | * |
shimniok | 0:a3d518d2f36f | 28 | * Example: |
shimniok | 0:a3d518d2f36f | 29 | * @code |
shimniok | 0:a3d518d2f36f | 30 | * #include "mbed.h" |
shimniok | 0:a3d518d2f36f | 31 | * #include "SerialGraphicLCD.h" |
shimniok | 0:a3d518d2f36f | 32 | * |
shimniok | 0:a3d518d2f36f | 33 | * SerialGraphicLCD lcd(p26, p25); |
shimniok | 0:a3d518d2f36f | 34 | * |
shimniok | 0:a3d518d2f36f | 35 | * int main() { |
shimniok | 0:a3d518d2f36f | 36 | * lcd.baud(115200); // default baud rate |
shimniok | 0:a3d518d2f36f | 37 | * while (1) { |
shimniok | 0:a3d518d2f36f | 38 | * lcd.clear(); |
shimniok | 0:a3d518d2f36f | 39 | * lcd.rect(3, 3, 20, 20); |
shimniok | 0:a3d518d2f36f | 40 | * lcd.printf("Hello World!"); |
shimniok | 0:a3d518d2f36f | 41 | * lcd.pixel(14, 35, true); |
shimniok | 0:a3d518d2f36f | 42 | * lcd.pixel(16, 36, true); |
shimniok | 0:a3d518d2f36f | 43 | * lcd.pixel(18, 37, true); |
shimniok | 0:a3d518d2f36f | 44 | * lcd.pos(5, 30); |
shimniok | 0:a3d518d2f36f | 45 | * lcd.printf("Hi"); |
shimniok | 0:a3d518d2f36f | 46 | * lcd.circle(50, 20, 20, true); |
shimniok | 0:a3d518d2f36f | 47 | * lcd.pos(50, 20); |
shimniok | 0:a3d518d2f36f | 48 | * lcd.printf("Howdy"); |
shimniok | 0:a3d518d2f36f | 49 | * lcd.line(0, 0, 25, 25, true); |
shimniok | 0:a3d518d2f36f | 50 | * wait(2); |
shimniok | 0:a3d518d2f36f | 51 | * } |
shimniok | 0:a3d518d2f36f | 52 | * } |
shimniok | 0:a3d518d2f36f | 53 | * @endcode |
shimniok | 0:a3d518d2f36f | 54 | */ |
shimniok | 0:a3d518d2f36f | 55 | class SerialGraphicLCD: public Serial { |
shimniok | 0:a3d518d2f36f | 56 | public: |
shimniok | 0:a3d518d2f36f | 57 | /** Create a new interface to a Serial Graphic LCD |
shimniok | 1:2f436b8aebf4 | 58 | * Note that the display lower left corner is coordinates 0, 0. |
shimniok | 1:2f436b8aebf4 | 59 | * Rows start at the top at 0, columns start at the left at 0. |
shimniok | 0:a3d518d2f36f | 60 | */ |
shimniok | 0:a3d518d2f36f | 61 | SerialGraphicLCD(PinName tx, PinName rx); |
shimniok | 0:a3d518d2f36f | 62 | |
shimniok | 0:a3d518d2f36f | 63 | /** clear the screen |
shimniok | 0:a3d518d2f36f | 64 | */ |
shimniok | 0:a3d518d2f36f | 65 | void clear(void); |
shimniok | 1:2f436b8aebf4 | 66 | |
shimniok | 1:2f436b8aebf4 | 67 | /** set text position in rows, columns |
shimniok | 1:2f436b8aebf4 | 68 | * |
shimniok | 1:2f436b8aebf4 | 69 | * @param row is the row coordinate |
shimniok | 1:2f436b8aebf4 | 70 | * @param col is the col coordinate |
shimniok | 1:2f436b8aebf4 | 71 | */ |
shimniok | 1:2f436b8aebf4 | 72 | void pos(int row, int col); |
shimniok | 0:a3d518d2f36f | 73 | |
shimniok | 1:2f436b8aebf4 | 74 | /** set text position in x, y coordinates |
shimniok | 0:a3d518d2f36f | 75 | * |
shimniok | 0:a3d518d2f36f | 76 | * @param x is the x coordinate |
shimniok | 0:a3d518d2f36f | 77 | * @param y is the y coordinate |
shimniok | 0:a3d518d2f36f | 78 | */ |
shimniok | 1:2f436b8aebf4 | 79 | void posXY(int x, int y); |
shimniok | 0:a3d518d2f36f | 80 | |
shimniok | 0:a3d518d2f36f | 81 | /** set or erase a pixel |
shimniok | 0:a3d518d2f36f | 82 | * |
shimniok | 0:a3d518d2f36f | 83 | * @param x is the x coordinate |
shimniok | 0:a3d518d2f36f | 84 | * @param y is the y coordinate |
shimniok | 0:a3d518d2f36f | 85 | * @param set if true sets the pixel, if false, erases it |
shimniok | 0:a3d518d2f36f | 86 | */ |
shimniok | 0:a3d518d2f36f | 87 | void pixel(int x, int y, bool set); |
shimniok | 0:a3d518d2f36f | 88 | |
shimniok | 0:a3d518d2f36f | 89 | /** draw or erase a line |
shimniok | 0:a3d518d2f36f | 90 | * |
shimniok | 0:a3d518d2f36f | 91 | * @param x1 is the x coordinate of the start of the line |
shimniok | 0:a3d518d2f36f | 92 | * @param y1 is the y coordinate of the start of the line |
shimniok | 0:a3d518d2f36f | 93 | * @param x2 is the x coordinate of the end of the line |
shimniok | 0:a3d518d2f36f | 94 | * @param y2 is the y coordinate of the end of the line |
shimniok | 0:a3d518d2f36f | 95 | * @param set if true sets the line, if false, erases it |
shimniok | 0:a3d518d2f36f | 96 | */ |
shimniok | 0:a3d518d2f36f | 97 | void line(int x1, int y1, int x2, int y2, bool set); |
shimniok | 0:a3d518d2f36f | 98 | |
shimniok | 0:a3d518d2f36f | 99 | /** set or reset a circle |
shimniok | 0:a3d518d2f36f | 100 | * |
shimniok | 0:a3d518d2f36f | 101 | * @param x is the x coordinate of the circle center |
shimniok | 0:a3d518d2f36f | 102 | * @param y is the y coordinate of the circle center |
shimniok | 0:a3d518d2f36f | 103 | * @param r is the radius of the circle |
shimniok | 0:a3d518d2f36f | 104 | * @param set if true sets the pixel, if false, clears it |
shimniok | 0:a3d518d2f36f | 105 | */ |
shimniok | 0:a3d518d2f36f | 106 | void circle(int x, int y, int r, bool set); |
shimniok | 0:a3d518d2f36f | 107 | |
shimniok | 0:a3d518d2f36f | 108 | /** draw or erase a rectangle |
shimniok | 0:a3d518d2f36f | 109 | * |
shimniok | 0:a3d518d2f36f | 110 | * @param x1 is the x coordinate of the upper left of the rectangle |
shimniok | 0:a3d518d2f36f | 111 | * @param y1 is the y coordinate of the upper left of the rectangle |
shimniok | 0:a3d518d2f36f | 112 | * @param x2 is the x coordinate of the lower right of the rectangle |
shimniok | 0:a3d518d2f36f | 113 | * @param y2 is the y coordinate of the lower right of the rectangle |
shimniok | 0:a3d518d2f36f | 114 | */ |
shimniok | 0:a3d518d2f36f | 115 | void rect(int x1, int y1, int x2, int y2); |
shimniok | 0:a3d518d2f36f | 116 | |
shimniok | 0:a3d518d2f36f | 117 | /** erase a rectangular area |
shimniok | 0:a3d518d2f36f | 118 | * |
shimniok | 0:a3d518d2f36f | 119 | * @param x1 is the x coordinate of the upper left of the area |
shimniok | 0:a3d518d2f36f | 120 | * @param y1 is the y coordinate of the upper left of the area |
shimniok | 0:a3d518d2f36f | 121 | * @param x2 is the x coordinate of the lower right of the area |
shimniok | 0:a3d518d2f36f | 122 | * @param y2 is the y coordinate of the lower right of the area |
shimniok | 0:a3d518d2f36f | 123 | */ |
shimniok | 0:a3d518d2f36f | 124 | void erase(int x1, int y1, int x2, int y2); |
shimniok | 0:a3d518d2f36f | 125 | |
shimniok | 0:a3d518d2f36f | 126 | /** set backlight duty cycle |
shimniok | 0:a3d518d2f36f | 127 | * |
shimniok | 0:a3d518d2f36f | 128 | * @param i is the duty cycle from 0 to 100; 0 is off, 100 is full power |
shimniok | 0:a3d518d2f36f | 129 | */ |
shimniok | 0:a3d518d2f36f | 130 | void backlight(int i); |
shimniok | 0:a3d518d2f36f | 131 | |
shimniok | 0:a3d518d2f36f | 132 | /** clear screen and put in reverse mode |
shimniok | 0:a3d518d2f36f | 133 | */ |
shimniok | 0:a3d518d2f36f | 134 | void reverseMode(void); |
shimniok | 0:a3d518d2f36f | 135 | |
shimniok | 0:a3d518d2f36f | 136 | /** configure the lcd baud rate so you have to call this along with baud() to change |
shimniok | 0:a3d518d2f36f | 137 | * communication speeds |
shimniok | 0:a3d518d2f36f | 138 | * |
shimniok | 0:a3d518d2f36f | 139 | * @param b is the baud rate, LCD_4800, LCD_9600, LCD_19200, LCD_38400, LCD_57600 or LCD_115200 |
shimniok | 0:a3d518d2f36f | 140 | */ |
shimniok | 0:a3d518d2f36f | 141 | void lcdbaud(int b); |
shimniok | 1:2f436b8aebf4 | 142 | |
shimniok | 1:2f436b8aebf4 | 143 | |
shimniok | 1:2f436b8aebf4 | 144 | /** sets the resolution of the LCD so that the pos() call works properly |
shimniok | 1:2f436b8aebf4 | 145 | * defaults to LCD_128x64. |
shimniok | 1:2f436b8aebf4 | 146 | * |
shimniok | 1:2f436b8aebf4 | 147 | * @param type is the type of LCD, either LCD_128x64 or LCD_160x128 |
shimniok | 1:2f436b8aebf4 | 148 | */ |
shimniok | 1:2f436b8aebf4 | 149 | void resolution(int type); |
shimniok | 1:2f436b8aebf4 | 150 | |
shimniok | 1:2f436b8aebf4 | 151 | /** sets the resolution of the LCD in x and y coordinates which determines |
shimniok | 1:2f436b8aebf4 | 152 | * how rows and columns are calculated in the pos() call. Defaults to |
shimniok | 1:2f436b8aebf4 | 153 | * x=128, y=64 |
shimniok | 1:2f436b8aebf4 | 154 | * |
shimniok | 1:2f436b8aebf4 | 155 | * @param x is the number of horizontal pixels |
shimniok | 1:2f436b8aebf4 | 156 | * @param y is the number of vertical pixels |
shimniok | 1:2f436b8aebf4 | 157 | */ |
shimniok | 1:2f436b8aebf4 | 158 | void resolution(int x, int y); |
shimniok | 1:2f436b8aebf4 | 159 | |
shimniok | 1:2f436b8aebf4 | 160 | private: |
shimniok | 1:2f436b8aebf4 | 161 | int _xMax; |
shimniok | 1:2f436b8aebf4 | 162 | int _yMax; |
shimniok | 0:a3d518d2f36f | 163 | }; |
shimniok | 0:a3d518d2f36f | 164 | |
shimniok | 0:a3d518d2f36f | 165 | |
shimniok | 0:a3d518d2f36f | 166 | #endif |