Library to control a QVGA TFT connected to SPI. You can use printf to print text The lib can handle different fonts, draw lines, circles, rect and bmp

Committer:
dreschpe
Date:
Wed Jul 13 08:25:10 2011 +0000
Revision:
1:aa3356b16080
Parent:
0:cccc5726bdf3
Child:
2:0cc880f230ad
Add Docu

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 0:cccc5726bdf3 1 /* mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller
dreschpe 0:cccc5726bdf3 2 * Copyright (c) 2011 Peter Drescher - DC2PD
dreschpe 0:cccc5726bdf3 3 *
dreschpe 0:cccc5726bdf3 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dreschpe 0:cccc5726bdf3 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dreschpe 0:cccc5726bdf3 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dreschpe 0:cccc5726bdf3 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dreschpe 0:cccc5726bdf3 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dreschpe 0:cccc5726bdf3 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dreschpe 0:cccc5726bdf3 10 * THE SOFTWARE.
dreschpe 0:cccc5726bdf3 11 */
dreschpe 0:cccc5726bdf3 12
dreschpe 0:cccc5726bdf3 13 #ifndef MBED_SPI_TFT_H
dreschpe 0:cccc5726bdf3 14 #define MBED_SPI_TFT_H
dreschpe 0:cccc5726bdf3 15
dreschpe 1:aa3356b16080 16 /** Display control class, based on GraphicsDisplay and TextDisplay
dreschpe 1:aa3356b16080 17 *
dreschpe 1:aa3356b16080 18 * Example:
dreschpe 1:aa3356b16080 19 * @code
dreschpe 1:aa3356b16080 20 * #include "stdio.h"
dreschpe 1:aa3356b16080 21 * #include "mbed.h"
dreschpe 1:aa3356b16080 22 * #include "SPI_TFT.h"
dreschpe 1:aa3356b16080 23 * #include "string"
dreschpe 1:aa3356b16080 24 * #include "Arial12x12.h"
dreschpe 1:aa3356b16080 25 * #include "Arial24x23.h"
dreschpe 1:aa3356b16080 26 *
dreschpe 1:aa3356b16080 27 *
dreschpe 1:aa3356b16080 28 *
dreschpe 1:aa3356b16080 29 * // the TFT is connected to SPI pin 5-7
dreschpe 1:aa3356b16080 30 * SPI_TFT TFT(p5, p6, p7, p8, p15,"TFT"); // mosi, miso, sclk, cs, reset
dreschpe 1:aa3356b16080 31 *
dreschpe 1:aa3356b16080 32 * int main() {
dreschpe 1:aa3356b16080 33 * TFT.claim(stdout); // send stdout to the TFT display
dreschpe 1:aa3356b16080 34 * //TFT.claim(stderr); // send stderr to the TFT display
dreschpe 1:aa3356b16080 35 *
dreschpe 1:aa3356b16080 36 * TFT.background(Black); // set background to black
dreschpe 1:aa3356b16080 37 * TFT.foreground(White); // set chars to white
dreschpe 1:aa3356b16080 38 * TFT.cls(); // clear the screen
dreschpe 1:aa3356b16080 39 * TFT.set_font((unsigned char*) Arial12x12); // select the font
dreschpe 1:aa3356b16080 40 *
dreschpe 1:aa3356b16080 41 * TFT.set_orientation(0);
dreschpe 1:aa3356b16080 42 * TFT.locate(0,0);
dreschpe 1:aa3356b16080 43 * printf(" Hello Mbed 0");
dreschpe 1:aa3356b16080 44 * TFT.set_font((unsigned char*) Arial24x23); // select font 2
dreschpe 1:aa3356b16080 45 * TFT.locate(2,5);
dreschpe 1:aa3356b16080 46 * TFT.printf("Bigger Font");
dreschpe 1:aa3356b16080 47 * }
dreschpe 1:aa3356b16080 48 * @endcode
dreschpe 1:aa3356b16080 49 */
dreschpe 1:aa3356b16080 50
dreschpe 1:aa3356b16080 51
dreschpe 0:cccc5726bdf3 52 #include "mbed.h"
dreschpe 0:cccc5726bdf3 53 #include "GraphicsDisplay.h"
dreschpe 0:cccc5726bdf3 54
dreschpe 0:cccc5726bdf3 55 #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
dreschpe 0:cccc5726bdf3 56
dreschpe 0:cccc5726bdf3 57 #define SPI_START (0x70) /* Start byte for SPI transfer */
dreschpe 0:cccc5726bdf3 58 #define SPI_RD (0x01) /* WR bit 1 within start */
dreschpe 0:cccc5726bdf3 59 #define SPI_WR (0x00) /* WR bit 0 within start */
dreschpe 0:cccc5726bdf3 60 #define SPI_DATA (0x02) /* RS bit 1 within start byte */
dreschpe 0:cccc5726bdf3 61 #define SPI_INDEX (0x00) /* RS bit 0 within start byte */
dreschpe 0:cccc5726bdf3 62
dreschpe 0:cccc5726bdf3 63
dreschpe 0:cccc5726bdf3 64 /* some RGB color definitions */
dreschpe 0:cccc5726bdf3 65 #define Black 0x0000 /* 0, 0, 0 */
dreschpe 0:cccc5726bdf3 66 #define Navy 0x000F /* 0, 0, 128 */
dreschpe 0:cccc5726bdf3 67 #define DarkGreen 0x03E0 /* 0, 128, 0 */
dreschpe 0:cccc5726bdf3 68 #define DarkCyan 0x03EF /* 0, 128, 128 */
dreschpe 0:cccc5726bdf3 69 #define Maroon 0x7800 /* 128, 0, 0 */
dreschpe 0:cccc5726bdf3 70 #define Purple 0x780F /* 128, 0, 128 */
dreschpe 0:cccc5726bdf3 71 #define Olive 0x7BE0 /* 128, 128, 0 */
dreschpe 0:cccc5726bdf3 72 #define LightGrey 0xC618 /* 192, 192, 192 */
dreschpe 0:cccc5726bdf3 73 #define DarkGrey 0x7BEF /* 128, 128, 128 */
dreschpe 0:cccc5726bdf3 74 #define Blue 0x001F /* 0, 0, 255 */
dreschpe 0:cccc5726bdf3 75 #define Green 0x07E0 /* 0, 255, 0 */
dreschpe 0:cccc5726bdf3 76 #define Cyan 0x07FF /* 0, 255, 255 */
dreschpe 0:cccc5726bdf3 77 #define Red 0xF800 /* 255, 0, 0 */
dreschpe 0:cccc5726bdf3 78 #define Magenta 0xF81F /* 255, 0, 255 */
dreschpe 0:cccc5726bdf3 79 #define Yellow 0xFFE0 /* 255, 255, 0 */
dreschpe 0:cccc5726bdf3 80 #define White 0xFFFF /* 255, 255, 255 */
dreschpe 0:cccc5726bdf3 81
dreschpe 0:cccc5726bdf3 82
dreschpe 0:cccc5726bdf3 83
dreschpe 1:aa3356b16080 84
dreschpe 1:aa3356b16080 85
dreschpe 0:cccc5726bdf3 86 class SPI_TFT : public GraphicsDisplay {
dreschpe 0:cccc5726bdf3 87 public:
dreschpe 1:aa3356b16080 88
dreschpe 1:aa3356b16080 89 /** Create a SPI_TFT object connected to SPI and two pins
dreschpe 1:aa3356b16080 90 *
dreschpe 1:aa3356b16080 91 * @param mosi,miso,sclk SPI
dreschpe 1:aa3356b16080 92 * @param cs pin connected to CS of display
dreschpe 1:aa3356b16080 93 * @param reset pin connected to RESET of display
dreschpe 1:aa3356b16080 94 *
dreschpe 1:aa3356b16080 95 */
dreschpe 0:cccc5726bdf3 96 SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset,const char* name ="TFT");
dreschpe 1:aa3356b16080 97
dreschpe 1:aa3356b16080 98 /** Get the width of the screen in pixel
dreschpe 1:aa3356b16080 99 *
dreschpe 1:aa3356b16080 100 * @param
dreschpe 1:aa3356b16080 101 * @returns width of screen in pixel
dreschpe 1:aa3356b16080 102 *
dreschpe 1:aa3356b16080 103 */
dreschpe 0:cccc5726bdf3 104 virtual int width();
dreschpe 1:aa3356b16080 105
dreschpe 1:aa3356b16080 106 /** Get the height of the screen in pixel
dreschpe 1:aa3356b16080 107 *
dreschpe 1:aa3356b16080 108 * @param
dreschpe 1:aa3356b16080 109 * @returns height of screen in pixel
dreschpe 1:aa3356b16080 110 *
dreschpe 1:aa3356b16080 111 */
dreschpe 0:cccc5726bdf3 112 virtual int height();
dreschpe 1:aa3356b16080 113
dreschpe 1:aa3356b16080 114 /** Draw a pixel at x,y with color
dreschpe 1:aa3356b16080 115 *
dreschpe 1:aa3356b16080 116 * @param x horizontal position
dreschpe 1:aa3356b16080 117 * @param y vertical position
dreschpe 1:aa3356b16080 118 * @param color 16 bit pixel color
dreschpe 1:aa3356b16080 119 **/
dreschpe 0:cccc5726bdf3 120 virtual void pixel(int x, int y, int colour);
dreschpe 1:aa3356b16080 121
dreschpe 1:aa3356b16080 122 /** draw a circle
dreschpe 1:aa3356b16080 123 *
dreschpe 1:aa3356b16080 124 * @param x0,y0 center
dreschpe 1:aa3356b16080 125 * @param r radius
dreschpe 1:aa3356b16080 126 * @param color 16 bit color *
dreschpe 1:aa3356b16080 127 *
dreschpe 1:aa3356b16080 128 */
dreschpe 0:cccc5726bdf3 129 void circle(int x, int y, int r, int colour);
dreschpe 1:aa3356b16080 130
dreschpe 1:aa3356b16080 131 /** draw a 1 pixel line
dreschpe 1:aa3356b16080 132 *
dreschpe 1:aa3356b16080 133 * @param x0,y0 start point
dreschpe 1:aa3356b16080 134 * @param x1,y1 stop point
dreschpe 1:aa3356b16080 135 * @param color 16 bit color
dreschpe 1:aa3356b16080 136 *
dreschpe 1:aa3356b16080 137 **/
dreschpe 0:cccc5726bdf3 138 void line(int x0, int y0, int x1, int y1, int colour);
dreschpe 1:aa3356b16080 139
dreschpe 1:aa3356b16080 140 /** draw a rect
dreschpe 1:aa3356b16080 141 *
dreschpe 1:aa3356b16080 142 * @param x0,y0 top left corner
dreschpe 1:aa3356b16080 143 * @param x1,y1 down right corner
dreschpe 1:aa3356b16080 144 * @param color 16 bit color
dreschpe 1:aa3356b16080 145 * *
dreschpe 1:aa3356b16080 146 **/
dreschpe 0:cccc5726bdf3 147 void rect(int x0, int y0, int x1, int y1, int colour);
dreschpe 1:aa3356b16080 148
dreschpe 1:aa3356b16080 149 /** draw a filled rect
dreschpe 1:aa3356b16080 150 *
dreschpe 1:aa3356b16080 151 * @param x0,y0 top left corner
dreschpe 1:aa3356b16080 152 * @param x1,y1 down right corner
dreschpe 1:aa3356b16080 153 * @param color 16 bit color
dreschpe 1:aa3356b16080 154 *
dreschpe 1:aa3356b16080 155 **/
dreschpe 0:cccc5726bdf3 156 void fillrect(int x0, int y0, int x1, int y1, int colour);
dreschpe 1:aa3356b16080 157
dreschpe 1:aa3356b16080 158 /** setup cursor position
dreschpe 1:aa3356b16080 159 *
dreschpe 1:aa3356b16080 160 * @param column 0 to max
dreschpe 1:aa3356b16080 161 * @param row 0 to max
dreschpe 1:aa3356b16080 162 * max depend on font size
dreschpe 1:aa3356b16080 163 **/
dreschpe 0:cccc5726bdf3 164 void locate(int column, int row);
dreschpe 1:aa3356b16080 165
dreschpe 1:aa3356b16080 166 /** Fill the screen with _backgroun color
dreschpe 1:aa3356b16080 167 *
dreschpe 1:aa3356b16080 168 **/
dreschpe 0:cccc5726bdf3 169 virtual void cls (void);
dreschpe 1:aa3356b16080 170
dreschpe 1:aa3356b16080 171 /** calculate the max number of char in a line
dreschpe 1:aa3356b16080 172 *
dreschpe 1:aa3356b16080 173 * @returns max columns
dreschpe 1:aa3356b16080 174 * depends on actual font size
dreschpe 1:aa3356b16080 175 *
dreschpe 1:aa3356b16080 176 */
dreschpe 0:cccc5726bdf3 177 int columns(void);
dreschpe 1:aa3356b16080 178
dreschpe 1:aa3356b16080 179 /** calculate the max number of columns
dreschpe 1:aa3356b16080 180 *
dreschpe 1:aa3356b16080 181 * @returns max column
dreschpe 1:aa3356b16080 182 * depends on actual font size
dreschpe 1:aa3356b16080 183 *
dreschpe 1:aa3356b16080 184 */
dreschpe 0:cccc5726bdf3 185 int rows(void);
dreschpe 1:aa3356b16080 186
dreschpe 1:aa3356b16080 187 /** put a char on the screen
dreschpe 1:aa3356b16080 188 *
dreschpe 1:aa3356b16080 189 * @param value char to print
dreschpe 1:aa3356b16080 190 * @returns printed char
dreschpe 1:aa3356b16080 191 *
dreschpe 1:aa3356b16080 192 */
dreschpe 0:cccc5726bdf3 193 int _putc(int value);
dreschpe 1:aa3356b16080 194
dreschpe 1:aa3356b16080 195 /** draw a character on given position out of the active font to the TFT
dreschpe 1:aa3356b16080 196 *
dreschpe 1:aa3356b16080 197 * @param col column
dreschpe 1:aa3356b16080 198 * @param row row
dreschpe 1:aa3356b16080 199 * @param c char to print
dreschpe 1:aa3356b16080 200 *
dreschpe 1:aa3356b16080 201 */
dreschpe 0:cccc5726bdf3 202 virtual void character(int col, int row, int c);
dreschpe 1:aa3356b16080 203
dreschpe 1:aa3356b16080 204 /** paint a bitmap on the TFT
dreschpe 1:aa3356b16080 205 *
dreschpe 1:aa3356b16080 206 * @param x,y : upper left corner
dreschpe 1:aa3356b16080 207 * @param w width of bitmap
dreschpe 1:aa3356b16080 208 * @param h high of bitmap
dreschpe 1:aa3356b16080 209 * @param *bitmap pointer to the bitmap data
dreschpe 1:aa3356b16080 210 *
dreschpe 1:aa3356b16080 211 * bitmap format: 16 bit R5 G6 B5
dreschpe 1:aa3356b16080 212 *
dreschpe 1:aa3356b16080 213 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
dreschpe 1:aa3356b16080 214 * use winhex to load this file and mark data stating at offset 0x46 to end
dreschpe 1:aa3356b16080 215 * use edit -> copy block -> C Source to export C array
dreschpe 1:aa3356b16080 216 * paste this array into your program
dreschpe 1:aa3356b16080 217 *
dreschpe 1:aa3356b16080 218 */
dreschpe 0:cccc5726bdf3 219 void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap);
dreschpe 1:aa3356b16080 220
dreschpe 1:aa3356b16080 221 /** select the font to use
dreschpe 1:aa3356b16080 222 *
dreschpe 1:aa3356b16080 223 * @param f pointer to font array
dreschpe 1:aa3356b16080 224 *
dreschpe 1:aa3356b16080 225 * font array can created with GLCD Font Creator from http://www.mikroe.com
dreschpe 1:aa3356b16080 226 * you have to add 4 parameter at the beginning of the font array to use:
dreschpe 1:aa3356b16080 227 * - the number of byte / char
dreschpe 1:aa3356b16080 228 * - the vertial size in pixel
dreschpe 1:aa3356b16080 229 * - the horizontal size in pixel
dreschpe 1:aa3356b16080 230 * - the number of byte per vertical line
dreschpe 1:aa3356b16080 231 * you also have to change the array to char[]
dreschpe 1:aa3356b16080 232 *
dreschpe 1:aa3356b16080 233 */
dreschpe 0:cccc5726bdf3 234 void set_font(unsigned char* f);
dreschpe 1:aa3356b16080 235
dreschpe 1:aa3356b16080 236 /** Set the orientation of the screen
dreschpe 1:aa3356b16080 237 * x,y: 0,0 is always top left
dreschpe 1:aa3356b16080 238 *
dreschpe 1:aa3356b16080 239 * @param o direction to use the screen (0-3) 90° Steps
dreschpe 1:aa3356b16080 240 *
dreschpe 1:aa3356b16080 241 */
dreschpe 0:cccc5726bdf3 242 void set_orientation(unsigned int o);
dreschpe 0:cccc5726bdf3 243
dreschpe 0:cccc5726bdf3 244 SPI _spi;
dreschpe 0:cccc5726bdf3 245 DigitalOut _cs;
dreschpe 0:cccc5726bdf3 246 DigitalOut _reset;
dreschpe 0:cccc5726bdf3 247 unsigned char* font;
dreschpe 0:cccc5726bdf3 248
dreschpe 0:cccc5726bdf3 249 protected:
dreschpe 1:aa3356b16080 250
dreschpe 1:aa3356b16080 251 /** draw a horizontal line
dreschpe 1:aa3356b16080 252 *
dreschpe 1:aa3356b16080 253 * @param x0 horizontal start
dreschpe 1:aa3356b16080 254 * @param x1 horizontal stop
dreschpe 1:aa3356b16080 255 * @param y vertical position
dreschpe 1:aa3356b16080 256 * @param color 16 bit color
dreschpe 1:aa3356b16080 257 *
dreschpe 1:aa3356b16080 258 **/
dreschpe 0:cccc5726bdf3 259 void hline(int x0, int x1, int y, int colour);
dreschpe 1:aa3356b16080 260
dreschpe 1:aa3356b16080 261 /** draw a vertical line
dreschpe 1:aa3356b16080 262 *
dreschpe 1:aa3356b16080 263 * @param x horizontal position
dreschpe 1:aa3356b16080 264 * @param y0 vertical start
dreschpe 1:aa3356b16080 265 * @param y1 vertical stop
dreschpe 1:aa3356b16080 266 * @param color 16 bit color
dreschpe 1:aa3356b16080 267 **/
dreschpe 0:cccc5726bdf3 268 void vline(int y0, int y1, int x, int colour);
dreschpe 1:aa3356b16080 269
dreschpe 1:aa3356b16080 270 /** Set draw window region
dreschpe 1:aa3356b16080 271 *
dreschpe 1:aa3356b16080 272 * @param x horizontal position
dreschpe 1:aa3356b16080 273 * @param y vertical position
dreschpe 1:aa3356b16080 274 * @param w window width in pixel
dreschpe 1:aa3356b16080 275 * @param h window height in pixels
dreschpe 1:aa3356b16080 276 **/
dreschpe 0:cccc5726bdf3 277 void window (unsigned int x, unsigned int y, unsigned int w, unsigned int h);
dreschpe 1:aa3356b16080 278
dreschpe 1:aa3356b16080 279 /** Set draw window region to whole screen
dreschpe 1:aa3356b16080 280 *
dreschpe 1:aa3356b16080 281 **/
dreschpe 0:cccc5726bdf3 282 void WindowMax (void);
dreschpe 1:aa3356b16080 283
dreschpe 1:aa3356b16080 284 /** Init the HX8347D controller
dreschpe 1:aa3356b16080 285 *
dreschpe 1:aa3356b16080 286 */
dreschpe 0:cccc5726bdf3 287 void tft_reset();
dreschpe 1:aa3356b16080 288
dreschpe 1:aa3356b16080 289 /** Write data to the LCD controller
dreschpe 1:aa3356b16080 290 *
dreschpe 1:aa3356b16080 291 * @param dat data written to LCD controller
dreschpe 1:aa3356b16080 292 *
dreschpe 1:aa3356b16080 293 */
dreschpe 0:cccc5726bdf3 294 void wr_dat(int value);
dreschpe 1:aa3356b16080 295
dreschpe 1:aa3356b16080 296 /** Write a command the LCD controller
dreschpe 1:aa3356b16080 297 *
dreschpe 1:aa3356b16080 298 * @param cmd: command to be written
dreschpe 1:aa3356b16080 299 *
dreschpe 1:aa3356b16080 300 */
dreschpe 0:cccc5726bdf3 301 void wr_cmd(int value);
dreschpe 1:aa3356b16080 302
dreschpe 1:aa3356b16080 303 /** Start data sequence to the LCD controller
dreschpe 1:aa3356b16080 304 *
dreschpe 1:aa3356b16080 305 */
dreschpe 0:cccc5726bdf3 306 void wr_dat_start();
dreschpe 1:aa3356b16080 307
dreschpe 1:aa3356b16080 308 /** Stop of data writing to the LCD controller
dreschpe 1:aa3356b16080 309 *
dreschpe 1:aa3356b16080 310 */
dreschpe 0:cccc5726bdf3 311 void wr_dat_stop();
dreschpe 1:aa3356b16080 312
dreschpe 1:aa3356b16080 313 /** write data to the LCD controller
dreschpe 1:aa3356b16080 314 *
dreschpe 1:aa3356b16080 315 * @param data to be written
dreschpe 1:aa3356b16080 316 * *
dreschpe 1:aa3356b16080 317 */
dreschpe 0:cccc5726bdf3 318 void wr_dat_only(unsigned short dat);
dreschpe 1:aa3356b16080 319
dreschpe 1:aa3356b16080 320 /** Read data from the LCD controller
dreschpe 1:aa3356b16080 321 *
dreschpe 1:aa3356b16080 322 * @returns data from LCD controller
dreschpe 1:aa3356b16080 323 *
dreschpe 1:aa3356b16080 324 */
dreschpe 0:cccc5726bdf3 325 unsigned short rd_dat(void);
dreschpe 1:aa3356b16080 326
dreschpe 1:aa3356b16080 327 /** Write a value to the to a LCD register
dreschpe 1:aa3356b16080 328 *
dreschpe 1:aa3356b16080 329 * @param reg register to be written
dreschpe 1:aa3356b16080 330 * @param val data to be written
dreschpe 1:aa3356b16080 331 */
dreschpe 0:cccc5726bdf3 332 void wr_reg (unsigned char reg, unsigned short val);
dreschpe 1:aa3356b16080 333
dreschpe 1:aa3356b16080 334 /** Read a LCD register
dreschpe 1:aa3356b16080 335 *
dreschpe 1:aa3356b16080 336 * @param reg register to be read
dreschpe 1:aa3356b16080 337 * @returns value of the register
dreschpe 1:aa3356b16080 338 */
dreschpe 0:cccc5726bdf3 339 unsigned short rd_reg (unsigned char reg);
dreschpe 0:cccc5726bdf3 340
dreschpe 0:cccc5726bdf3 341 unsigned int orientation;
dreschpe 0:cccc5726bdf3 342 unsigned int char_x;
dreschpe 0:cccc5726bdf3 343
dreschpe 0:cccc5726bdf3 344 };
dreschpe 0:cccc5726bdf3 345
dreschpe 0:cccc5726bdf3 346 #endif