Lib for the new LCD Display with ILI9341 controller

Dependents:   RM3100

Fork of SPI_TFT_ILI9341 by Peter Drescher

Committer:
x893
Date:
Tue Mar 06 08:39:59 2018 +0000
Revision:
14:c177053accff
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
x893 14:c177053accff 1 #ifndef MBED_SPI_TFT_HX8347D_H
x893 14:c177053accff 2 #define MBED_SPI_TFT_HX8347D_H
x893 14:c177053accff 3
x893 14:c177053accff 4 #include "mbed.h"
x893 14:c177053accff 5 #include "GraphicsDisplay.h"
x893 14:c177053accff 6
x893 14:c177053accff 7 #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
x893 14:c177053accff 8
x893 14:c177053accff 9
x893 14:c177053accff 10 /* some RGB color definitions */
x893 14:c177053accff 11 #define Black 0x0000 /* 0, 0, 0 */
x893 14:c177053accff 12 #define Navy 0x000F /* 0, 0, 128 */
x893 14:c177053accff 13 #define DarkGreen 0x03E0 /* 0, 128, 0 */
x893 14:c177053accff 14 #define DarkCyan 0x03EF /* 0, 128, 128 */
x893 14:c177053accff 15 #define Maroon 0x7800 /* 128, 0, 0 */
x893 14:c177053accff 16 #define Purple 0x780F /* 128, 0, 128 */
x893 14:c177053accff 17 #define Olive 0x7BE0 /* 128, 128, 0 */
x893 14:c177053accff 18 #define LightGrey 0xC618 /* 192, 192, 192 */
x893 14:c177053accff 19 #define DarkGrey 0x7BEF /* 128, 128, 128 */
x893 14:c177053accff 20 #define Blue 0x001F /* 0, 0, 255 */
x893 14:c177053accff 21 #define Green 0x07E0 /* 0, 255, 0 */
x893 14:c177053accff 22 #define Cyan 0x07FF /* 0, 255, 255 */
x893 14:c177053accff 23 #define Red 0xF800 /* 255, 0, 0 */
x893 14:c177053accff 24 #define Magenta 0xF81F /* 255, 0, 255 */
x893 14:c177053accff 25 #define Yellow 0xFFE0 /* 255, 255, 0 */
x893 14:c177053accff 26 #define White 0xFFFF /* 255, 255, 255 */
x893 14:c177053accff 27 #define Orange 0xFD20 /* 255, 165, 0 */
x893 14:c177053accff 28 #define GreenYellow 0xAFE5 /* 173, 255, 47 */
x893 14:c177053accff 29
x893 14:c177053accff 30
x893 14:c177053accff 31 /** Display control class, based on GraphicsDisplay and TextDisplay
x893 14:c177053accff 32 *
x893 14:c177053accff 33 * Example:
x893 14:c177053accff 34 * @code
x893 14:c177053accff 35 * #include "stdio.h"
x893 14:c177053accff 36 * #include "mbed.h"
x893 14:c177053accff 37 * #include "SPI_TFT_HX8347D.h"
x893 14:c177053accff 38 * #include "string"
x893 14:c177053accff 39 * #include "Arial12x12.h"
x893 14:c177053accff 40 * #include "Arial24x23.h"
x893 14:c177053accff 41 *
x893 14:c177053accff 42 *
x893 14:c177053accff 43 *
x893 14:c177053accff 44 * // the TFT is connected to SPI pin 5-7 and IO's 8-10
x893 14:c177053accff 45 * SPI_TFT_HX8347D TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc
x893 14:c177053accff 46 * If your display need a signal for switch the backlight use a aditional IO pin in your program
x893 14:c177053accff 47 *
x893 14:c177053accff 48 * int main() {
x893 14:c177053accff 49 * TFT.claim(stdout); // send stdout to the TFT display
x893 14:c177053accff 50 * //TFT.claim(stderr); // send stderr to the TFT display
x893 14:c177053accff 51 *
x893 14:c177053accff 52 * TFT.background(Black); // set background to black
x893 14:c177053accff 53 * TFT.foreground(White); // set chars to white
x893 14:c177053accff 54 * TFT.cls(); // clear the screen
x893 14:c177053accff 55 * TFT.set_font((unsigned char*) Arial12x12); // select the font
x893 14:c177053accff 56 *
x893 14:c177053accff 57 * TFT.set_orientation(0);
x893 14:c177053accff 58 * printf(" Hello Mbed 0");
x893 14:c177053accff 59 * TFT.set_font((unsigned char*) Arial24x23); // select font 2
x893 14:c177053accff 60 * TFT.locate(48,115);
x893 14:c177053accff 61 * TFT.printf("Bigger Font");
x893 14:c177053accff 62 * }
x893 14:c177053accff 63 * @endcode
x893 14:c177053accff 64 */
x893 14:c177053accff 65 class SPI_TFT_HX8347D : public GraphicsDisplay , public SPI {
x893 14:c177053accff 66 public:
x893 14:c177053accff 67
x893 14:c177053accff 68 /** Create a SPI_TFT object connected to SPI and three pins
x893 14:c177053accff 69 *
x893 14:c177053accff 70 * @param mosi pin connected to SDO of display
x893 14:c177053accff 71 * @param miso pin connected to SDI of display
x893 14:c177053accff 72 * @param sclk pin connected to RS of display
x893 14:c177053accff 73 * @param cs pin connected to CS of display
x893 14:c177053accff 74 * @param reset pin connected to RESET of display
x893 14:c177053accff 75 * @param dc pin connected to WR of display
x893 14:c177053accff 76 * the IM pins have to be set to 1110 (3-0)
x893 14:c177053accff 77 */
x893 14:c177053accff 78 SPI_TFT_HX8347D(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT");
x893 14:c177053accff 79
x893 14:c177053accff 80 /** Get the width of the screen in pixel
x893 14:c177053accff 81 *
x893 14:c177053accff 82 * @returns width of screen in pixel
x893 14:c177053accff 83 *
x893 14:c177053accff 84 */
x893 14:c177053accff 85 virtual int width();
x893 14:c177053accff 86
x893 14:c177053accff 87 /** Get the height of the screen in pixel
x893 14:c177053accff 88 *
x893 14:c177053accff 89 * @returns height of screen in pixel
x893 14:c177053accff 90 *
x893 14:c177053accff 91 */
x893 14:c177053accff 92 virtual int height();
x893 14:c177053accff 93
x893 14:c177053accff 94 /** Draw a pixel at x,y with color
x893 14:c177053accff 95 *
x893 14:c177053accff 96 * @param x horizontal position
x893 14:c177053accff 97 * @param y vertical position
x893 14:c177053accff 98 * @param color 16 bit pixel color
x893 14:c177053accff 99 */
x893 14:c177053accff 100 virtual void pixel(int x, int y,int colour);
x893 14:c177053accff 101
x893 14:c177053accff 102 /** draw a circle
x893 14:c177053accff 103 *
x893 14:c177053accff 104 * @param x0,y0 center
x893 14:c177053accff 105 * @param r radius
x893 14:c177053accff 106 * @param color 16 bit color *
x893 14:c177053accff 107 *
x893 14:c177053accff 108 */
x893 14:c177053accff 109 void circle(int x, int y, int r, int colour);
x893 14:c177053accff 110
x893 14:c177053accff 111 /** draw a filled circle
x893 14:c177053accff 112 *
x893 14:c177053accff 113 * @param x0,y0 center
x893 14:c177053accff 114 * @param r radius
x893 14:c177053accff 115 * @param color 16 bit color *
x893 14:c177053accff 116 */
x893 14:c177053accff 117 void fillcircle(int x, int y, int r, int colour);
x893 14:c177053accff 118
x893 14:c177053accff 119
x893 14:c177053accff 120 /** draw a 1 pixel line
x893 14:c177053accff 121 *
x893 14:c177053accff 122 * @param x0,y0 start point
x893 14:c177053accff 123 * @param x1,y1 stop point
x893 14:c177053accff 124 * @param color 16 bit color
x893 14:c177053accff 125 *
x893 14:c177053accff 126 */
x893 14:c177053accff 127 void line(int x0, int y0, int x1, int y1, int colour);
x893 14:c177053accff 128
x893 14:c177053accff 129 /** draw a rect
x893 14:c177053accff 130 *
x893 14:c177053accff 131 * @param x0,y0 top left corner
x893 14:c177053accff 132 * @param x1,y1 down right corner
x893 14:c177053accff 133 * @param color 16 bit color
x893 14:c177053accff 134 * *
x893 14:c177053accff 135 */
x893 14:c177053accff 136 void rect(int x0, int y0, int x1, int y1, int colour);
x893 14:c177053accff 137
x893 14:c177053accff 138 /** draw a filled rect
x893 14:c177053accff 139 *
x893 14:c177053accff 140 * @param x0,y0 top left corner
x893 14:c177053accff 141 * @param x1,y1 down right corner
x893 14:c177053accff 142 * @param color 16 bit color
x893 14:c177053accff 143 *
x893 14:c177053accff 144 */
x893 14:c177053accff 145 void fillrect(int x0, int y0, int x1, int y1, int colour);
x893 14:c177053accff 146
x893 14:c177053accff 147 /** setup cursor position
x893 14:c177053accff 148 *
x893 14:c177053accff 149 * @param x x-position (top left)
x893 14:c177053accff 150 * @param y y-position
x893 14:c177053accff 151 */
x893 14:c177053accff 152 virtual void locate(int x, int y);
x893 14:c177053accff 153
x893 14:c177053accff 154 /** Fill the screen with _backgroun color
x893 14:c177053accff 155 *
x893 14:c177053accff 156 */
x893 14:c177053accff 157 virtual void cls (void);
x893 14:c177053accff 158
x893 14:c177053accff 159 /** calculate the max number of char in a line
x893 14:c177053accff 160 *
x893 14:c177053accff 161 * @returns max columns
x893 14:c177053accff 162 * depends on actual font size
x893 14:c177053accff 163 *
x893 14:c177053accff 164 */
x893 14:c177053accff 165 virtual int columns(void);
x893 14:c177053accff 166
x893 14:c177053accff 167 /** calculate the max number of columns
x893 14:c177053accff 168 *
x893 14:c177053accff 169 * @returns max column
x893 14:c177053accff 170 * depends on actual font size
x893 14:c177053accff 171 *
x893 14:c177053accff 172 */
x893 14:c177053accff 173 virtual int rows(void);
x893 14:c177053accff 174
x893 14:c177053accff 175 /** put a char on the screen
x893 14:c177053accff 176 *
x893 14:c177053accff 177 * @param value char to print
x893 14:c177053accff 178 * @returns printed char
x893 14:c177053accff 179 *
x893 14:c177053accff 180 */
x893 14:c177053accff 181 virtual int _putc(int value);
x893 14:c177053accff 182
x893 14:c177053accff 183 /** draw a character on given position out of the active font to the TFT
x893 14:c177053accff 184 *
x893 14:c177053accff 185 * @param x x-position of char (top left)
x893 14:c177053accff 186 * @param y y-position
x893 14:c177053accff 187 * @param c char to print
x893 14:c177053accff 188 *
x893 14:c177053accff 189 */
x893 14:c177053accff 190 virtual void character(int x, int y, int c);
x893 14:c177053accff 191
x893 14:c177053accff 192 /** paint a bitmap on the TFT
x893 14:c177053accff 193 *
x893 14:c177053accff 194 * @param x,y : upper left corner
x893 14:c177053accff 195 * @param w width of bitmap
x893 14:c177053accff 196 * @param h high of bitmap
x893 14:c177053accff 197 * @param *bitmap pointer to the bitmap data
x893 14:c177053accff 198 *
x893 14:c177053accff 199 * bitmap format: 16 bit R5 G6 B5
x893 14:c177053accff 200 *
x893 14:c177053accff 201 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
x893 14:c177053accff 202 * use winhex to load this file and mark data stating at offset 0x46 to end
x893 14:c177053accff 203 * use edit -> copy block -> C Source to export C array
x893 14:c177053accff 204 * paste this array into your program
x893 14:c177053accff 205 *
x893 14:c177053accff 206 * define the array as static const unsigned char to put it into flash memory
x893 14:c177053accff 207 * cast the pointer to (unsigned char *) :
x893 14:c177053accff 208 * tft.Bitmap(10,40,309,50,(unsigned char *)scala);
x893 14:c177053accff 209 */
x893 14:c177053accff 210 void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap);
x893 14:c177053accff 211
x893 14:c177053accff 212
x893 14:c177053accff 213 /** paint a 16 bit BMP from filesytem on the TFT (slow)
x893 14:c177053accff 214 *
x893 14:c177053accff 215 * @param x,y : position of upper left corner
x893 14:c177053accff 216 * @param *Name_BMP name of the BMP file with drive: "/local/test.bmp"
x893 14:c177053accff 217 *
x893 14:c177053accff 218 * @returns 1 if bmp file was found and painted
x893 14:c177053accff 219 * @returns 0 if bmp file was found not found
x893 14:c177053accff 220 * @returns -1 if file is no bmp
x893 14:c177053accff 221 * @returns -2 if bmp file is no 16 bit bmp
x893 14:c177053accff 222 * @returns -3 if bmp file is to big for screen
x893 14:c177053accff 223 * @returns -4 if buffer malloc go wrong
x893 14:c177053accff 224 *
x893 14:c177053accff 225 * bitmap format: 16 bit R5 G6 B5
x893 14:c177053accff 226 *
x893 14:c177053accff 227 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
x893 14:c177053accff 228 * copy to internal file system or SD card
x893 14:c177053accff 229 */
x893 14:c177053accff 230
x893 14:c177053accff 231 int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP);
x893 14:c177053accff 232
x893 14:c177053accff 233
x893 14:c177053accff 234
x893 14:c177053accff 235 /** select the font to use
x893 14:c177053accff 236 *
x893 14:c177053accff 237 * @param f pointer to font array
x893 14:c177053accff 238 *
x893 14:c177053accff 239 * font array can created with GLCD Font Creator from http://www.mikroe.com
x893 14:c177053accff 240 * you have to add 4 parameter at the beginning of the font array to use:
x893 14:c177053accff 241 * - the number of byte / char
x893 14:c177053accff 242 * - the vertial size in pixel
x893 14:c177053accff 243 * - the horizontal size in pixel
x893 14:c177053accff 244 * - the number of byte per vertical line
x893 14:c177053accff 245 * you also have to change the array to char[]
x893 14:c177053accff 246 *
x893 14:c177053accff 247 */
x893 14:c177053accff 248 void set_font(unsigned char* f);
x893 14:c177053accff 249
x893 14:c177053accff 250 /** Set the orientation of the screen
x893 14:c177053accff 251 * x,y: 0,0 is always top left
x893 14:c177053accff 252 *
x893 14:c177053accff 253 * @param o direction to use the screen (0-3)
x893 14:c177053accff 254 *
x893 14:c177053accff 255 */
x893 14:c177053accff 256 void set_orientation(unsigned int o);
x893 14:c177053accff 257
x893 14:c177053accff 258
x893 14:c177053accff 259 /** read out the manufacturer ID of the LCD
x893 14:c177053accff 260 * can used for checking the connection to the display
x893 14:c177053accff 261 * @returns ID
x893 14:c177053accff 262 */
x893 14:c177053accff 263 int Read_ID(void);
x893 14:c177053accff 264
x893 14:c177053accff 265 DigitalOut _cs;
x893 14:c177053accff 266 DigitalOut _reset;
x893 14:c177053accff 267 DigitalOut _dc;
x893 14:c177053accff 268 unsigned char* font;
x893 14:c177053accff 269
x893 14:c177053accff 270
x893 14:c177053accff 271
x893 14:c177053accff 272
x893 14:c177053accff 273 protected:
x893 14:c177053accff 274
x893 14:c177053accff 275 /** Set draw window region to whole screen
x893 14:c177053accff 276 *
x893 14:c177053accff 277 */
x893 14:c177053accff 278 void WindowMax (void);
x893 14:c177053accff 279
x893 14:c177053accff 280
x893 14:c177053accff 281 /** draw a horizontal line
x893 14:c177053accff 282 *
x893 14:c177053accff 283 * @param x0 horizontal start
x893 14:c177053accff 284 * @param x1 horizontal stop
x893 14:c177053accff 285 * @param y vertical position
x893 14:c177053accff 286 * @param color 16 bit color
x893 14:c177053accff 287 *
x893 14:c177053accff 288 */
x893 14:c177053accff 289 void hline(int x0, int x1, int y, int colour);
x893 14:c177053accff 290
x893 14:c177053accff 291 /** draw a vertical line
x893 14:c177053accff 292 *
x893 14:c177053accff 293 * @param x horizontal position
x893 14:c177053accff 294 * @param y0 vertical start
x893 14:c177053accff 295 * @param y1 vertical stop
x893 14:c177053accff 296 * @param color 16 bit color
x893 14:c177053accff 297 */
x893 14:c177053accff 298 void vline(int y0, int y1, int x, int colour);
x893 14:c177053accff 299
x893 14:c177053accff 300 /** Set draw window region
x893 14:c177053accff 301 *
x893 14:c177053accff 302 * @param x horizontal position
x893 14:c177053accff 303 * @param y vertical position
x893 14:c177053accff 304 * @param w window width in pixel
x893 14:c177053accff 305 * @param h window height in pixels
x893 14:c177053accff 306 */
x893 14:c177053accff 307 virtual void window (unsigned int x,unsigned int y, unsigned int w, unsigned int h);
x893 14:c177053accff 308
x893 14:c177053accff 309
x893 14:c177053accff 310
x893 14:c177053accff 311 /** Init the HX8347D controller
x893 14:c177053accff 312 *
x893 14:c177053accff 313 */
x893 14:c177053accff 314 void tft_reset();
x893 14:c177053accff 315
x893 14:c177053accff 316 /** Write data to the LCD controller
x893 14:c177053accff 317 *
x893 14:c177053accff 318 * @param dat data written to LCD controller
x893 14:c177053accff 319 *
x893 14:c177053accff 320 */
x893 14:c177053accff 321 //void wr_dat(unsigned int value);
x893 14:c177053accff 322 void wr_dat(unsigned char value);
x893 14:c177053accff 323
x893 14:c177053accff 324 /** Write a command the LCD controller
x893 14:c177053accff 325 *
x893 14:c177053accff 326 * @param cmd: command to be written
x893 14:c177053accff 327 *
x893 14:c177053accff 328 */
x893 14:c177053accff 329 void wr_cmd(unsigned char value);
x893 14:c177053accff 330
x893 14:c177053accff 331 /** Start data sequence to the LCD controller
x893 14:c177053accff 332 *
x893 14:c177053accff 333 */
x893 14:c177053accff 334 //void wr_dat_start();
x893 14:c177053accff 335
x893 14:c177053accff 336 /** Stop of data writing to the LCD controller
x893 14:c177053accff 337 *
x893 14:c177053accff 338 */
x893 14:c177053accff 339 //void wr_dat_stop();
x893 14:c177053accff 340
x893 14:c177053accff 341 /** write data to the LCD controller
x893 14:c177053accff 342 *
x893 14:c177053accff 343 * @param data to be written
x893 14:c177053accff 344 * *
x893 14:c177053accff 345 */
x893 14:c177053accff 346 //void wr_dat_only(unsigned short dat);
x893 14:c177053accff 347
x893 14:c177053accff 348 /** Read byte from the LCD controller
x893 14:c177053accff 349 *
x893 14:c177053accff 350 * @param cmd comand to controller
x893 14:c177053accff 351 * @returns data from LCD controller
x893 14:c177053accff 352 *
x893 14:c177053accff 353 */
x893 14:c177053accff 354 char rd_byte(unsigned char cmd);
x893 14:c177053accff 355
x893 14:c177053accff 356
x893 14:c177053accff 357 int rd_32(unsigned char cmd);
x893 14:c177053accff 358
x893 14:c177053accff 359
x893 14:c177053accff 360 /** Write a value to the to a LCD register
x893 14:c177053accff 361 *
x893 14:c177053accff 362 * @param reg register to be written
x893 14:c177053accff 363 * @param val data to be written
x893 14:c177053accff 364 */
x893 14:c177053accff 365 //void wr_reg (unsigned char reg, unsigned char val);
x893 14:c177053accff 366
x893 14:c177053accff 367 /** Read a LCD register
x893 14:c177053accff 368 *
x893 14:c177053accff 369 * @param reg register to be read
x893 14:c177053accff 370 * @returns value of the register
x893 14:c177053accff 371 */
x893 14:c177053accff 372 //unsigned short rd_reg (unsigned char reg);
x893 14:c177053accff 373
x893 14:c177053accff 374 #if defined TARGET_NUCLEO_L152RE || defined TARGET_NUCLEO_F103RB || defined TARGET_LPC1768
x893 14:c177053accff 375 /** fast SPI write function for optimized versions
x893 14:c177053accff 376 *
x893 14:c177053accff 377 * @param data data written to SPI
x893 14:c177053accff 378 *
x893 14:c177053accff 379 */
x893 14:c177053accff 380 virtual void f_write(int data);
x893 14:c177053accff 381 virtual void spi_bsy(void);
x893 14:c177053accff 382 virtual void spi_16(bool s);
x893 14:c177053accff 383
x893 14:c177053accff 384 #endif
x893 14:c177053accff 385
x893 14:c177053accff 386 unsigned char spi_port;
x893 14:c177053accff 387 unsigned int orientation;
x893 14:c177053accff 388 unsigned int char_x;
x893 14:c177053accff 389 unsigned int char_y;
x893 14:c177053accff 390 unsigned char spi_num;
x893 14:c177053accff 391
x893 14:c177053accff 392
x893 14:c177053accff 393 };
x893 14:c177053accff 394
x893 14:c177053accff 395 #endif