Adapted from Peter Dresche's original for Waveshare 2.8inch TFT Touch Shield Board and Mbed 6. RGB order reversed by changing reg 16 commands, spi write code adjusted as there is no reset pin but there is data command pin. Wait commands changed for new thread_sleep style, Stream class explicitly included. 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

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