Mark Peter Vargha / SPI_TFT_ILI9341

Fork of SPI_TFT_ILI9341 by Peter Drescher

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 #define LCD_COLOR_BLUE          Blue
00046 #define LCD_COLOR_GREEN         Green
00047 #define LCD_COLOR_RED           Red
00048 #define LCD_COLOR_CYAN          Cyan
00049 #define LCD_COLOR_MAGENTA       Magenta
00050 #define LCD_COLOR_YELLOW        Yellow
00051 //#define LCD_COLOR_LIGHTBLUE     0xFF8080FF
00052 //#define LCD_COLOR_LIGHTGREEN    0xFF80FF80
00053 //#define LCD_COLOR_LIGHTRED      0xFFFF8080
00054 //#define LCD_COLOR_LIGHTCYAN     0xFF80FFFF
00055 //#define LCD_COLOR_LIGHTMAGENTA  0xFFFF80FF
00056 //#define LCD_COLOR_LIGHTYELLOW   0xFFFFFF80
00057 //#define LCD_COLOR_DARKBLUE      0xFF000080
00058 #define LCD_COLOR_DARKGREEN     DarkGreen
00059 #define LCD_COLOR_DARKRED       RGB(0x80, 0x0, 0x0)
00060 #define LCD_COLOR_DARKCYAN      DarkCyan
00061 //#define LCD_COLOR_DARKMAGENTA   0xFF800080
00062 //#define LCD_COLOR_DARKYELLOW    0xFF808000
00063 #define LCD_COLOR_WHITE         White
00064 #define LCD_COLOR_LIGHTGRAY     LightGrey
00065 //#define LCD_COLOR_GRAY          0xFF808080
00066 #define LCD_COLOR_DARKGRAY      DarkGrey
00067 #define LCD_COLOR_BLACK         Black
00068 //#define LCD_COLOR_BROWN         0xFFA52A2A
00069 #define LCD_COLOR_ORANGE        Orange
00070 #define LCD_COLOR_TRANSPARENT   0xFF000000
00071 
00072 #define FONT_OFFSET_INDEX 0
00073 #define FONT_WIDTH_INDEX 1
00074 #define FONT_HEIGHT_INDEX 2
00075 #define FONT_BSP_INDEX 3
00076 
00077 /**
00078   * @brief  Line mode structures definition
00079   */
00080 enum Text_AlignModeTypdef
00081 {
00082   CENTER_MODE             = 0x01,    /* center mode */
00083   RIGHT_MODE              = 0x02,    /* right mode  */
00084   LEFT_MODE               = 0x03,    /* left mode   */
00085 };
00086 
00087 /** Display control class, based on GraphicsDisplay and TextDisplay
00088  *
00089  * Example:
00090  * @code
00091  * #include "stdio.h"
00092  * #include "mbed.h"
00093  * #include "SPI_TFT_ILI9341.h"
00094  * #include "string"
00095  * #include "Arial12x12.h"
00096  * #include "Arial24x23.h"
00097  *
00098  *
00099  *
00100  * // the TFT is connected to SPI pin 5-7 and IO's 8-10
00101  * SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc
00102  * If your display need a signal for switch the backlight use a aditional IO pin in your program
00103  *
00104  * int main() {
00105  *     TFT.claim(stdout);      // send stdout to the TFT display
00106  *     //TFT.claim(stderr);      // send stderr to the TFT display
00107  *
00108  *     TFT.background(Black);    // set background to black
00109  *     TFT.foreground(White);    // set chars to white
00110  *     TFT.cls();                // clear the screen
00111  *     TFT.set_font((unsigned char*) Arial12x12);  // select the font
00112  *
00113  *     TFT.set_orientation(0);
00114  *     printf("  Hello Mbed 0");
00115  *     TFT.set_font((unsigned char*) Arial24x23);  // select font 2
00116  *     TFT.locate(48,115);
00117  *     TFT.printf("Bigger Font");
00118  *  }
00119  * @endcode
00120  */
00121 class SPI_TFT_ILI9341 : public GraphicsDisplay , public SPI
00122 {
00123 public:
00124 
00125     /** Create a SPI_TFT object connected to SPI and three pins
00126      *
00127      * @param mosi pin connected to SDO of display
00128      * @param miso pin connected to SDI of display
00129      * @param sclk pin connected to RS of display
00130      * @param cs pin connected to CS of display
00131      * @param reset pin connected to RESET of display
00132      * @param dc pin connected to WR of display
00133      * the IM pins have to be set to 1110 (3-0)
00134      */
00135     SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT");
00136 
00137     /** Get the width of the screen in pixel
00138      *
00139      * @returns width of screen in pixel
00140      *
00141      */
00142     virtual int width();
00143 
00144     /** Get the height of the screen in pixel
00145      *
00146      * @returns height of screen in pixel
00147      *
00148      */
00149     virtual int height();
00150 
00151     /** Draw a pixel at x,y with color
00152      *
00153      * @param x horizontal position
00154      * @param y vertical position
00155      * @param color 16 bit pixel color
00156      */
00157     virtual void pixel(int x, int y, int colour);
00158 
00159     /** draw a circle
00160      *
00161      * @param x,y center
00162      * @param r radius
00163      * @param color 16 bit color                                                                 *
00164      *
00165      */
00166     void circle(int x, int y, int r, int colour);
00167 
00168     /** draw a filled circle
00169      *
00170      * @param x,y center
00171      * @param r radius
00172      * @param color 16 bit color                                                                 *
00173      */
00174     void fillcircle(int x, int y, int r, int colour);
00175 
00176 
00177     /** draw a 1 pixel line
00178      *
00179      * @param x0,y0 start point
00180      * @param x1,y1 stop point
00181      * @param color 16 bit color
00182      *
00183      */
00184     void line(int x0, int y0, int x1, int y1, int colour);
00185 
00186     /** draw a rect
00187      *
00188      * @param x0,y0 top left corner
00189      * @param x1,y1 down right corner
00190      * @param color 16 bit color
00191      *                                                   *
00192      */
00193     void rect(int x0, int y0, int x1, int y1, int colour);
00194 
00195     /** draw a filled rect
00196      *
00197      * @param x0,y0 top left corner
00198      * @param x1,y1 down right corner
00199      * @param color 16 bit color
00200      *
00201      */
00202     void fillrect(int x0, int y0, int x1, int y1, int colour);
00203 
00204     /** setup cursor position
00205      *
00206      * @param x x-position (top left)
00207      * @param y y-position
00208      */
00209     virtual void locate(int x, int y);
00210 
00211     /** Fill the screen with _backgroun color
00212      *
00213      */
00214     virtual void cls(void);
00215 
00216     /** calculate the max number of char in a line
00217      *
00218      * @returns max columns
00219      * depends on actual font size
00220      *
00221      */
00222     virtual int columns(void);
00223 
00224     /** calculate the max number of columns
00225      *
00226      * @returns max column
00227      * depends on actual font size
00228      *
00229      */
00230     virtual int rows(void);
00231 
00232     /** put a char on the screen
00233      *
00234      * @param value char to print
00235      * @returns printed char
00236      *
00237      */
00238     virtual int _putc(int value);
00239 
00240     /** draw a character on given position out of the active font to the TFT
00241      *
00242      * @param x x-position of char (top left)
00243      * @param y y-position
00244      * @param c char to print
00245      *
00246      */
00247     virtual void character(int x, int y, int c);
00248 
00249     /** paint a bitmap on the TFT
00250      *
00251      * @param x,y : upper left corner
00252      * @param w width of bitmap
00253      * @param h high of bitmap
00254      * @param *bitmap pointer to the bitmap data
00255      *
00256      *   bitmap format: 16 bit R5 G6 B5
00257      *
00258      *   use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
00259      *   use winhex to load this file and mark data stating at offset 0x46 to end
00260      *   use edit -> copy block -> C Source to export C array
00261      *   paste this array into your program
00262      *
00263      *   define the array as static const unsigned char to put it into flash memory
00264      *   cast the pointer to (unsigned char *) :
00265      *   tft.Bitmap(10,40,309,50,(unsigned char *)scala);
00266      */
00267     void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned char *bitmap);
00268 
00269 
00270     /** paint a 16 bit BMP from filesytem on the TFT (slow)
00271     *
00272     * @param x,y : position of upper left corner
00273     * @param *Name_BMP name of the BMP file with drive: "/local/test.bmp"
00274     *
00275     * @returns 1 if bmp file was found and painted
00276     * @returns  0 if bmp file was found not found
00277     * @returns -1 if file is no bmp
00278     * @returns -2 if bmp file is no 16 bit bmp
00279     * @returns -3 if bmp file is to big for screen
00280     * @returns -4 if buffer malloc go wrong
00281     *
00282     *   bitmap format: 16 bit R5 G6 B5
00283     *
00284     *   use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
00285     *   copy to internal file system or SD card
00286     */
00287 
00288     int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP);
00289 
00290 
00291 
00292     /** select the font to use
00293      *
00294      * @param f pointer to font array
00295      *
00296      *   font array can created with GLCD Font Creator from http://www.mikroe.com
00297      *   you have to add 4 parameter at the beginning of the font array to use:
00298      *   - the number of byte / char
00299      *   - the vertial size in pixel
00300      *   - the horizontal size in pixel
00301      *   - the number of byte per vertical line
00302      *   you also have to change the array to char[]
00303      *
00304      */
00305     void set_font(unsigned char* f);
00306 
00307     /** Set the orientation of the screen
00308      *  x,y: 0,0 is always top left
00309      *
00310      * @param o direction to use the screen (0-3)
00311      *
00312      */
00313     void set_orientation(unsigned int o);
00314 
00315 
00316     /** read out the manufacturer ID of the LCD
00317      *  can used for checking the connection to the display
00318      *  @returns ID
00319      */
00320     int Read_ID(void);
00321 
00322     /** Setup character cursor position, calculated from current font size.
00323      *
00324      * @param row
00325      * @param column
00326      */
00327     void locateChar(uint16_t row, uint16_t column, Text_AlignModeTypdef mode = LEFT_MODE, uint16_t textLength = 1);
00328 
00329     /** Calculates character's left coordinate in pixels.
00330      *
00331      * @param column The character's display column
00332      * @returns the x coordinate in pixels
00333      *
00334      */
00335     uint16_t getCharLeft(uint16_t column);
00336 
00337     /** Calculates character's top coordinate in pixels.
00338      *
00339      * @param row The character's display row
00340      * @returns the y coordinate in pixels
00341      *
00342      */
00343     uint16_t getCharTop(uint16_t row);
00344 
00345     /** Calculates character's bottom coordinate in pixels.
00346      *
00347      * @param row The character's display row
00348      * @returns the y coordinate in pixels
00349      *
00350      */
00351     uint16_t getCharBottom(uint16_t row);
00352 
00353     /** Calculates character's right coordinate in pixel.
00354      *
00355      * @param column The character's display column
00356      * @returns the x coordinate in pixels
00357      *
00358      */
00359     uint16_t getCharRight(uint16_t column);
00360 
00361     void ClearStringLine(uint16_t row);
00362 
00363     DigitalOut _cs;
00364     DigitalOut _reset;
00365     DigitalOut _dc;
00366     unsigned char* font;
00367 
00368 
00369 
00370 
00371 protected:
00372 
00373     /** Set draw window region to whole screen
00374      *
00375      */
00376     void WindowMax (void);
00377 
00378 
00379     /** draw a horizontal line
00380      *
00381      * @param x0 horizontal start
00382      * @param x1 horizontal stop
00383      * @param y vertical position
00384      * @param color 16 bit color
00385      *
00386      */
00387     void hline(int x0, int x1, int y, int colour);
00388 
00389     /** draw a vertical line
00390      *
00391      * @param x horizontal position
00392      * @param y0 vertical start
00393      * @param y1 vertical stop
00394      * @param color 16 bit color
00395      */
00396     void vline(int y0, int y1, int x, int colour);
00397 
00398     /** Set draw window region
00399      *
00400      * @param x horizontal position
00401      * @param y vertical position
00402      * @param w window width in pixel
00403      * @param h window height in pixels
00404      */
00405     virtual void window (unsigned int x, unsigned int y, unsigned int w, unsigned int h);
00406 
00407 
00408 
00409     /** Init the ILI9341 controller
00410      *
00411      */
00412     void tft_reset();
00413 
00414     /** Write data to the LCD controller
00415     *
00416     * @param dat data written to LCD controller
00417     *
00418     */
00419     //void wr_dat(unsigned int value);
00420     void wr_dat(unsigned char value);
00421 
00422     /** Write a command the LCD controller
00423      *
00424      * @param cmd: command to be written
00425      *
00426      */
00427     void wr_cmd(unsigned char value);
00428 
00429     /** Start data sequence to the LCD controller
00430     *
00431     */
00432     //void wr_dat_start();
00433 
00434     /** Stop of data writing to the LCD controller
00435      *
00436      */
00437     //void wr_dat_stop();
00438 
00439     /** write data to the LCD controller
00440      *
00441      * @param data to be written
00442      *                                           *
00443      */
00444     //void wr_dat_only(unsigned short dat);
00445 
00446     /** Read byte from the LCD controller
00447      *
00448      * @param cmd comand to controller
00449      * @returns data from LCD controller
00450      *
00451      */
00452     char rd_byte(unsigned char cmd);
00453 
00454 
00455     int rd_32(unsigned char cmd);
00456 
00457 
00458     /** Write a value to the to a LCD register
00459      *
00460      * @param reg register to be written
00461      * @param val data to be written
00462      */
00463     //void wr_reg (unsigned char reg, unsigned char val);
00464 
00465     /** Read a LCD register
00466      *
00467      * @param reg register to be read
00468      * @returns value of the register
00469      */
00470     //unsigned short rd_reg (unsigned char reg);
00471 
00472 #if defined TARGET_NUCLEO_L152RE || defined TARGET_NUCLEO_F103RB || defined TARGET_LPC1768
00473     /** fast SPI write function for optimized versions
00474      *
00475      * @param data  data written to SPI
00476      *
00477      */
00478     virtual void f_write(int data);
00479     virtual void spi_bsy(void);
00480     virtual void spi_16(bool s);
00481 
00482 #endif
00483 
00484     unsigned char spi_port;
00485     unsigned int orientation;
00486     unsigned int char_x;
00487     unsigned int char_y;
00488     unsigned char spi_num;
00489 
00490 
00491 };
00492 
00493 #endif