Angélica Muniz / SPI_TFT_ILI9341
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI_TFT_ILI9341.h Source File

SPI_TFT_ILI9341.h

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