Chinese module HY-1.8 SPI TFT lcd Display library.

Dependencies:   BurstSPI

Dependents:   KL25Z_DCF77_HY-1_8LCD

Fork of HY-1_8TFT_ST7735 by Paul Staron

Committer:
star297
Date:
Mon Mar 25 22:46:23 2013 +0000
Revision:
0:35a1964228b4
Child:
1:ef70eaf15b18
1st version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:35a1964228b4 1 // ST7735 8 Bit SPI Library
star297 0:35a1964228b4 2
star297 0:35a1964228b4 3 #ifndef MBED_ST7735_TFT_H
star297 0:35a1964228b4 4 #define MBED_ST7735_TFT_H
star297 0:35a1964228b4 5
star297 0:35a1964228b4 6 #include "mbed.h"
star297 0:35a1964228b4 7 #include "GraphicsDisplay.h"
star297 0:35a1964228b4 8
star297 0:35a1964228b4 9 #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
star297 0:35a1964228b4 10
star297 0:35a1964228b4 11 /*define ST7735 Commands */
star297 0:35a1964228b4 12
star297 0:35a1964228b4 13 #define ST7735_NOP 0x0
star297 0:35a1964228b4 14 #define ST7735_SWRESET 0x01
star297 0:35a1964228b4 15 #define ST7735_RDDID 0x04
star297 0:35a1964228b4 16 #define ST7735_RDDST 0x09
star297 0:35a1964228b4 17
star297 0:35a1964228b4 18 #define ST7735_SLPIN 0x10
star297 0:35a1964228b4 19 #define ST7735_SLPOUT 0x11
star297 0:35a1964228b4 20 #define ST7735_PTLON 0x12
star297 0:35a1964228b4 21 #define ST7735_NORON 0x13
star297 0:35a1964228b4 22
star297 0:35a1964228b4 23 #define ST7735_INVOFF 0x20
star297 0:35a1964228b4 24 #define ST7735_INVON 0x21
star297 0:35a1964228b4 25 #define ST7735_DISPOFF 0x28
star297 0:35a1964228b4 26 #define ST7735_DISPON 0x29
star297 0:35a1964228b4 27 #define ST7735_CASET 0x2A
star297 0:35a1964228b4 28 #define ST7735_RASET 0x2B
star297 0:35a1964228b4 29 #define ST7735_RAMWR 0x2C
star297 0:35a1964228b4 30 #define ST7735_RAMRD 0x2E
star297 0:35a1964228b4 31
star297 0:35a1964228b4 32 #define ST7735_COLMOD 0x3A
star297 0:35a1964228b4 33 #define ST7735_MADCTL 0x36
star297 0:35a1964228b4 34
star297 0:35a1964228b4 35
star297 0:35a1964228b4 36 #define ST7735_FRMCTR1 0xB1
star297 0:35a1964228b4 37 #define ST7735_FRMCTR2 0xB2
star297 0:35a1964228b4 38 #define ST7735_FRMCTR3 0xB3
star297 0:35a1964228b4 39 #define ST7735_INVCTR 0xB4
star297 0:35a1964228b4 40 #define ST7735_DISSET5 0xB6
star297 0:35a1964228b4 41
star297 0:35a1964228b4 42 #define ST7735_PWCTR1 0xC0
star297 0:35a1964228b4 43 #define ST7735_PWCTR2 0xC1
star297 0:35a1964228b4 44 #define ST7735_PWCTR3 0xC2
star297 0:35a1964228b4 45 #define ST7735_PWCTR4 0xC3
star297 0:35a1964228b4 46 #define ST7735_PWCTR5 0xC4
star297 0:35a1964228b4 47 #define ST7735_VMCTR1 0xC5
star297 0:35a1964228b4 48
star297 0:35a1964228b4 49 #define ST7735_RDID1 0xDA
star297 0:35a1964228b4 50 #define ST7735_RDID2 0xDB
star297 0:35a1964228b4 51 #define ST7735_RDID3 0xDC
star297 0:35a1964228b4 52 #define ST7735_RDID4 0xDD
star297 0:35a1964228b4 53
star297 0:35a1964228b4 54 #define ST7735_PWCTR6 0xFC
star297 0:35a1964228b4 55
star297 0:35a1964228b4 56 #define ST7735_GMCTRP1 0xE0
star297 0:35a1964228b4 57 #define ST7735_GMCTRN1 0xE1
star297 0:35a1964228b4 58
star297 0:35a1964228b4 59 /* some RGB color definitions RED BLUE GREEN */
star297 0:35a1964228b4 60 #define Black 0x0000 /* 0, 0, 0 */
star297 0:35a1964228b4 61 #define Navy 0x000F /* 0, 0, 128 */
star297 0:35a1964228b4 62 #define DarkGreen 0x03E0 /* 0, 128, 0 */
star297 0:35a1964228b4 63 #define DarkCyan 0x03EF /* 0, 128, 128 */
star297 0:35a1964228b4 64 #define Maroon 0x7800 /* 128, 0, 0 */
star297 0:35a1964228b4 65 #define Purple 0x780F /* 128, 0, 128 */
star297 0:35a1964228b4 66 #define Olive 0x7BE0 /* 128, 128, 0 */
star297 0:35a1964228b4 67 #define LightGrey 0xC618 /* 192, 192, 192 */
star297 0:35a1964228b4 68 #define DarkGrey 0x7BEF /* 128, 128, 128 */
star297 0:35a1964228b4 69 #define Green 0x002F /* 0, 0, 255 */
star297 0:35a1964228b4 70 #define Blue 0x07E0 /* 0, 255, 0 */
star297 0:35a1964228b4 71 #define Cyan 0x07FF /* 0, 255, 255 */
star297 0:35a1964228b4 72 #define Red 0xFF00 /* 255, 0, 0 */
star297 0:35a1964228b4 73 #define Yellow 0xF81F /* 255, 0, 255 */
star297 0:35a1964228b4 74 #define Magenta 0xFFE0 /* 255, 255, 0 */
star297 0:35a1964228b4 75 #define White 0xFFFF /* 255, 255, 255 */
star297 0:35a1964228b4 76 #define Orange 0xFD20 /* 255, 165, 0 */
star297 0:35a1964228b4 77 #define GreenYellow 0xAFE5 /* 173, 255, 47 */
star297 0:35a1964228b4 78 #define LightBlue 0x04FF
star297 0:35a1964228b4 79
star297 0:35a1964228b4 80 class ST7735_TFT : public GraphicsDisplay {
star297 0:35a1964228b4 81 public:
star297 0:35a1964228b4 82
star297 0:35a1964228b4 83 /** Create a ST7735_TFT object connected to SPI and three pins. ST7735 requires rs pin to toggle between data/command
star297 0:35a1964228b4 84 *
star297 0:35a1964228b4 85 * @param mosi(SDA),miso(NC),sclk(SCK) SPI
star297 0:35a1964228b4 86 * @param cs pin connected to CS of display (called SS for 'Slave Select' in ST7735 datasheet)
star297 0:35a1964228b4 87 * @param rs pin connected to RS (A0)of display (called D/CX in ST7735 datasheet)
star297 0:35a1964228b4 88 * @param reset pin connected to RESET of display
star297 0:35a1964228b4 89 *
star297 0:35a1964228b4 90 */
star297 0:35a1964228b4 91 ST7735_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset,const char* name ="TFT");
star297 0:35a1964228b4 92
star297 0:35a1964228b4 93 /** Get the width of the screen in pixel
star297 0:35a1964228b4 94 *
star297 0:35a1964228b4 95 * @param
star297 0:35a1964228b4 96 * @returns width of screen in pixel
star297 0:35a1964228b4 97 *
star297 0:35a1964228b4 98 */
star297 0:35a1964228b4 99 virtual int width();
star297 0:35a1964228b4 100
star297 0:35a1964228b4 101 /** Get the height of the screen in pixel
star297 0:35a1964228b4 102 *
star297 0:35a1964228b4 103 * @returns height of screen in pixel
star297 0:35a1964228b4 104 *
star297 0:35a1964228b4 105 */
star297 0:35a1964228b4 106 virtual int height();
star297 0:35a1964228b4 107
star297 0:35a1964228b4 108 /** Draw a pixel at x,y with color
star297 0:35a1964228b4 109 *
star297 0:35a1964228b4 110 * @param x horizontal position
star297 0:35a1964228b4 111 * @param y vertical position
star297 0:35a1964228b4 112 * @param color 16 bit pixel color
star297 0:35a1964228b4 113 */
star297 0:35a1964228b4 114 virtual void pixel(int x, int y, int colour);
star297 0:35a1964228b4 115
star297 0:35a1964228b4 116 /** Get colour of pixel at x,y
star297 0:35a1964228b4 117 *
star297 0:35a1964228b4 118 * @param x horizontal position
star297 0:35a1964228b4 119 * @param y vertical position
star297 0:35a1964228b4 120 */
star297 0:35a1964228b4 121
star297 0:35a1964228b4 122 int getpixel(unsigned int x, unsigned int y);
star297 0:35a1964228b4 123
star297 0:35a1964228b4 124 /** draw a circle
star297 0:35a1964228b4 125 *
star297 0:35a1964228b4 126 * @param x0,y0 center
star297 0:35a1964228b4 127 * @param r radius
star297 0:35a1964228b4 128 * @param color 16 bit color *
star297 0:35a1964228b4 129 *
star297 0:35a1964228b4 130 */
star297 0:35a1964228b4 131
star297 0:35a1964228b4 132 void circle(int x, int y, int r, int colour);
star297 0:35a1964228b4 133
star297 0:35a1964228b4 134 /** draw a filled circle
star297 0:35a1964228b4 135 *
star297 0:35a1964228b4 136 * @param x0,y0 center
star297 0:35a1964228b4 137 * @param r radius
star297 0:35a1964228b4 138 * @param color 16 bit color *
star297 0:35a1964228b4 139 *
star297 0:35a1964228b4 140 * use circle with different radius,
star297 0:35a1964228b4 141 * can miss some pixel
star297 0:35a1964228b4 142 */
star297 0:35a1964228b4 143 void fillcircle(int x, int y, int r, int colour);
star297 0:35a1964228b4 144
star297 0:35a1964228b4 145 /** draw a 1 pixel line
star297 0:35a1964228b4 146 *
star297 0:35a1964228b4 147 * @param x0,y0 start point
star297 0:35a1964228b4 148 * @param x1,y1 stop point
star297 0:35a1964228b4 149 * @param color 16 bit color
star297 0:35a1964228b4 150 *
star297 0:35a1964228b4 151 */
star297 0:35a1964228b4 152 void line(int x0, int y0, int x1, int y1, int colour);
star297 0:35a1964228b4 153
star297 0:35a1964228b4 154 /** draw a rect
star297 0:35a1964228b4 155 *
star297 0:35a1964228b4 156 * @param x0,y0 top left corner
star297 0:35a1964228b4 157 * @param x1,y1 down right corner
star297 0:35a1964228b4 158 * @param color 16 bit color
star297 0:35a1964228b4 159 * *
star297 0:35a1964228b4 160 */
star297 0:35a1964228b4 161 void rect(int x0, int y0, int x1, int y1, int colour);
star297 0:35a1964228b4 162
star297 0:35a1964228b4 163 /** draw a filled rect
star297 0:35a1964228b4 164 *
star297 0:35a1964228b4 165 * @param x0,y0 top left corner
star297 0:35a1964228b4 166 * @param x1,y1 down right corner
star297 0:35a1964228b4 167 * @param color 16 bit color
star297 0:35a1964228b4 168 *
star297 0:35a1964228b4 169 */
star297 0:35a1964228b4 170 void fillrect(int x0, int y0, int x1, int y1, int colour);
star297 0:35a1964228b4 171
star297 0:35a1964228b4 172 /** setup cursor position
star297 0:35a1964228b4 173 *
star297 0:35a1964228b4 174 * @param x x-position (top left)
star297 0:35a1964228b4 175 * @param y y-position
star297 0:35a1964228b4 176 */
star297 0:35a1964228b4 177 virtual void locate(int x, int y);
star297 0:35a1964228b4 178
star297 0:35a1964228b4 179 /** Fill the screen with _backgroun color
star297 0:35a1964228b4 180 *
star297 0:35a1964228b4 181 */
star297 0:35a1964228b4 182 virtual void cls (void);
star297 0:35a1964228b4 183
star297 0:35a1964228b4 184
star297 0:35a1964228b4 185 /** calculate the max number of char in a line
star297 0:35a1964228b4 186 *
star297 0:35a1964228b4 187 * @returns max columns
star297 0:35a1964228b4 188 * depends on actual font size
star297 0:35a1964228b4 189 *
star297 0:35a1964228b4 190 */
star297 0:35a1964228b4 191 virtual int columns(void);
star297 0:35a1964228b4 192
star297 0:35a1964228b4 193 /** calculate the max number of columns
star297 0:35a1964228b4 194 *
star297 0:35a1964228b4 195 * @returns max column
star297 0:35a1964228b4 196 * depends on actual font size
star297 0:35a1964228b4 197 *
star297 0:35a1964228b4 198 */
star297 0:35a1964228b4 199 virtual int rows(void);
star297 0:35a1964228b4 200
star297 0:35a1964228b4 201 /** put a char on the screen
star297 0:35a1964228b4 202 *
star297 0:35a1964228b4 203 * @param value char to print
star297 0:35a1964228b4 204 * @returns printed char
star297 0:35a1964228b4 205 *
star297 0:35a1964228b4 206 */
star297 0:35a1964228b4 207 virtual int _putc(int value);
star297 0:35a1964228b4 208
star297 0:35a1964228b4 209 /** draw a character on given position out of the active font to the TFT
star297 0:35a1964228b4 210 *
star297 0:35a1964228b4 211 * @param x x-position of char (top left)
star297 0:35a1964228b4 212 * @param y y-position
star297 0:35a1964228b4 213 * @param c char to print
star297 0:35a1964228b4 214 *
star297 0:35a1964228b4 215 */
star297 0:35a1964228b4 216 virtual void character(int x, int y, int c);
star297 0:35a1964228b4 217
star297 0:35a1964228b4 218 /** paint a bitmap on the TFT
star297 0:35a1964228b4 219 *
star297 0:35a1964228b4 220 * @param x,y : upper left corner
star297 0:35a1964228b4 221 * @param w width of bitmap
star297 0:35a1964228b4 222 * @param h high of bitmap
star297 0:35a1964228b4 223 * @param *bitmap pointer to the bitmap data
star297 0:35a1964228b4 224 *
star297 0:35a1964228b4 225 * bitmap format: 16 bit R5 G6 B5
star297 0:35a1964228b4 226 *
star297 0:35a1964228b4 227 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
star297 0:35a1964228b4 228 * use winhex to load this file and mark data stating at offset 0x46 to end
star297 0:35a1964228b4 229 * use edit -> copy block -> C Source to export C array
star297 0:35a1964228b4 230 * paste this array into your program
star297 0:35a1964228b4 231 *
star297 0:35a1964228b4 232 * define the array as static const unsigned char to put it into flash memory
star297 0:35a1964228b4 233 * cast the pointer to (unsigned char *) :
star297 0:35a1964228b4 234 * tft.Bitmap(10,40,309,50,(unsigned char *)scala);
star297 0:35a1964228b4 235 */
star297 0:35a1964228b4 236 void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap);
star297 0:35a1964228b4 237
star297 0:35a1964228b4 238
star297 0:35a1964228b4 239 /** paint a 16 bit BMP from local filesytem on the TFT (slow)
star297 0:35a1964228b4 240 *
star297 0:35a1964228b4 241 * @param x,y : upper left corner
star297 0:35a1964228b4 242 * @param *Name_BMP name of the BMP file
star297 0:35a1964228b4 243 * @returns 1 if bmp file was found and painted
star297 0:35a1964228b4 244 * @returns -1 if bmp file was found not found
star297 0:35a1964228b4 245 * @returns -2 if bmp file is not 16bit
star297 0:35a1964228b4 246 * @returns -3 if bmp file is to big for screen
star297 0:35a1964228b4 247 * @returns -4 if buffer malloc go wrong
star297 0:35a1964228b4 248 *
star297 0:35a1964228b4 249 * bitmap format: 16 bit R5 G6 B5
star297 0:35a1964228b4 250 *
star297 0:35a1964228b4 251 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
star297 0:35a1964228b4 252 * copy to internal file system
star297 0:35a1964228b4 253 *
star297 0:35a1964228b4 254 */
star297 0:35a1964228b4 255
star297 0:35a1964228b4 256 int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP);
star297 0:35a1964228b4 257
star297 0:35a1964228b4 258 /** Read an area from the LCD RAM to MCU RAM
star297 0:35a1964228b4 259 *
star297 0:35a1964228b4 260 * @param x,y : upper left corner
star297 0:35a1964228b4 261 * @param w width of bitmap
star297 0:35a1964228b4 262 * @param h high of bitmap
star297 0:35a1964228b4 263 * @param *buffer pointer to the buffer
star297 0:35a1964228b4 264 */
star297 0:35a1964228b4 265
star297 0:35a1964228b4 266 void read_area(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *buffer);
star297 0:35a1964228b4 267
star297 0:35a1964228b4 268 /** select the font to use
star297 0:35a1964228b4 269 *
star297 0:35a1964228b4 270 * @param f pointer to font array
star297 0:35a1964228b4 271 *
star297 0:35a1964228b4 272 * font array can created with GLCD Font Creator from http://www.mikroe.com
star297 0:35a1964228b4 273 * you have to add 4 parameter at the beginning of the font array to use:
star297 0:35a1964228b4 274 * - the number of byte / char
star297 0:35a1964228b4 275 * - the vertial size in pixel
star297 0:35a1964228b4 276 * - the horizontal size in pixel
star297 0:35a1964228b4 277 * - the number of byte per vertical line
star297 0:35a1964228b4 278 * you also have to change the array to char[]
star297 0:35a1964228b4 279 *
star297 0:35a1964228b4 280 */
star297 0:35a1964228b4 281 void set_font(unsigned char* f);
star297 0:35a1964228b4 282
star297 0:35a1964228b4 283 /** Set the orientation of the screen
star297 0:35a1964228b4 284 * x,y: 0,0 is always top left
star297 0:35a1964228b4 285 *
star297 0:35a1964228b4 286 * @param o direction to use the screen (0-3) 90&#65533; Steps
star297 0:35a1964228b4 287 *
star297 0:35a1964228b4 288 */
star297 0:35a1964228b4 289 void set_orientation(unsigned int o);
star297 0:35a1964228b4 290
star297 0:35a1964228b4 291 SPI _spi;
star297 0:35a1964228b4 292 DigitalOut _cs;
star297 0:35a1964228b4 293 DigitalOut _rs;
star297 0:35a1964228b4 294 DigitalOut _reset;
star297 0:35a1964228b4 295 unsigned char* font;
star297 0:35a1964228b4 296
star297 0:35a1964228b4 297 protected:
star297 0:35a1964228b4 298
star297 0:35a1964228b4 299 /** draw a horizontal line
star297 0:35a1964228b4 300 *
star297 0:35a1964228b4 301 * @param x0 horizontal start
star297 0:35a1964228b4 302 * @param x1 horizontal stop
star297 0:35a1964228b4 303 * @param y vertical position
star297 0:35a1964228b4 304 * @param color 16 bit color
star297 0:35a1964228b4 305 *
star297 0:35a1964228b4 306 */
star297 0:35a1964228b4 307 void hline(int x0, int x1, int y, int colour);
star297 0:35a1964228b4 308
star297 0:35a1964228b4 309 /** draw a vertical line
star297 0:35a1964228b4 310 *
star297 0:35a1964228b4 311 * @param x horizontal position
star297 0:35a1964228b4 312 * @param y0 vertical start
star297 0:35a1964228b4 313 * @param y1 vertical stop
star297 0:35a1964228b4 314 * @param color 16 bit color
star297 0:35a1964228b4 315 */
star297 0:35a1964228b4 316 void vline(int y0, int y1, int x, int colour);
star297 0:35a1964228b4 317
star297 0:35a1964228b4 318 /** Set draw window region
star297 0:35a1964228b4 319 *
star297 0:35a1964228b4 320 * @param x horizontal position
star297 0:35a1964228b4 321 * @param y vertical position
star297 0:35a1964228b4 322 * @param w window width in pixel
star297 0:35a1964228b4 323 * @param h window height in pixels
star297 0:35a1964228b4 324 */
star297 0:35a1964228b4 325 void window (unsigned int x, unsigned int y, unsigned int w, unsigned int h);
star297 0:35a1964228b4 326
star297 0:35a1964228b4 327 /** Set draw window region to whole screen
star297 0:35a1964228b4 328 *
star297 0:35a1964228b4 329 */
star297 0:35a1964228b4 330 void WindowMax (void);
star297 0:35a1964228b4 331
star297 0:35a1964228b4 332 /** Init the ST7735 controller
star297 0:35a1964228b4 333 *
star297 0:35a1964228b4 334 */
star297 0:35a1964228b4 335 void tft_reset();
star297 0:35a1964228b4 336
star297 0:35a1964228b4 337 /** Write data to the LCD controller
star297 0:35a1964228b4 338 *
star297 0:35a1964228b4 339 * @param dat data written to LCD controller
star297 0:35a1964228b4 340 *
star297 0:35a1964228b4 341 */
star297 0:35a1964228b4 342 void wr_dat(int value);
star297 0:35a1964228b4 343
star297 0:35a1964228b4 344 /** Write a command the LCD controller
star297 0:35a1964228b4 345 *
star297 0:35a1964228b4 346 * @param cmd: command to be written
star297 0:35a1964228b4 347 *
star297 0:35a1964228b4 348 */
star297 0:35a1964228b4 349 void wr_cmd(int value);
star297 0:35a1964228b4 350
star297 0:35a1964228b4 351 /** Start data sequence to the LCD controller
star297 0:35a1964228b4 352 *
star297 0:35a1964228b4 353 */
star297 0:35a1964228b4 354 void wr_dat_start();
star297 0:35a1964228b4 355
star297 0:35a1964228b4 356 /** Stop of data writing to the LCD controller
star297 0:35a1964228b4 357 *
star297 0:35a1964228b4 358 */
star297 0:35a1964228b4 359 void wr_dat_stop();
star297 0:35a1964228b4 360
star297 0:35a1964228b4 361 /** write data to the LCD controller
star297 0:35a1964228b4 362 *
star297 0:35a1964228b4 363 * @param data to be written
star297 0:35a1964228b4 364 * *
star297 0:35a1964228b4 365 */
star297 0:35a1964228b4 366 void wr_dat_only(unsigned short dat);
star297 0:35a1964228b4 367
star297 0:35a1964228b4 368 /** Read data from the LCD controller
star297 0:35a1964228b4 369 *
star297 0:35a1964228b4 370 * @returns data from LCD controller
star297 0:35a1964228b4 371 *
star297 0:35a1964228b4 372 */
star297 0:35a1964228b4 373 unsigned short rd_dat(void);
star297 0:35a1964228b4 374
star297 0:35a1964228b4 375 /** Write a value to the to a LCD register
star297 0:35a1964228b4 376 *
star297 0:35a1964228b4 377 * @param reg register to be written
star297 0:35a1964228b4 378 * @param val data to be written
star297 0:35a1964228b4 379 */
star297 0:35a1964228b4 380 void wr_reg (unsigned char reg, unsigned short val);
star297 0:35a1964228b4 381
star297 0:35a1964228b4 382 /** Read a LCD register
star297 0:35a1964228b4 383 *
star297 0:35a1964228b4 384 * @param reg register to be read
star297 0:35a1964228b4 385 * @returns value of the register
star297 0:35a1964228b4 386 */
star297 0:35a1964228b4 387 unsigned short rd_reg (unsigned char reg);
star297 0:35a1964228b4 388
star297 0:35a1964228b4 389 unsigned int orientation;
star297 0:35a1964228b4 390 unsigned int char_x;
star297 0:35a1964228b4 391 unsigned int char_y;
star297 0:35a1964228b4 392 };
star297 0:35a1964228b4 393 #endif