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

Dependents:   TFT_Test1 SourceCodePro31-SB Mandelbrot Mindwave-screen ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI_TFT.h Source File

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