This is a mbed library for a 1.8 inch 128x160 pixel SPI TFT display
Dependents: ST7735_Pong ST7735_TFT SPI18TFT_FRDM-KL25Z SPI18TFT ... more
ST7735_TFT.h
00001 /* mbed library for 128*160 pixel display TFT based on ST7735 LCD Controller 00002 * ST7735 specific routines (initialization, window addressing, pixel output) 00003 * Copyright (c) 2011 Jonne Valola 00004 * 00005 * WARNING !! WORK IN PROGRESS !!! 00006 * 00007 * Graphics routines and SPI routines derived work used with permission from: 00008 * mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller 00009 * Copyright (c) 2011 Peter Drescher - DC2PD 00010 * 00011 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00012 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00013 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00014 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00015 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00017 * THE SOFTWARE. 00018 */ 00019 00020 #ifndef MBED_ST7735_TFT_H 00021 #define MBED_ST7735_TFT_H 00022 00023 #include "mbed.h" 00024 #include "GraphicsDisplay.h" 00025 00026 #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue 00027 00028 /*define ST7735 Commands */ 00029 00030 #define ST7735_NOP 0x0 00031 #define ST7735_SWRESET 0x01 00032 #define ST7735_RDDID 0x04 00033 #define ST7735_RDDST 0x09 00034 00035 #define ST7735_SLPIN 0x10 00036 #define ST7735_SLPOUT 0x11 00037 #define ST7735_PTLON 0x12 00038 #define ST7735_NORON 0x13 00039 00040 #define ST7735_INVOFF 0x20 00041 #define ST7735_INVON 0x21 00042 #define ST7735_DISPOFF 0x28 00043 #define ST7735_DISPON 0x29 00044 #define ST7735_CASET 0x2A 00045 #define ST7735_RASET 0x2B 00046 #define ST7735_RAMWR 0x2C 00047 #define ST7735_RAMRD 0x2E 00048 00049 #define ST7735_COLMOD 0x3A 00050 #define ST7735_MADCTL 0x36 00051 00052 00053 #define ST7735_FRMCTR1 0xB1 00054 #define ST7735_FRMCTR2 0xB2 00055 #define ST7735_FRMCTR3 0xB3 00056 #define ST7735_INVCTR 0xB4 00057 #define ST7735_DISSET5 0xB6 00058 00059 #define ST7735_PWCTR1 0xC0 00060 #define ST7735_PWCTR2 0xC1 00061 #define ST7735_PWCTR3 0xC2 00062 #define ST7735_PWCTR4 0xC3 00063 #define ST7735_PWCTR5 0xC4 00064 #define ST7735_VMCTR1 0xC5 00065 00066 #define ST7735_RDID1 0xDA 00067 #define ST7735_RDID2 0xDB 00068 #define ST7735_RDID3 0xDC 00069 #define ST7735_RDID4 0xDD 00070 00071 #define ST7735_PWCTR6 0xFC 00072 00073 #define ST7735_GMCTRP1 0xE0 00074 #define ST7735_GMCTRN1 0xE1 00075 00076 /* some RGB color definitions */ 00077 #define Black 0x0000 /* 0, 0, 0 */ 00078 #define Navy 0x000F /* 0, 0, 128 */ 00079 #define DarkGreen 0x03E0 /* 0, 128, 0 */ 00080 #define DarkCyan 0x03EF /* 0, 128, 128 */ 00081 #define Maroon 0x7800 /* 128, 0, 0 */ 00082 #define Purple 0x780F /* 128, 0, 128 */ 00083 #define Olive 0x7BE0 /* 128, 128, 0 */ 00084 #define LightGrey 0xC618 /* 192, 192, 192 */ 00085 #define DarkGrey 0x7BEF /* 128, 128, 128 */ 00086 #define Blue 0x001F /* 0, 0, 255 */ 00087 #define Green 0x07E0 /* 0, 255, 0 */ 00088 #define Cyan 0x07FF /* 0, 255, 255 */ 00089 #define Red 0xF800 /* 255, 0, 0 */ 00090 #define Magenta 0xF81F /* 255, 0, 255 */ 00091 #define Yellow 0xFFE0 /* 255, 255, 0 */ 00092 #define White 0xFFFF /* 255, 255, 255 */ 00093 #define Orange 0xFD20 /* 255, 165, 0 */ 00094 #define GreenYellow 0xAFE5 /* 173, 255, 47 */ 00095 00096 class ST7735_TFT : public GraphicsDisplay { 00097 public: 00098 00099 /** Create a ST7735_TFT object connected to SPI and three pins. ST7735 requires rs pin to toggle between data/command 00100 * 00101 * @param mosi,miso,sclk SPI 00102 * @param cs pin connected to CS of display (called SS for 'Slave Select' in ST7735 datasheet) 00103 * @param rs pin connected to RS of display (called D/CX in ST7735 datasheet) 00104 * @param reset pin connected to RESET of display 00105 * 00106 */ 00107 ST7735_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset,const char* name ="TFT"); 00108 00109 /** Get the width of the screen in pixel 00110 * 00111 * @param 00112 * @returns width of screen in pixel 00113 * 00114 */ 00115 virtual int width(); 00116 00117 /** Get the height of the screen in pixel 00118 * 00119 * @returns height of screen in pixel 00120 * 00121 */ 00122 virtual int height(); 00123 00124 /** Draw a pixel at x,y with color 00125 * 00126 * @param x horizontal position 00127 * @param y vertical position 00128 * @param color 16 bit pixel color 00129 */ 00130 virtual void pixel(int x, int y, int colour); 00131 00132 /** Get colour of pixel at x,y 00133 * 00134 * @param x horizontal position 00135 * @param y vertical position 00136 */ 00137 00138 int getpixel(unsigned int x, unsigned int y); 00139 00140 /** draw a circle 00141 * 00142 * @param x0,y0 center 00143 * @param r radius 00144 * @param color 16 bit color * 00145 * 00146 */ 00147 00148 void circle(int x, int y, int r, int colour); 00149 00150 /** draw a filled circle 00151 * 00152 * @param x0,y0 center 00153 * @param r radius 00154 * @param color 16 bit color * 00155 * 00156 * use circle with different radius, 00157 * can miss some pixel 00158 */ 00159 void fillcircle(int x, int y, int r, int colour); 00160 00161 /** draw a 1 pixel line 00162 * 00163 * @param x0,y0 start point 00164 * @param x1,y1 stop point 00165 * @param color 16 bit color 00166 * 00167 */ 00168 void line(int x0, int y0, int x1, int y1, int colour); 00169 00170 /** draw a rect 00171 * 00172 * @param x0,y0 top left corner 00173 * @param x1,y1 down right corner 00174 * @param color 16 bit color 00175 * * 00176 */ 00177 void rect(int x0, int y0, int x1, int y1, int colour); 00178 00179 /** draw a filled rect 00180 * 00181 * @param x0,y0 top left corner 00182 * @param x1,y1 down right corner 00183 * @param color 16 bit color 00184 * 00185 */ 00186 void fillrect(int x0, int y0, int x1, int y1, int colour); 00187 00188 /** setup cursor position 00189 * 00190 * @param x x-position (top left) 00191 * @param y y-position 00192 */ 00193 void locate(int x, int y); 00194 00195 /** Fill the screen with _backgroun color 00196 * 00197 */ 00198 virtual void cls (void); 00199 00200 /** calculate the max number of char in a line 00201 * 00202 * @returns max columns 00203 * depends on actual font size 00204 * 00205 */ 00206 int columns(void); 00207 00208 /** calculate the max number of columns 00209 * 00210 * @returns max column 00211 * depends on actual font size 00212 * 00213 */ 00214 int rows(void); 00215 00216 /** put a char on the screen 00217 * 00218 * @param value char to print 00219 * @returns printed char 00220 * 00221 */ 00222 int _putc(int value); 00223 00224 /** draw a character on given position out of the active font to the TFT 00225 * 00226 * @param x x-position of char (top left) 00227 * @param y y-position 00228 * @param c char to print 00229 * 00230 */ 00231 virtual void character(int x, int y, int c); 00232 00233 /** paint a bitmap on the TFT 00234 * 00235 * @param x,y : upper left corner 00236 * @param w width of bitmap 00237 * @param h high of bitmap 00238 * @param *bitmap pointer to the bitmap data 00239 * 00240 * bitmap format: 16 bit R5 G6 B5 00241 * 00242 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5 00243 * use winhex to load this file and mark data stating at offset 0x46 to end 00244 * use edit -> copy block -> C Source to export C array 00245 * paste this array into your program 00246 * 00247 * define the array as static const unsigned char to put it into flash memory 00248 * cast the pointer to (unsigned char *) : 00249 * tft.Bitmap(10,40,309,50,(unsigned char *)scala); 00250 */ 00251 void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap); 00252 00253 00254 /** paint a 16 bit BMP from local filesytem on the TFT (slow) 00255 * 00256 * @param x,y : upper left corner 00257 * @param *Name_BMP name of the BMP file 00258 * @returns 1 if bmp file was found and painted 00259 * @returns -1 if bmp file was found not found 00260 * @returns -2 if bmp file is not 16bit 00261 * @returns -3 if bmp file is to big for screen 00262 * @returns -4 if buffer malloc go wrong 00263 * 00264 * bitmap format: 16 bit R5 G6 B5 00265 * 00266 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5 00267 * copy to internal file system 00268 * 00269 */ 00270 00271 int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP); 00272 00273 /** Read an area from the LCD RAM to MCU RAM 00274 * 00275 * @param x,y : upper left corner 00276 * @param w width of bitmap 00277 * @param h high of bitmap 00278 * @param *buffer pointer to the buffer 00279 */ 00280 00281 void read_area(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *buffer); 00282 00283 /** select the font to use 00284 * 00285 * @param f pointer to font array 00286 * 00287 * font array can created with GLCD Font Creator from http://www.mikroe.com 00288 * you have to add 4 parameter at the beginning of the font array to use: 00289 * - the number of byte / char 00290 * - the vertial size in pixel 00291 * - the horizontal size in pixel 00292 * - the number of byte per vertical line 00293 * you also have to change the array to char[] 00294 * 00295 */ 00296 void set_font(unsigned char* f); 00297 00298 /** Set the orientation of the screen 00299 * x,y: 0,0 is always top left 00300 * 00301 * @param o direction to use the screen (0-3) 90� Steps 00302 * 00303 */ 00304 void set_orientation(unsigned int o); 00305 00306 SPI _spi; 00307 DigitalOut _cs; 00308 DigitalOut _rs; 00309 DigitalOut _reset; 00310 unsigned char* font; 00311 00312 protected: 00313 00314 /** draw a horizontal line 00315 * 00316 * @param x0 horizontal start 00317 * @param x1 horizontal stop 00318 * @param y vertical position 00319 * @param color 16 bit color 00320 * 00321 */ 00322 void hline(int x0, int x1, int y, int colour); 00323 00324 /** draw a vertical line 00325 * 00326 * @param x horizontal position 00327 * @param y0 vertical start 00328 * @param y1 vertical stop 00329 * @param color 16 bit color 00330 */ 00331 void vline(int y0, int y1, int x, int colour); 00332 00333 /** Set draw window region 00334 * 00335 * @param x horizontal position 00336 * @param y vertical position 00337 * @param w window width in pixel 00338 * @param h window height in pixels 00339 */ 00340 void window (unsigned int x, unsigned int y, unsigned int w, unsigned int h); 00341 00342 /** Set draw window region to whole screen 00343 * 00344 */ 00345 void WindowMax (void); 00346 00347 /** Init the ST7735 controller 00348 * 00349 */ 00350 void tft_reset(); 00351 00352 /** Write data to the LCD controller 00353 * 00354 * @param dat data written to LCD controller 00355 * 00356 */ 00357 void wr_dat(int value); 00358 00359 /** Write a command the LCD controller 00360 * 00361 * @param cmd: command to be written 00362 * 00363 */ 00364 void wr_cmd(int value); 00365 00366 /** Start data sequence to the LCD controller 00367 * 00368 */ 00369 void wr_dat_start(); 00370 00371 /** Stop of data writing to the LCD controller 00372 * 00373 */ 00374 void wr_dat_stop(); 00375 00376 /** write data to the LCD controller 00377 * 00378 * @param data to be written 00379 * * 00380 */ 00381 void wr_dat_only(unsigned short dat); 00382 00383 /** Read data from the LCD controller 00384 * 00385 * @returns data from LCD controller 00386 * 00387 */ 00388 unsigned short rd_dat(void); 00389 00390 /** Write a value to the to a LCD register 00391 * 00392 * @param reg register to be written 00393 * @param val data to be written 00394 */ 00395 void wr_reg (unsigned char reg, unsigned short val); 00396 00397 /** Read a LCD register 00398 * 00399 * @param reg register to be read 00400 * @returns value of the register 00401 */ 00402 unsigned short rd_reg (unsigned char reg); 00403 00404 unsigned int orientation; 00405 unsigned int char_x; 00406 unsigned int char_y; 00407 00408 00409 }; 00410 00411 #endif
Generated on Fri Jul 15 2022 16:25:48 by 1.7.2