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