Lib for the new LCD Display with ILI9341 controller supporting two displays

Dependents:   CANary_9341_test CANary_9341 CANary

Fork of SPI_TFT_ILI9341 by Peter Drescher

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI_TFTx2.h Source File

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