V1.1 of the ILI9340 driver for SPI with integrated graphics functions.

Dependents:   ILI9340_Driver SDCard_Aitendo_2p2TFT TFT22ok_test_ILI9340_Driver

This driver will drive any display that uses an ILI9340 display controller in SPI mode - such as the adafruits 2.2" 240 x 320 display found here: http://www.adafruit.com/products/1480

All this code has been ported from other peoples hard work - Thanks to All !

Committer:
dextorslabs
Date:
Sun Jun 01 16:52:16 2014 +0000
Revision:
1:216d35e347b8
Parent:
0:ea46340642a9
Added more graphics functions to give rectangles with rounded corners and filled circles.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dextorslabs 0:ea46340642a9 1 /***************************************************************
dextorslabs 1:216d35e347b8 2 ILI9340_Driver v1.1 01.06.14 Ian Weston
dextorslabs 0:ea46340642a9 3
dextorslabs 0:ea46340642a9 4 Driver and integrated graphics library for displays that use the
dextorslabs 1:216d35e347b8 5 ILI9340 controller in SPI mode. Such as the Adafruit 2.2" display.
dextorslabs 0:ea46340642a9 6
dextorslabs 1:216d35e347b8 7 This code has been ported from several sources. The driver section
dextorslabs 0:ea46340642a9 8 was completely ported from the Adafruits Arduino source code, and
dextorslabs 0:ea46340642a9 9 the graphics functions were ported from the Adafruits GFX library
dextorslabs 0:ea46340642a9 10 and some elements were ported from code by Elmicros seeduio port.
dextorslabs 0:ea46340642a9 11
dextorslabs 0:ea46340642a9 12 Future revisions will include more advanced graphics functions.
dextorslabs 0:ea46340642a9 13
dextorslabs 0:ea46340642a9 14 ***************************************************************/
dextorslabs 0:ea46340642a9 15
dextorslabs 0:ea46340642a9 16
dextorslabs 0:ea46340642a9 17
dextorslabs 0:ea46340642a9 18 #include "mbed.h"
dextorslabs 0:ea46340642a9 19
dextorslabs 0:ea46340642a9 20 #ifndef ILI9340_DRIVER_h
dextorslabs 0:ea46340642a9 21 #define ILI9340_DRIVER_h
dextorslabs 0:ea46340642a9 22
dextorslabs 0:ea46340642a9 23
dextorslabs 0:ea46340642a9 24 #define _TFTWIDTH 240
dextorslabs 0:ea46340642a9 25 #define _TFTHEIGHT 320
dextorslabs 0:ea46340642a9 26
dextorslabs 0:ea46340642a9 27 #define ILI9340_NOP 0x00
dextorslabs 0:ea46340642a9 28 #define ILI9340_SWRESET 0x01
dextorslabs 0:ea46340642a9 29 #define ILI9340_RDDID 0x04
dextorslabs 0:ea46340642a9 30 #define ILI9340_RDDST 0x09
dextorslabs 0:ea46340642a9 31
dextorslabs 0:ea46340642a9 32 #define ILI9340_SLPIN 0x10
dextorslabs 0:ea46340642a9 33 #define ILI9340_SLPOUT 0x11
dextorslabs 0:ea46340642a9 34 #define ILI9340_PTLON 0x12
dextorslabs 0:ea46340642a9 35 #define ILI9340_NORON 0x13
dextorslabs 0:ea46340642a9 36
dextorslabs 0:ea46340642a9 37 #define ILI9340_RDMODE 0x0A
dextorslabs 0:ea46340642a9 38 #define ILI9340_RDMADCTL 0x0B
dextorslabs 0:ea46340642a9 39 #define ILI9340_RDPIXFMT 0x0C
dextorslabs 0:ea46340642a9 40 #define ILI9340_RDIMGFMT 0x0A
dextorslabs 0:ea46340642a9 41 #define ILI9340_RDSELFDIAG 0x0F
dextorslabs 0:ea46340642a9 42
dextorslabs 0:ea46340642a9 43 #define ILI9340_INVOFF 0x20
dextorslabs 0:ea46340642a9 44 #define ILI9340_INVON 0x21
dextorslabs 0:ea46340642a9 45 #define ILI9340_GAMMASET 0x26
dextorslabs 0:ea46340642a9 46 #define ILI9340_DISPOFF 0x28
dextorslabs 0:ea46340642a9 47 #define ILI9340_DISPON 0x29
dextorslabs 0:ea46340642a9 48
dextorslabs 0:ea46340642a9 49 #define ILI9340_CASET 0x2A
dextorslabs 0:ea46340642a9 50 #define ILI9340_PASET 0x2B
dextorslabs 0:ea46340642a9 51 #define ILI9340_RAMWR 0x2C
dextorslabs 0:ea46340642a9 52 #define ILI9340_RAMRD 0x2E
dextorslabs 0:ea46340642a9 53
dextorslabs 0:ea46340642a9 54 #define ILI9340_PTLAR 0x30
dextorslabs 0:ea46340642a9 55 #define ILI9340_MADCTL 0x36
dextorslabs 0:ea46340642a9 56
dextorslabs 0:ea46340642a9 57
dextorslabs 0:ea46340642a9 58 #define ILI9340_MADCTL_MY 0x80
dextorslabs 0:ea46340642a9 59 #define ILI9340_MADCTL_MX 0x40
dextorslabs 0:ea46340642a9 60 #define ILI9340_MADCTL_MV 0x20
dextorslabs 0:ea46340642a9 61 #define ILI9340_MADCTL_ML 0x10
dextorslabs 0:ea46340642a9 62 #define ILI9340_MADCTL_RGB 0x00
dextorslabs 0:ea46340642a9 63 #define ILI9340_MADCTL_BGR 0x08
dextorslabs 0:ea46340642a9 64 #define ILI9340_MADCTL_MH 0x04
dextorslabs 0:ea46340642a9 65
dextorslabs 0:ea46340642a9 66 #define ILI9340_PIXFMT 0x3A
dextorslabs 0:ea46340642a9 67
dextorslabs 0:ea46340642a9 68 #define ILI9340_FRMCTR1 0xB1
dextorslabs 0:ea46340642a9 69 #define ILI9340_FRMCTR2 0xB2
dextorslabs 0:ea46340642a9 70 #define ILI9340_FRMCTR3 0xB3
dextorslabs 0:ea46340642a9 71 #define ILI9340_INVCTR 0xB4
dextorslabs 0:ea46340642a9 72 #define ILI9340_DFUNCTR 0xB6
dextorslabs 0:ea46340642a9 73
dextorslabs 0:ea46340642a9 74 #define ILI9340_PWCTR1 0xC0
dextorslabs 0:ea46340642a9 75 #define ILI9340_PWCTR2 0xC1
dextorslabs 0:ea46340642a9 76 #define ILI9340_PWCTR3 0xC2
dextorslabs 0:ea46340642a9 77 #define ILI9340_PWCTR4 0xC3
dextorslabs 0:ea46340642a9 78 #define ILI9340_PWCTR5 0xC4
dextorslabs 0:ea46340642a9 79 #define ILI9340_VMCTR1 0xC5
dextorslabs 0:ea46340642a9 80 #define ILI9340_VMCTR2 0xC7
dextorslabs 0:ea46340642a9 81
dextorslabs 0:ea46340642a9 82 #define ILI9340_RDID1 0xDA
dextorslabs 0:ea46340642a9 83 #define ILI9340_RDID2 0xDB
dextorslabs 0:ea46340642a9 84 #define ILI9340_RDID3 0xDC
dextorslabs 0:ea46340642a9 85 #define ILI9340_RDID4 0xDD
dextorslabs 0:ea46340642a9 86
dextorslabs 0:ea46340642a9 87 #define ILI9340_GMCTRP1 0xE0
dextorslabs 0:ea46340642a9 88 #define ILI9340_GMCTRN1 0xE1
dextorslabs 0:ea46340642a9 89 /*
dextorslabs 0:ea46340642a9 90 #define ILI9340_PWCTR6 0xFC
dextorslabs 0:ea46340642a9 91
dextorslabs 0:ea46340642a9 92 */
dextorslabs 0:ea46340642a9 93
dextorslabs 0:ea46340642a9 94 // Color definitions
dextorslabs 0:ea46340642a9 95 #define ILI9340_BLACK 0x0000
dextorslabs 0:ea46340642a9 96 #define ILI9340_BLUE 0x001F
dextorslabs 0:ea46340642a9 97 #define ILI9340_RED 0xF800
dextorslabs 0:ea46340642a9 98 #define ILI9340_GREEN 0x07E0
dextorslabs 0:ea46340642a9 99 #define ILI9340_CYAN 0x07FF
dextorslabs 0:ea46340642a9 100 #define ILI9340_MAGENTA 0xF81F
dextorslabs 0:ea46340642a9 101 #define ILI9340_YELLOW 0xFFE0
dextorslabs 0:ea46340642a9 102 #define ILI9340_WHITE 0xFFFF
dextorslabs 0:ea46340642a9 103
dextorslabs 0:ea46340642a9 104
dextorslabs 0:ea46340642a9 105
dextorslabs 0:ea46340642a9 106 class ILI9340_Display {
dextorslabs 0:ea46340642a9 107
dextorslabs 0:ea46340642a9 108 public:
dextorslabs 0:ea46340642a9 109
dextorslabs 0:ea46340642a9 110 uint16_t _height;
dextorslabs 0:ea46340642a9 111 uint16_t _width;
dextorslabs 0:ea46340642a9 112
dextorslabs 0:ea46340642a9 113 ILI9340_Display(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rst, PinName dc);
dextorslabs 0:ea46340642a9 114
dextorslabs 0:ea46340642a9 115 void DispInit();
dextorslabs 0:ea46340642a9 116 void WriteCommand(uint8_t);
dextorslabs 0:ea46340642a9 117 void WriteData(uint8_t);
dextorslabs 0:ea46340642a9 118 void SetRotation(uint8_t);
dextorslabs 0:ea46340642a9 119 void InvertDisplay(bool);
dextorslabs 0:ea46340642a9 120 void SetAddrWindow(uint16_t, uint16_t, uint16_t, uint16_t);
dextorslabs 0:ea46340642a9 121
dextorslabs 0:ea46340642a9 122 void DrawPixel(uint16_t, uint16_t, uint16_t);
dextorslabs 0:ea46340642a9 123 void FillScreen(uint16_t);
dextorslabs 0:ea46340642a9 124 void DrawFastVLine(int16_t, int16_t, int16_t, uint16_t);
dextorslabs 0:ea46340642a9 125 void DrawFastHLine(int16_t, int16_t, int16_t, uint16_t);
dextorslabs 0:ea46340642a9 126 void FillRect(int16_t, int16_t, int16_t, int16_t, uint16_t);
dextorslabs 0:ea46340642a9 127 void DrawRect(int16_t, int16_t, int16_t, int16_t, uint16_t);
dextorslabs 0:ea46340642a9 128 void DrawCircle(int16_t, int16_t, int16_t, uint16_t);
dextorslabs 1:216d35e347b8 129 void FillCircle(int16_t, int16_t, int16_t, uint16_t);
dextorslabs 1:216d35e347b8 130 void FillCircleHelper(int16_t, int16_t, int16_t, uint8_t, int16_t, uint16_t);
dextorslabs 1:216d35e347b8 131 void DrawCircleHelper( int16_t, int16_t, int16_t, uint8_t, uint16_t);
dextorslabs 1:216d35e347b8 132 void DrawRoundRect(int16_t, int16_t, int16_t, int16_t, int16_t, uint16_t);
dextorslabs 1:216d35e347b8 133 void FillRoundRect(int16_t, int16_t, int16_t, int16_t, int16_t, uint16_t);
dextorslabs 0:ea46340642a9 134 uint16_t Colour565(uint8_t, uint8_t, uint8_t);
dextorslabs 0:ea46340642a9 135
dextorslabs 0:ea46340642a9 136 void DrawAscii(unsigned char, uint16_t, uint16_t, uint16_t, uint16_t);
dextorslabs 0:ea46340642a9 137 void DrawString(char *string, uint16_t, uint16_t, uint8_t, uint16_t);
dextorslabs 0:ea46340642a9 138 void IntToChars (char*, int, uint8_t, uint8_t, uint16_t, uint16_t, uint8_t, uint16_t);
dextorslabs 0:ea46340642a9 139
dextorslabs 0:ea46340642a9 140 void Swap(int16_t*, int16_t*);
dextorslabs 0:ea46340642a9 141 void DrawLine(int16_t, int16_t, int16_t, int16_t, uint16_t);
dextorslabs 0:ea46340642a9 142
dextorslabs 0:ea46340642a9 143 protected:
dextorslabs 0:ea46340642a9 144 SPI spi; // mosi, miso, sclk
dextorslabs 0:ea46340642a9 145 DigitalOut cs;
dextorslabs 0:ea46340642a9 146 DigitalOut rst;
dextorslabs 0:ea46340642a9 147 DigitalOut dc;
dextorslabs 0:ea46340642a9 148
dextorslabs 0:ea46340642a9 149 uint8_t orientation;
dextorslabs 0:ea46340642a9 150
dextorslabs 0:ea46340642a9 151 };
dextorslabs 0:ea46340642a9 152
dextorslabs 0:ea46340642a9 153
dextorslabs 0:ea46340642a9 154
dextorslabs 0:ea46340642a9 155
dextorslabs 0:ea46340642a9 156
dextorslabs 0:ea46340642a9 157 #endif