Lib for the new LCD Display with ILI9341 controller

Dependents:   TFT_Test_ILI9341 touch_piirto TFT_banggood TFT_Test_ILI9341-a-fish ... more

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