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_ILI9341.h Source File

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