Jeff Lewis / SPI_TFT_ILI9341

Dependents:   Paint_9341 IUT-BED2021

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 
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 DarkCyan        0x03EF      /*   0, 128, 128 */
00030 #define Maroon          0x7800      /* 128,   0,   0 */
00031 #define Purple          0x780F      /* 128,   0, 128 */
00032 #define Pink            0xF59E      /* 238, 136, 238 */
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 GreenYellow     0xAFE5      /* 173, 255,  47 */
00044 #define Orange          0xFC00
00045 #define Brown           0x8208
00046 #define BlueGreen       0x4410
00047 #define White           0xFFFF
00048 #define LightPink       0xFC18
00049 #define DarkOrange      0xE3C0
00050 #define DarkGreen       0x0400
00051 #define Mustard         0x8400
00052 
00053 
00054 /** Display control class, based on GraphicsDisplay and TextDisplay
00055  *
00056  * Example:
00057  * @code
00058  * #include "stdio.h"
00059  * #include "mbed.h"
00060  * #include "SPI_TFT_ILI9341.h"
00061  * #include "string"
00062  * #include "Arial12x12.h"
00063  * #include "Arial24x23.h"
00064  * 
00065  *
00066  *
00067  * // the TFT is connected to SPI pin 5-7 and IO's 8-10
00068  * SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc
00069  * // If your display need a signal for switch the backlight use a aditional IO pin in your program 
00070  *
00071  * int main() {
00072  *     TFT.claim(stdout);      // send stdout to the TFT display 
00073  *     //TFT.claim(stderr);      // send stderr to the TFT display
00074  *
00075  *     TFT.background(Black);    // set background to black
00076  *     TFT.foreground(White);    // set chars to white
00077  *     TFT.cls();                // clear the screen
00078  *     TFT.set_font((unsigned char*) Arial12x12);  // select the font
00079  *     
00080  *     TFT.set_orientation(0);
00081  *     printf("  Hello Mbed 0");
00082  *     TFT.set_font((unsigned char*) Arial24x23);  // select font 2
00083  *     TFT.locate(48,115);
00084  *     TFT.printf("Bigger Font");
00085  * }
00086  * @endcode
00087  */
00088  class SPI_TFT_ILI9341 : public GraphicsDisplay {
00089  public:
00090 
00091   /** Create a SPI_TFT object connected to SPI and three pins
00092    *
00093    * @param mosi pin connected to SDO of display
00094    * @param miso pin connected to SDI of display
00095    * @param sclk pin connected to RS of display 
00096    * @param cs pin connected to CS of display
00097    * @param reset pin connected to RESET of display
00098    * @param dc pin connected to WR of display
00099    * the IM pins have to be set to 1110 (3-0) 
00100    */ 
00101   SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT");
00102     
00103   /** Get the width of the screen in pixel
00104    *
00105    * @param 
00106    * @returns width of screen in pixel
00107    */    
00108   virtual int width();
00109 
00110   /** Get the height of the screen in pixel
00111    *
00112    * @returns height of screen in pixel 
00113    */     
00114   virtual int height();
00115     
00116   /** Draw a pixel at x,y with color 
00117    *  
00118    * @param x horizontal position
00119    * @param y vertical position
00120    * @param color 16 bit pixel color
00121    */    
00122   virtual void pixel(int x, int y,int colour);
00123     
00124   /** Draw a circle
00125    *
00126    * @param x0,y0 center
00127    * @param r radius
00128    * @param color 16 bit color
00129    */    
00130   void circle(int x, int y, int r, int colour); 
00131   
00132   /** Draw a filled circle
00133    *
00134    * @param x0,y0 center
00135    * @param r radius
00136    * @param color 16 bit color
00137    *
00138    * use circle with different radius,
00139    * can miss some pixel
00140    */    
00141   void fillcircle(int x, int y, int r, int colour); 
00142   
00143  
00144     
00145   /** Draw a 1 pixel line
00146    *
00147    * @param x0,y0 start point
00148    * @param x1,y1 stop point
00149    * @param color 16 bit color
00150    */    
00151   void line(int x0, int y0, int x1, int y1, int colour);
00152     
00153   /** Draw a rect
00154    *
00155    * @param x0,y0 top left corner
00156    * @param x1,y1 down right corner
00157    * @param color 16 bit color
00158    */    
00159   void rect(int x0, int y0, int x1, int y1, int colour);
00160     
00161   /** Draw a filled rect
00162    *
00163    * @param x0,y0 top left corner
00164    * @param x1,y1 down right corner
00165    * @param color 16 bit color
00166    */    
00167   void fillrect(int x0, int y0, int x1, int y1, int colour);
00168     
00169   /** Setup cursor position
00170    *
00171    * @param x x-position (top left)
00172    * @param y y-position 
00173    */   
00174   virtual void locate(int x, int y);
00175     
00176   /** Fill the screen with _backgroun color
00177    */   
00178   virtual void cls (void);   
00179     
00180   /** Calculate the max number of char in a line
00181    *
00182    * @returns max columns, depends on actual font size
00183    */    
00184   virtual int columns(void);
00185     
00186   /** Calculate the max number of columns
00187    *
00188    * @returns max column, depends on actual font size
00189    */   
00190   virtual int rows(void);
00191     
00192   /** Put a char on the screen
00193    *
00194    * @param value char to print
00195    * @returns printed char
00196    */
00197   virtual int _putc(int value);
00198     
00199   /** Draw a character on given position out of the active font to the TFT
00200    *
00201    * @param x x-position of char (top left) 
00202    * @param y y-position
00203    * @param c char to print
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 const char *bitmap);
00226     
00227 //#if DEVICE_LOCALFILESYSTEM
00228    /** Paint a 16 bit BMP from local filesytem on the TFT (slow) 
00229    *
00230    * @param x,y : upper left corner 
00231    * @param *Name_BMP name of the BMP file
00232    * @returns 1 if bmp file was found and painted
00233    * @returns -1 if bmp file was found not found
00234    * @returns -2 if bmp file is not 16bit
00235    * @returns -3 if bmp file is to big for screen 
00236    * @returns -4 if buffer malloc go wrong
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    * copy to internal file system            
00242    */      
00243     
00244   int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP);  
00245 //#endif
00246     
00247     
00248   /** Select the font to use
00249    *
00250    * @param f pointer to font array 
00251    *                                                                              
00252    * font array can created with GLCD Font Creator from http://www.mikroe.com
00253    * you have to add 4 parameter at the beginning of the font array to use: 
00254    * - the number of byte / char
00255    * - the vertial size in pixel
00256    * - the horizontal size in pixel
00257    * - the number of byte per vertical line
00258    * you also have to change the array to char[]
00259    */  
00260   void set_font(unsigned char* f);
00261    
00262   /** Set the orientation of the screen
00263    *  x,y: 0,0 is always top left 
00264    *
00265    * @param o direction to use the screen (0-3)  
00266    */  
00267   void set_orientation(unsigned int o);
00268     
00269   SPI _spi;
00270   DigitalOut _cs; 
00271   PinName _reset;
00272   DigitalOut _dc;
00273   unsigned char* font;
00274   
00275   
00276   
00277    
00278 protected:
00279 
00280   /** Set draw window region to whole screen
00281    *
00282    */  
00283   void WindowMax (void);
00284 
00285 
00286   /** Draw a horizontal line
00287    *
00288    * @param x0 horizontal start
00289    * @param x1 horizontal stop
00290    * @param y vertical position
00291    * @param color 16 bit color
00292    */
00293   void hline(int x0, int x1, int y, int colour);
00294     
00295   /** Draw a vertical line
00296    *
00297    * @param x horizontal position
00298    * @param y0 vertical start 
00299    * @param y1 vertical stop
00300    * @param color 16 bit color
00301    */
00302   void vline(int y0, int y1, int x, int colour);
00303     
00304   /** Set draw window region
00305    *
00306    * @param x horizontal position
00307    * @param y vertical position
00308    * @param w window width in pixel
00309    * @param h window height in pixels
00310    */    
00311   virtual void window (unsigned int x,unsigned int y, unsigned int w, unsigned int h);
00312     
00313  
00314     
00315   /** Init the HX8347D controller 
00316    *
00317    */
00318   void tft_reset();
00319     
00320    /** Write data to the LCD controller
00321    *
00322    * @param dat data written to LCD controller
00323    */   
00324   //void wr_dat(unsigned int value);
00325   void wr_dat(unsigned char value);
00326     
00327   /** Write a command the LCD controller 
00328    *
00329    * @param cmd: command to be written   
00330    *
00331    */   
00332   void wr_cmd(unsigned char value);
00333     
00334    /** Start data sequence to the LCD controller
00335    * 
00336    */   
00337   //void wr_dat_start();
00338     
00339   /** Stop of data writing to the LCD controller
00340    *   
00341    */  
00342   //void wr_dat_stop();
00343     
00344   /** write data to the LCD controller
00345    *
00346    * @param data to be written 
00347    *                                           *
00348    */    
00349   //void wr_dat_only(unsigned short dat);
00350     
00351   /** Read data from the LCD controller
00352    *
00353    * @returns data from LCD controller
00354    *  
00355    */    
00356   //unsigned short rd_dat(void);
00357     
00358   /** Write a value to the to a LCD register
00359    *
00360    * @param reg register to be written
00361    * @param val data to be written
00362    */   
00363   //void wr_reg (unsigned char reg, unsigned char val);
00364     
00365   /** Read a LCD register
00366    *
00367    * @param reg register to be read
00368    * @returns value of the register 
00369    */    
00370   //unsigned short rd_reg (unsigned char reg);
00371     
00372   unsigned char spi_port; 
00373   unsigned int orientation;
00374   unsigned int char_x;
00375   unsigned int char_y;
00376  
00377     
00378 };
00379 
00380 #endif