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